Skip to content

Commit

Permalink
refactor: config package (#182)
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao authored Nov 4, 2022
1 parent faeac45 commit a44d663
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 58 deletions.
4 changes: 3 additions & 1 deletion config/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"os"
"path/filepath"

"github.com/notaryproject/notation-go/dir"
)

// save stores the cfg struct to file
Expand All @@ -24,7 +26,7 @@ func save(filePath string, cfg interface{}) error {

// load reads file, parses json and stores in cfg struct
func load(filePath string, cfg interface{}) error {
file, err := os.Open(filePath)
file, err := dir.ConfigFS().Open(filePath)
if err != nil {
return err
}
Expand Down
35 changes: 7 additions & 28 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package config provides the ability to load and save config.json and
// signingkeys.json.
package config

import (
Expand All @@ -7,38 +9,19 @@ import (
"github.com/notaryproject/notation-go/dir"
)

// CertificateReference is a named file path.
type CertificateReference struct {
Name string `json:"name"`
Path string `json:"path"`
}

// Is checks whether the given name is equal with the Name variable
func (c CertificateReference) Is(name string) bool {
return c.Name == name
}

// Config reflects the config.json file.
// Specification: https://github.com/notaryproject/notation/pull/76
type Config struct {
VerificationCertificates VerificationCertificates `json:"verificationCerts"`
InsecureRegistries []string `json:"insecureRegistries"`
CredentialsStore string `json:"credsStore,omitempty"`
CredentialHelpers map[string]string `json:"credHelpers,omitempty"`
InsecureRegistries []string `json:"insecureRegistries"`
CredentialsStore string `json:"credsStore,omitempty"`
CredentialHelpers map[string]string `json:"credHelpers,omitempty"`
// SignatureFormat defines the signature envelope type for signing
SignatureFormat string `json:"signatureFormat,omitempty"`
}

// VerificationCertificates is a collection of public certs used for verification.
type VerificationCertificates struct {
Certificates []CertificateReference `json:"certs"`
}

// NewConfig creates a new config file
func NewConfig() *Config {
return &Config{
InsecureRegistries: []string{},
}
return &Config{}
}

// Save stores the config to file
Expand All @@ -53,12 +36,8 @@ func (c *Config) Save() error {
// LoadConfig reads the config from file or return a default config if not found.
func LoadConfig() (*Config, error) {
var config Config
path, err := dir.ConfigFS().SysPath(dir.PathConfigFile)
if err != nil {
return nil, err
}

err = load(path, &config)
err := load(dir.PathConfigFile, &config)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return NewConfig(), nil
Expand Down
12 changes: 0 additions & 12 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ const (
)

var sampleConfig = &Config{
VerificationCertificates: VerificationCertificates{
Certificates: []CertificateReference{
{
Name: "wabbit-networks",
Path: "/home/demo/.config/notation/certificate/wabbit-networks.crt",
},
{
Name: "import.acme-rockets",
Path: "/home/demo/.config/notation/certificate/import.acme-rockets.crt",
},
},
},
InsecureRegistries: []string{
"registry.wabbit-networks.io",
},
Expand Down
6 changes: 1 addition & 5 deletions config/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ func NewSigningKeys() *SigningKeys {
// or return a default config if not found.
func LoadSigningKeys() (*SigningKeys, error) {
var config SigningKeys
path, err := dir.ConfigFS().SysPath(dir.PathSigningKeys)
if err != nil {
return nil, err
}
err = load(path, &config)
err := load(dir.PathSigningKeys, &config)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return NewSigningKeys(), nil
Expand Down
12 changes: 0 additions & 12 deletions config/testdata/config.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
{
"verificationCerts": {
"certs": [
{
"name": "wabbit-networks",
"path": "/home/demo/.config/notation/certificate/wabbit-networks.crt"
},
{
"name": "import.acme-rockets",
"path": "/home/demo/.config/notation/certificate/import.acme-rockets.crt"
}
]
},
"insecureRegistries": [
"registry.wabbit-networks.io"
],
Expand Down

0 comments on commit a44d663

Please sign in to comment.