Skip to content

Commit c8b5d44

Browse files
committed
Remove KeyVault cmdlets dependencies
1 parent 5b848ce commit c8b5d44

File tree

5 files changed

+580
-561
lines changed

5 files changed

+580
-561
lines changed

src/ResourceManager/Resources/Commands.Resources.Test/Commands.Resources.Test.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@
7070
<Reference Include="Microsoft.Azure.KeyVault.Core">
7171
<HintPath>..\..\..\packages\Microsoft.Azure.KeyVault.Core.1.0.0\lib\net40\Microsoft.Azure.KeyVault.Core.dll</HintPath>
7272
</Reference>
73-
<Reference Include="Microsoft.Azure.Management.KeyVault, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
74-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.KeyVault.1.0.1\lib\net40\Microsoft.Azure.Management.KeyVault.dll</HintPath>
75-
<Private>True</Private>
76-
</Reference>
7773
<Reference Include="Microsoft.Azure.Management.Authorization">
7874
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.1.1.0\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
7975
</Reference>

src/ResourceManager/Resources/Commands.Resources.Test/KeyVaultSetupTemplate.json

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,64 @@
88
"description": "Name of the Vault"
99
}
1010
},
11+
"tenantId": {
12+
"type": "string",
13+
"metadata": {
14+
"description": "Tenant Id for the subscription. Available from Get-AzureRMSubscription PowerShell cmdlet"
15+
}
16+
},
17+
"objectId": {
18+
"type": "string",
19+
"metadata": {
20+
"description": "Object Id of the AD user. Available from Get-AzureRMADUser or Get-AzureRMADServicePrincipal cmdlets"
21+
}
22+
},
23+
"keysPermissions": {
24+
"type": "array",
25+
"defaultValue": [ "all" ],
26+
"metadata": {
27+
"description": "Permissions to grant user to keys in the vault. Valid values are: all, create, import, update, get, list, delete, backup, restore, encrypt, decrypt, wrapkey, unwrapkey, sign, and verify."
28+
}
29+
},
30+
"secretsPermissions": {
31+
"type": "array",
32+
"defaultValue": [ "all" ],
33+
"metadata": {
34+
"description": "Permissions to grant user to secrets in the vault. Valid values are: all, get, set, list, and delete."
35+
}
36+
},
37+
"skuName": {
38+
"type": "string",
39+
"defaultValue": "Standard",
40+
"allowedValues": [
41+
"Standard",
42+
"Premium"
43+
],
44+
"metadata": {
45+
"description": "SKU for the vault"
46+
}
47+
},
48+
"enabledForDeployment": {
49+
"type": "bool",
50+
"defaultValue": true,
51+
"metadata": {
52+
"description": "Specifies if the vault is enabled for a VM deployment"
53+
}
54+
},
55+
"enabledForTemplateDeployment": {
56+
"type": "bool",
57+
"defaultValue": true,
58+
"metadata": {
59+
"description": "Specifies if the vault is enabled for a ARM template deployment"
60+
}
61+
},
62+
"enableVaultForVolumeEncryption": {
63+
"type": "bool",
64+
"defaultValue": false,
65+
"metadata": {
66+
"description": "Specifies if the vault is enabled for volume encryption"
67+
}
68+
},
1169
"secretName": {
1270
"type": "string",
1371
"metadata": {
@@ -23,13 +81,45 @@
2381
},
2482
"resources": [
2583
{
26-
"type": "Microsoft.KeyVault/vaults/secrets",
27-
"name": "[concat(parameters('keyVaultName'), '/', parameters('secretName'))]",
84+
"type": "Microsoft.KeyVault/vaults",
85+
"name": "[parameters('keyVaultName')]",
2886
"apiVersion": "2015-06-01",
29-
"tags": { "displayName": "secret" },
87+
"location": "[resourceGroup().location]",
88+
"tags": { "displayName": "Vault" },
3089
"properties": {
31-
"value": "[parameters('secretValue')]"
32-
}
90+
"enabledForDeployment": "[parameters('enabledForDeployment')]",
91+
"enabledForTemplateDeployment": "[parameters('enabledForTemplateDeployment')]",
92+
"enabledForVolumeEncryption": "[parameters('enableVaultForVolumeEncryption')]",
93+
"tenantId": "[parameters('tenantId')]",
94+
"accessPolicies": [
95+
{
96+
"tenantId": "[parameters('tenantId')]",
97+
"objectId": "[parameters('objectId')]",
98+
"permissions": {
99+
"keys": "[parameters('keysPermissions')]",
100+
"secrets": "[parameters('secretsPermissions')]"
101+
}
102+
}
103+
],
104+
"sku": {
105+
"name": "[parameters('skuName')]",
106+
"family": "A"
107+
}
108+
},
109+
"resources": [
110+
{
111+
"type": "secrets",
112+
"name": "[parameters('secretName')]",
113+
"apiVersion": "2015-06-01",
114+
"tags": { "displayName": "secret" },
115+
"properties": {
116+
"value": "[parameters('secretValue')]"
117+
},
118+
"dependsOn": [
119+
"[concat('Microsoft.KeyVault/vaults/', parameters('keyVaultName'))]"
120+
]
121+
}
122+
]
33123
}
34124
]
35125
}

