Skip to content

Commit 935126b

Browse files
authored
Copy Directory from AzureTenant and Type from AzureEnvironment in DeepCopy of Azure Context (#402)
* Define and implement DeepCloneable interface for Azure* class such as AzureContext 1. Define and implementation `IDeepCloneable` interface 2. Remove unused codes in `DefaultProfile` * Address the review comments * Address review comments
1 parent 9b5f8b9 commit 935126b

File tree

3 files changed

+26
-56
lines changed

3 files changed

+26
-56
lines changed

src/Authentication.Abstractions.Test/ContextUnitTests.cs

+12-3
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,18 @@ public void TestDeepCopy()
3434
IAzureAccount account = new AzureAccount()
3535
{
3636
37-
Type = "User"
37+
Type = AzureAccount.AccountType.User
3838
};
3939

4040
IAzureEnvironment environment = new AzureEnvironment()
4141
{
42-
Name = "my environment"
42+
Name = "my environment",
43+
Type = AzureEnvironment.TypeDiscovered
4344
};
4445
IAzureTenant tenant = new AzureTenant()
4546
{
46-
Id = "DDDDDDDD-DDDD-DDDD-DDDD-DDDDDDDDDDDD"
47+
Id = "DDDDDDDD-DDDD-DDDD-DDDD-DDDDDDDDDDDD",
48+
Directory = SubHomeTenant
4749
};
4850
IAzureContext original = new AzureContext(subscription, account, environment, tenant);
4951
const string PropertyKey = "customPropertyKey";
@@ -68,6 +70,13 @@ public void TestDeepCopy()
6870
// custom property
6971
Assert.Equal(SubHomeTenant, clone.Subscription.GetHomeTenant());
7072
Assert.Equal(PropertyValue, clone.GetProperty(PropertyKey));
73+
74+
// implementation specific attributes
75+
Assert.Equal(((AzureEnvironment)original.Environment).Type, ((AzureEnvironment)clone.Environment).Type);
76+
Assert.Equal(((AzureTenant)original.Tenant).Directory, ((AzureTenant)clone.Tenant).Directory);
77+
78+
//tokenCache checking
79+
Assert.Null(clone.TokenCache.CacheData);
7180
}
7281

7382
[Fact]

src/Authentication.Abstractions/Extensions/AzureContextExtensions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ public static IAzureContext DeepCopy(this IAzureContext context)
171171
deepCopy.Tenant.CopyFrom(context.Tenant);
172172
deepCopy.Subscription.CopyFrom(context.Subscription);
173173
deepCopy.Environment.CopyFrom(context.Environment);
174+
175+
//TODO:Whenever to introduce more properties that needs to be copied here, extract the logic to a new method.
176+
if (context.Tenant is AzureTenant azureTenant)
177+
{
178+
((AzureTenant)deepCopy.Tenant).Directory = azureTenant.Directory;
179+
}
180+
if (context.Environment is AzureEnvironment azureEnvironment)
181+
{
182+
((AzureEnvironment)deepCopy.Environment).Type = azureEnvironment.Type;
183+
}
174184
deepCopy.CopyPropertiesFrom(context);
175185
return deepCopy;
176186
}

src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs

+4-53
Original file line numberDiff line numberDiff line change
@@ -86,59 +86,7 @@ public IAzureContextContainer DefaultProfile
8686
set
8787
{
8888
_profile = value;
89-
/* If the profile is from customer setting, the ServicePrincipalSecret and CertificatePassword
90-
* which is required to get access token is always removed.
91-
* Try to recover ServicePrincipalSecret and CertificatePassword from the buildin profile.
92-
*/
93-
if (null != _profile && null != AzureRmProfileProvider.Instance.Profile.Accounts)
94-
{
95-
var accountMap = CreateAccountMap(AzureRmProfileProvider.Instance.Profile.Accounts);
96-
foreach (var currentAccount in _profile.Accounts.Where(p => null != p?.Id))
97-
{
98-
if (accountMap.TryGetValue(currentAccount.Id, out IList<IAzureAccount> accList))
99-
{
100-
foreach (var acc in accList)
101-
{
102-
if (currentAccount.GetTenants().All(tId => acc.GetTenants().Contains(tId)))
103-
{
104-
MergeProperty(currentAccount, acc, AzureAccount.Property.ServicePrincipalSecret);
105-
MergeProperty(currentAccount, acc, AzureAccount.Property.CertificatePassword);
106-
break;
107-
}
108-
}
109-
}
110-
}
111-
}
112-
}
113-
}
114-
115-
private static Dictionary<string, IList<IAzureAccount>> CreateAccountMap(IEnumerable<IAzureAccount> accounts)
116-
{
117-
var accountMap = new Dictionary<string, IList<IAzureAccount>>();
118-
foreach (var acc in accounts.Where(acc => null != acc?.Id && (null != acc.GetProperty(AzureAccount.Property.ServicePrincipalSecret)
119-
|| null != acc.GetProperty(AzureAccount.Property.CertificatePassword))))
120-
{
121-
if (!accountMap.ContainsKey(acc.Id))
122-
{
123-
accountMap[acc.Id] = new List<IAzureAccount>();
124-
}
125-
accountMap[acc.Id].Add(acc);
12689
}
127-
return accountMap;
128-
}
129-
130-
private static bool MergeProperty(IExtensibleModel dest, IExtensibleModel src, string propertyName)
131-
{
132-
if (string.IsNullOrEmpty(dest.GetProperty(propertyName)))
133-
{
134-
var propertyValue = src.GetProperty(propertyName);
135-
if (!string.IsNullOrEmpty(propertyValue))
136-
{
137-
dest.SetProperty(propertyName, propertyValue);
138-
return true;
139-
}
140-
}
141-
return false;
14290
}
14391

14492
private IAzureContextContainer GetDefaultProfile()
@@ -174,7 +122,10 @@ private IAzureContextContainer CloneProfileAndModifyContext()
174122
}
175123
profile.DefaultContext = profile.DefaultContext.DeepCopy();
176124
profile.DefaultContext.Subscription.CopyFrom(matchingSub);
177-
profile.DefaultContext.Tenant.Id = matchingSub.GetTenant();
125+
profile.DefaultContext.Tenant = new AzureTenant()
126+
{
127+
Id = matchingSub.GetTenant()
128+
};
178129
var matchingUser = profile.Accounts.FirstOrDefault(account => account.Id.Equals(matchingSub.GetAccount()));
179130
if (matchingUser != null)
180131
{

0 commit comments

Comments
 (0)