Skip to content

Commit

Permalink
Sort masterkeys according to decryption-order
Browse files Browse the repository at this point in the history
Co-authored-by: Gabriel Martinez <19713226+GMartinez-Sisti@users.noreply.github.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Bastien Wermeille <bastien.wermeille@gmail.com>
Co-authored-by: Hidde Beydals <hiddeco@users.noreply.github.com>
Signed-off-by: Boris Kreitchman <bkreitch@gmail.com>
  • Loading branch information
5 people committed Dec 18, 2023
1 parent 3028179 commit ba7d1a2
Show file tree
Hide file tree
Showing 21 changed files with 366 additions and 140 deletions.
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ Given that, the only command a SOPS user needs is:
encrypted if modified, and saved back to its original location. All of these
steps, apart from the actual editing, are transparent to the user.

The order in which available decryption methods are tried can be specified with
``--decryption-order`` option or **SOPS_DECRYPTION_ORDER** environment variable
as a comma separated list. The default order is ``age,pgp``. Offline methods are
tried first and then the remaining ones.

Test with the dev PGP key
~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
7 changes: 7 additions & 0 deletions age/keysource.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
SopsAgeKeyUserConfigPath = "sops/age/keys.txt"
// On macOS, os.UserConfigDir() ignores XDG_CONFIG_HOME. So we handle that manually.
xdgConfigHome = "XDG_CONFIG_HOME"
// String representation of the key type
KeyType = "age"
)

// log is the global logger for any age MasterKey.
Expand Down Expand Up @@ -225,6 +227,11 @@ func (key *MasterKey) ToMap() map[string]interface{} {
return out
}

// TypeToString converts key type to a string.
func (key *MasterKey) TypeToString() string {
return KeyType
}

func getUserConfigDir() (string, error) {
if runtime.GOOS == "darwin" {
if userConfigDir, ok := os.LookupEnv(xdgConfigHome); ok && userConfigDir != "" {
Expand Down
10 changes: 10 additions & 0 deletions azkv/keysource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import (
"github.com/getsops/sops/v3/logging"
)

const (
// String representation of the key type
KeyType = "azure_kv"
)

var (
// log is the global logger for any Azure Key Vault MasterKey.
log *logrus.Logger
Expand Down Expand Up @@ -215,6 +220,11 @@ func (key MasterKey) ToMap() map[string]interface{} {
return out
}

// TypeToString converts key type to a string.
func (key *MasterKey) TypeToString() string {
return KeyType
}

// getTokenCredential returns the tokenCredential of the MasterKey, or
// azidentity.NewDefaultAzureCredential.
func (key *MasterKey) getTokenCredential() (azcore.TokenCredential, error) {
Expand Down
1 change: 1 addition & 0 deletions cmd/sops/codes/codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
NoFileSpecified int = 100
CouldNotRetrieveKey int = 128
NoEncryptionKeyFound int = 111
DuplicateDecryptionKeyType int = 112
FileHasNotBeenModified int = 200
NoEditorFound int = 201
FailedToCompareVersions int = 202
Expand Down
15 changes: 9 additions & 6 deletions cmd/sops/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ type DecryptTreeOpts struct {
Tree *sops.Tree
// KeyServices are the key services to be used for decryption of the data key
KeyServices []keyservice.KeyServiceClient
// DecryptionOrder is the order in which available decryption methods are tried
DecryptionOrder []string
// IgnoreMac is whether or not to ignore the Message Authentication Code included in the SOPS tree
IgnoreMac bool
// Cipher is the cryptographic cipher to use to decrypt the values inside the tree
Expand All @@ -80,7 +82,7 @@ type DecryptTreeOpts struct {

// DecryptTree decrypts the tree passed in through the DecryptTreeOpts and additionally returns the decrypted data key
func DecryptTree(opts DecryptTreeOpts) (dataKey []byte, err error) {
dataKey, err = opts.Tree.Metadata.GetDataKeyWithKeyServices(opts.KeyServices)
dataKey, err = opts.Tree.Metadata.GetDataKeyWithKeyServices(opts.KeyServices, opts.DecryptionOrder)
if err != nil {
return nil, NewExitError(err, codes.CouldNotRetrieveKey)
}
Expand Down Expand Up @@ -222,11 +224,12 @@ func GetKMSKeyWithEncryptionCtx(tree *sops.Tree) (keyGroupIndex int, keyIndex in

// GenericDecryptOpts represents decryption options and config
type GenericDecryptOpts struct {
Cipher sops.Cipher
InputStore sops.Store
InputPath string
IgnoreMAC bool
KeyServices []keyservice.KeyServiceClient
Cipher sops.Cipher
InputStore sops.Store
InputPath string
IgnoreMAC bool
KeyServices []keyservice.KeyServiceClient
DecryptionOrder []string
}

// LoadEncryptedFileWithBugFixes is a wrapper around LoadEncryptedFile which includes
Expand Down
24 changes: 13 additions & 11 deletions cmd/sops/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ const notBinaryHint = ("This is likely not an encrypted binary file?" +
" If not, use --output-type to select the correct output type.")

type decryptOpts struct {
Cipher sops.Cipher
InputStore sops.Store
OutputStore sops.Store
InputPath string
IgnoreMAC bool
Extract []interface{}
KeyServices []keyservice.KeyServiceClient
Cipher sops.Cipher
InputStore sops.Store
OutputStore sops.Store
InputPath string
IgnoreMAC bool
Extract []interface{}
KeyServices []keyservice.KeyServiceClient
DecryptionOrder []string
}

func decrypt(opts decryptOpts) (decryptedFile []byte, err error) {
Expand All @@ -37,10 +38,11 @@ func decrypt(opts decryptOpts) (decryptedFile []byte, err error) {
}

_, err = common.DecryptTree(common.DecryptTreeOpts{
Cipher: opts.Cipher,
IgnoreMac: opts.IgnoreMAC,
Tree: tree,
KeyServices: opts.KeyServices,
Cipher: opts.Cipher,
IgnoreMac: opts.IgnoreMAC,
Tree: tree,
KeyServices: opts.KeyServices,
DecryptionOrder: opts.DecryptionOrder,
})
if err != nil {
return nil, err
Expand Down
21 changes: 13 additions & 8 deletions cmd/sops/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import (
)

type editOpts struct {
Cipher sops.Cipher
InputStore common.Store
OutputStore common.Store
InputPath string
IgnoreMAC bool
KeyServices []keyservice.KeyServiceClient
ShowMasterKeys bool
Cipher sops.Cipher
InputStore common.Store
OutputStore common.Store
InputPath string
IgnoreMAC bool
KeyServices []keyservice.KeyServiceClient
DecryptionOrder []string
ShowMasterKeys bool
}

type editExampleOpts struct {
Expand Down Expand Up @@ -96,7 +97,11 @@ func edit(opts editOpts) ([]byte, error) {
}
// Decrypt the file
dataKey, err := common.DecryptTree(common.DecryptTreeOpts{
Cipher: opts.Cipher, IgnoreMac: opts.IgnoreMAC, Tree: tree, KeyServices: opts.KeyServices,
Cipher: opts.Cipher,
IgnoreMac: opts.IgnoreMAC,
Tree: tree,
KeyServices: opts.KeyServices,
DecryptionOrder: opts.DecryptionOrder,
})
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit ba7d1a2

Please sign in to comment.