From 21f63ea22b5833821638a2ad9584f7befa2f7093 Mon Sep 17 00:00:00 2001 From: Junjie Gao <43160897+JeyJeyGao@users.noreply.github.com> Date: Fri, 22 Jul 2022 22:16:51 +0800 Subject: [PATCH] config package resolve comments Signed-off-by: Junjie Gao <43160897+JeyJeyGao@users.noreply.github.com> --- config/base.go | 45 ++++++++++++++++++++++++++++++++++++++----- config/config.go | 30 +++++++++++++++-------------- config/config_test.go | 16 +++++++-------- config/keys.go | 12 ++++++------ config/keys_test.go | 4 ++-- config/util.go | 4 ++-- dir/path.go | 11 +++++++++++ plugin/plugin.go | 18 ++++++++--------- 8 files changed, 94 insertions(+), 46 deletions(-) diff --git a/config/base.go b/config/base.go index a157db33..e6d97c52 100644 --- a/config/base.go +++ b/config/base.go @@ -15,9 +15,9 @@ var ( // SigningKeysPath is the path for signingkeys.json SigningKeysPath string - // configInfo is the information of config.json - configInfo *ConfigFile - configOnce sync.Once + // fileInfo is the information of config.json + fileInfo *File + fileInfoOnce sync.Once // signingKeysInfo is the information of signingkeys.json signingKeysInfo *SigningKeys @@ -27,14 +27,49 @@ var ( func init() { ConfigPath = dir.Path.Config() SigningKeysPath = dir.Path.SigningKeyConfig() - } -// Configuration is a interface to manage notation config +// Configuration is an interface to manage notation config type Configuration interface { Save() error } +// Config is the main config struct of notation-go +type Config struct { + *File + *SigningKeys +} + +// Save stores sub-configurations to files +func (c *Config) Save() error { + if err := c.File.Save(); err != nil { + return err + } + if err := c.SigningKeys.Save(); err != nil { + return err + } + return nil +} + +// LoadOnce returns the previously read config file. +// If previous config file does not exist, it reads the config from file +// or return a default config if not found. +// The returned config is only suitable for read only scenarios for short-lived processes. +func LoadOnce() (*Config, error) { + fileInfo, err := loadFileOnce() + if err != nil { + return nil, err + } + signingKeysInfo, err := loadSigningKeysOnce() + if err != nil { + return nil, err + } + return &Config{ + File: fileInfo, + SigningKeys: signingKeysInfo, + }, nil +} + // Save stores the config to file func Save(filePath string, config interface{}) error { dir := filepath.Dir(filePath) diff --git a/config/config.go b/config/config.go index ac21a9fb..e3a02be5 100644 --- a/config/config.go +++ b/config/config.go @@ -16,11 +16,13 @@ func (c CertificateReference) Is(name string) bool { return c.Name == name } -// ConfigFile reflects the config file. +// File reflects the config file. // Specification: https://github.com/notaryproject/notation/pull/76 -type ConfigFile struct { +type File struct { VerificationCertificates VerificationCertificates `json:"verificationCerts"` InsecureRegistries []string `json:"insecureRegistries"` + CredentialsStore string `json:"credsStore,omitempty"` + CredentialHelpers map[string]string `json:"credHelpers,omitempty"` } // VerificationCertificates is a collection of public certs used for verification. @@ -29,20 +31,20 @@ type VerificationCertificates struct { } // NewConfig creates a new config file -func NewConfig() *ConfigFile { - return &ConfigFile{ +func NewConfig() *File { + return &File{ InsecureRegistries: []string{}, } } // Save stores the config to file -func (f *ConfigFile) Save() error { +func (f *File) Save() error { return Save(ConfigPath, f) } -// LoadConfig reads the config from file or return a default config if not found. -func LoadConfig() (*ConfigFile, error) { - var config ConfigFile +// loadFile reads the config from file or return a default config if not found. +func loadFile() (*File, error) { + var config File err := Load(ConfigPath, &config) if err != nil { if errors.Is(err, fs.ErrNotExist) { @@ -53,14 +55,14 @@ func LoadConfig() (*ConfigFile, error) { return &config, nil } -// LoadConfigOnce returns the previously read config file. -// If previous config file does not exists, it reads the config from file +// loadConfigOnce returns the previously read config file. +// If previous config file does not exist, it reads the config from file // or return a default config if not found. // The returned config is only suitable for read only scenarios for short-lived processes. -func LoadConfigOnce() (*ConfigFile, error) { +func loadFileOnce() (*File, error) { var err error - configOnce.Do(func() { - configInfo, err = LoadConfig() + fileInfoOnce.Do(func() { + fileInfo, err = loadFile() }) - return configInfo, err + return fileInfo, err } diff --git a/config/config_test.go b/config/config_test.go index 6d6f1acd..a3904c22 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -13,7 +13,7 @@ const ( nonexistentPath = "./testdata/nonexistent.json" ) -var sampleConfig = &ConfigFile{ +var sampleConfig = &File{ VerificationCertificates: VerificationCertificates{ Certificates: []CertificateReference{ { @@ -31,7 +31,7 @@ var sampleConfig = &ConfigFile{ }, } -func TestLoadConfig(t *testing.T) { +func TestLoadFile(t *testing.T) { t.Cleanup(func() { // restore path ConfigPath = dir.Path.Config() @@ -42,7 +42,7 @@ func TestLoadConfig(t *testing.T) { tests := []struct { name string args args - want *ConfigFile + want *File wantErr bool }{ { @@ -61,19 +61,19 @@ func TestLoadConfig(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { ConfigPath = tt.args.filePath - got, err := LoadConfig() + got, err := loadFile() if (err != nil) != tt.wantErr { - t.Errorf("LoadConfig() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("loadFile() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("LoadConfig() = %v, want %v", got, tt.want) + t.Errorf("loadFile() = %v, want %v", got, tt.want) } }) } } -func TestSaveConfigFile(t *testing.T) { +func TestSaveFile(t *testing.T) { t.Cleanup(func() { // restore path ConfigPath = dir.Path.Config() @@ -81,7 +81,7 @@ func TestSaveConfigFile(t *testing.T) { root := t.TempDir() ConfigPath = filepath.Join(root, "config.json") sampleConfig.Save() - config, err := LoadConfig() + config, err := loadFile() if err != nil { t.Fatal("Load config file from temp dir failed") } diff --git a/config/keys.go b/config/keys.go index d6ebd44b..1325939a 100644 --- a/config/keys.go +++ b/config/keys.go @@ -48,9 +48,9 @@ func NewSigningKeys() *SigningKeys { return &SigningKeys{Keys: []KeySuite{}} } -// LoadSigningKeys reads the config from file +// loadSigningKeys reads the config from file // or return a default config if not found. -func LoadSigningKeys() (*SigningKeys, error) { +func loadSigningKeys() (*SigningKeys, error) { var config SigningKeys err := Load(SigningKeysPath, &config) if err != nil { @@ -62,14 +62,14 @@ func LoadSigningKeys() (*SigningKeys, error) { return &config, nil } -// LoadSigningKeysOnce returns the previously read config file. -// If previous config file does not exists, it reads the config from file +// loadSigningKeysOnce returns the previously read config file. +// If previous config file does not exist, it reads the config from file // or return a default config if not found. // The returned config is only suitable for read only scenarios for short-lived processes. -func LoadSigningKeysOnce() (*SigningKeys, error) { +func loadSigningKeysOnce() (*SigningKeys, error) { var err error signingKeysInfoOnce.Do(func() { - signingKeysInfo, err = LoadSigningKeys() + signingKeysInfo, err = loadSigningKeys() }) return signingKeysInfo, err } diff --git a/config/keys_test.go b/config/keys_test.go index 00352c7d..b6405272 100644 --- a/config/keys_test.go +++ b/config/keys_test.go @@ -70,7 +70,7 @@ func TestLoadSigningKeysInfo(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { SigningKeysPath = tt.args.filePath - got, err := LoadSigningKeys() + got, err := loadSigningKeys() if err != nil { t.Errorf("LoadSigningKeysInfo() error = %v", err) return @@ -90,7 +90,7 @@ func TestSaveSigningKeys(t *testing.T) { root := t.TempDir() SigningKeysPath = filepath.Join(root, "signingkeys.json") sampleSigningKeysInfo.Save() - info, err := LoadSigningKeys() + info, err := loadSigningKeys() if err != nil { t.Fatal("Load signingkeys.json from temp dir failed.") } diff --git a/config/util.go b/config/util.go index 6ef309a2..a726668d 100644 --- a/config/util.go +++ b/config/util.go @@ -12,7 +12,7 @@ var ( // IsRegistryInsecure checks whether the registry is in the list of insecure registries. func IsRegistryInsecure(target string) bool { - config, err := LoadConfigOnce() + config, err := loadFileOnce() if err != nil { return false } @@ -27,7 +27,7 @@ func IsRegistryInsecure(target string) bool { // ResolveKey resolves the key by name. // The default key is attempted if name is empty. func ResolveKey(name string) (KeySuite, error) { - config, err := LoadSigningKeysOnce() + config, err := loadSigningKeysOnce() if err != nil { return KeySuite{}, err } diff --git a/dir/path.go b/dir/path.go index 85f2e009..f0668f8b 100644 --- a/dir/path.go +++ b/dir/path.go @@ -10,16 +10,27 @@ import ( const ( // SignatureExtension defines the extension of the signature files SignatureExtension = ".sig" + // ConfigFile is the name of config file ConfigFile = "config.json" + // LocalKeysDir is the directory name for local key store LocalKeysDir = "localkeys" + // SigningKeysFile is the file name of signing key info SigningKeysFile = "signingkeys.json" + // TrustPolicyFile is the file name of trust policy info TrustPolicyFile = "trustpolicy.json" + // TrustStoreDir is the directory name of trust store TrustStoreDir = "truststore" + + // KeyExtension defines the extension of the key files + KeyExtension = ".key" + + // CertificateExtension defines the extension of the certificate files + CertificateExtension = ".crt" ) // PathManager contains the union directory file system and methods diff --git a/plugin/plugin.go b/plugin/plugin.go index 76c420c9..35bbdc1f 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -122,12 +122,12 @@ type DescribeKeyResponse struct { // GenerateSignatureRequest contains the parameters passed in a generate-signature request. type GenerateSignatureRequest struct { - ContractVersion string `json:"contractVersion"` - KeyID string `json:"keyId"` - KeySpec signer.KeySpec `json:"keySpec"` - Hash string `json:"hashAlgorithm"` - Payload []byte `json:"payload"` - PluginConfig map[string]string `json:"pluginConfig,omitempty"` + ContractVersion string `json:"contractVersion"` + KeyID string `json:"keyId"` + KeySpec signer.KeySpec `json:"keySpec"` + Hash string `json:"hashAlgorithm"` + Payload []byte `json:"payload"` + PluginConfig map[string]string `json:"pluginConfig,omitempty"` } func (GenerateSignatureRequest) Command() Command { @@ -136,9 +136,9 @@ func (GenerateSignatureRequest) Command() Command { // GenerateSignatureResponse is the response of a generate-signature request. type GenerateSignatureResponse struct { - KeyID string `json:"keyId"` - Signature []byte `json:"signature"` - SigningAlgorithm signer.SignatureAlgorithm `json:"signingAlgorithm"` + KeyID string `json:"keyId"` + Signature []byte `json:"signature"` + SigningAlgorithm signer.SignatureAlgorithm `json:"signingAlgorithm"` // Ordered list of certificates starting with leaf certificate // and ending with root certificate.