Skip to content

Commit

Permalink
provider: fix a typo in the credential logic
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Santos <[email protected]>
  • Loading branch information
nicks committed Aug 28, 2024
1 parent 4f190d9 commit 841ddad
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (

var hostRegexp = regexp.MustCompile(`^[a-zA-Z0-9:.-]+$`)

const dockerHubConfigfileKey = "https://index.docker.io/v1/"
const dockerHubHost = "hub.docker.com"

// Ensure DockerProvider satisfies various provider interfaces.
var (
_ provider.Provider = &DockerProvider{}
Expand Down Expand Up @@ -134,29 +137,22 @@ func (p *DockerProvider) Configure(ctx context.Context, req provider.ConfigureRe
// If DOCKER_USERNAME and DOCKER_PASSWORD are not set, or if they are empty,
// retrieve them from the credential store
if username == "" || password == "" {
// List of possible credential hosts to check
credsHosts := []string{
"index.docker.io/v1/",
}

// Attempt to retrieve credentials from multiple credsHost
for _, credsHost := range credsHosts {
if credsHostEnv := os.Getenv("DOCKER_LOGIN_HOST"); credsHostEnv != "" {
credsHost = credsHostEnv
}

// Use the getUserCreds function to retrieve credentials from Docker config
var err error
username, password, err = tools.GetUserCreds(credsHost)
if err == nil {
// Credentials were successfully retrieved, break the loop
break
}
// Loosely adapted from
// https://github.com/moby/buildkit/blob/b9a3e7b31958b83f9ab1850a8c2ab1c66bf21f1f/session/auth/authprovider/authprovider.go#L243
//
// The Docker Hub host is a special case
// that stores its credentials differently in the store.
configfileKey := host
if host == dockerHubHost {
configfileKey = dockerHubConfigfileKey
}

// If credentials could not be retrieved, report an error
if username == "" || password == "" {
resp.Diagnostics.AddError("Credential Store Error", "Failed to retrieve credentials from the Docker config file.")
// Use the getUserCreds function to retrieve credentials from Docker config
var err error
username, password, err = tools.GetUserCreds(configfileKey)
if err != nil {
resp.Diagnostics.AddError("Credential Store Error",
fmt.Sprintf("Failed to retrieve credentials from the Docker config file: %w", err))

Check failure on line 155 in internal/provider/provider.go

View workflow job for this annotation

GitHub Actions / Build and Test

fmt.Sprintf does not support error-wrapping directive %w
}
}

Expand Down

0 comments on commit 841ddad

Please sign in to comment.