Ejemplo n.º 1
0
func DockerLogin(username, password, email string) (string, error) {

	if username == "" && password == "" && email == "" {
		// Attempt login with .dockercfg file.
		configFile, err := registry.LoadConfig(os.Getenv("HOME"))
		if err != nil {
			return "", err
		}
		authconfig, ok := configFile.Configs[registry.IndexServerAddress()]
		if !ok {
			return "", fmt.Errorf("Error: Unable to login, no data for index server.")
		}
		status, err := registry.Login(&authconfig, registry.HTTPRequestFactory(nil))
		if err != nil {
			return "", err
		}
		return status, nil
	} else {
		// Attempt login with this function's auth params.
		authconfig := registry.AuthConfig{
			Username:      username,
			Email:         email,
			Password:      password,
			ServerAddress: registry.IndexServerAddress(),
		}
		status, err := registry.Login(&authconfig, registry.HTTPRequestFactory(nil))
		if err != nil {
			return "", err
		}
		return status, nil
	}

	return "", fmt.Errorf("Auth params don't make sense.")

}
Ejemplo n.º 2
0
func TestCreateAccount(t *testing.T) {
	t.Skip("FIXME: please remove dependency on external services")
	tokenBuffer := make([]byte, 16)
	_, err := rand.Read(tokenBuffer)
	if err != nil {
		t.Fatal(err)
	}
	token := hex.EncodeToString(tokenBuffer)[:12]
	username := "******" + token
	authConfig := &registry.AuthConfig{
		Username:      username,
		Password:      "******",
		Email:         fmt.Sprintf("*****@*****.**", token),
		ServerAddress: "https://registry-stage.hub.docker.com/v1/",
	}
	status, err := registry.Login(authConfig, nil)
	if err != nil {
		t.Fatal(err)
	}
	expectedStatus := fmt.Sprintf(
		"Account created. Please see the documentation of the registry %s for instructions how to activate it.",
		authConfig.ServerAddress,
	)
	if status != expectedStatus {
		t.Fatalf("Expected status: \"%s\", found \"%s\" instead.", expectedStatus, status)
	}

	status, err = registry.Login(authConfig, nil)
	if err == nil {
		t.Fatalf("Expected error but found nil instead")
	}

	expectedError := "Login: Account is not Active"

	if !strings.Contains(err.Error(), expectedError) {
		t.Fatalf("Expected message \"%s\" but found \"%s\" instead", expectedError, err)
	}
}
Ejemplo n.º 3
0
func TestLogin(t *testing.T) {
	t.Skip("FIXME: please remove dependency on external services")
	os.Setenv("DOCKER_INDEX_URL", "https://registry-stage.hub.docker.com/v1/")
	defer os.Setenv("DOCKER_INDEX_URL", "")
	authConfig := &registry.AuthConfig{
		Username:      "******",
		Password:      "******",
		Email:         "*****@*****.**",
		ServerAddress: "https://registry-stage.hub.docker.com/v1/",
	}
	status, err := registry.Login(authConfig, nil)
	if err != nil {
		t.Fatal(err)
	}
	if status != "Login Succeeded" {
		t.Fatalf("Expected status \"Login Succeeded\", found \"%s\" instead", status)
	}
}