Skip to content

Commit d327e56

Browse files
authored
[KeyVault] Shifted the location of key CVM release policy to GitHub (#20005)
* Shifted the location of key CVM release policy to GitHub * Throw exception with more details if get default CVM Policy failed
1 parent 534cdbb commit d327e56

File tree

6 files changed

+118
-21
lines changed

6 files changed

+118
-21
lines changed

src/KeyVault/KeyVault/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Shifted the location of key CVM release policy to GitHub [#19984]
22+
* Added fallback logic (reading default CVM policy from a local copy) if fetching default CVM Policy from GitHub failed.
2123

2224
## Version 4.9.0
2325
* Bumped API version to 2022-07-01

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

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using System.Linq;
2929
using System.Management.Automation;
3030
using System.Net.Http;
31+
using System.Reflection;
3132
using System.Security;
3233
using Track2Sdk = Azure.Security.KeyVault.Keys;
3334

@@ -73,10 +74,11 @@ public class AddAzureKeyVaultKey : KeyVaultCmdletBase
7374

7475
#region Constants
7576

76-
private const string DefaultCVMPolicyUrl = "https://cvmprivatepreviewsa.blob.core.windows.net/cvmpublicpreviewcontainer/skr-policy.json";
77-
77+
private const string DefaultCVMPolicyUrl = "https://raw.githubusercontent.com/Azure/confidential-computing-cvm/main/cvm_deployment/key/skr-policy.json";
78+
private const string DefaultCVMPolicyPath = "Microsoft.Azure.Commands.KeyVault.Resources.skr-policy.json";
79+
7880
#endregion
79-
81+
8082
#region Input Parameter Definitions
8183

8284
/// <summary>
@@ -445,22 +447,11 @@ private void NormalizeKeySourceParameters()
445447

446448
if (this.UseDefaultCVMPolicy.IsPresent)
447449
{
448-
try
450+
ReleasePolicy = new PSKeyReleasePolicy()
449451
{
450-
using (var client = new HttpClient())
451-
{
452-
ReleasePolicy = new PSKeyReleasePolicy()
453-
{
454-
PolicyContent = client.GetStringAsync(DefaultCVMPolicyUrl).ConfigureAwait(true).GetAwaiter().GetResult(),
455-
Immutable = this.Immutable.IsPresent
456-
};
457-
}
458-
}
459-
catch(Exception e)
460-
{
461-
// Swallow exception to fetch default policy
462-
WriteWarning(string.Format(Resources.FetchDefaultCVMPolicyFailed, e.Message));
463-
}
452+
PolicyContent = GetDefaultCVMPolicy(),
453+
Immutable = this.Immutable.IsPresent
454+
};
464455
}
465456

466457
if(this.IsParameterBound(c => c.ReleasePolicyPath))
@@ -629,5 +620,36 @@ internal Track2Sdk.JsonWebKey CreateTrack2WebKeyFromFile()
629620

630621
return converterChain.ConvertToTrack2SdkKeyFromFile(keyFile, KeyFilePassword, converterExtraInfo);
631622
}
623+
624+
private string GetDefaultCVMPolicy()
625+
{
626+
string defaultCVMPolicy = null;
627+
628+
try
629+
{
630+
using (var client = new HttpClient())
631+
{
632+
defaultCVMPolicy = client.GetStringAsync(DefaultCVMPolicyUrl).ConfigureAwait(true).GetAwaiter().GetResult();
633+
}
634+
635+
}
636+
catch (Exception e)
637+
{
638+
WriteWarning(string.Format(Resources.FetchDefaultCVMPolicyFromLocal, e.Message));
639+
try
640+
{
641+
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(DefaultCVMPolicyPath))
642+
using (var reader = new StreamReader(stream))
643+
{
644+
defaultCVMPolicy = reader.ReadToEnd();
645+
}
646+
}
647+
catch (Exception ex)
648+
{
649+
throw new AzPSArgumentException(string.Format(Resources.FetchDefaultCVMPolicyFailedWithErrorMessage, ex.Message), nameof(UseDefaultCVMPolicy));
650+
};
651+
}
652+
return defaultCVMPolicy;
653+
}
632654
}
633655
}

src/KeyVault/KeyVault/KeyVault.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
<None Update="Az.KeyVault.Extension\*" CopyToOutputDirectory="PreserveNewest" />
3636
</ItemGroup>
3737

38+
<ItemGroup>
39+
<EmbeddedResource Include="Resources\skr-policy.json" />
40+
</ItemGroup>
41+
3842
<ItemGroup>
3943
<Compile Update="Properties\Resources.Designer.cs">
4044
<DesignTime>True</DesignTime>

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

Lines changed: 11 additions & 2 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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,13 @@ You can find the object ID using Azure Active Directory Module for Windows Power
606606
<data name="PurgeManagedHsmWarningWhatIf" xml:space="preserve">
607607
<value>Purge managed HSM</value>
608608
</data>
609-
<data name="FetchDefaultCVMPolicyFailed" xml:space="preserve">
609+
<data name="FetchDefaultCVMPolicyFailedWithErrorMessage" xml:space="preserve">
610610
<value>Fetch default CVM Policy failed, {0}</value>
611611
</data>
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>
615618
</root>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"anyOf": [
3+
{
4+
"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+
},
48+
{
49+
"claim": "x-ms-compliance-status",
50+
"equals": "azure-compliant-cvm"
51+
}
52+
],
53+
"authority": "https://sharedweu.weu.attest.azure.net/"
54+
}
55+
],
56+
"version": "1.0.0"
57+
}

0 commit comments

Comments
 (0)