Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can auth and pull image locally, but not on dev servers #1002

Open
esn89 opened this issue Aug 1, 2023 · 5 comments
Open

Can auth and pull image locally, but not on dev servers #1002

esn89 opened this issue Aug 1, 2023 · 5 comments

Comments

@esn89
Copy link

esn89 commented Aug 1, 2023

I am pulling from a private GCR registry which needs to be granted permissions via GCP IAM:

Get authentication token:

// authenticateGcr looks for credentials with Google's FindDefaultCredentials to get a token
func authenticateGcr() (*oauth2.Token, error) {

	var token *oauth2.Token
	ctx_gcp := context.Background()
	scopes := []string{
		"https://www.googleapis.com/auth/cloud-platform",
	}
	credentials, err := google.FindDefaultCredentials(ctx_gcp, scopes...)
	if err != nil {
		return token, fmt.Errorf("authenticateGcr: %w", err)
	} else {
		token, err = credentials.TokenSource.Token()
		if err != nil {
			return token, fmt.Errorf("authenticateGcr: %w", err)
		}
	}
	return token, nil
}

Then use the token and do a pull:

func pullImage(client *docker.Client) error {

	token, err := authenticateGcr()
	if err != nil {
		return fmt.Errorf("pullImage: %w", err)
	}

	err = client.PullImage(docker.PullImageOptions{
		Registry:   "",
		Repository: "gcr.io/private-gcp-project/private/work/image",
		Tag:        "supertag1",
		// let the progress bar library do this, it gets ugly on the CLI
		//OutputStream: os.Stdout,
	}, docker.AuthConfiguration{
		//Username: "oauth2accesstoken",
		//Password: token.AccessToken,
		RegistryToken: token.AccessToken,
	})

	if err != nil {
		return fmt.Errorf("pullImage: %w", err)
	}
	return nil
}

What's fascinating is that on my local machine (OSX), it works fine and I can even exec into the container without issues.

However, when I build for Linux via:

GOOS=linux GOARCH=amd64 go build

And I run it on my Linux servers, I get:

Error: Run: pullImage: API error (404): pull access denied for gcr.io/private-gcp-project/private/work/image, repository does not exist or may require 'docker login': denied: Permission denied for "supertag1" from request "/v2/private-gcp-project/private/work/image/manifests/supertag1". 

I am quite puzzled by this error message. I have done login via gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin gcr.io. I wonder if it has anything to do with the v2 versioning?

Here is the ~/.docker/config for both environments:

{
	"credsStore": "desktop",
	"credHelpers": {
		"asia.gcr.io": "gcloud",
		"eu.gcr.io": "gcloud",
		"gcr.io": "gcloud",
		"marketplace.gcr.io": "gcloud",
		"staging-k8s.gcr.io": "gcloud",
		"us.gcr.io": "gcloud"
	},
	"currentContext": "desktop-linux"
}
@fsouza
Copy link
Owner

fsouza commented Sep 9, 2023

Do you not need to set username, password and registry to make it compatible with docker login -u oauth2accesstoken --password-stdin gcr.io? (I see that you commented it in the code, so maybe you tried it already, but I'm curious on what happened in that case)

@Arpit529Srivastava
Copy link

Arpit529Srivastava commented Oct 4, 2024

@esn89 @fsouza can you please assign this issue to me, I can fix this
Also please add the issue under the label hactoberfest and hactoberfest-accepted

@esn89
Copy link
Author

esn89 commented Oct 4, 2024

@Arpit529Srivastava thanks for taking initiative. I don't have permissions to assign I believe.

@fsouza
Copy link
Owner

fsouza commented Oct 4, 2024

@Arpit529Srivastava go for it. I believe the hacktoberfest-accepted label needs to be applied to the PR, but please let me know if that's wrong and I can add it

@Arpit529Srivastava
Copy link

Arpit529Srivastava commented Oct 4, 2024

@fsouza thanks
will make the pr very soon also assign the issue to me please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants