diff --git a/api.go b/api.go index 82fb4b4ad3..f044fe9c96 100644 --- a/api.go +++ b/api.go @@ -61,9 +61,13 @@ func getBoolParam(value string) (bool, error) { func getAuth(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error { // FIXME: Handle multiple login at once + // FIXME: return specific error code if config file missing? authConfig, err := auth.LoadConfig(srv.runtime.root) if err != nil { - return err + if err != auth.ErrConfigFileMissing { + return err + } + authConfig = &auth.AuthConfig{} } b, err := json.Marshal(&auth.AuthConfig{Username: authConfig.Username, Email: authConfig.Email}) if err != nil { @@ -82,7 +86,10 @@ func postAuth(srv *Server, version float64, w http.ResponseWriter, r *http.Reque authConfig, err := auth.LoadConfig(srv.runtime.root) if err != nil { - return err + if err != auth.ErrConfigFileMissing { + return err + } + authConfig = &auth.AuthConfig{} } if config.Username == authConfig.Username { config.Password = authConfig.Password diff --git a/api_test.go b/api_test.go index f94ba23fa0..f364c6c895 100644 --- a/api_test.go +++ b/api_test.go @@ -26,8 +26,7 @@ func TestGetAuth(t *testing.T) { defer nuke(runtime) srv := &Server{ - runtime: runtime, - registry: registry.NewRegistry(runtime.root), + runtime: runtime, } r := httptest.NewRecorder() @@ -56,7 +55,7 @@ func TestGetAuth(t *testing.T) { t.Fatalf("%d OK or 0 expected, received %d\n", http.StatusOK, r.Code) } - newAuthConfig := srv.registry.GetAuthConfig(false) + newAuthConfig := registry.NewRegistry(runtime.root).GetAuthConfig(false) if newAuthConfig.Username != authConfig.Username || newAuthConfig.Email != authConfig.Email { t.Fatalf("The auth configuration hasn't been set correctly") @@ -247,8 +246,7 @@ func TestGetImagesSearch(t *testing.T) { defer nuke(runtime) srv := &Server{ - runtime: runtime, - registry: registry.NewRegistry(runtime.root), + runtime: runtime, } r := httptest.NewRecorder() @@ -504,15 +502,16 @@ func TestPostAuth(t *testing.T) { defer nuke(runtime) srv := &Server{ - runtime: runtime, - registry: registry.NewRegistry(runtime.root), + runtime: runtime, } - authConfigOrig := &auth.AuthConfig{ + config := &auth.AuthConfig{ Username: "utest", Email: "utest@yopmail.com", } - srv.registry.ResetClient(authConfigOrig) + + authStr := auth.EncodeAuth(config) + auth.SaveConfig(runtime.root, authStr, config.Email) r := httptest.NewRecorder() if err := getAuth(srv, API_VERSION, r, nil, nil); err != nil { @@ -524,7 +523,7 @@ func TestPostAuth(t *testing.T) { t.Fatal(err) } - if authConfig.Username != authConfigOrig.Username || authConfig.Email != authConfigOrig.Email { + if authConfig.Username != config.Username || authConfig.Email != config.Email { t.Errorf("The retrieve auth mismatch with the one set.") } } diff --git a/auth/auth.go b/auth/auth.go index 2b99c95038..9c34604419 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -3,6 +3,7 @@ package auth import ( "encoding/base64" "encoding/json" + "errors" "fmt" "io/ioutil" "net/http" @@ -17,6 +18,12 @@ const CONFIGFILE = ".dockercfg" // the registry server we want to login against const INDEX_SERVER = "https://index.docker.io/v1" +//const INDEX_SERVER = "http://indexstaging-docker.dotcloud.com/" + +var ( + ErrConfigFileMissing error = errors.New("The Auth config file is missing") +) + type AuthConfig struct { Username string `json:"username"` Password string `json:"password"` @@ -75,7 +82,7 @@ func DecodeAuth(authStr string) (*AuthConfig, error) { func LoadConfig(rootPath string) (*AuthConfig, error) { confFile := path.Join(rootPath, CONFIGFILE) if _, err := os.Stat(confFile); err != nil { - return &AuthConfig{}, fmt.Errorf("The Auth config file is missing") + return nil, ErrConfigFileMissing } b, err := ioutil.ReadFile(confFile) if err != nil { @@ -97,7 +104,7 @@ func LoadConfig(rootPath string) (*AuthConfig, error) { } // save the auth config -func saveConfig(rootPath, authStr string, email string) error { +func SaveConfig(rootPath, authStr string, email string) error { confFile := path.Join(rootPath, CONFIGFILE) if len(email) == 0 { os.Remove(confFile) @@ -161,7 +168,9 @@ func Login(authConfig *AuthConfig) (string, error) { status = "Login Succeeded\n" storeConfig = true } else if resp.StatusCode == 401 { - saveConfig(authConfig.rootPath, "", "") + if err := SaveConfig(authConfig.rootPath, "", ""); err != nil { + return "", err + } return "", fmt.Errorf("Wrong login/password, please try again") } else { return "", fmt.Errorf("Login: %s (Code: %d; Headers: %s)", body, @@ -175,7 +184,9 @@ func Login(authConfig *AuthConfig) (string, error) { } if storeConfig { authStr := EncodeAuth(authConfig) - saveConfig(authConfig.rootPath, authStr, authConfig.Email) + if err := SaveConfig(authConfig.rootPath, authStr, authConfig.Email); err != nil { + return "", err + } } return status, nil } diff --git a/runtime_test.go b/runtime_test.go index 6c4ec5ded4..8f62fa80fc 100644 --- a/runtime_test.go +++ b/runtime_test.go @@ -2,7 +2,6 @@ package docker import ( "fmt" - "github.com/dotcloud/docker/registry" "github.com/dotcloud/docker/utils" "io" "io/ioutil" @@ -71,8 +70,7 @@ func init() { // Create the "Server" srv := &Server{ - runtime: runtime, - registry: registry.NewRegistry(runtime.root), + runtime: runtime, } // Retrieve the Image if err := srv.ImagePull(unitTestImageName, "", "", os.Stdout, false); err != nil {