Skip to content
This repository was archived by the owner on Apr 7, 2024. It is now read-only.

feat: support credential function#45

Merged
shizhMSFT merged 11 commits into
oras-project:mainfrom
wangxiaoxuan273:credential
Apr 20, 2023
Merged

feat: support credential function#45
shizhMSFT merged 11 commits into
oras-project:mainfrom
wangxiaoxuan273:credential

Conversation

@wangxiaoxuan273

Copy link
Copy Markdown
Collaborator

Resolves #33

@codecov-commenter

codecov-commenter commented Apr 7, 2023

Copy link
Copy Markdown

Codecov Report

Merging #45 (086f9b2) into main (87a3ee4) will increase coverage by 0.21%.
The diff coverage is 85.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##             main      #45      +/-   ##
==========================================
+ Coverage   78.32%   78.53%   +0.21%     
==========================================
  Files           3        3              
  Lines         203      219      +16     
==========================================
+ Hits          159      172      +13     
- Misses         33       35       +2     
- Partials       11       12       +1     
Impacted Files Coverage Δ
registry.go 74.07% <85.00%> (+3.02%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Comment thread credential.go Outdated
"oras.land/oras-go/v2/registry/remote/auth"
)

func Credential(store Store) func(context.Context, string) (auth.Credential, error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: missing documentation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have some unit tests? It would be somewhat bit similar to https://github.com/oras-project/oras-go/blob/main/registry/remote/auth/client_test.go#L2220-L2239.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added doc comment.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added unit tests.

Comment thread credential.go Outdated
if registry == "" {
return auth.EmptyCredential, nil
}
return store.Get(ctx, registry)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It MUST be included

#18 (reply in thread)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added docker.io redirection.

Comment thread registry.go Outdated

@Wwwsylvia Wwwsylvia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread registry.go Outdated
// Credential returns a Credential() function that can be used by auth.Client.
func Credential(store Store) func(context.Context, string) (auth.Credential, error) {
return func(ctx context.Context, reg string) (auth.Credential, error) {
reg = mapHostname(reg)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 3 concepts in the docker: registry name, hostname, and credential key.

Let's do some experiments.

$ docker login docker.io -u <username> -p <password>
Login Succeeded
$ cat ~/.docker/config.json
{
        "auths": {
                "https://index.docker.io/v1/": {
                        "auth": "dummy/auth=="
                }
        }
}
$ rm ~/.docker/config.json
$ docker login registry-1.docker.io -u <username> -p <password>
Login Succeeded
$ cat ~/.docker/config.json
{
        "auths": {
                "registry-1.docker.io": {
                        "auth": "dummy/auth=="
                }
        }
}
$ rm ~/.docker/config.json
$ docker login index.docker.io -u <username> -p <password>
Login Succeeded
$ cat ~/.docker/config.json
{
        "auths": {
                "index.docker.io": {
                        "auth": "dummy/auth=="
                }
        }
}
$ rm ~/.docker/config.json
$ docker login https://index.docker.io/v1/ -u <username> -p <password>
Login Succeeded
$ cat ~/.docker/config.json
{
        "auths": {
                "https://index.docker.io/v1/": {
                        "auth": "dummy/auth=="
                }
        }
}

As you can observe,

  • The registry name docker.io maps to hostname registry-1.docker.io where the credential key is https://index.docker.io/v1/
  • The registry name registry-1.docker.io maps to hostname registry-1.docker.io where the credential key is registry-1.docker.io.

Therefore, mapHostname() does not do correct mapping and need to be fixed.

/cc @Wwwsylvia

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very tricky. Thanks @shizhMSFT for the explanation.

Signed-off-by: wangxiaoxuan273 <wangxiaoxuan119@gmail.com>
Signed-off-by: wangxiaoxuan273 <wangxiaoxuan119@gmail.com>
Signed-off-by: wangxiaoxuan273 <wangxiaoxuan119@gmail.com>
Signed-off-by: wangxiaoxuan273 <wangxiaoxuan119@gmail.com>
Signed-off-by: wangxiaoxuan273 <wangxiaoxuan119@gmail.com>
Signed-off-by: wangxiaoxuan273 <wangxiaoxuan119@gmail.com>
Signed-off-by: wangxiaoxuan273 <wangxiaoxuan119@gmail.com>
Comment thread registry.go Outdated

// The Docker CLI expects that the 'docker.io' credential
// will be added under the key "https://index.docker.io/v1/"
func getLoginRegistry(hostname string) string {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe getStoreRegistryName

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about mapCredentialKeyName? 🤔

@wangxiaoxuan273 wangxiaoxuan273 Apr 19, 2023

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the function name to mapStoreRegistryName

Comment thread registry.go Outdated
}

// It is expected that the traffic targetting "registry-1.docker.io"
// will be redirected to "https://index.docker.io/v1/"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like getAuthenticationRegistryName

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think map might be better than get

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we don't need to make it a function.

@wangxiaoxuan273 wangxiaoxuan273 Apr 19, 2023

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a function here makes the code clean. I changed the name to mapAuthenticationRegistryName.

Signed-off-by: wangxiaoxuan273 <wangxiaoxuan119@gmail.com>

@Wwwsylvia Wwwsylvia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread registry.go Outdated
}
}

// The Docker CLI expects that the 'docker.io' credential

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Wondering if we should move this comment inside. 🤔

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved them inside.

Comment thread registry.go Outdated
}
}

// The Docker CLI expects that the 'docker.io' credential

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation starts with <function name> bla bla.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any reference so that other contributors can understand?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are private functions and don't need documentation. I moved the comments inside the function.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added reference code link.

Comment thread registry.go Outdated
return registry
}

// It is expected that the traffic targetting "registry-1.docker.io"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation starts with <function name> bla bla.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any reference so that other contributors can understand?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are private functions and don't need documentation. I moved the comments inside the function.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added reference code link.

Comment thread registry.go Outdated

// It is expected that the traffic targetting "registry-1.docker.io"
// will be redirected to "https://index.docker.io/v1/"
func mapAuthenticationRegistryName(target string) string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should rename target to hostname for better readability.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed.

Signed-off-by: wangxiaoxuan273 <wangxiaoxuan119@gmail.com>
Signed-off-by: wangxiaoxuan273 <wangxiaoxuan119@gmail.com>

@Wwwsylvia Wwwsylvia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread registry.go Outdated
func mapStoreRegistryName(registry string) string {
// The Docker CLI expects that the 'docker.io' credential
// will be added under the key "https://index.docker.io/v1/"
// See: https://github.com/moby/moby/blob/master/registry/config.go#L25-L48

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never use a branch name (e.g. master) in the link. Use tag or commit digest so that the link won't expire.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the link to: https://github.com/moby/moby/blob/v24.0.0-beta.2/registry/config.go#L25-L48

Signed-off-by: wangxiaoxuan273 <wangxiaoxuan119@gmail.com>

@shizhMSFT shizhMSFT left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@shizhMSFT shizhMSFT merged commit 9d0706f into oras-project:main Apr 20, 2023
@wangxiaoxuan273 wangxiaoxuan273 deleted the credential branch April 20, 2023 09:10
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Credential() method

6 participants