Skip to content

Commit 4639212

Browse files
authored
[KeyVault] - Update setup script to include additional role assignments for MHSM (#14743)
## What - Adds `Managed HSM Crypto Officer` and `Managed HSM Crypto User` role assignments to test user when deploying MHSM. ## Why Recent RBAC changes for MHSM recently went live which impact how existing permissions are defined. In order to access key operations the Administrator role is no longer sufficient. To keep our tests running we need to add the Crypto Officer and Crypto User role assignments to our test application after the managed HSM is activated.
1 parent eae6e13 commit 4639212

File tree

3 files changed

+71
-61
lines changed

3 files changed

+71
-61
lines changed

sdk/keyvault/keyvault-keys/recordings/node/cryptographyclient_for_managed_hsm_skipped_if_mhsm_is_not_deployed_with_aes_crypto_algorithms/recording_encrypts_and_decrypts_using_aescbc.js

Lines changed: 49 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/keyvault/keyvault-keys/test/public/crypto.hsm.spec.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { authenticate } from "../utils/testAuthentication";
1111
import { stringToUint8Array, uint8ArrayToString } from "../utils/crypto";
1212
import TestClient from "../utils/testClient";
1313
import { getServiceVersion, onVersions } from "../utils/utils.common";
14+
import { isNode } from "@azure/core-http";
1415

1516
onVersions({ minVer: "7.2" }).describe(
1617
"CryptographyClient for managed HSM (skipped if MHSM is not deployed)",
@@ -41,11 +42,6 @@ onVersions({ minVer: "7.2" }).describe(
4142
keyName = testClient.formatName("cryptography-client-test" + keySuffix);
4243
});
4344

44-
afterEach(async function() {
45-
await testClient?.flushKey(keyName);
46-
await recorder.stop();
47-
});
48-
4945
describe("with AES crypto algorithms", async function() {
5046
it("encrypts and decrypts using AES-GCM", async function(this: Context) {
5147
keyVaultKey = await hsmClient.createKey(keyName, "AES", { keySize: 256 });
@@ -65,29 +61,34 @@ onVersions({ minVer: "7.2" }).describe(
6561
authenticationTag: encryptResult.authenticationTag
6662
});
6763
assert.equal(text, uint8ArrayToString(decryptResult.result));
64+
await testClient?.flushKey(keyName);
65+
await recorder.stop();
6866
});
6967

7068
it("encrypts and decrypts using AES-CBC", async function(this: Context) {
69+
if (!isNode) {
70+
this.skip();
71+
}
7172
keyVaultKey = await hsmClient.createKey(keyName, "AES", { keySize: 256 });
7273
cryptoClient = new CryptographyClient(keyVaultKey.id!, credential);
7374
const text = this.test!.title;
75+
// We are using a predictable IV to support our recorded tests; however, you should use a cryptographically secure IV or omit it and
76+
// let the client library generate it for you.
77+
const iv = stringToUint8Array("xxxxxxxxxxxxxxxx");
7478
const encryptResult = await cryptoClient.encrypt({
7579
algorithm: "A256CBCPAD",
7680
plaintext: stringToUint8Array(text),
77-
iv: stringToUint8Array(text)
81+
iv
7882
});
79-
// There is a service-level issue where `iv` is not returned
80-
// from the service as part of the result. Until it's resolved
81-
// we have to pend this and just pass the same iv
82-
// back to decrypt for now.
83-
// assert.exists(encryptResult.iv);
8483

8584
const decryptResult = await cryptoClient.decrypt({
8685
algorithm: "A256CBCPAD",
8786
ciphertext: encryptResult.result!,
88-
iv: stringToUint8Array(text) // Replace with `encryptResult.iv!` once ADO 9361749 is resolved.
87+
iv
8988
});
9089
assert.equal(uint8ArrayToString(decryptResult.result), text);
90+
await testClient?.flushKey(keyName);
91+
await recorder.stop();
9192
});
9293
});
9394
}

sdk/keyvault/test-resources-post.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,12 @@ if (Test-Path $sdpath) {
106106
az keyvault security-domain download --hsm-name $hsmName --security-domain-file $sdPath --sd-quorum 2 --sd-wrapping-keys $wrappingFiles
107107

108108
Log "Security domain downloaded to '$sdPath'; Managed HSM is now active at '$hsmUrl'"
109+
110+
# Force a sleep to wait for Managed HSM activation to propagate through Cosmos replication. Issue tracked in AzDo.
111+
Log "Sleeping for 30 seconds to allow activation to propagate..."
112+
Start-Sleep -Seconds 30
113+
114+
Log "Creating additional required role assignments for resource access."
115+
New-AzKeyVaultRoleAssignment -HsmName $hsmName -RoleDefinitionName "Managed HSM Crypto Officer" -ObjectID $DeploymentOutputs["CLIENT_OBJECT_ID"]
116+
New-AzKeyVaultRoleAssignment -HsmName $hsmName -RoleDefinitionName "Managed HSM Crypto User" -ObjectID $DeploymentOutputs["CLIENT_OBJECT_ID"]
117+
Log "Done."

0 commit comments

Comments
 (0)