Skip to content

Commit 9519997

Browse files
committed
Merge branch 'dev' of https://github.com/Azure/azure-powershell into FixSetResource
2 parents 894b03c + 484f3bd commit 9519997

File tree

26 files changed

+492
-367
lines changed

26 files changed

+492
-367
lines changed

src/Common/Commands.Common/AzurePSCmdlet.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public abstract class AzurePSCmdlet : PSCmdlet, IDisposable
3838
protected readonly ConcurrentQueue<string> _debugMessages;
3939

4040
private RecordingTracingInterceptor _httpTracingInterceptor;
41-
41+
4242
private DebugStreamTraceListener _adalListener;
4343
protected static AzurePSDataCollectionProfile _dataCollectionProfile = null;
4444
protected static string _errorRecordFolderPath = null;
@@ -272,6 +272,12 @@ protected bool IsVerbose()
272272
base.WriteError(errorRecord);
273273
}
274274

275+
protected new void ThrowTerminatingError(ErrorRecord errorRecord)
276+
{
277+
FlushDebugMessages(IsDataCollectionAllowed());
278+
base.ThrowTerminatingError(errorRecord);
279+
}
280+
275281
protected new void WriteObject(object sendToPipeline)
276282
{
277283
FlushDebugMessages();
@@ -515,6 +521,11 @@ protected virtual void Dispose(bool disposing)
515521

516522
public void Dispose()
517523
{
524+
try
525+
{
526+
FlushDebugMessages();
527+
}
528+
catch { }
518529
Dispose(true);
519530
GC.SuppressFinalize(this);
520531
}

src/ResourceManager/Common/Commands.ScenarioTests.ResourceManager.Common/Mocks/MockClientFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public void AddUserAgent(string productName)
189189

190190
/// <summary>
191191
/// This class exists to allow adding an additional reference to the httpClient to prevent the client
192-
/// from being disposed. Should not be used execpt in this mocked context.
192+
/// from being disposed. Should not be used except in this mocked context.
193193
/// </summary>
194194
class PassThroughDelegatingHandler : DelegatingHandler
195195
{

src/ResourceManager/DataFactories/Commands.DataFactories.Test/Commands.DataFactories.Test.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@
9595
<SpecificVersion>False</SpecificVersion>
9696
<HintPath>..\..\..\packages\Microsoft.Data.Services.Client.5.6.4\lib\net40\Microsoft.Data.Services.Client.dll</HintPath>
9797
</Reference>
98+
<Reference Include="Microsoft.DataTransfer.Gateway.Encryption, Version=1.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
99+
<SpecificVersion>False</SpecificVersion>
100+
<HintPath>..\..\..\packages\Microsoft.DataTransfer.Gateway.Encryption.1.7.5798-preview-001\lib\net45\Microsoft.DataTransfer.Gateway.Encryption.dll</HintPath>
101+
</Reference>
98102
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory">
99103
<HintPath>..\..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.18.206251556\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
100104
</Reference>

src/ResourceManager/DataFactories/Commands.DataFactories.Test/UnitTests/NewDataFactoryEncryptValueTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using Microsoft.Azure.Commands.DataFactories;
1616
using Microsoft.Azure.Commands.DataFactories.Test;
17+
using Microsoft.DataTransfer.Gateway.Encryption;
1718
using Microsoft.WindowsAzure.Commands.ScenarioTest;
1819
using Moq;
1920
using System;
@@ -109,5 +110,24 @@ public void TestOnPremDatasourceEncryptionWinAuth()
109110
this.dataFactoriesClientMock.Verify(f => f.OnPremisesEncryptString(secureString, ResourceGroupName, DataFactoryName, GatewayName, credential, linkedServiceType, nonCredentialValue, authenticationType, serverName, databaseName), Times.Once());
110111
this.commandRuntimeMock.Verify(f => f.WriteObject(expectedOutput), Times.Once());
111112
}
113+
114+
[Fact]
115+
[Trait(Category.AcceptanceType, Category.CheckIn)]
116+
public void TestOnPremDatasourceEncryption_LinkedServiceTypeParsing()
117+
{
118+
// Built-in linked service types
119+
Assert.True(DataFactoryClient.GetLinkedServiceType("OnPremisesSqlLinkedService") == LinkedServiceType.OnPremisesSqlLinkedService);
120+
Assert.True(DataFactoryClient.GetLinkedServiceType("OnPremisesFileSystemLinkedService") == LinkedServiceType.OnPremisesFileSystemLinkedService);
121+
Assert.True(DataFactoryClient.GetLinkedServiceType("OnPremisesOracleLinkedService") == LinkedServiceType.OnPremisesOracleLinkedService);
122+
Assert.True(DataFactoryClient.GetLinkedServiceType("OnPremisesOdbcLinkedService") == LinkedServiceType.OnPremisesOdbcLinkedService);
123+
Assert.True(DataFactoryClient.GetLinkedServiceType("OnPremisesPostgreSqlLinkedService") == LinkedServiceType.OnPremisesPostgreSqlLinkedService);
124+
Assert.True(DataFactoryClient.GetLinkedServiceType("OnPremisesTeradataLinkedService") == LinkedServiceType.OnPremisesTeradataLinkedService);
125+
Assert.True(DataFactoryClient.GetLinkedServiceType("OnPremisesMySQLLinkedService") == LinkedServiceType.OnPremisesMySQLLinkedService);
126+
Assert.True(DataFactoryClient.GetLinkedServiceType("OnPremisesDB2LinkedService") == LinkedServiceType.OnPremisesDB2LinkedService);
127+
Assert.True(DataFactoryClient.GetLinkedServiceType("OnPremisesSybaseLinkedService") == LinkedServiceType.OnPremisesSybaseLinkedService);
128+
129+
// Generic linked service types should be converted to Unknown type
130+
Assert.True(DataFactoryClient.GetLinkedServiceType("HdfsLinkedService") == LinkedServiceType.Unknown);
131+
}
112132
}
113133
}

