Skip to content

Commit 924ed2b

Browse files
committed
Adding new files that were missed in the previous commit
1 parent fae1539 commit 924ed2b

File tree

4 files changed

+282
-0
lines changed

4 files changed

+282
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Common.Authentication;
16+
using Microsoft.Azure.Management.Authorization;
17+
using Microsoft.Azure.Management.Resources;
18+
using Microsoft.Azure.Subscriptions;
19+
using Microsoft.Azure.Test.HttpRecorder;
20+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
21+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
22+
using Microsoft.Azure.Test;
23+
using System;
24+
using System.Linq;
25+
using Microsoft.Azure.Gallery;
26+
using Microsoft.Azure.Graph.RBAC;
27+
using Microsoft.Azure.Management.KeyVault;
28+
using Microsoft.Azure.Common.Authentication.Models;
29+
using System.Collections.Generic;
30+
31+
32+
namespace Microsoft.Azure.Commands.KeyVault.Test
33+
{
34+
public class KeyVaultEnvSetupHelper : EnvironmentSetupHelper
35+
{
36+
public void SetupEnvironment()
37+
{
38+
base.SetupEnvironment(AzureModule.AzureResourceManager);
39+
40+
TestEnvironment csmEnvironment = new CSMTestEnvironmentFactory().GetTestEnvironment();
41+
42+
if (csmEnvironment.SubscriptionId != null)
43+
{
44+
//Overwrite the default subscription and default account
45+
//with ones using user ID and tenant ID from auth context
46+
var user = GetUser(csmEnvironment);
47+
var tenantId = GetTenantId(csmEnvironment);
48+
49+
var testSubscription = new AzureSubscription()
50+
{
51+
Id = new Guid(csmEnvironment.SubscriptionId),
52+
Name = ProfileClient.Profile.DefaultSubscription.Name,
53+
Environment = ProfileClient.Profile.DefaultSubscription.Environment,
54+
Account = user,
55+
Properties = new Dictionary<AzureSubscription.Property, string>
56+
{
57+
{AzureSubscription.Property.Default, "True"},
58+
{
59+
AzureSubscription.Property.StorageAccount,
60+
Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT")
61+
},
62+
{AzureSubscription.Property.Tenants, tenantId},
63+
}
64+
};
65+
66+
var testAccount = new AzureAccount()
67+
{
68+
Id = user,
69+
Type = AzureAccount.AccountType.User,
70+
Properties = new Dictionary<AzureAccount.Property, string>
71+
{
72+
{AzureAccount.Property.Subscriptions, csmEnvironment.SubscriptionId},
73+
}
74+
};
75+
76+
ProfileClient.Profile.Accounts.Remove(ProfileClient.Profile.DefaultSubscription.Account);
77+
ProfileClient.Profile.Subscriptions[testSubscription.Id] = testSubscription;
78+
ProfileClient.Profile.Accounts[testAccount.Id] = testAccount;
79+
ProfileClient.SetSubscriptionAsDefault(testSubscription.Name, testSubscription.Account);
80+
81+
ProfileClient.Profile.Save();
82+
}
83+
}
84+
85+
private string GetTenantId(TestEnvironment environment)
86+
{
87+
if (HttpMockServer.Mode == HttpRecorderMode.Record)
88+
{
89+
HttpMockServer.Variables["TenantId"] = environment.AuthorizationContext.TenatId;
90+
return environment.AuthorizationContext.TenatId;
91+
}
92+
else
93+
{
94+
return HttpMockServer.Variables["TenantId"];
95+
}
96+
}
97+
98+
private string GetUser(TestEnvironment environment)
99+
{
100+
if (HttpMockServer.Mode == HttpRecorderMode.Record)
101+
{
102+
HttpMockServer.Variables["User"] = environment.AuthorizationContext.UserId;
103+
return environment.AuthorizationContext.UserId;
104+
}
105+
else
106+
{
107+
return HttpMockServer.Variables["User"];
108+
}
109+
}
110+
111+
}
112+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Linq;
17+
using Microsoft.Azure.Commands.Tags.Model;
18+
using KeyVaultManagement = Microsoft.Azure.Management.KeyVault;
19+
using PSResourceManagerModels = Microsoft.Azure.Commands.Resources.Models;
20+
21+
22+
namespace Microsoft.Azure.Commands.KeyVault.Models
23+
{
24+
public class PSVault : PSVaultIdentityItem
25+
{
26+
public PSVault(KeyVaultManagement.Vault vault, PSResourceManagerModels.ActiveDirectory.ActiveDirectoryClient adClient)
27+
{
28+
var vaultTenantDisplayName = ModelExtensions.GetDisplayNameForTenant(vault.Properties.TenantId, adClient);
29+
VaultName = vault.Name;
30+
Location = vault.Location;
31+
ResourceId = vault.Id;
32+
ResourceGroupName = (new PSResourceManagerModels.ResourceIdentifier(vault.Id)).ResourceGroupName;
33+
Tags = TagsConversionHelper.CreateTagHashtable(vault.Tags);
34+
Sku = vault.Properties.Sku.Name;
35+
TenantId = vault.Properties.TenantId;
36+
TenantName = vaultTenantDisplayName;
37+
VaultUri = vault.Properties.VaultUri;
38+
EnabledForDeployment = vault.Properties.EnabledForDeployment;
39+
AccessPolicies = vault.Properties.AccessPolicies.Select(s => new PSVaultAccessPolicy(s, adClient)).ToArray();
40+
OriginalVault = vault;
41+
}
42+
public string VaultUri { get; private set; }
43+
44+
public Guid TenantId { get; private set; }
45+
46+
public string TenantName { get; private set; }
47+
48+
public string Sku { get; private set; }
49+
50+
public bool EnabledForDeployment { get; private set; }
51+
52+
public PSVaultAccessPolicy[] AccessPolicies { get; private set; }
53+
54+
public string AccessPoliciesText { get { return ModelExtensions.ConstructAccessPoliciesList(AccessPolicies); } }
55+
56+
//If we got this vault from the server, save the over-the-wire version, to
57+
//allow easy updates
58+
public KeyVaultManagement.Vault OriginalVault { get; private set; }
59+
}
60+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using KeyVaultManagement = Microsoft.Azure.Management.KeyVault;
18+
using PSResourceManagerModels = Microsoft.Azure.Commands.Resources.Models;
19+
20+
namespace Microsoft.Azure.Commands.KeyVault.Models
21+
{
22+
public class PSVaultAccessPolicy
23+
{
24+
public PSVaultAccessPolicy(Guid tenantId, Guid objectId, string[] permissionsToKeys, string[] permissionsToSecrets)
25+
{
26+
TenantId = tenantId;
27+
ObjectId = objectId;
28+
PermissionsToSecrets = permissionsToSecrets == null ? new List<string>() : new List<string>(permissionsToSecrets);
29+
PermissionsToKeys = permissionsToKeys == null ? new List<string>() : new List<string>(permissionsToKeys);
30+
}
31+
public PSVaultAccessPolicy(KeyVaultManagement.AccessPolicyEntry s, PSResourceManagerModels.ActiveDirectory.ActiveDirectoryClient adClient)
32+
{
33+
ObjectId = s.ObjectId;
34+
DisplayName = ModelExtensions.GetDisplayNameForADObject(s.ObjectId, adClient);
35+
TenantId = s.TenantId;
36+
TenantName = s.TenantId.ToString();
37+
PermissionsToSecrets = new List<string>(s.PermissionsToSecrets);
38+
PermissionsToKeys = new List<string>(s.PermissionsToKeys);
39+
}
40+
41+
public Guid TenantId { get; private set; }
42+
43+
public string TenantName { get; private set; }
44+
45+
public Guid ObjectId { get; private set; }
46+
47+
public string DisplayName { get; private set; }
48+
49+
public List<string> PermissionsToKeys { get; private set; }
50+
51+
public string PermissionsToKeysStr { get { return string.Join(", ", PermissionsToKeys); } }
52+
53+
public List<string> PermissionsToSecrets { get; private set; }
54+
55+
public string PermissionsToSecretsStr { get { return string.Join(", ", PermissionsToSecrets); } }
56+
}
57+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System.Collections;
16+
using Microsoft.Azure.Commands.Tags.Model;
17+
using PSResourceManagerModels = Microsoft.Azure.Commands.Resources.Models;
18+
using ResourceManagement = Microsoft.Azure.Management.Resources.Models;
19+
20+
namespace Microsoft.Azure.Commands.KeyVault.Models
21+
{
22+
public class PSVaultIdentityItem
23+
{
24+
public PSVaultIdentityItem()
25+
{
26+
27+
}
28+
public PSVaultIdentityItem(ResourceManagement.GenericResourceExtended resource)
29+
{
30+
PSResourceManagerModels.ResourceIdentifier identifier = new PSResourceManagerModels.ResourceIdentifier(resource.Id);
31+
VaultName = identifier.ResourceName;
32+
ResourceId = resource.Id;
33+
ResourceGroupName = identifier.ResourceGroupName;
34+
Location = resource.Location;
35+
Tags = TagsConversionHelper.CreateTagHashtable(resource.Tags);
36+
}
37+
public string ResourceId { get; protected set; }
38+
39+
public string VaultName { get; protected set; }
40+
41+
public string ResourceGroupName { get; protected set; }
42+
43+
public string Location { get; protected set; }
44+
45+
public Hashtable[] Tags { get; protected set; }
46+
47+
public string TagsTable
48+
{
49+
get { return PSResourceManagerModels.ResourcesExtensions.ConstructTagsTable(Tags); }
50+
}
51+
52+
}
53+
}

0 commit comments

Comments
 (0)