-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #388 from zong-zhe/refactor-oci-auth
feat: add cache for credential to reduce the probability that kpm would be considered a threat
- Loading branch information
Showing
8 changed files
with
291 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package downloader | ||
|
||
import ( | ||
"fmt" | ||
|
||
dockerauth "oras.land/oras-go/pkg/auth/docker" | ||
remoteauth "oras.land/oras-go/v2/registry/remote/auth" | ||
) | ||
|
||
// CredClient is the client to get the credentials. | ||
type CredClient struct { | ||
credsClient *dockerauth.Client | ||
} | ||
|
||
// LoadCredentialFile loads the credential file and return the CredClient. | ||
func LoadCredentialFile(filepath string) (*CredClient, error) { | ||
authClient, err := dockerauth.NewClientWithDockerFallback(filepath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
dockerAuthClient, ok := authClient.(*dockerauth.Client) | ||
if !ok { | ||
return nil, fmt.Errorf("authClient is not *docker.Client type") | ||
} | ||
|
||
return &CredClient{ | ||
credsClient: dockerAuthClient, | ||
}, nil | ||
} | ||
|
||
// GetAuthClient returns the auth client. | ||
func (cred *CredClient) GetAuthClient() *dockerauth.Client { | ||
return cred.credsClient | ||
} | ||
|
||
// Credential will reture the credential info cache in CredClient | ||
func (cred *CredClient) Credential(hostName string) (*remoteauth.Credential, error) { | ||
if len(hostName) == 0 { | ||
return nil, fmt.Errorf("hostName is empty") | ||
} | ||
username, password, err := cred.credsClient.Credential(hostName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &remoteauth.Credential{ | ||
Username: username, | ||
Password: password, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.