Skip to content

Commit e783741

Browse files
authored
Merge pull request #1927 from felixfontein/binary-store-encryption-opts
Ignore encryption selection options for binary store (and warn when they are used)
2 parents 8117a49 + 6bb6621 commit e783741

File tree

6 files changed

+71
-6
lines changed

6 files changed

+71
-6
lines changed

cmd/sops/main.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ func main() {
10231023
}
10241024
svcs := keyservices(c)
10251025

1026-
encConfig, err := getEncryptConfig(c, fileNameOverride, nil)
1026+
encConfig, err := getEncryptConfig(c, fileNameOverride, inputStore, nil)
10271027
if err != nil {
10281028
return toExitError(err)
10291029
}
@@ -1369,7 +1369,7 @@ func main() {
13691369
}
13701370
} else {
13711371
// File doesn't exist, edit the example file instead
1372-
encConfig, err := getEncryptConfig(c, fileName, nil)
1372+
encConfig, err := getEncryptConfig(c, fileName, inputStore, nil)
13731373
if err != nil {
13741374
return toExitError(err)
13751375
}
@@ -1908,7 +1908,7 @@ func main() {
19081908
}
19091909
var output []byte
19101910
if isEncryptMode {
1911-
encConfig, err := getEncryptConfig(c, fileNameOverride, config)
1911+
encConfig, err := getEncryptConfig(c, fileNameOverride, inputStore, config)
19121912
if err != nil {
19131913
return toExitError(err)
19141914
}
@@ -1996,7 +1996,7 @@ func main() {
19961996
output, err = edit(opts)
19971997
} else {
19981998
// File doesn't exist, edit the example file instead
1999-
encConfig, err := getEncryptConfig(c, fileNameOverride, config)
1999+
encConfig, err := getEncryptConfig(c, fileNameOverride, inputStore, config)
20002000
if err != nil {
20012001
return toExitError(err)
20022002
}
@@ -2050,7 +2050,7 @@ func main() {
20502050
}
20512051
}
20522052

2053-
func getEncryptConfig(c *cli.Context, fileName string, optionalConfig *config.Config) (encryptConfig, error) {
2053+
func getEncryptConfig(c *cli.Context, fileName string, inputStore common.Store, optionalConfig *config.Config) (encryptConfig, error) {
20542054
unencryptedSuffix := c.String("unencrypted-suffix")
20552055
encryptedSuffix := c.String("encrypted-suffix")
20562056
encryptedRegex := c.String("encrypted-regex")
@@ -2090,6 +2090,38 @@ func getEncryptConfig(c *cli.Context, fileName string, optionalConfig *config.Co
20902090
}
20912091
}
20922092

2093+
isSingleValueStore := false
2094+
if svs, ok := inputStore.(sops.SingleValueStore); ok {
2095+
isSingleValueStore = svs.IsSingleValueStore()
2096+
}
2097+
2098+
if isSingleValueStore {
2099+
// Warn about settings that potentially disable encryption of the single key.
2100+
if unencryptedSuffix != "" {
2101+
log.Warn(fmt.Sprintf("Using an unencrypted suffix does not make sense with the input store (the %s store produces one key that should always be encrypted) and will be ignored.", inputStore.Name()))
2102+
}
2103+
if encryptedSuffix != "" {
2104+
log.Warn(fmt.Sprintf("Using an encrypted suffix does not make sense with the input store (the %s store produces one key that should always be encrypted) and will be ignored.", inputStore.Name()))
2105+
}
2106+
if encryptedRegex != "" {
2107+
log.Warn(fmt.Sprintf("Using an encrypted regex does not make sense with the input store (the %s store produces one key that should always be encrypted) and will be ignored.", inputStore.Name()))
2108+
}
2109+
if unencryptedRegex != "" {
2110+
log.Warn(fmt.Sprintf("Using an unencrypted regex does not make sense with the input store (the %s store produces one key that should always be encrypted) and will be ignored.", inputStore.Name()))
2111+
}
2112+
if encryptedCommentRegex != "" {
2113+
log.Warn(fmt.Sprintf("Using an encrypted comment regex does not make sense with the input store (the %s store never produces comments) and will be ignored.", inputStore.Name()))
2114+
}
2115+
// Do not warn about unencryptedCommentRegex and macOnlyEncrypted since they cannot have any effect.
2116+
unencryptedSuffix = ""
2117+
encryptedSuffix = ""
2118+
encryptedRegex = ""
2119+
unencryptedRegex = ""
2120+
encryptedCommentRegex = ""
2121+
unencryptedCommentRegex = ""
2122+
macOnlyEncrypted = false
2123+
}
2124+
20932125
cryptRuleCount := 0
20942126
if unencryptedSuffix != "" {
20952127
cryptRuleCount++
@@ -2115,7 +2147,7 @@ func getEncryptConfig(c *cli.Context, fileName string, optionalConfig *config.Co
21152147
}
21162148

21172149
// only supply the default UnencryptedSuffix when EncryptedSuffix, EncryptedRegex, and others are not provided
2118-
if cryptRuleCount == 0 {
2150+
if cryptRuleCount == 0 && !isSingleValueStore {
21192151
unencryptedSuffix = sops.DefaultUnencryptedSuffix
21202152
}
21212153

sops.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,14 @@ type Store interface {
734734
PlainFileEmitter
735735
ValueEmitter
736736
CheckEncrypted
737+
Name() string
738+
}
739+
740+
// SingleValueStore is the interface for determining whether a store uses only
741+
// one single key and no comments. This is basically identifying the binary store.
742+
type SingleValueStore interface {
743+
Store
744+
IsSingleValueStore() bool
737745
}
738746

739747
// MasterKeyCount returns the number of master keys available

stores/dotenv/store.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ func NewStore(c *config.DotenvStoreConfig) *Store {
2323
return &Store{config: *c}
2424
}
2525

26+
func (store *Store) Name() string {
27+
return "dotenv"
28+
}
29+
2630
// LoadEncryptedFile loads an encrypted file's bytes onto a sops.Tree runtime object
2731
func (store *Store) LoadEncryptedFile(in []byte) (sops.Tree, error) {
2832
branches, err := store.LoadPlainFile(in)

stores/ini/store.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func NewStore(c *config.INIStoreConfig) *Store {
2121
return &Store{config: c}
2222
}
2323

24+
func (store *Store) Name() string {
25+
return "ini"
26+
}
27+
2428
func (store Store) encodeTree(branches sops.TreeBranches) ([]byte, error) {
2529
iniFile := ini.Empty(ini.LoadOptions{AllowNonUniqueSections: true})
2630
iniFile.DeleteSection(ini.DefaultSection)

stores/json/store.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,25 @@ func NewStore(c *config.JSONStoreConfig) *Store {
2222
return &Store{config: *c}
2323
}
2424

25+
func (store *Store) Name() string {
26+
return "json"
27+
}
28+
2529
// BinaryStore handles storage of binary data in a JSON envelope.
2630
type BinaryStore struct {
2731
store Store
2832
config config.JSONBinaryStoreConfig
2933
}
3034

35+
// The binary store uses a single key ("data") to store everything.
36+
func (store *BinaryStore) IsSingleValueStore() bool {
37+
return true
38+
}
39+
40+
func (store *BinaryStore) Name() string {
41+
return "binary"
42+
}
43+
3144
func NewBinaryStore(c *config.JSONBinaryStoreConfig) *BinaryStore {
3245
return &BinaryStore{config: *c, store: *NewStore(&config.JSONStoreConfig{
3346
Indent: c.Indent,

stores/yaml/store.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func NewStore(c *config.YAMLStoreConfig) *Store {
2424
return &Store{config: *c}
2525
}
2626

27+
func (store *Store) Name() string {
28+
return "yaml"
29+
}
30+
2731
func (store Store) appendCommentToList(comment string, list []interface{}) []interface{} {
2832
if comment != "" {
2933
for _, commentLine := range strings.Split(comment, "\n") {

0 commit comments

Comments
 (0)