Skip to content

Commit 2ade87b

Browse files
authored
Merge pull request #1947 from felixfontein/azkv-version
AZKV: Also allow to omit version for AZKV keys specified in key groups
2 parents e783741 + 03ff3df commit 2ade87b

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

azkv/keysource.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ type MasterKey struct {
6464
clientOptions *azkeys.ClientOptions
6565
}
6666

67-
// NewMasterKey creates a new MasterKey from a URL, key name and version,
67+
// newMasterKey creates a new MasterKey from a URL, key name and version,
6868
// setting the creation date to the current date.
69-
func NewMasterKey(vaultURL string, keyName string, keyVersion string) *MasterKey {
69+
func newMasterKey(vaultURL string, keyName string, keyVersion string) *MasterKey {
7070
return &MasterKey{
7171
VaultURL: vaultURL,
7272
Name: keyName,
@@ -75,6 +75,22 @@ func NewMasterKey(vaultURL string, keyName string, keyVersion string) *MasterKey
7575
}
7676
}
7777

78+
// NewMasterKey creates a new MasterKey from a URL, key name and (mandatory) version,
79+
// setting the creation date to the current date.
80+
func NewMasterKey(vaultURL string, keyName string, keyVersion string) *MasterKey {
81+
return newMasterKey(vaultURL, keyName, keyVersion)
82+
}
83+
84+
// NewMasterKey creates a new MasterKey from a URL, key name and (optional) version,
85+
// setting the creation date to the current date.
86+
func NewMasterKeyWithOptionalVersion(vaultURL string, keyName string, keyVersion string) (*MasterKey, error) {
87+
key := newMasterKey(vaultURL, keyName, keyVersion)
88+
if err := key.ensureKeyHasVersion(context.Background()); err != nil {
89+
return nil, err
90+
}
91+
return key, nil
92+
}
93+
7894
// NewMasterKeyFromURL takes an Azure Key Vault key URL, and returns a new
7995
// MasterKey. The URL format is {vaultUrl}/keys/{keyName}/{keyVersion}.
8096
func NewMasterKeyFromURL(url string) (*MasterKey, error) {
@@ -88,9 +104,9 @@ func NewMasterKeyFromURL(url string) (*MasterKey, error) {
88104
// version of the key. We need to put the actual version in the sops metadata block though
89105
var key *MasterKey
90106
if len(parts[3]) > 1 {
91-
key = NewMasterKey(parts[1], parts[2], parts[3][1:])
107+
key = newMasterKey(parts[1], parts[2], parts[3][1:])
92108
} else {
93-
key = NewMasterKey(parts[1], parts[2], "")
109+
key = newMasterKey(parts[1], parts[2], "")
94110
}
95111
err := key.ensureKeyHasVersion(context.Background())
96112
return key, err

azkv/keysource_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,15 @@ func TestMasterKey_EncryptIfNeeded(t *testing.T) {
181181
}
182182

183183
func TestMasterKey_NeedsRotation(t *testing.T) {
184-
key := NewMasterKey("", "", "")
184+
key := newMasterKey("", "", "")
185185
assert.False(t, key.NeedsRotation())
186186

187187
key.CreationDate = key.CreationDate.Add(-(azkvTTL + time.Second))
188188
assert.True(t, key.NeedsRotation())
189189
}
190190

191191
func TestMasterKey_ToString(t *testing.T) {
192-
key := NewMasterKey("https://test.vault.azure.net", "key-name", "key-version")
192+
key := newMasterKey("https://test.vault.azure.net", "key-name", "key-version")
193193
assert.Equal(t, "https://test.vault.azure.net/keys/key-name/key-version", key.ToString())
194194
}
195195

config/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,11 @@ func extractMasterKeys(group keyGroup) (sops.KeyGroup, error) {
330330
keyGroup = append(keyGroup, gcpkms.NewMasterKeyFromResourceID(k.ResourceID))
331331
}
332332
for _, k := range group.AzureKV {
333-
keyGroup = append(keyGroup, azkv.NewMasterKey(k.VaultURL, k.Key, k.Version))
333+
if key, err := azkv.NewMasterKeyWithOptionalVersion(k.VaultURL, k.Key, k.Version); err == nil {
334+
keyGroup = append(keyGroup, key)
335+
} else {
336+
return nil, err
337+
}
334338
}
335339
for _, k := range group.Vault {
336340
if masterKey, err := hcvault.NewMasterKeyFromURI(k); err == nil {

0 commit comments

Comments
 (0)