src/ResourceManager/DataFactories/Commands.DataFactories.Test/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<package id="Microsoft.Data.Edm" version="5.6.4" targetFramework="net45" />
1818
<package id="Microsoft.Data.OData" version="5.6.4" targetFramework="net45" />
1919
<package id="Microsoft.Data.Services.Client" version="5.6.4" targetFramework="net45" />
20+
<package id="Microsoft.DataTransfer.Gateway.Encryption" version="1.7.5798-preview-001" targetFramework="net45" />
2021
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.18.206251556" targetFramework="net45" />
2122
<package id="Microsoft.Net.Http" version="2.2.28" targetFramework="net45" />
2223
<package id="Microsoft.Rest.ClientRuntime" version="1.4.1" targetFramework="net45" />

src/ResourceManager/DataFactories/Commands.DataFactories/Commands.DataFactories.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@
8585
<SpecificVersion>False</SpecificVersion>
8686
<HintPath>..\..\..\packages\Microsoft.Data.Services.Client.5.6.4\lib\net40\Microsoft.Data.Services.Client.dll</HintPath>
8787
</Reference>
88-
<Reference Include="Microsoft.DataTransfer.Gateway.Encryption, Version=1.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
88+
<Reference Include="Microsoft.DataTransfer.Gateway.Encryption, Version=1.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
8989
<SpecificVersion>False</SpecificVersion>
90-
<HintPath>..\..\..\packages\Microsoft.DataTransfer.Gateway.Encryption.1.5.1-preview\lib\net45\Microsoft.DataTransfer.Gateway.Encryption.dll</HintPath>
90+
<HintPath>..\..\..\packages\Microsoft.DataTransfer.Gateway.Encryption.1.7.5798-preview-001\lib\net45\Microsoft.DataTransfer.Gateway.Encryption.dll</HintPath>
9191
</Reference>
9292
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.18.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
9393
<HintPath>..\..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.18.206251556\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>

src/ResourceManager/DataFactories/Commands.DataFactories/Encrypt/NewAzureDataFactoryEncryptValueCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public class NewAzureDataFactoryEncryptValueCommand : DataFactoryBaseCmdlet
5252
HelpMessage = "The linked service type.")]
5353
[ValidateSet("OnPremisesSqlLinkedService", "OnPremisesFileSystemLinkedService", "OnPremisesOracleLinkedService",
5454
"OnPremisesOdbcLinkedService", "OnPremisesPostgreSqlLinkedService", "OnPremisesTeradataLinkedService",
55-
"OnPremisesMySQLLinkedService", "OnPremisesDB2LinkedService", "OnPremisesSybaseLinkedService",
55+
"OnPremisesMySQLLinkedService", "OnPremisesDB2LinkedService", "OnPremisesSybaseLinkedService",
56+
"HdfsLinkedService",
5657
IgnoreCase = true)]
5758
public string Type { get; set; }
5859

