|
13 | 13 | // ----------------------------------------------------------------------------------
|
14 | 14 |
|
15 | 15 | using Microsoft.Azure.Commands.Common;
|
| 16 | +using Microsoft.Azure.Commands.Common.Authentication; |
| 17 | +using Microsoft.Azure.Commands.Common.Authentication.Abstractions; |
16 | 18 | using Microsoft.Azure.Commands.Common.Exceptions;
|
17 | 19 | using Microsoft.Azure.Commands.KeyVault.Helpers;
|
18 | 20 | using Microsoft.Azure.Commands.KeyVault.Models;
|
19 | 21 | using Microsoft.Azure.Commands.KeyVault.Properties;
|
20 | 22 | using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
|
| 23 | +using Microsoft.Azure.Internal.Common; |
21 | 24 | using Microsoft.Azure.KeyVault.WebKey;
|
22 | 25 | using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
|
23 |
| -using Microsoft.WindowsAzure.Commands.Common.CustomAttributes; |
| 26 | +using Microsoft.Rest; |
24 | 27 | using Microsoft.WindowsAzure.Commands.Utilities.Common;
|
25 | 28 |
|
| 29 | +using Newtonsoft.Json.Linq; |
| 30 | + |
26 | 31 | using System;
|
27 | 32 | using System.Collections;
|
28 | 33 | using System.IO;
|
29 | 34 | using System.Linq;
|
30 | 35 | using System.Management.Automation;
|
31 |
| -using System.Net.Http; |
32 | 36 | using System.Reflection;
|
33 | 37 | using System.Security;
|
| 38 | + |
34 | 39 | using Track2Sdk = Azure.Security.KeyVault.Keys;
|
35 | 40 |
|
36 | 41 | namespace Microsoft.Azure.Commands.KeyVault
|
@@ -75,8 +80,9 @@ public class AddAzureKeyVaultKey : KeyVaultCmdletBase
|
75 | 80 |
|
76 | 81 | #region Constants
|
77 | 82 |
|
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"; |
80 | 86 |
|
81 | 87 | #endregion
|
82 | 88 |
|
@@ -376,7 +382,6 @@ public class AddAzureKeyVaultKey : KeyVaultCmdletBase
|
376 | 382 | ParameterSetName = ResourceIdCreateParameterSet)]
|
377 | 383 | public string ReleasePolicyPath { get; set; }
|
378 | 384 |
|
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.")] |
380 | 385 | [Parameter(Mandatory = false,
|
381 | 386 | ParameterSetName = HsmInteractiveCreateParameterSet,
|
382 | 387 | 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()
|
626 | 631 | private string GetDefaultCVMPolicy()
|
627 | 632 | {
|
628 | 633 | string defaultCVMPolicy = null;
|
629 |
| - |
630 | 634 | try
|
631 | 635 | {
|
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) |
633 | 638 | {
|
634 |
| - defaultCVMPolicy = client.GetStringAsync(DefaultCVMPolicyUrl).ConfigureAwait(true).GetAwaiter().GetResult(); |
| 639 | + throw new AzPSException(string.Format(Resources.NoVaultWithGivenNameFound, VaultName), ErrorKind.UserError); |
635 | 640 | }
|
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) |
642 | 648 | {
|
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); |
648 | 650 | }
|
649 |
| - catch (Exception ex) |
| 651 | + using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(DefaultCVMPolicyTemplatePath)) |
| 652 | + using (var reader = new StreamReader(stream)) |
650 | 653 | {
|
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)); |
653 | 661 | }
|
654 | 662 | return defaultCVMPolicy;
|
655 | 663 | }
|
|
0 commit comments