Skip to content

Commit 61cc3f7

Browse files
committed
AZKV: Also allow to omit version for AZKV keys specified in key groups.
Signed-off-by: Felix Fontein <[email protected]>
1 parent e783741 commit 61cc3f7

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

azkv/keysource.go

Lines changed: 12 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,14 @@ func NewMasterKey(vaultURL string, keyName string, keyVersion string) *MasterKey
7575
}
7676
}
7777

78+
// NewMasterKey creates a new MasterKey from a URL, key name and (optional) version,
79+
// setting the creation date to the current date.
80+
func NewMasterKey(vaultURL string, keyName string, keyVersion string) (*MasterKey, error) {
81+
key := newMasterKey(vaultURL, keyName, keyVersion)
82+
err := key.ensureKeyHasVersion(context.Background())
83+
return key, err
84+
}
85+
7886
// NewMasterKeyFromURL takes an Azure Key Vault key URL, and returns a new
7987
// MasterKey. The URL format is {vaultUrl}/keys/{keyName}/{keyVersion}.
8088
func NewMasterKeyFromURL(url string) (*MasterKey, error) {
@@ -88,9 +96,9 @@ func NewMasterKeyFromURL(url string) (*MasterKey, error) {
8896
// version of the key. We need to put the actual version in the sops metadata block though
8997
var key *MasterKey
9098
if len(parts[3]) > 1 {
91-
key = NewMasterKey(parts[1], parts[2], parts[3][1:])
99+
key = newMasterKey(parts[1], parts[2], parts[3][1:])
92100
} else {
93-
key = NewMasterKey(parts[1], parts[2], "")
101+
key = newMasterKey(parts[1], parts[2], "")
94102
}
95103
err := key.ensureKeyHasVersion(context.Background())
96104
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.NewMasterKey(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)