Skip to content

Commit 25dcc16

Browse files
authored
[KeyVault] Switch the location of skr policy from github to maa endpoint (#24807)
* switch skr policy from github to maa endpoint * add change log * refine code
1 parent d572184 commit 25dcc16

File tree

10 files changed

+52
-82
lines changed

10 files changed

+52
-82
lines changed

src/KeyVault/KeyVault/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* [Breaking change] Removed the offline fallback policy if specify parameter `UseDefaultCVMPolicy` in `Add-AzKeyVaultKey`. Key creation will fail if unable to get regional default CVM SKR policy from MAA Service Discovery API.
2122
* [Breaking change] Removed parameter `Value` from `Invoke-AzKeyVaultKeyOperation`.
2223
* [Breaking change] Removed property `Result` from the output type `PSKeyOperationResult` of `Invoke-AzKeyVaultKeyOperation`.
2324
* [Breaking Change] Replaced parameter `EnableRbacAuthorization` by `DisableRbacAuthorization` in `New-AzKeyVault` and `Update-AzKeyVault`.

src/KeyVault/KeyVault/Commands/Key/AddAzureKeyVaultKey.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,29 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using Microsoft.Azure.Commands.Common;
16+
using Microsoft.Azure.Commands.Common.Authentication;
17+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1618
using Microsoft.Azure.Commands.Common.Exceptions;
1719
using Microsoft.Azure.Commands.KeyVault.Helpers;
1820
using Microsoft.Azure.Commands.KeyVault.Models;
1921
using Microsoft.Azure.Commands.KeyVault.Properties;
2022
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
23+
using Microsoft.Azure.Internal.Common;
2124
using Microsoft.Azure.KeyVault.WebKey;
2225
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
23-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
26+
using Microsoft.Rest;
2427
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2528

29+
using Newtonsoft.Json.Linq;
30+
2631
using System;
2732
using System.Collections;
2833
using System.IO;
2934
using System.Linq;
3035
using System.Management.Automation;
31-
using System.Net.Http;
3236
using System.Reflection;
3337
using System.Security;
38+
3439
using Track2Sdk = Azure.Security.KeyVault.Keys;
3540

3641
namespace Microsoft.Azure.Commands.KeyVault
@@ -75,8 +80,9 @@ public class AddAzureKeyVaultKey : KeyVaultCmdletBase
7580

7681
#region Constants
7782

78-
private const string DefaultCVMPolicyUrl = "https://raw.githubusercontent.com/Azure/confidential-computing-cvm/main/cvm_deployment/key/skr-policy.json";
79-
private const string DefaultCVMPolicyPath = "Microsoft.Azure.Commands.KeyVault.Resources.skr-policy.json";
83+
private const string DefaultCVMPolicyApi = "subscriptions/{0}/providers/Microsoft.Attestation/Locations/{1}/defaultProvider";
84+
private const string DefaultCVMPolicyTemplatePath = "Microsoft.Azure.Commands.KeyVault.Resources.skr-policy.json";
85+
private const string MaaEnpointApiVersion = "2020-10-01";
8086

8187
#endregion
8288

@@ -376,7 +382,6 @@ public class AddAzureKeyVaultKey : KeyVaultCmdletBase
376382
ParameterSetName = ResourceIdCreateParameterSet)]
377383
public string ReleasePolicyPath { get; set; }
378384

