Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add configuration for stores #930

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions cmd/sops/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.mozilla.org/sops/v3"
"go.mozilla.org/sops/v3/cmd/sops/codes"
. "go.mozilla.org/sops/v3/cmd/sops/formats"
"go.mozilla.org/sops/v3/config"
"go.mozilla.org/sops/v3/keys"
"go.mozilla.org/sops/v3/keyservice"
"go.mozilla.org/sops/v3/kms"
Expand All @@ -30,32 +31,37 @@ type ExampleFileEmitter interface {
EmitExample() []byte
}

type Configurable interface {
Configure(*config.Config)
}

// Store handles marshaling and unmarshaling from SOPS files
type Store interface {
sops.Store
ExampleFileEmitter
Configurable
}

type storeConstructor = func() Store
type storeConstructor = func(*config.StoresConfig) Store

func newBinaryStore() Store {
return &json.BinaryStore{}
func newBinaryStore(c *config.StoresConfig) Store {
return json.NewBinaryStore(&c.JSONBinary)
}

func newDotenvStore() Store {
return &dotenv.Store{}
func newDotenvStore(c *config.StoresConfig) Store {
return dotenv.NewStore(&c.Dotenv)
}

func newIniStore() Store {
return &ini.Store{}
func newIniStore(c *config.StoresConfig) Store {
return ini.NewStore(&c.INI)
}

func newJsonStore() Store {
return &json.Store{}
func newJsonStore(c *config.StoresConfig) Store {
return json.NewStore(&c.JSON)
}

func newYamlStore() Store {
return &yaml.Store{}
func newYamlStore(c *config.StoresConfig) Store {
return yaml.NewStore(&c.YAML)
}

var storeConstructors = map[Format]storeConstructor{
Expand Down Expand Up @@ -151,27 +157,27 @@ func NewExitError(i interface{}, exitCode int) *cli.ExitError {

// StoreForFormat returns the correct format-specific implementation
// of the Store interface given the format.
func StoreForFormat(format Format) Store {
func StoreForFormat(format Format, c *config.StoresConfig) Store {
storeConst, found := storeConstructors[format]
if !found {
storeConst = storeConstructors[Binary] // default
}
return storeConst()
return storeConst(c)
}

// DefaultStoreForPath returns the correct format-specific implementation
// of the Store interface given the path to a file
func DefaultStoreForPath(path string) Store {
func DefaultStoreForPath(c *config.StoresConfig, path string) Store {
format := FormatForPath(path)
return StoreForFormat(format)
return StoreForFormat(format, c)
}

// DefaultStoreForPathOrFormat returns the correct format-specific implementation
// of the Store interface given the formatString if specified, or the path to a file.
// This is to support the cli, where both are provided.
func DefaultStoreForPathOrFormat(path, format string) Store {
func DefaultStoreForPathOrFormat(c *config.StoresConfig, path string, format string) Store {
formatFmt := FormatForPathOrString(path, format)
return StoreForFormat(formatFmt)
return StoreForFormat(formatFmt, c)
}

// KMS_ENC_CTX_BUG_FIXED_VERSION represents the SOPS version in which the
Expand Down
22 changes: 20 additions & 2 deletions cmd/sops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1037,11 +1037,29 @@ func keyservices(c *cli.Context) (svcs []keyservice.KeyServiceClient) {
}

func inputStore(context *cli.Context, path string) common.Store {
return common.DefaultStoreForPathOrFormat(path, context.String("input-type"))
var configPath string
if context.String("config") != "" {
configPath = context.String("config")
} else {
// Ignore config not found errors returned from FindConfigFile since the config file is not mandatory
configPath, _ = config.FindConfigFile(".")
}
storesConf, _ := config.LoadStoresConfig(configPath)

return common.DefaultStoreForPathOrFormat(storesConf, path, context.String("input-type"))
}

func outputStore(context *cli.Context, path string) common.Store {
return common.DefaultStoreForPathOrFormat(path, context.String("output-type"))
var configPath string
if context.String("config") != "" {
configPath = context.String("config")
} else {
// Ignore config not found errors returned from FindConfigFile since the config file is not mandatory
configPath, _ = config.FindConfigFile(".")
}
storesConf, _ := config.LoadStoresConfig(configPath)

return common.DefaultStoreForPathOrFormat(storesConf, path, context.String("output-type"))
}

func parseTreePath(arg string) ([]interface{}, error) {
Expand Down
6 changes: 5 additions & 1 deletion cmd/sops/subcommand/updatekeys/updatekeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func UpdateKeys(opts Opts) error {
}

func updateFile(opts Opts) error {
store := common.DefaultStoreForPath(opts.InputPath)
sc, err := config.LoadStoresConfig(opts.ConfigPath)
if err != nil {
return err
}
store := common.DefaultStoreForPath(sc, opts.InputPath)
log.Printf("Syncing keys for file %s", opts.InputPath)
tree, err := common.LoadEncryptedFile(store, opts.InputPath)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,30 @@ func FindConfigFile(start string) (string, error) {
return "", fmt.Errorf("Config file not found")
}

type DotenvStoreConfig struct{}

type INIStoreConfig struct{}

type JSONStoreConfig struct{}

type JSONBinaryStoreConfig struct{}

type YAMLStoreConfig struct {
Indent int `yaml:"indent"`
}

type StoresConfig struct {
Dotenv DotenvStoreConfig `yaml:"dotenv"`
INI INIStoreConfig `yaml:"ini"`
JSONBinary JSONBinaryStoreConfig `yaml:"json_binary"`
JSON JSONStoreConfig `yaml:"json"`
YAML YAMLStoreConfig `yaml:"yaml"`
}

type configFile struct {
CreationRules []creationRule `yaml:"creation_rules"`
DestinationRules []destinationRule `yaml:"destination_rules"`
Stores StoresConfig `yaml:"stores"`
}

type keyGroup struct {
Expand Down Expand Up @@ -368,3 +389,11 @@ func LoadDestinationRuleForFile(confPath string, filePath string, kmsEncryptionC
}
return parseDestinationRuleForFile(conf, filePath, kmsEncryptionContext)
}

func LoadStoresConfig(confPath string) (*StoresConfig, error) {
conf, err := loadConfigFile(confPath)
if err != nil {
return nil, err
}
return &conf.Stores, nil
}
9 changes: 9 additions & 0 deletions stores/dotenv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"go.mozilla.org/sops/v3"
"go.mozilla.org/sops/v3/config"
"go.mozilla.org/sops/v3/stores"
)

Expand All @@ -15,6 +16,11 @@ const SopsPrefix = "sops_"

// Store handles storage of dotenv data
type Store struct {
config config.DotenvStoreConfig
}

func NewStore(c *config.DotenvStoreConfig) *Store {
return &Store{config: *c}
}

// LoadEncryptedFile loads an encrypted file's bytes onto a sops.Tree runtime object
Expand Down Expand Up @@ -188,3 +194,6 @@ func isComplexValue(v interface{}) bool {
}
return false
}

func (store *Store) Configure(c *config.Config) {
}
9 changes: 9 additions & 0 deletions stores/ini/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ import (
"strings"

"go.mozilla.org/sops/v3"
"go.mozilla.org/sops/v3/config"
"go.mozilla.org/sops/v3/stores"
"gopkg.in/ini.v1"
)

// Store handles storage of ini data.
type Store struct {
config *config.INIStoreConfig
}

func NewStore(c *config.INIStoreConfig) *Store {
return &Store{config: c}
}

func (store Store) encodeTree(branches sops.TreeBranches) ([]byte, error) {
Expand Down Expand Up @@ -282,3 +288,6 @@ func (store *Store) EmitExample() []byte {
}
return bytes
}

func (store *Store) Configure(c *config.Config) {
}
19 changes: 18 additions & 1 deletion stores/json/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,27 @@ import (
"io"

"go.mozilla.org/sops/v3"
"go.mozilla.org/sops/v3/config"
"go.mozilla.org/sops/v3/stores"
)

// Store handles storage of JSON data.
type Store struct {
config config.JSONStoreConfig
}

func NewStore(c *config.JSONStoreConfig) *Store {
return &Store{config: *c}
}

// BinaryStore handles storage of binary data in a JSON envelope.
type BinaryStore struct {
store Store
store Store
config config.JSONBinaryStoreConfig
}

func NewBinaryStore(c *config.JSONBinaryStoreConfig) *BinaryStore {
return &BinaryStore{config: *c}
}

// LoadEncryptedFile loads an encrypted json file onto a sops.Tree object
Expand Down Expand Up @@ -317,3 +328,9 @@ func (store *Store) EmitExample() []byte {
}
return bytes
}

func (store *Store) Configure(c *config.Config) {
}

func (store *BinaryStore) Configure(c *config.Config) {
}
25 changes: 17 additions & 8 deletions stores/yaml/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import (
"io"
"strings"

"gopkg.in/yaml.v3"
"go.mozilla.org/sops/v3"
"go.mozilla.org/sops/v3/config"
"go.mozilla.org/sops/v3/stores"
"gopkg.in/yaml.v3"
)

// Store handles storage of YAML data
type Store struct {
config config.YAMLStoreConfig
}

func NewStore(c *config.YAMLStoreConfig) *Store {
return &Store{config: *c}
}

func (store Store) appendCommentToList(comment string, list []interface{}) []interface{} {
Expand Down Expand Up @@ -76,7 +82,7 @@ func (store Store) nodeToTreeValue(node *yaml.Node, commentsWereHandled bool) (i
node.Decode(&result)
return result, nil
case yaml.AliasNode:
return store.nodeToTreeValue(node.Alias, false);
return store.nodeToTreeValue(node.Alias, false)
}
return nil, nil
}
Expand All @@ -100,7 +106,7 @@ func (store Store) appendYamlNodeToTreeBranch(node *yaml.Node, branch sops.TreeB
case yaml.MappingNode:
for i := 0; i < len(node.Content); i += 2 {
key := node.Content[i]
value := node.Content[i + 1]
value := node.Content[i+1]
branch = store.appendCommentToMap(key.HeadComment, branch)
branch = store.appendCommentToMap(key.LineComment, branch)
handleValueComments := value.Kind == yaml.ScalarNode || value.Kind == yaml.AliasNode
Expand Down Expand Up @@ -206,7 +212,7 @@ func (store *Store) appendSequence(in []interface{}, sequence *yaml.Node) {
if beginning {
comments = store.addCommentsHead(sequence, comments)
} else {
comments = store.addCommentsFoot(sequence.Content[len(sequence.Content) - 1], comments)
comments = store.addCommentsFoot(sequence.Content[len(sequence.Content)-1], comments)
}
}
}
Expand All @@ -233,7 +239,7 @@ func (store *Store) appendTreeBranch(branch sops.TreeBranch, mapping *yaml.Node)
if beginning {
comments = store.addCommentsHead(mapping, comments)
} else {
comments = store.addCommentsFoot(mapping.Content[len(mapping.Content) - 1], comments)
comments = store.addCommentsFoot(mapping.Content[len(mapping.Content)-1], comments)
}
}
}
Expand Down Expand Up @@ -317,7 +323,7 @@ func (store *Store) LoadPlainFile(in []byte) (sops.TreeBranches, error) {
// EmitEncryptedFile returns the encrypted bytes of the yaml file corresponding to a
// sops.Tree runtime object
func (store *Store) EmitEncryptedFile(in sops.Tree) ([]byte, error) {
var b bytes.Buffer
var b bytes.Buffer
e := yaml.NewEncoder(io.Writer(&b))
e.SetIndent(4)
for _, branch := range in.Branches {
Expand All @@ -331,7 +337,7 @@ func (store *Store) EmitEncryptedFile(in sops.Tree) ([]byte, error) {
// Create copy of branch with metadata appended
branch = append(sops.TreeBranch(nil), branch...)
branch = append(branch, sops.TreeItem{
Key: "sops",
Key: "sops",
Value: stores.MetadataFromInternal(in.Metadata),
})
// Marshal branch to global mapping node
Expand All @@ -349,7 +355,7 @@ func (store *Store) EmitEncryptedFile(in sops.Tree) ([]byte, error) {
// EmitPlainFile returns the plaintext bytes of the yaml file corresponding to a
// sops.TreeBranches runtime object
func (store *Store) EmitPlainFile(branches sops.TreeBranches) ([]byte, error) {
var b bytes.Buffer
var b bytes.Buffer
e := yaml.NewEncoder(io.Writer(&b))
e.SetIndent(4)
for _, branch := range branches {
Expand Down Expand Up @@ -391,3 +397,6 @@ func (store *Store) EmitExample() []byte {
}
return bytes
}

func (store *Store) Configure(c *config.Config) {
}