src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/DeploymentTests.ps1

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,22 @@ function Test-NewDeploymentWithKeyVaultReference
8888
# Test
8989
New-AzureRmResourceGroup -Name $rgname -Location $rglocation
9090

91-
$keyVault = New-AzureRmKeyVault -VaultName $keyVaultname -ResourceGroupName $rgname -Location $location -EnabledForTemplateDeployment
92-
$parameters = @{ "keyVaultName" = $keyVaultname; "secretName" = $secretName; "secretValue" = $hostplanName }
93-
New-AzureRmResourceGroupDeployment -Name $rname -ResourceGroupName $rgname -TemplateFile keyVaultSetupTemplate.json -TemplateParameterObject $parameters
91+
$context = Get-AzureRmContext
92+
$subscriptionId = $context.Subscription.SubscriptionId
93+
$account = Get-AzureAccount -Name $context.Account.Id
94+
$tenantId = $account.Tenants
95+
$adUser = Get-AzureRmADUser -UserPrincipalName $context.Account.Id
96+
$objectId = $adUser.Id
97+
$KeyVaultResourceId = "/subscriptions/" + $subscriptionId + "/resourcegroups/" + $rgname + "/providers/Microsoft.KeyVault/vaults/" + $keyVaultname
98+
99+
$parameters = @{ "keyVaultName" = $keyVaultname; "secretName" = $secretName; "secretValue" = $hostplanName; "tenantId" = $tenantId; "objectId" = $objectId }
100+
$deployment = New-AzureRmResourceGroupDeployment -Name $rname -ResourceGroupName $rgname -TemplateFile keyVaultSetupTemplate.json -TemplateParameterObject $parameters
101+
102+
# Assert
103+
Assert-AreEqual Succeeded $deployment.ProvisioningState
94104

95105
$content = (Get-Content keyVaultTemplateParams.json) -join '' | ConvertFrom-Json
96-
$content.hostingPlanName.reference.KeyVault.id = $keyVault.resourceid
106+
$content.hostingPlanName.reference.KeyVault.id = $KeyVaultResourceId
97107
$content.hostingPlanName.reference.SecretName = $secretName
98108
$content | ConvertTo-Json -depth 999 | Out-File keyVaultTemplateParams.json
99109

src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/ResourcesController.cs

Lines changed: 18 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@
2424
using Microsoft.Azure.Graph.RBAC;
2525
using Microsoft.Azure.Insights;
2626
using Microsoft.Azure.Management.Authorization;
27-
using Microsoft.Azure.Management.KeyVault;
2827
using Microsoft.Azure.Management.Resources;
2928
using Microsoft.Azure.Subscriptions;
3029
using Microsoft.Azure.Test;
3130
using Microsoft.Azure.Test.HttpRecorder;
3231
using Microsoft.WindowsAzure.Commands.Common;
3332
using Microsoft.WindowsAzure.Commands.ScenarioTest;
34-
using Microsoft.Azure.Common.Authentication.Models;
33+
3534