379-
[CmdletParameterBreakingChangeWithVersion(nameof(UseDefaultCVMPolicy), "12.0.0", "6.0.0", ChangeDescription = "The offline fallback policy will be removed. Key creation will fail if unable to get regional default CVM SKR policy from MAA Service Discovery API.")]
380385
[Parameter(Mandatory = false,
381386
ParameterSetName = HsmInteractiveCreateParameterSet,
382387
HelpMessage = "Specifies to use default policy under which the key can be exported for CVM disk encryption.")]
@@ -626,30 +631,33 @@ internal Track2Sdk.JsonWebKey CreateTrack2WebKeyFromFile()
626631
private string GetDefaultCVMPolicy()
627632
{
628633
string defaultCVMPolicy = null;
629-
630634
try
631635
{
632-
using (var client = new HttpClient())
636+
var location = keyVaultManagementCmdletBase.ListVaults("", null)?.FirstOrDefault(r => r.VaultName.Equals(VaultName ?? HsmName, StringComparison.OrdinalIgnoreCase))?.Location;
637+
if (null == location)
633638
{
634-
defaultCVMPolicy = client.GetStringAsync(DefaultCVMPolicyUrl).ConfigureAwait(true).GetAwaiter().GetResult();
639+
throw new AzPSException(string.Format(Resources.NoVaultWithGivenNameFound, VaultName), ErrorKind.UserError);
635640
}
636-
637-
}
638-
catch (Exception e)
639-
{
640-
WriteWarning(string.Format(Resources.FetchDefaultCVMPolicyFromLocal, e.Message));
641-
try
641+
string endpoint = DefaultContext.Environment.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager);
642+
string defaultCVMPolicyUrl = string.Format(DefaultCVMPolicyApi, DefaultContext.Subscription.Id, location);
643+
ServiceClientCredentials creds = AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(DefaultContext, endpoint);
644+
var serviceClient = AzureSession.Instance.ClientFactory.CreateArmClient<AzureRestClient>(DefaultContext, AzureEnvironment.Endpoint.ResourceManager);
645+
string response = serviceClient.Operations.GetResourceWithFullResponse(defaultCVMPolicyUrl, MaaEnpointApiVersion)?.Body;
646+
var regionalMaaEndpoint = JObject.Parse(response)?["properties"]?["attestUri"]?.ToString();
647+
if (null == regionalMaaEndpoint)
642648
{
643-
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(DefaultCVMPolicyPath))
644-
using (var reader = new StreamReader(stream))
645-
{
646-
defaultCVMPolicy = reader.ReadToEnd();
647-
}
649+
throw new AzPSException($"unable to get regional MAA endpoint from {defaultCVMPolicyUrl}.", ErrorKind.ServiceError);
648650
}
649-
catch (Exception ex)
651+
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(DefaultCVMPolicyTemplatePath))
652+
using (var reader = new StreamReader(stream))
650653
{
651-
throw new AzPSArgumentException(string.Format(Resources.FetchDefaultCVMPolicyFailedWithErrorMessage, ex.Message), nameof(UseDefaultCVMPolicy));
652-
};
654+
defaultCVMPolicy = reader.ReadToEnd();
655+
}
656+
defaultCVMPolicy = defaultCVMPolicy.Replace("{regional-maa-endpoint}", regionalMaaEndpoint);
657+
}
658+
catch (Exception ex)
659+
{
660+
throw new AzPSArgumentException(string.Format(Resources.FetchDefaultCVMPolicyFailedWithErrorMessage, ex.Message), nameof(UseDefaultCVMPolicy));
653661
}
654662
return defaultCVMPolicy;
655663
}

src/KeyVault/KeyVault/Models/KeyVaultCmdletBase.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using System.Collections;
1617
using System.Collections.Generic;
1718
using System.Diagnostics.Tracing;
1819
using System.Linq;
1920
using System.Management.Automation;
2021
using Azure.Core.Diagnostics;
2122
using Microsoft.Azure.Commands.Common.Authentication;
23+
using Microsoft.Azure.Commands.KeyVault.Properties;
2224
using Microsoft.Azure.Commands.KeyVault.Track2Models;
2325
using Microsoft.Azure.Commands.ResourceManager.Common;
26+
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
2427

2528
namespace Microsoft.Azure.Commands.KeyVault.Models
2629
{
@@ -47,6 +50,7 @@ internal IKeyVaultDataServiceClient DataServiceClient
4750
this.dataServiceClient = value;
4851
}
4952
}
53+
private IKeyVaultDataServiceClient dataServiceClient;
5054

5155
internal IKeyVaultDataServiceClient Track2DataClient
5256
{
@@ -68,6 +72,9 @@ internal IKeyVaultDataServiceClient Track2DataClient
6872
_track2DataServiceClient = value;
6973
}
7074
}
75+
private IKeyVaultDataServiceClient _track2DataServiceClient;
76+
77+
internal static readonly KeyVaultManagementCmdletBase keyVaultManagementCmdletBase = new KeyVaultManagementCmdletBase();
7178

