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

feat: support credential function #45

Merged
merged 11 commits into from
Apr 20, 2023
Merged

feat: support credential function #45

merged 11 commits into from
Apr 20, 2023

Conversation

wangxiaoxuan273
Copy link
Collaborator

Resolves #33

@codecov-commenter
Copy link

codecov-commenter commented Apr 7, 2023

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.

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

func Credential(store Store) func(context.Context, string) (auth.Credential, error) {
Copy link
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
Member

Choose a reason for hiding this comment

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

Copy link
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
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.

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

Choose a reason for hiding this comment

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

Copy link

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
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.

registry.go Outdated Show resolved Hide resolved
Copy link
Member

@Wwwsylvia Wwwsylvia left a comment

Choose a reason for hiding this comment

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

LGTM

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
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
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 <[email protected]>
Signed-off-by: wangxiaoxuan273 <[email protected]>
Signed-off-by: wangxiaoxuan273 <[email protected]>
Signed-off-by: wangxiaoxuan273 <[email protected]>
Signed-off-by: wangxiaoxuan273 <[email protected]>
Signed-off-by: wangxiaoxuan273 <[email protected]>
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
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
Member

Choose a reason for hiding this comment

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

How about mapCredentialKeyName? 🤔

Copy link
Collaborator Author

@wangxiaoxuan273 wangxiaoxuan273 Apr 19, 2023

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

registry.go Outdated
if hostname == "docker.io" {
return "https://index.docker.io/v1/"
}
return hostname
}

// It is expected that the traffic targetting "registry-1.docker.io"
// will be redirected to "https://index.docker.io/v1/"
Copy link
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
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
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.

Copy link
Collaborator Author

@wangxiaoxuan273 wangxiaoxuan273 Apr 19, 2023

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 <[email protected]>
Copy link
Member

@Wwwsylvia Wwwsylvia left a comment

Choose a reason for hiding this comment

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

LGTM

registry.go Outdated
}
}

// The Docker CLI expects that the 'docker.io' credential
Copy link
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
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.

registry.go Outdated
}
}

// The Docker CLI expects that the 'docker.io' credential
Copy link
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
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
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
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.

registry.go Outdated
return registry
}

// It is expected that the traffic targetting "registry-1.docker.io"
Copy link
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
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
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
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.

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
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
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 <[email protected]>
Signed-off-by: wangxiaoxuan273 <[email protected]>
Copy link
Member

@Wwwsylvia Wwwsylvia left a comment

Choose a reason for hiding this comment

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

LGTM

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
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
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 <[email protected]>
Copy link
Contributor

@shizhMSFT shizhMSFT left a comment

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