44	"context" 
55	encodingjson "encoding/json" 
66	"fmt" 
7+ 
78	"net" 
89	"net/url" 
910	"os" 
@@ -38,6 +39,7 @@ import (
3839	"github.com/getsops/sops/v3/keyservice" 
3940	"github.com/getsops/sops/v3/kms" 
4041	"github.com/getsops/sops/v3/logging" 
42+ 	"github.com/getsops/sops/v3/ovhkms" 
4143	"github.com/getsops/sops/v3/pgp" 
4244	"github.com/getsops/sops/v3/stores/dotenv" 
4345	"github.com/getsops/sops/v3/stores/json" 
@@ -559,6 +561,7 @@ func main() {
559561						pgpFps  :=  c .StringSlice ("pgp" )
560562						kmsArns  :=  c .StringSlice ("kms" )
561563						gcpKmses  :=  c .StringSlice ("gcp-kms" )
564+ 						ovhKmses  :=  c .StringSlice ("ovh-kms" )
562565						vaultURIs  :=  c .StringSlice ("hc-vault-transit" )
563566						azkvs  :=  c .StringSlice ("azure-kv" )
564567						ageRecipients  :=  c .StringSlice ("age" )
@@ -575,6 +578,14 @@ func main() {
575578						for  _ , kms  :=  range  gcpKmses  {
576579							group  =  append (group , gcpkms .NewMasterKeyFromResourceID (kms ))
577580						}
581+ 						for  _ , kms  :=  range  ovhKmses  {
582+ 							k , err  :=  ovhkms .NewMasterKeyFromKeyID (kms )
583+ 							if  err  !=  nil  {
584+ 								log .WithError (err ).Error ("Failed to add key" )
585+ 								continue 
586+ 							}
587+ 							group  =  append (group , k )
588+ 						}
578589						for  _ , uri  :=  range  vaultURIs  {
579590							k , err  :=  hcvault .NewMasterKeyFromURI (uri )
580591							if  err  !=  nil  {
@@ -1118,6 +1129,14 @@ func main() {
11181129					Name :  "rm-pgp" ,
11191130					Usage : "remove the provided comma-separated list of PGP fingerprints from the list of master keys on the given file" ,
11201131				},
1132+ 				cli.StringFlag {
1133+ 					Name :  "add-ovh-kms" ,
1134+ 					Usage : "add the provided comma-separated list of OVH KMS key resource IDs from the list of master keys on the given file" ,
1135+ 				},
1136+ 				cli.StringFlag {
1137+ 					Name :  "rm-ovh-kms" ,
1138+ 					Usage : "remove the provided comma-separated list of OVH KMS key resource IDs from the list of master keys on the given file" ,
1139+ 				},
11211140				cli.StringFlag {
11221141					Name :  "filename-override" ,
11231142					Usage : "Use this filename instead of the provided argument for loading configuration, and for determining input type and output type" ,
@@ -1144,8 +1163,8 @@ func main() {
11441163					return  toExitError (err )
11451164				}
11461165				if  _ , err  :=  os .Stat (fileName ); os .IsNotExist (err ) {
1147- 					if  c .String ("add-kms" ) !=  ""  ||  c .String ("add-pgp" ) !=  ""  ||  c .String ("add-gcp-kms" ) !=  ""  ||  c .String ("add-hc-vault-transit" ) !=  ""  ||  c .String ("add-azure-kv" ) !=  ""  ||  c .String ("add-age" ) !=  ""  || 
1148- 						c .String ("rm-kms" ) !=  ""  ||  c .String ("rm-pgp" ) !=  ""  ||  c .String ("rm-gcp-kms" ) !=  ""  ||  c .String ("rm-hc-vault-transit" ) !=  ""  ||  c .String ("rm-azure-kv" ) !=  ""  ||  c .String ("rm-age" ) !=  ""  {
1166+ 					if  c .String ("add-kms" ) !=  ""  ||  c .String ("add-pgp" ) !=  ""  ||  c .String ("add-gcp-kms" ) !=  ""  ||  c .String ("add-hc-vault-transit" ) !=  ""  ||  c .String ("add-azure-kv" ) !=  ""  ||  c .String ("add-age" ) !=  ""  ||   c . String ( "add-ovh-kms" )  !=   ""   || 
1167+ 						c .String ("rm-kms" ) !=  ""  ||  c .String ("rm-pgp" ) !=  ""  ||  c .String ("rm-gcp-kms" ) !=  ""  ||  c .String ("rm-hc-vault-transit" ) !=  ""  ||  c .String ("rm-azure-kv" ) !=  ""  ||  c .String ("rm-age" ) !=  ""  ||   c . String ( "rm-ovh-kms" )  !=   ""   {
11491168						return  common .NewExitError (fmt .Sprintf ("Error: cannot add or remove keys on non-existent file %q, use the `edit` subcommand instead." , fileName ), codes .CannotChangeKeysFromNonExistentFile )
11501169					}
11511170				}
@@ -1620,6 +1639,11 @@ func main() {
16201639			Usage :  "comma separated list of GCP KMS resource IDs" ,
16211640			EnvVar : "SOPS_GCP_KMS_IDS" ,
16221641		},
1642+ 		cli.StringFlag {
1643+ 			Name :   "ovh-kms" ,
1644+ 			Usage :  "comma separated list of OVH KMS Key IDs with endpoint (egs: eu-west-sbg.okms.ovh.net/12345678-1234-1234-1234-123456789012)" ,
1645+ 			EnvVar : "SOPS_OVH_KMS_IDS" ,
1646+ 		},
16231647		cli.StringFlag {
16241648			Name :   "azure-kv" ,
16251649			Usage :  "comma separated list of Azure Key Vault URLs" ,
@@ -1802,8 +1826,8 @@ func main() {
18021826			return  toExitError (err )
18031827		}
18041828		if  _ , err  :=  os .Stat (fileName ); os .IsNotExist (err ) {
1805- 			if  c .String ("add-kms" ) !=  ""  ||  c .String ("add-pgp" ) !=  ""  ||  c .String ("add-gcp-kms" ) !=  ""  ||  c .String ("add-hc-vault-transit" ) !=  ""  ||  c .String ("add-azure-kv" ) !=  ""  ||  c .String ("add-age" ) !=  ""  || 
1806- 				c .String ("rm-kms" ) !=  ""  ||  c .String ("rm-pgp" ) !=  ""  ||  c .String ("rm-gcp-kms" ) !=  ""  ||  c .String ("rm-hc-vault-transit" ) !=  ""  ||  c .String ("rm-azure-kv" ) !=  ""  ||  c .String ("rm-age" ) !=  ""  {
1829+ 			if  c .String ("add-kms" ) !=  ""  ||  c .String ("add-pgp" ) !=  ""  ||  c .String ("add-gcp-kms" ) !=  ""  ||  c .String ("add-hc-vault-transit" ) !=  ""  ||  c .String ("add-azure-kv" ) !=  ""  ||  c .String ("add-age" ) !=  ""  ||   c . String ( "add-ovh-kms" )  !=   ""   || 
1830+ 				c .String ("rm-kms" ) !=  ""  ||  c .String ("rm-pgp" ) !=  ""  ||  c .String ("rm-gcp-kms" ) !=  ""  ||  c .String ("rm-hc-vault-transit" ) !=  ""  ||  c .String ("rm-azure-kv" ) !=  ""  ||  c .String ("rm-age" ) !=  ""  ||   c . String ( "rm-ovh-kms" )  !=   ""   {
18071831				return  common .NewExitError (fmt .Sprintf ("Error: cannot add or remove keys on non-existent file %q, use `--kms` and `--pgp` instead." , fileName ), codes .CannotChangeKeysFromNonExistentFile )
18081832			}
18091833			if  isEncryptMode  ||  isDecryptMode  ||  isRotateMode  {
@@ -2096,7 +2120,7 @@ func getEncryptConfig(c *cli.Context, fileName string) (encryptConfig, error) {
20962120	}, nil 
20972121}
20982122
2099- func  getMasterKeys (c  * cli.Context , kmsEncryptionContext  map [string ]* string , kmsOptionName  string , pgpOptionName  string , gcpKmsOptionName  string , azureKvOptionName  string , hcVaultTransitOptionName  string , ageOptionName  string ) ([]keys.MasterKey , error ) {
2123+ func  getMasterKeys (c  * cli.Context , kmsEncryptionContext  map [string ]* string , kmsOptionName  string , pgpOptionName  string , gcpKmsOptionName  string , azureKvOptionName  string , hcVaultTransitOptionName  string , ageOptionName  string ,  ovhKmsOptionName   string ) ([]keys.MasterKey , error ) {
21002124	var  masterKeys  []keys.MasterKey 
21012125	for  _ , k  :=  range  kms .MasterKeysFromArnString (c .String (kmsOptionName ), kmsEncryptionContext , c .String ("aws-profile" )) {
21022126		masterKeys  =  append (masterKeys , k )
@@ -2128,16 +2152,23 @@ func getMasterKeys(c *cli.Context, kmsEncryptionContext map[string]*string, kmsO
21282152	for  _ , k  :=  range  ageKeys  {
21292153		masterKeys  =  append (masterKeys , k )
21302154	}
2155+ 	ovhKeys , err  :=  ovhkms .MasterKeysFromResourceIDString (c .String (ovhKmsOptionName ))
2156+ 	if  err  !=  nil  {
2157+ 		return  nil , err 
2158+ 	}
2159+ 	for  _ , k  :=  range  ovhKeys  {
2160+ 		masterKeys  =  append (masterKeys , k )
2161+ 	}
21312162	return  masterKeys , nil 
21322163}
21332164
21342165func  getRotateOpts (c  * cli.Context , fileName  string , inputStore  common.Store , outputStore  common.Store , svcs  []keyservice.KeyServiceClient , decryptionOrder  []string ) (rotateOpts , error ) {
21352166	kmsEncryptionContext  :=  kms .ParseKMSContext (c .String ("encryption-context" ))
2136- 	addMasterKeys , err  :=  getMasterKeys (c , kmsEncryptionContext , "add-kms" , "add-pgp" , "add-gcp-kms" , "add-azure-kv" , "add-hc-vault-transit" , "add-age" )
2167+ 	addMasterKeys , err  :=  getMasterKeys (c , kmsEncryptionContext , "add-kms" , "add-pgp" , "add-gcp-kms" , "add-azure-kv" , "add-hc-vault-transit" , "add-age" ,  "add-ovh-kms" )
21372168	if  err  !=  nil  {
21382169		return  rotateOpts {}, err 
21392170	}
2140- 	rmMasterKeys , err  :=  getMasterKeys (c , kmsEncryptionContext , "rm-kms" , "rm-pgp" , "rm-gcp-kms" , "rm-azure-kv" , "rm-hc-vault-transit" , "rm-age" )
2171+ 	rmMasterKeys , err  :=  getMasterKeys (c , kmsEncryptionContext , "rm-kms" , "rm-pgp" , "rm-gcp-kms" , "rm-azure-kv" , "rm-hc-vault-transit" , "rm-age" ,  "rm-ovh-kms" )
21412172	if  err  !=  nil  {
21422173		return  rotateOpts {}, err 
21432174	}
@@ -2282,6 +2313,7 @@ func keyGroups(c *cli.Context, file string) ([]sops.KeyGroup, error) {
22822313	var  azkvKeys  []keys.MasterKey 
22832314	var  hcVaultMkKeys  []keys.MasterKey 
22842315	var  ageMasterKeys  []keys.MasterKey 
2316+ 	var  ovhKmsKeys  []keys.MasterKey 
22852317	kmsEncryptionContext  :=  kms .ParseKMSContext (c .String ("encryption-context" ))
22862318	if  c .String ("encryption-context" ) !=  ""  &&  kmsEncryptionContext  ==  nil  {
22872319		return  nil , common .NewExitError ("Invalid KMS encryption context format" , codes .ErrorInvalidKMSEncryptionContextFormat )
@@ -2328,6 +2360,15 @@ func keyGroups(c *cli.Context, file string) ([]sops.KeyGroup, error) {
23282360			ageMasterKeys  =  append (ageMasterKeys , k )
23292361		}
23302362	}
2363+ 	if  c .String ("ovh-kms" ) !=  ""  {
2364+ 		ovhKeys , err  :=  ovhkms .MasterKeysFromResourceIDString (c .String ("ovh-kms" ))
2365+ 		if  err  !=  nil  {
2366+ 			return  nil , err 
2367+ 		}
2368+ 		for  _ , k  :=  range  ovhKeys  {
2369+ 			pgpKeys  =  append (pgpKeys , k )
2370+ 		}
2371+ 	}
23312372	if  c .String ("kms" ) ==  ""  &&  c .String ("pgp" ) ==  ""  &&  c .String ("gcp-kms" ) ==  ""  &&  c .String ("azure-kv" ) ==  ""  &&  c .String ("hc-vault-transit" ) ==  ""  &&  c .String ("age" ) ==  ""  {
23322373		conf , err  :=  loadConfig (c , file , kmsEncryptionContext )
23332374		// config file might just not be supplied, without any error 
@@ -2347,6 +2388,7 @@ func keyGroups(c *cli.Context, file string) ([]sops.KeyGroup, error) {
23472388	group  =  append (group , pgpKeys ... )
23482389	group  =  append (group , hcVaultMkKeys ... )
23492390	group  =  append (group , ageMasterKeys ... )
2391+ 	group  =  append (group , ovhKmsKeys ... )
23502392	log .Debugf ("Master keys available:  %+v" , group )
23512393	return  []sops.KeyGroup {group }, nil 
23522394}
0 commit comments