7279
protected string GetDefaultFileForOperation(string operationName, string vaultName, string entityName)
7380
{
@@ -78,9 +85,6 @@ protected string GetDefaultFileForOperation(string operationName, string vaultNa
7885
return filename;
7986
}
8087

81-
private IKeyVaultDataServiceClient dataServiceClient;
82-
private IKeyVaultDataServiceClient _track2DataServiceClient;
83-
8488
/// <summary>
8589
/// Utility function that will continually iterate over the updated KeyVaultObjectFilterOptions until the options
8690
/// NextLink is null, and writes all the retrieved objects.

src/KeyVault/KeyVault/Models/KeyVaultManagementCmdletBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ protected T FilterByTag<T>(T vault, Hashtable tag) where T : PSKeyVaultIdentityI
123123
return FilterByTag(new List<T> { vault }, tag).FirstOrDefault();
124124
}
125125

126-
protected List<PSKeyVaultIdentityItem> ListVaults(string resourceGroupName, Hashtable tag, ResourceTypeName? resourceTypeName = ResourceTypeName.Vault)
126+
internal List<PSKeyVaultIdentityItem> ListVaults(string resourceGroupName, Hashtable tag, ResourceTypeName? resourceTypeName = ResourceTypeName.Vault)
127127
{
128128
var vaults = new List<PSKeyVaultIdentityItem>();
129129

src/KeyVault/KeyVault/Properties/Resources.Designer.cs

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

src/KeyVault/KeyVault/Properties/Resources.resx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,6 @@ You can find the object ID using Azure Active Directory Module for Windows Power
612612
<data name="RecoverHsm" xml:space="preserve">
613613
<value>Recover HSM?</value>
614614
</data>
615-
<data name="FetchDefaultCVMPolicyFromLocal" xml:space="preserve">
616-
<value>Fetching default CVM policy from remote failed because {0}. Trying to fetch default CVM policy from local backup copy.</value>
617-
</data>
618615
<data name="UpdateKeyVaultSetting" xml:space="preserve">
619616
<value>Update vault setting</value>
620617
</data>
@@ -627,4 +624,7 @@ You can find the object ID using Azure Active Directory Module for Windows Power
627624
<data name="UseManagedIdentityAndSasTokenNeitherExist" xml:space="preserve">
628625
<value>Please choose either SasToken or UseUserManagedIdentity as authentication method.</value>
629626
</data>
627+
<data name="NoVaultWithGivenNameFound" xml:space="preserve">
628+
<value>Vault '{0}' does not exist in current subscription. If this vault exists in your tenant, please switch to the correct subscription.</value>
629+
</data>
630630
</root>

src/KeyVault/KeyVault/Resources/skr-policy.json

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,12 @@
22
"anyOf": [
33
{
44
"allOf": [
5-
{
6-
"claim": "x-ms-attestation-type",
7-
"equals": "sevsnpvm"
8-
},
9-
{
10-
"claim": "x-ms-compliance-status",
11-
"equals": "azure-compliant-cvm"
12-
}
13-
],
14-
"authority": "https://sharedeus.eus.attest.azure.net/"
15-
},
16-
{
17-
"allOf": [
18-
{
19-
"claim": "x-ms-attestation-type",
20-
"equals": "sevsnpvm"
21-
},
22-
{
23-
"claim": "x-ms-compliance-status",
24-
"equals": "azure-compliant-cvm"
25-
}
26-
],
27-
"authority": "https://sharedwus.wus.attest.azure.net/"
28-
},
29-
{
30-
"allOf": [
31-
{
32-
"claim": "x-ms-attestation-type",
33-
"equals": "sevsnpvm"
34-
},
35-
{
36-
"claim": "x-ms-compliance-status",
37-
"equals": "azure-compliant-cvm"
38-
}
39-
],
40-
"authority": "https://sharedneu.neu.attest.azure.net/"
41-
},
42-
{
43-
"allOf": [
44-
{
45-
"claim": "x-ms-attestation-type",
46-
"equals": "sevsnpvm"
47-
},
485
{
496
"claim": "x-ms-compliance-status",
507
"equals": "azure-compliant-cvm"
518
}
529
],
53-
"authority": "https://sharedweu.weu.attest.azure.net/"
10+
"authority": "{regional-maa-endpoint}"
5411
}
5512
],
5613
"version": "1.0.0"

0 commit comments

Comments
 (0)