Skip to content

Commit cef900d

Browse files
committed
Add method to convert type to a string
1 parent c312f85 commit cef900d

File tree

9 files changed

+35
-23
lines changed

9 files changed

+35
-23
lines changed

age/keysource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ func (key *MasterKey) ToMap() map[string]interface{} {
225225
return out
226226
}
227227

228+
// TypeToString converts key type to a string
229+
func (key *MasterKey) TypeToString() string {
230+
return "age"
231+
}
232+
228233
func getUserConfigDir() (string, error) {
229234
if runtime.GOOS == "darwin" {
230235
if userConfigDir, ok := os.LookupEnv(xdgConfigHome); ok && userConfigDir != "" {

azkv/keysource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ func (key MasterKey) ToMap() map[string]interface{} {
215215
return out
216216
}
217217

218+
// TypeToString converts key type to a string
219+
func (key *MasterKey) TypeToString() string {
220+
return "azure_kv"
221+
}
222+
218223
// getTokenCredential returns the tokenCredential of the MasterKey, or
219224
// azidentity.NewDefaultAzureCredential.
220225
func (key *MasterKey) getTokenCredential() (azcore.TokenCredential, error) {

gcpkms/keysource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ func (key MasterKey) ToMap() map[string]interface{} {
196196
return out
197197
}
198198

199+
// TypeToString converts key type to a string
200+
func (key *MasterKey) TypeToString() string {
201+
return "gcp_kms"
202+
}
203+
199204
// newKMSClient returns a GCP KMS client configured with the credentialJSON
200205
// and/or grpcConn, falling back to environmental defaults.
201206
// It returns an error if the ResourceID is invalid, or if the setup of the

hcvault/keysource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ func (key MasterKey) ToMap() map[string]interface{} {
216216
return out
217217
}
218218

219+
// TypeToString converts key type to a string
220+
func (key *MasterKey) TypeToString() string {
221+
return "hc_vault"
222+
}
223+
219224
// encryptPath returns the path for Encrypt requests.
220225
func (key *MasterKey) encryptPath() string {
221226
return path.Join(key.EnginePath, "encrypt", key.KeyName)

keys/keys.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ type MasterKey interface {
1010
NeedsRotation() bool
1111
ToString() string
1212
ToMap() map[string]interface{}
13+
TypeToString() string
1314
}

keyservice/keyservice.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,3 @@ func KeyFromMasterKey(mk keys.MasterKey) Key {
8282
panic(fmt.Sprintf("Tried to convert unknown MasterKey type %T to keyservice.Key", mk))
8383
}
8484
}
85-
86-
func MasterKeyTypeToString(mk keys.MasterKey) string {
87-
switch mk := mk.(type) {
88-
case *pgp.MasterKey:
89-
return "pgp"
90-
case *gcpkms.MasterKey:
91-
return "gcp_kms"
92-
case *hcvault.MasterKey:
93-
return "hc_vault"
94-
case *kms.MasterKey:
95-
return "kms"
96-
case *azkv.MasterKey:
97-
return "azure_kv"
98-
case *age.MasterKey:
99-
return "age"
100-
default:
101-
panic(fmt.Sprintf("Tried to convert unknown MasterKey type %T to string", mk))
102-
}
103-
}

kms/keysource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ func (key MasterKey) ToMap() map[string]interface{} {
297297
return out
298298
}
299299

300+
// TypeToString converts key type to a string
301+
func (key *MasterKey) TypeToString() string {
302+
return "kms"
303+
}
304+
300305
// createKMSConfig returns an AWS config with the credentialsProvider of the
301306
// MasterKey, or the default configuration sources.
302307
func (key MasterKey) createKMSConfig() (*aws.Config, error) {

pgp/keysource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,11 @@ func (key MasterKey) ToMap() map[string]interface{} {
449449
return out
450450
}
451451

452+
// TypeToString converts key type to a string
453+
func (key *MasterKey) TypeToString() string {
454+
return "pgp"
455+
}
456+
452457
// retrievePubKey attempts to retrieve the public key from the public keyring
453458
// by Fingerprint.
454459
func (key *MasterKey) retrievePubKey() (openpgp.Entity, error) {

sops.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,13 +702,13 @@ func sortKeyGroupIndices(group KeyGroup, decryptionOrder []string) []int {
702702
indices[i] = i
703703
}
704704
sort.SliceStable(indices, func(i, j int) bool {
705-
keyI := keyservice.MasterKeyTypeToString(group[indices[i]])
706-
keyJ := keyservice.MasterKeyTypeToString(group[indices[j]])
707-
priorityI, ok := priorities[keyI]
705+
keyTypeI := group[indices[i]].TypeToString()
706+
keyTypeJ := group[indices[j]].TypeToString()
707+
priorityI, ok := priorities[keyTypeI]
708708
if !ok {
709709
priorityI = maxPriority
710710
}
711-
priorityJ, ok := priorities[keyJ]
711+
priorityJ, ok := priorities[keyTypeJ]
712712
if !ok {
713713
priorityJ = maxPriority
714714
}

0 commit comments

Comments
 (0)