This repository was archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
feat: Implement File Store #43
Merged
Merged
Changes from 27 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
501cd29
file store
Wwwsylvia e02b9c8
draft get
Wwwsylvia 8a059dc
fix test
Wwwsylvia b028482
improvement
Wwwsylvia e61d317
draft put
Wwwsylvia c132d37
refactor put
Wwwsylvia 1fa25cf
draft delete
Wwwsylvia 9ec9a1a
fix
Wwwsylvia 6711216
test get
Wwwsylvia f25afb8
clean up
Wwwsylvia 23f4f98
test put
Wwwsylvia 3123148
remove not found error
Wwwsylvia 2cb14e4
draft encode/decode
Wwwsylvia 7a010c0
test decode
Wwwsylvia 5662d0c
fix get username password
Wwwsylvia 6399b9f
improvement and TODOs
Wwwsylvia 1f619a9
more test cases
Wwwsylvia 0bb94eb
improve unit tests
Wwwsylvia 93f93f5
fix savefile
Wwwsylvia 10ef4a9
document variables
Wwwsylvia e14d1c4
rename temp file
Wwwsylvia 5d1ab3e
minor fix test data
Wwwsylvia 3afa112
fix new
Wwwsylvia 81cd939
minor improve coverage
Wwwsylvia 3a35144
omit empty
Wwwsylvia 34882d3
change test auth config type
Wwwsylvia e66df92
empty line
Wwwsylvia c1ab88e
rename
Wwwsylvia 724e510
ingest
Wwwsylvia b021344
add reference comment
Wwwsylvia be237e3
use json.RawMessage
Wwwsylvia 5500dad
refactor
Wwwsylvia 8541222
extract
Wwwsylvia ebe4f8b
optimize decoding
Wwwsylvia 2ceda3e
remove unnecessary functions
Wwwsylvia ae78e11
add auths cache
Wwwsylvia dbb8dd2
refactor
Wwwsylvia e951fb5
clean up
Wwwsylvia 4952d07
refactors per comments
Wwwsylvia 0bd4c81
improvements per comments
Wwwsylvia 28a0d26
more improvements
Wwwsylvia 76fa5ed
rename testdata
Wwwsylvia 1db937c
improve testdata
Wwwsylvia 7b9d4a1
address comments
Wwwsylvia b3f9fd5
fix
Wwwsylvia 571dba0
check close error
Wwwsylvia f3eaeb0
fix error check
Wwwsylvia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,304 @@ | ||
| /* | ||
| Copyright The ORAS Authors. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package credentials | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/oras-project/oras-credentials-go/internal/ioutils" | ||
| "oras.land/oras-go/v2/registry/remote/auth" | ||
| ) | ||
|
|
||
| // FileStore implements a credentials store using the docker configuration file | ||
| // to keep the credentials in plain-text. | ||
| type FileStore struct { | ||
| // DisableSave disable saving credentials in plain text. | ||
| // If DisableSave is set to true, Put() will return ErrPlainTextSaveDisabled. | ||
| DisableSave bool | ||
| configPath string | ||
| content map[string]interface{} | ||
|
Wwwsylvia marked this conversation as resolved.
Outdated
|
||
| contentRWLock sync.RWMutex | ||
| } | ||
|
|
||
| const ( | ||
| configFieldAuthConfigs = "auths" | ||
| configFieldUsername = "username" | ||
| configFieldPassword = "password" | ||
| configFieldBasicAuth = "auth" | ||
| configFieldIdentityToken = "identitytoken" | ||
| configFieldRegistryToken = "registrytoken" | ||
| ) | ||
|
|
||
| var ( | ||
| // ErrInvalidConfigFormat is returned when the config format is invalid. | ||
| ErrInvalidConfigFormat = errors.New("invalid config format") | ||
| // ErrPlainTextSaveDisabled is returned by Put() when DisableSave is set | ||
| // to true. | ||
| ErrPlainTextSaveDisabled = errors.New("plain text save is disabled") | ||
|
Wwwsylvia marked this conversation as resolved.
Outdated
|
||
| ) | ||
|
|
||
| // authConfig contains authorization information for connecting to a Registry | ||
| type authConfig struct { | ||
| Username string `json:"username,omitempty"` | ||
| Password string `json:"password,omitempty"` | ||
| Auth string `json:"auth,omitempty"` | ||
|
|
||
| // IdentityToken is used to authenticate the user and get | ||
| // an access token for the registry. | ||
| IdentityToken string `json:"identitytoken,omitempty"` | ||
| // RegistryToken is a bearer token to be sent to a registry | ||
| RegistryToken string `json:"registrytoken,omitempty"` | ||
| } | ||
|
|
||
| // NewFileStore creates a new file credentials store. | ||
| func NewFileStore(configPath string) (*FileStore, error) { | ||
| fs := &FileStore{configPath: configPath} | ||
| configFile, err := os.Open(configPath) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| // init content map if the content file does not exist | ||
| fs.content = make(map[string]interface{}) | ||
| return fs, nil | ||
| } | ||
| return nil, fmt.Errorf("failed to open config file at %s: %w", configPath, err) | ||
| } | ||
| defer configFile.Close() | ||
|
|
||
| fi, err := configFile.Stat() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to stat config file at %s: %w", configPath, err) | ||
| } | ||
| if fi.IsDir() { | ||
| return nil, fmt.Errorf("%s: configPath cannot be a directory", configPath) | ||
| } | ||
|
|
||
| // decode config content if the config file exists | ||
| if err := json.NewDecoder(configFile).Decode(&fs.content); err != nil { | ||
| return nil, fmt.Errorf("failed to decode config file at %s: %w: %v", configPath, ErrInvalidConfigFormat, err) | ||
| } | ||
| return fs, nil | ||
| } | ||
|
|
||
| // Get retrieves credentials from the store for the given server address. | ||
| func (fs *FileStore) Get(_ context.Context, serverAddress string) (auth.Credential, error) { | ||
| fs.contentRWLock.RLock() | ||
| defer fs.contentRWLock.RUnlock() | ||
|
|
||
| authCfg, ok := fs.getAuthConfig(serverAddress) | ||
| if !ok { | ||
| return auth.EmptyCredential, nil | ||
| } | ||
|
|
||
| cred := auth.Credential{ | ||
| Username: authCfg.Username, | ||
| Password: authCfg.Password, | ||
| RefreshToken: authCfg.IdentityToken, | ||
| AccessToken: authCfg.RegistryToken, | ||
| } | ||
| if authCfg.Auth != "" { | ||
| var err error | ||
| // override username and password | ||
| cred.Username, cred.Password, err = decodeAuth(authCfg.Auth) | ||
| if err != nil { | ||
| return auth.EmptyCredential, fmt.Errorf("failed to decode username and password: %w: %v", ErrInvalidConfigFormat, err) | ||
| } | ||
| } | ||
| return cred, nil | ||
| } | ||
|
|
||
| // Put saves credentials into the store for the given server address. | ||
| // Returns ErrPlainTextSaveDisabled if fs.DisableSave is set to true. | ||
| func (fs *FileStore) Put(_ context.Context, serverAddress string, cred auth.Credential) error { | ||
| if fs.DisableSave { | ||
| return ErrPlainTextSaveDisabled | ||
| } | ||
|
|
||
| fs.contentRWLock.Lock() | ||
| defer fs.contentRWLock.Unlock() | ||
|
|
||
| fs.updateAuths(serverAddress, cred) | ||
| return fs.saveFile() | ||
| } | ||
|
|
||
| // Delete removes credentials from the store for the given server address. | ||
| func (fs *FileStore) Delete(ctx context.Context, serverAddress string) error { | ||
|
Wwwsylvia marked this conversation as resolved.
Outdated
|
||
| fs.contentRWLock.Lock() | ||
| defer fs.contentRWLock.Unlock() | ||
|
|
||
| if _, err := os.Stat(fs.configPath); os.IsNotExist(err) { | ||
| // no ops if the config file does not exist | ||
| return nil | ||
| } | ||
| authsMap, ok := fs.content[configFieldAuthConfigs].(map[string]interface{}) | ||
| if !ok { | ||
| // no ops | ||
| return nil | ||
| } | ||
| if _, ok = authsMap[serverAddress]; !ok { | ||
| // no ops | ||
| return nil | ||
| } | ||
|
|
||
| // update data | ||
| delete(authsMap, serverAddress) | ||
| fs.content[configFieldAuthConfigs] = authsMap | ||
| return fs.saveFile() | ||
| } | ||
|
|
||
| // getAuthConfig reads the config and returns authConfig for serverAddress. | ||
| func (fs *FileStore) getAuthConfig(serverAddress string) (authConfig, bool) { | ||
| authsMap, ok := fs.content[configFieldAuthConfigs].(map[string]interface{}) | ||
| if !ok { | ||
| return authConfig{}, false | ||
| } | ||
| authConfigObj, ok := authsMap[serverAddress].(map[string]interface{}) | ||
| if !ok { | ||
| return authConfig{}, false | ||
| } | ||
|
|
||
| var authCfg authConfig | ||
| for k, v := range authConfigObj { | ||
| switch k { | ||
| case configFieldUsername: | ||
| authCfg.Username, _ = v.(string) | ||
| case configFieldPassword: | ||
| authCfg.Password, _ = v.(string) | ||
| case configFieldBasicAuth: | ||
| authCfg.Auth, _ = v.(string) | ||
| case configFieldIdentityToken: | ||
| authCfg.IdentityToken, _ = v.(string) | ||
| case configFieldRegistryToken: | ||
| authCfg.RegistryToken, _ = v.(string) | ||
| } | ||
| } | ||
| return authCfg, true | ||
| } | ||
|
|
||
| // updateAuths updates the Auths field of fs.content based on cred. | ||
| func (fs *FileStore) updateAuths(serverAddress string, cred auth.Credential) { | ||
| authsMap, ok := fs.content[configFieldAuthConfigs].(map[string]interface{}) | ||
|
Wwwsylvia marked this conversation as resolved.
Outdated
|
||
| if !ok { | ||
| authsMap = make(map[string]interface{}) | ||
| } | ||
| authCfg, ok := authsMap[serverAddress].(map[string]interface{}) | ||
| if !ok { | ||
| authCfg = make(map[string]interface{}) | ||
| } | ||
| authCfg[configFieldBasicAuth] = encodeAuth(cred.Username, cred.Password) | ||
| authCfg[configFieldUsername] = "" | ||
| authCfg[configFieldPassword] = "" | ||
| authCfg[configFieldIdentityToken] = cred.RefreshToken | ||
| authCfg[configFieldRegistryToken] = cred.AccessToken | ||
|
|
||
| // omit empty fields | ||
| cleanAuthCfg := make(map[string]interface{}) | ||
| for k, v := range authCfg { | ||
| switch k { | ||
| case configFieldBasicAuth, | ||
| configFieldUsername, | ||
| configFieldPassword, | ||
| configFieldIdentityToken, | ||
| configFieldRegistryToken: | ||
| if v != "" { | ||
| cleanAuthCfg[k] = v | ||
| } | ||
| default: | ||
| // copy any other fields | ||
| cleanAuthCfg[k] = v | ||
| } | ||
| } | ||
|
|
||
| // update data | ||
| authsMap[serverAddress] = cleanAuthCfg | ||
| fs.content[configFieldAuthConfigs] = authsMap | ||
| } | ||
|
|
||
| // saveFile saves fs.content into fs.configPath. | ||
| func (fs *FileStore) saveFile() error { | ||
| jsonData, err := json.MarshalIndent(fs.content, "", "\t") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal credentials: %w", err) | ||
| } | ||
|
|
||
| dir := filepath.Dir(fs.configPath) | ||
| if err := os.MkdirAll(dir, 0777); err != nil { | ||
| return fmt.Errorf("failed to make directory %s: %w", dir, err) | ||
| } | ||
| ingest, err := ioutils.Ingest(bytes.NewReader(jsonData)) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to save config file: %w", err) | ||
| } | ||
|
|
||
| // handle symlink | ||
| targetPath := fs.configPath | ||
| if link, err := os.Readlink(fs.configPath); err == nil { | ||
| targetPath = link | ||
| } | ||
| // copy file with original ownership and permissions | ||
| ioutils.CopyFilePermissions(targetPath, ingest) | ||
| if err := os.Rename(ingest, targetPath); err != nil { | ||
| // clean up the ingest file | ||
| os.Remove(ingest) | ||
| return fmt.Errorf("failed to save config file: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // encodeAuth base64-encodes username and password into base64(username:password). | ||
| func encodeAuth(username, password string) string { | ||
| if username == "" && password == "" { | ||
| return "" | ||
| } | ||
|
|
||
| authStr := username + ":" + password | ||
| msg := []byte(authStr) | ||
| encoded := make([]byte, base64.StdEncoding.EncodedLen(len(msg))) | ||
| base64.StdEncoding.Encode(encoded, msg) | ||
| return string(encoded) | ||
|
Wwwsylvia marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| // decodeAuth decodes a base64 encoded string and returns username and password. | ||
| func decodeAuth(authStr string) (username string, password string, err error) { | ||
| if authStr == "" { | ||
| return "", "", nil | ||
| } | ||
|
Wwwsylvia marked this conversation as resolved.
|
||
|
|
||
| decodedLen := base64.StdEncoding.DecodedLen(len(authStr)) | ||
| decoded := make([]byte, decodedLen) | ||
| authByte := []byte(authStr) | ||
| n, err := base64.StdEncoding.Decode(decoded, authByte) | ||
| if err != nil { | ||
| return "", "", err | ||
| } | ||
| if n > decodedLen { | ||
| return "", "", errors.New("size mismatch") | ||
| } | ||
|
Wwwsylvia marked this conversation as resolved.
Outdated
|
||
| arr := strings.SplitN(string(decoded), ":", 2) | ||
|
Wwwsylvia marked this conversation as resolved.
Outdated
|
||
| if len(arr) != 2 { | ||
| return "", "", errors.New("auth does not conform username:password format") | ||
| } | ||
| password = strings.Trim(arr[1], "\x00") | ||
|
Wwwsylvia marked this conversation as resolved.
Outdated
|
||
| return arr[0], password, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.