src/ResourceManager/DataFactories/Commands.DataFactories/Models/DataFactoryClient.Encrypt.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public virtual string OnPremisesEncryptString(SecureString value,
3232
string authenticationType,
3333
string serverName, string databaseName)
3434
{
35-
LinkedServiceType linkedServiceType = type == null ? LinkedServiceType.OnPremisesSqlLinkedService : (LinkedServiceType)Enum.Parse(typeof(LinkedServiceType), type, true);
35+
LinkedServiceType linkedServiceType = type == null ? LinkedServiceType.OnPremisesSqlLinkedService : GetLinkedServiceType(type);
3636

3737
if (linkedServiceType == LinkedServiceType.OnPremisesSqlLinkedService && linkedServiceType == LinkedServiceType.OnPremisesOracleLinkedService
3838
&& linkedServiceType == LinkedServiceType.OnPremisesFileSystemLinkedService && (value == null || value.Length == 0))
@@ -59,5 +59,17 @@ public virtual string OnPremisesEncryptString(SecureString value,
5959
UserInputConnectionString connectionString = new UserInputConnectionString(value, nonCredentialValue, userName, password, linkedServiceType, authType, serverName, databaseName);
6060
return GatewayEncryptionClient.Encrypt(connectionString, gatewayEncryptionInfos);
6161
}
62+
63+
internal static LinkedServiceType GetLinkedServiceType(string typeName)
64+
{
65+
LinkedServiceType result;
66+
if (!Enum.TryParse<LinkedServiceType>(typeName, true, out result))
67+
{
68+
// Treat any non-existing type as a generic data source type for encryption
69+
return LinkedServiceType.Unknown;
70+
}
71+
72+
return result;
73+
}
6274
}
6375
}

src/ResourceManager/DataFactories/Commands.DataFactories/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<package id="Microsoft.Data.Edm" version="5.6.4" targetFramework="net45" />
1414
<package id="Microsoft.Data.OData" version="5.6.4" targetFramework="net45" />
1515
<package id="Microsoft.Data.Services.Client" version="5.6.4" targetFramework="net45" />
16-
<package id="Microsoft.DataTransfer.Gateway.Encryption" version="1.5.1-preview" targetFramework="net45" />
16+
<package id="Microsoft.DataTransfer.Gateway.Encryption" version="1.7.5798-preview-001" targetFramework="net45" />
1717
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.18.206251556" targetFramework="net45" />
1818
<package id="Microsoft.Net.Http" version="2.2.28" targetFramework="net45" />
1919
<package id="Microsoft.Rest.ClientRuntime" version="1.4.1" targetFramework="net45" />

src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
using System.Collections.Generic;
2222
using Microsoft.IdentityModel.Clients.ActiveDirectory;
2323
using Microsoft.WindowsAzure.Commands.ScenarioTest;
24+
using Microsoft.WindowsAzure.Commands.Common;
25+
using Moq;
2426