3635
namespace Microsoft.Azure.Commands.Resources.Test.ScenarioTests
3736
{
@@ -52,17 +51,15 @@ public sealed class ResourcesController
5251
public SubscriptionClient SubscriptionClient { get; private set; }
5352

5453
public GalleryClient GalleryClient { get; private set; }
55-
54+
5655
public InsightsClient InsightsClient { get; private set; }
5756

5857
public AuthorizationManagementClient AuthorizationManagementClient { get; private set; }
5958

60-
public KeyVaultManagementClient KeyVaultManagementClient { get; private set; }
61-
6259
public string UserDomain { get; private set; }
6360

64-
public static ResourcesController NewInstance
65-
{
61+
public static ResourcesController NewInstance
62+
{
6663
get
6764
{
6865
return new ResourcesController();
@@ -80,18 +77,18 @@ public void RunPsTest(params string[] scripts)
8077
var mockName = TestUtilities.GetCurrentMethodName(2);
8178

8279
RunPsTestWorkflow(
83-
() => scripts,
80+
() => scripts,
8481
// no custom initializer
85-
null,
82+
null,
8683
// no custom cleanup
8784
null,
8885
callingClassType,
8986
mockName);
9087
}
9188

9289
public void RunPsTestWorkflow(
93-
Func<string[]> scriptBuilder,
94-
Action<CSMTestEnvironmentFactory> initialize,
90+
Func<string[]> scriptBuilder,
91+
Action<CSMTestEnvironmentFactory> initialize,
9592
Action cleanup,
9693
string callingClassType,
9794
string mockName)
@@ -102,24 +99,23 @@ public void RunPsTestWorkflow(
10299

103100
this.csmTestFactory = new CSMTestEnvironmentFactory();
104101

105-
if(initialize != null)
102+
if (initialize != null)
106103
{
107104
initialize(this.csmTestFactory);
108105
}
109106

110107
helper.SetupEnvironment(AzureModule.AzureResourceManager);
111-
SetupAzureContext();
108+
112109
SetupManagementClients();
113-
110+
114111
var callingClassName = callingClassType
115112
.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
116113
.Last();
117-
helper.SetupModules(AzureModule.AzureResourceManager,
118-
"ScenarioTests\\Common.ps1",
119-
"ScenarioTests\\" + callingClassName + ".ps1",
120-
helper.RMProfileModule,
121-
helper.RMResourceModule,
122-
helper.GetRMModulePath("AzureRM.KeyVault.psd1"));
114+
helper.SetupModules(AzureModule.AzureResourceManager,
115+
"ScenarioTests\\Common.ps1",
116+
"ScenarioTests\\" + callingClassName + ".ps1",
117+
helper.RMProfileModule,
118+
helper.RMResourceModule);
123119

124120
try
125121
{
@@ -135,7 +131,7 @@ public void RunPsTestWorkflow(
135131
}
136132
finally
137133
{
138-
if(cleanup !=null)
134+
if (cleanup != null)
139135
{
140136
cleanup();
141137
}
@@ -152,7 +148,6 @@ private void SetupManagementClients()
152148
GraphClient = GetGraphClient();
153149
InsightsClient = GetInsightsClient();
154150
this.FeatureClient = this.GetFeatureClient();
155-
KeyVaultManagementClient = GetKeyVaultManagementClient();
156151
HttpClientHelperFactory.Instance = new TestHttpClientHelperFactory(this.csmTestFactory.GetTestEnvironment().Credentials as SubscriptionCloudCredentials);
157152

158153
helper.SetupManagementClients(ResourceManagementClient,
@@ -161,8 +156,7 @@ private void SetupManagementClients()
161156
AuthorizationManagementClient,
162157
GraphClient,
163158
InsightsClient,
164-
this.FeatureClient,
165-
KeyVaultManagementClient);
159+
this.FeatureClient);
166160
}
167161

168162
private GraphRbacManagementClient GetGraphClient()
@@ -227,81 +221,6 @@ private InsightsClient GetInsightsClient()
227221
return TestBase.GetServiceClient<InsightsClient>(this.csmTestFactory);
228222
}
229223

230-
private KeyVaultManagementClient GetKeyVaultManagementClient()
231-
{
232-
return TestBase.GetServiceClient<KeyVaultManagementClient>(this.csmTestFactory);
233-
}
234-
235-
private void SetupAzureContext()
236-
{
237-
TestEnvironment csmEnvironment = new CSMTestEnvironmentFactory().GetTestEnvironment();
238-
239-
if (csmEnvironment.SubscriptionId != null)
240-
{
241-
//Overwrite the default subscription and default account
242-
//with ones using user ID and tenant ID from auth context
243-
var user = GetUser(csmEnvironment);
244-
var tenantId = GetTenantId(csmEnvironment);
245-
246-
var testSubscription = new AzureSubscription()
247-
{
248-
Id = new Guid(csmEnvironment.SubscriptionId),
249-
Name = AzureRmProfileProvider.Instance.Profile.Context.Subscription.Name,
250-
Environment = AzureRmProfileProvider.Instance.Profile.Context.Environment.Name,
251-
Account = user,
252-
Properties = new Dictionary<AzureSubscription.Property, string>
253-
{
254-
{AzureSubscription.Property.Default, "True"},
255-
{
256-
AzureSubscription.Property.StorageAccount,
257-
Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT")
258-
},
259-
{AzureSubscription.Property.Tenants, tenantId},
260-
}
261-
};
262-
263-
var testAccount = new AzureAccount()
264-
{
265-
Id = user,
266-
Type = AzureAccount.AccountType.User,
267-
Properties = new Dictionary<AzureAccount.Property, string>
268-
{
269-
{AzureAccount.Property.Subscriptions, csmEnvironment.SubscriptionId},
270-
{AzureAccount.Property.Tenants, tenantId},
271-
}
272-
};
273-
274-
AzureRmProfileProvider.Instance.Profile.Context = new AzureContext(testSubscription, testAccount, AzureRmProfileProvider.Instance.Profile.Context.Environment, new AzureTenant { Id = new Guid(tenantId) });
275-
}
276-
277-
}
278-
279-
private string GetTenantId(TestEnvironment environment)
280-
{
281-
if (HttpMockServer.Mode == HttpRecorderMode.Record)
282-
{
283-
HttpMockServer.Variables["TenantId"] = environment.AuthorizationContext.TenantId;
284-
return environment.AuthorizationContext.TenantId;
285-
}
286-
else
287-
{
288-
return HttpMockServer.Variables["TenantId"];
289-
}
290-
}
291-
292-
private string GetUser(TestEnvironment environment)
293-
{
294-
if (HttpMockServer.Mode == HttpRecorderMode.Record)
295-
{
296-
HttpMockServer.Variables["User"] = environment.AuthorizationContext.UserId;
297-
return environment.AuthorizationContext.UserId;
298-
}
299-
else
300-
{
301-
return HttpMockServer.Variables["User"];
302-
}
303-
}
304-
305224
/// <summary>
306225
/// The test http client helper factory.
307226
/// </summary>

0 commit comments

Comments
 (0)