@@ -157,13 +157,19 @@ func main() {
157157 inputStore := inputStore (c , fileName )
158158
159159 svcs := keyservices (c )
160+
161+ order , err := decryptionOrder (c .String ("decryption-order" ))
162+ if err != nil {
163+ return toExitError (err )
164+ }
160165 opts := decryptOpts {
161- OutputStore : & dotenv.Store {},
162- InputStore : inputStore ,
163- InputPath : fileName ,
164- Cipher : aes .NewCipher (),
165- KeyServices : svcs ,
166- IgnoreMAC : c .Bool ("ignore-mac" ),
166+ OutputStore : & dotenv.Store {},
167+ InputStore : inputStore ,
168+ InputPath : fileName ,
169+ Cipher : aes .NewCipher (),
170+ KeyServices : svcs ,
171+ DecryptionOrder : order ,
172+ IgnoreMAC : c .Bool ("ignore-mac" ),
167173 }
168174
169175 output , err := decrypt (opts )
@@ -226,13 +232,19 @@ func main() {
226232 outputStore := outputStore (c , fileName )
227233
228234 svcs := keyservices (c )
235+
236+ order , err := decryptionOrder (c .String ("decryption-order" ))
237+ if err != nil {
238+ return toExitError (err )
239+ }
229240 opts := decryptOpts {
230- OutputStore : outputStore ,
231- InputStore : inputStore ,
232- InputPath : fileName ,
233- Cipher : aes .NewCipher (),
234- KeyServices : svcs ,
235- IgnoreMAC : c .Bool ("ignore-mac" ),
241+ OutputStore : outputStore ,
242+ InputStore : inputStore ,
243+ InputPath : fileName ,
244+ Cipher : aes .NewCipher (),
245+ KeyServices : svcs ,
246+ DecryptionOrder : order ,
247+ IgnoreMAC : c .Bool ("ignore-mac" ),
236248 }
237249
238250 output , err := decrypt (opts )
@@ -300,21 +312,25 @@ func main() {
300312 if info .IsDir () && ! c .Bool ("recursive" ) {
301313 return fmt .Errorf ("can't operate on a directory without --recursive flag." )
302314 }
315+ order , err := decryptionOrder (c .String ("decryption-order" ))
316+ if err != nil {
317+ return toExitError (err )
318+ }
303319 err = filepath .Walk (path , func (subPath string , info os.FileInfo , err error ) error {
304320 if err != nil {
305321 return toExitError (err )
306322 }
307323 if ! info .IsDir () {
308324 err = publishcmd .Run (publishcmd.Opts {
309- ConfigPath : configPath ,
310- InputPath : subPath ,
311- Cipher : aes .NewCipher (),
312- KeyServices : keyservices (c ),
313- InputStore : inputStore ( c , subPath ) ,
314- Interactive : ! c . Bool ( "yes" ),
315- OmitExtensions : c .Bool ("omit-extensions " ),
316- Recursive : c .Bool ("recursive " ),
317- RootPath : path ,
325+ ConfigPath : configPath ,
326+ InputPath : subPath ,
327+ Cipher : aes .NewCipher (),
328+ KeyServices : keyservices (c ),
329+ DecryptionOrder : order ,
330+ InputStore : inputStore ( c , subPath ),
331+ Interactive : ! c .Bool ("yes " ),
332+ OmitExtensions : c .Bool ("omit-extensions " ),
333+ Recursive : c . Bool ( "recursive" ) ,
318334 })
319335 if cliErr , ok := err .(* cli.ExitError ); ok && cliErr != nil {
320336 return cliErr
@@ -746,6 +762,11 @@ func main() {
746762 Name : "output" ,
747763 Usage : "Save the output after encryption or decryption to the file specified" ,
748764 },
765+ cli.StringFlag {
766+ Name : "decryption-order" ,
767+ Usage : "comma separated list of decryption key types" ,
768+ EnvVar : "SOPS_DECRYPTION_ORDER" ,
769+ },
749770 }, keyserviceFlags ... )
750771
751772 app .Action = func (c * cli.Context ) error {
@@ -827,6 +848,10 @@ func main() {
827848 outputStore := outputStore (c , fileName )
828849 svcs := keyservices (c )
829850
851+ order , err := decryptionOrder (c .String ("decryption-order" ))
852+ if err != nil {
853+ return toExitError (err )
854+ }
830855 var output []byte
831856 if c .Bool ("encrypt" ) {
832857 var groups []sops.KeyGroup
@@ -862,13 +887,14 @@ func main() {
862887 return common .NewExitError (fmt .Errorf ("error parsing --extract path: %s" , err ), codes .InvalidTreePathFormat )
863888 }
864889 output , err = decrypt (decryptOpts {
865- OutputStore : outputStore ,
866- InputStore : inputStore ,
867- InputPath : fileName ,
868- Cipher : aes .NewCipher (),
869- Extract : extract ,
870- KeyServices : svcs ,
871- IgnoreMAC : c .Bool ("ignore-mac" ),
890+ OutputStore : outputStore ,
891+ InputStore : inputStore ,
892+ InputPath : fileName ,
893+ Cipher : aes .NewCipher (),
894+ Extract : extract ,
895+ KeyServices : svcs ,
896+ DecryptionOrder : order ,
897+ IgnoreMAC : c .Bool ("ignore-mac" ),
872898 })
873899 }
874900 if c .Bool ("rotate" ) {
@@ -943,6 +969,7 @@ func main() {
943969 InputPath : fileName ,
944970 Cipher : aes .NewCipher (),
945971 KeyServices : svcs ,
972+ DecryptionOrder : order ,
946973 IgnoreMAC : c .Bool ("ignore-mac" ),
947974 AddMasterKeys : addMasterKeys ,
948975 RemoveMasterKeys : rmMasterKeys ,
@@ -962,14 +989,15 @@ func main() {
962989 return toExitError (err )
963990 }
964991 output , err = set (setOpts {
965- OutputStore : outputStore ,
966- InputStore : inputStore ,
967- InputPath : fileName ,
968- Cipher : aes .NewCipher (),
969- KeyServices : svcs ,
970- IgnoreMAC : c .Bool ("ignore-mac" ),
971- Value : value ,
972- TreePath : path ,
992+ OutputStore : outputStore ,
993+ InputStore : inputStore ,
994+ InputPath : fileName ,
995+ Cipher : aes .NewCipher (),
996+ KeyServices : svcs ,
997+ DecryptionOrder : order ,
998+ IgnoreMAC : c .Bool ("ignore-mac" ),
999+ Value : value ,
1000+ TreePath : path ,
9731001 })
9741002 }
9751003
@@ -978,13 +1006,14 @@ func main() {
9781006 _ , statErr := os .Stat (fileName )
9791007 fileExists := statErr == nil
9801008 opts := editOpts {
981- OutputStore : outputStore ,
982- InputStore : inputStore ,
983- InputPath : fileName ,
984- Cipher : aes .NewCipher (),
985- KeyServices : svcs ,
986- IgnoreMAC : c .Bool ("ignore-mac" ),
987- ShowMasterKeys : c .Bool ("show-master-keys" ),
1009+ OutputStore : outputStore ,
1010+ InputStore : inputStore ,
1011+ InputPath : fileName ,
1012+ Cipher : aes .NewCipher (),
1013+ KeyServices : svcs ,
1014+ DecryptionOrder : order ,
1015+ IgnoreMAC : c .Bool ("ignore-mac" ),
1016+ ShowMasterKeys : c .Bool ("show-master-keys" ),
9881017 }
9891018 if fileExists {
9901019 output , err = edit (opts )
@@ -1319,3 +1348,18 @@ func extractSetArguments(set string) (path []interface{}, valueToInsert interfac
13191348 }
13201349 return path , valueToInsert , nil
13211350}
1351+
1352+ func decryptionOrder (decryptionOrder string ) ([]string , error ) {
1353+ if decryptionOrder == "" {
1354+ return sops .SopsDecryptionOrderDefault , nil
1355+ }
1356+ orderList := strings .Split (decryptionOrder , "," )
1357+ unique := make (map [string ]bool )
1358+ for _ , v := range orderList {
1359+ if _ , ok := unique [v ]; ok {
1360+ return nil , common .NewExitError (fmt .Sprintf ("Duplicate decryption key type: %s" , v ), codes .DuplicateDecryptionKeyType )
1361+ }
1362+ unique [v ] = true
1363+ }
1364+ return orderList , nil
1365+ }
0 commit comments