2527
namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
2628
{
@@ -32,6 +34,7 @@ public class AzureRMProfileTests
3234
private static string DefaultSubscriptionName = "Contoso Subscription";
3335
private static string DefaultDomain = "contoso.com";
3436
private static Guid DefaultTenant = Guid.NewGuid();
37+
private static AzureContext Context;
3538

3639
private static RMProfileClient SetupTestEnvironment(List<string> tenants, params List<string>[] subscriptionLists)
3740
{
@@ -45,7 +48,7 @@ private static RMProfileClient SetupTestEnvironment(List<string> tenants, params
4548
}, true);
4649
mock.MoqClients = true;
4750
AzureSession.ClientFactory = mock;
48-
var context = new AzureContext(new AzureSubscription()
51+
Context = new AzureContext(new AzureSubscription()
4952
{
5053
Account = DefaultAccount,
5154
Environment = EnvironmentName.AzureCloud,
@@ -56,10 +59,64 @@ private static RMProfileClient SetupTestEnvironment(List<string> tenants, params
5659
AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
5760
new AzureTenant() { Domain = DefaultDomain, Id = DefaultTenant });
5861
var profile = new AzureRMProfile();
59-
profile.Context = context;
62+
profile.Context = Context;
6063
return new RMProfileClient(profile);
6164
}
6265

66+
[Fact]
67+
[Trait(Category.AcceptanceType, Category.CheckIn)]
68+
public void TokenIdAndAccountIdMismatch()
69+
{
70+
var tenants = new List<string> { Guid.NewGuid().ToString(), DefaultTenant.ToString() };
71+
var secondsubscriptionInTheFirstTenant = Guid.NewGuid().ToString();
72+
var firstList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
73+
var secondList = new List<string> { Guid.NewGuid().ToString() };
74+
var thirdList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
75+
var fourthList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
76+
var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);
77+
78+
var tokens = new Queue<MockAccessToken>();
79+
tokens.Enqueue(new MockAccessToken
80+
{
81+
UserId = "[email protected]",
82+
LoginType = LoginType.OrgId,
83+
AccessToken = "bbb"
84+
});
85+
tokens.Enqueue(new MockAccessToken
86+
{
87+
UserId = "[email protected]",
88+
LoginType = LoginType.OrgId,
89+
AccessToken = "bbb",
90+
TenantId = tenants.First()
91+
});
92+
tokens.Enqueue(new MockAccessToken
93+
{
94+
UserId = "[email protected]",
95+
LoginType = LoginType.OrgId,
96+
AccessToken = "bbb",
97+
TenantId = tenants.Last()
98+
});
99+
100+
((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
101+
{
102+
var token = tokens.Dequeue();
103+
account.Id = token.UserId;
104+
return token;
105+
};
106+
107+
var azureRmProfile = client.Login(
108+
Context.Account,
109+
Context.Environment,
110+
null,
111+
secondsubscriptionInTheFirstTenant,
112+
null,
113+
null);
114+
115+
var tenantsInAccount = azureRmProfile.Context.Account.GetPropertyAsArray( AzureAccount.Property.Tenants);
116+
Assert.Equal(1, tenantsInAccount.Length);
117+
Assert.Equal(tenants.First(), tenantsInAccount[0]);
118+
}
119+
63120
[Fact]
64121
[Trait(Category.AcceptanceType, Category.CheckIn)]
65122
public void MultipleTenantsAndSubscriptionsSucceed()

src/ResourceManager/Profile/Commands.Profile.Test/ProfileController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ private void SetupManagementClients()
120120
helper.SetupManagementClients(SubscriptionClient);
121121
}
122122

123-
124123
private SubscriptionClient GetSubscriptionClient()
125124
{
126125
return TestBase.GetServiceClient<SubscriptionClient>(this.csmTestFactory);

src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public AzureRMProfile Login(
7777
{
7878
var tenants = ListAccountTenants(account, environment, password, promptBehavior).Select(s => s.Id.ToString()).ToArray();
7979
account.SetProperty(AzureAccount.Property.Tenants, null);
80+
string accountId = null;
8081

8182
for (int i = 0; i < tenants.Count(); i++)
8283
{
@@ -90,7 +91,26 @@ public AzureRMProfile Login(
9091
try
9192
{
9293
token = AcquireAccessToken(account, environment, tenant, password, ShowDialog.Auto);
93-
account.SetOrAppendProperty(AzureAccount.Property.Tenants, tenant);
94+
95+
if (accountId == null)
96+
{
97+
accountId = account.Id;
98+
account.SetOrAppendProperty(AzureAccount.Property.Tenants, tenant);
99+
}
100+
else if (accountId.Equals(account.Id, StringComparison.OrdinalIgnoreCase))
101+
{
102+
account.SetOrAppendProperty(AzureAccount.Property.Tenants, tenant);
103+
}
104+
else
105+
{ // if account ID is different from the first tenant account id we need to ignore current tenant
106+
WriteWarningMessage(string.Format(
107+
Microsoft.Azure.Commands.Profile.Properties.Resources.AccountIdMismatch,
108+
account.Id,
109+
tenant,
110+
accountId));
111+
account.Id = accountId;
112+
token = null;
113+
}
94114
}
95115
catch
96116
{

src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@
120120
<data name="AccessTokenRequiresAccount" xml:space="preserve">
121121
<value>AccountId must be provided to use an AccessToken credential.</value>
122122
</data>
123+
<data name="AccountIdMismatch" xml:space="preserve">
124+
<value>Account ID '{0}' for tenant '{1}' does not match home Account ID '{2}'</value>
125+
</data>
123126
<data name="AccountIdRequired" xml:space="preserve">
124127
<value>Access token credentials must provide the AccountId parameter.</value>
125128
</data>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@
354354
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceGroupTests\TestFindResourceGroup.json">
355355
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
356356
</None>
357+
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceGroupTests\TestGetNonExistingResourceGroupWithDebugStream.json">
358+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
359+
</None>
357360
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceGroupTests\TestNewDeploymentAndProviderRegistration.json">
358361
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
359362
</None>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,11 @@ public void TestRemoveDeployment()
8787
{
8888
ResourcesController.NewInstance.RunPsTest("Test-RemoveDeployment");
8989
}
90+
91+
[Fact]
92+
public void TestGetNonExistingResourceGroupWithDebugStream()
93+
{
94+
ResourcesController.NewInstance.RunPsTest("Test-GetNonExistingResourceGroupWithDebugStream");
95+
}
9096
}
9197
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,16 @@ function Test-FindResourceGroup
305305
Clean-ResourceGroup $rgname
306306
Clean-ResourceGroup $rgname2
307307
}
308+
}
309+
310+
<#
311+
.SYNOPSIS
312+
Tests remove non exist resource group and debug stream gets printed
313+
#>
314+
function Test-GetNonExistingResourceGroupWithDebugStream
315+
{
316+
$ErrorActionPreference="Continue"
317+
$output = $(Get-AzureRmResourceGroup -Name "InvalidNonExistRocks" -Debug) 2>&1 5>&1 | Out-String
318+
$ErrorActionPreference="Stop"
319+
Assert-True { $output -Like "*============================ HTTP RESPONSE ============================*" }
308320
}

0 commit comments

Comments
 (0)