Skip to content
Merged
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
20 changes: 12 additions & 8 deletions sops.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ func (branch TreeBranch) walkBranch(in TreeBranch, path []string, commentsStack
}
key, ok := item.Key.(string)
if !ok {
return nil, fmt.Errorf("Tree contains a non-string key (type %T): %s. Only string keys are"+
"supported", item.Key, item.Key)
return nil, fmt.Errorf(
"Tree contains a non-string key (type %T): %s. Only string keys are supported", item.Key, item.Key)
}
newV, err := branch.walkValue(item.Value, append(path, key), commentsStack, onLeaves)
if err != nil {
Expand Down Expand Up @@ -554,6 +554,8 @@ func (tree Tree) Encrypt(key []byte, cipher Cipher) (string, error) {
if ok && tree.Metadata.UnencryptedCommentRegex != "" {
// If an encrypted comment matches tree.Metadata.UnencryptedCommentRegex, decryption will fail
// as the MAC does not match, and the commented value will not be decrypted.
// Note that cipher.Encrypt() returns a string, but we stored the result in an interface{}
// variable, so we have to cast it back.
matched, _ := regexp.Match(tree.Metadata.UnencryptedCommentRegex, []byte(in.(string)))
if matched {
return nil, fmt.Errorf("Encrypted comment %q matches UnencryptedCommentRegex! Make sure that UnencryptedCommentRegex cannot match an encrypted comment.", in)
Expand Down Expand Up @@ -595,13 +597,13 @@ func (tree Tree) Decrypt(key []byte, cipher Cipher) (string, error) {
}
walk := func(branch TreeBranch) error {
_, err := branch.walkBranch(branch, make([]string, 0), make([][]string, 0), func(in interface{}, path []string, commentsStack [][]string) (interface{}, error) {
c, ok := in.(Comment)
encrypted := tree.shouldBeEncrypted(path, commentsStack, ok)
c, isComment := in.(Comment)
encrypted := tree.shouldBeEncrypted(path, commentsStack, isComment)
var v interface{}
if encrypted {
var err error
pathString := strings.Join(path, ":") + ":"
if ok {
if isComment {
v, err = cipher.Decrypt(c.Value, key, pathString)
if err != nil {
// Assume the comment was not encrypted in the first place
Expand All @@ -612,18 +614,20 @@ func (tree Tree) Decrypt(key []byte, cipher Cipher) (string, error) {
"SOPS.")
v = c
}
} else {
v, err = cipher.Decrypt(in.(string), key, pathString)
} else if inStr, inIsStr := in.(string); inIsStr {
v, err = cipher.Decrypt(inStr, key, pathString)
if err != nil {
return nil, fmt.Errorf("Could not decrypt value: %s", err)
}
} else {
return nil, fmt.Errorf("Expected encrypted value as string, but got %T", in)
}
} else {
v = in
}
if !tree.Metadata.MACOnlyEncrypted || encrypted {
// Only add to MAC if not a comment
if _, ok := v.(Comment); !ok {
if !isComment {
bytes, err := ToBytes(v)
if err != nil {
return nil, fmt.Errorf("Could not convert %s to bytes: %s", in, err)
Expand Down