Skip to content

Commit

Permalink
fix key version tracking (#5757)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishoffman authored and briankassouf committed Nov 12, 2018
1 parent 963f5a3 commit 39f7a2d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 15 additions & 5 deletions vault/seal/azurekeyvault/azurekeyvault.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,14 @@ func (v *AzureKeyVaultSeal) SetConfig(config map[string]string) (map[string]stri
}

// Test the client connection using provided key ID
_, err = client.GetKey(context.Background(), v.buildBaseURL(), v.keyName, "")
keyInfo, err := client.GetKey(context.Background(), v.buildBaseURL(), v.keyName, "")
if err != nil {
return nil, errwrap.Wrapf("error fetching Azure Key Vault seal key information: {{err}}", err)
}
if keyInfo.Key == nil {
return nil, errors.New("no key information returned")
}
v.currentKeyID.Store(parseKeyVersion(to.String(keyInfo.Key.Kid)))

v.client = client
}
Expand Down Expand Up @@ -185,10 +189,9 @@ func (v *AzureKeyVaultSeal) Encrypt(ctx context.Context, plaintext []byte) (*phy
return nil, err
}

// Kid gets returned as a full URL, get the last bit which is just
// the version
keyVersionParts := strings.Split(to.String(resp.Kid), "/")
keyVersion := keyVersionParts[len(keyVersionParts)-1]
// Store the current key version
keyVersion := parseKeyVersion(to.String(resp.Kid))
v.currentKeyID.Store(keyVersion)

ret := &physical.EncryptedBlobInfo{
Ciphertext: env.Ciphertext,
Expand Down Expand Up @@ -265,3 +268,10 @@ func (v *AzureKeyVaultSeal) getKeyVaultClient() (*keyvault.BaseClient, error) {
client.Authorizer = authorizer
return &client, nil
}

// Kid gets returned as a full URL, get the last bit which is just
// the version
func parseKeyVersion(kid string) string {
keyVersionParts := strings.Split(kid, "/")
return keyVersionParts[len(keyVersionParts)-1]
}
4 changes: 4 additions & 0 deletions vault/seal/azurekeyvault/azurekeyvault_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func TestAzureKeyVault_Lifecycle(t *testing.T) {
}

s := NewSeal(logging.NewVaultLogger(log.Trace))
_, err := s.SetConfig(nil)
if err != nil {
t.Fatalf("err: %s", err.Error())
}

// Test Encrypt and Decrypt calls
input := []byte("foo")
Expand Down

0 comments on commit 39f7a2d

Please sign in to comment.