containerd icon indicating copy to clipboard operation
containerd copied to clipboard

Add HostsFromHostDir() to remotes/docker/config

Open minuk-dev opened this issue 1 year ago • 5 comments

related: https://github.com/containerd/nerdctl/issues/2844

  • remotes/docker/config package provides ConfigureHosts() and HostDirFromRoot() functions only.
  • But, its caller cannot know the actual hosts from a host. It causes that the caller cannot call ConfigureHosts() method with its proper Credentials.
  • If it provides HostsFromHostDir(), nerdctl can find the proper credential for a image.

e.g. https://github.com/containerd/nerdctl/blob/40fe8b6211b0ec3cc200fe53512b557559701a29/pkg/imgutil/dockerconfigresolver/dockerconfigresolver.go#L116-L122

		if ho.HostDir != nil {
			hosts := dockerconfig.HostsFromHostDir(ctx, refHostname)  // <- what I want to achieve.
			for _, host := hosts {
				authCreds, err := NewAuthCreds(host)
				if err != nil {
					return nil, err
				}
				if authCreds != nil {
					ho.Credentials = authCreds
					break
				}
			}
		} else {
			authCreds, err := NewAuthCreds(refHostname)
			if err != nil {
				return nil, err
			}
			ho.Credentials = authCreds
		}

minuk-dev avatar Apr 28 '24 15:04 minuk-dev

Hi @minuk-dev. Thanks for your PR.

I'm waiting for a containerd member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

k8s-ci-robot avatar Apr 28 '24 15:04 k8s-ci-robot

Can you explain further the case this is trying to solve for. We are intentionally not supporting pre-fetching of the auth credentials to avoid credentials being passed to the wrong hosts. What is the information missing to get the correct credentials for the mirror?

dmcgowan avatar May 01 '24 03:05 dmcgowan

(It's only on nerdctl. I don't know other program's scenario.)

In the containerd/nerdctl#2844's description,

When I configured like below

/etc/containerd/config.toml

    [plugins."io.containerd.grpc.v1.cri".registry]
      config_path = "/etc/containerd/certs.d"

and

/etc/containerd/certs.d/gcr.io/hosts.toml

server = "https://gcr.io"

[host."https://harbor.example.com:8443/v2/gcr.io"]
  capabilities = ["pull", "resolve"]
  override_path = true

And when I request to pull the image gcr.io/~~~, I'll request to harbor.example.com:8443 because ConfigureHosts() returns RegistryHost (https://github.com/containerd/containerd/blob/d9a58a892b77f292b842b849f059d8d8e8972b4a/core/remotes/docker/registry.go#L69) with harbor.example.com:8443. But the Authorizer in RegistryHost is configured for gcr.io. So Credentials() (https://github.com/containerd/containerd/blob/652da9f49c8215da2ee72e64907aeff3c583d780/core/remotes/docker/config/hosts.go#L62) are called with gcr.io.

In other words, even if the request is sent to harbor.example.com:8443 because of ConfigureHosts(), but the Credentials() is called with gcr.io. It means when I login to harbor.example.com:8443, it does not use the credentials. So, I want to make containerd provides the actual hosts in order to configure Credentials() of HostOptions.

minuk-dev avatar May 01 '24 14:05 minuk-dev

But the Authorizer in RegistryHost is configured for gcr.io.

The point of using a callback is that this can be dynamically looked up when requested from the registry. It seems odd that nerdctl would pre-fetch just to return it in the closure, is there any specific reason it does this or was just originally done that way?

dmcgowan avatar May 01 '24 18:05 dmcgowan

In order to make RegistryHost using ConfigureHosts(), I should call with credential options with the original host, not the actual host because HostDir is parsed into containerd completely. So, outside of containerd, cannot know the actual host. Is there any way to fetch the actual host or inject it into registryhost's credential?

Or, Do you mean that why use WithAuthCreds() instead of WithAuthHeader()?. (https://github.com/containerd/containerd/blob/2ec82c47037c14c59e9457e0d6b3ecf8260f3a7e/core/remotes/docker/authorizer.go#L65-L77)

minuk-dev avatar May 02 '24 13:05 minuk-dev