Skip to content

Commit a55bb4f

Browse files
authored
Merge pull request #9069 from aim-for-better/FixPowerShell
[HDInsight] - Add a cmdlet to update gateway credential
2 parents 1728ebb + 308bed2 commit a55bb4f

17 files changed

+505
-400
lines changed

src/HDInsight/HDInsight.Test/HDInsight.Test.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<PsModuleName>HDInsight</PsModuleName>
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.Azure.Management.HDInsight" Version="2.0.8" />
14+
<PackageReference Include="Microsoft.Azure.Management.HDInsight" Version="2.1.0" />
1515
<PackageReference Include="Microsoft.Azure.Management.HDInsight.Job" Version="2.0.7" />
1616
</ItemGroup>
1717

src/HDInsight/HDInsight.Test/HDInsightTestBase.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ public virtual void SetupManagementClientForJobTests()
9191
hdinsightManagementMock.Setup(c => c.GetClusterConfigurations(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
9292
.Returns(configurationResponse)
9393
.Verifiable();
94+
95+
var listConfigurationsResponse = new ClusterListConfigurationsResponse
96+
{
97+
Configurations = new Dictionary<string, ClusterConfiguration>
98+
{
99+
{
100+
"core-site", new ClusterConfiguration
101+
{
102+
Configuration=configurationResponse
103+
}
104+
}
105+
}
106+
};
107+
108+
hdinsightManagementMock.Setup(c => c.ListConfigurations(It.IsAny<string>(), It.IsAny<string>()))
109+
.Returns(listConfigurationsResponse)
110+
.Verifiable();
94111
}
95112
}
96113
}

src/HDInsight/HDInsight.Test/UnitTests/HttpTests.cs

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.WindowsAzure.Commands.Common;
1717
using Microsoft.WindowsAzure.Commands.ScenarioTest;
1818
using Moq;
19+
using System;
1920
using System.Management.Automation;
2021
using System.Net;
2122
using Xunit;
@@ -24,8 +25,7 @@ namespace Microsoft.Azure.Commands.HDInsight.Test
2425
{
2526
public class HttpTests : HDInsightTestBase
2627
{
27-
private GrantAzureHDInsightHttpServicesAccessCommand grantcmdlet;
28-
private RevokeAzureHDInsightHttpServicesAccessCommand revokecmdlet;
28+
private SetAzureHDInsightGatewayCredentialCommand setcmdlet;
2929

3030
private readonly PSCredential _httpCred;
3131

@@ -35,30 +35,25 @@ public HttpTests(Xunit.Abstractions.ITestOutputHelper output)
3535
base.SetupTestsForManagement();
3636
_httpCred = new PSCredential("hadoopuser", string.Format("Password1!").ConvertToSecureString());
3737

38-
grantcmdlet = new GrantAzureHDInsightHttpServicesAccessCommand
38+
setcmdlet = new SetAzureHDInsightGatewayCredentialCommand
3939
{
4040
CommandRuntime = commandRuntimeMock.Object,
4141
HDInsightManagementClient = hdinsightManagementMock.Object,
42-
ClusterName = ClusterName,
42+
Name = ClusterName,
4343
ResourceGroupName = ResourceGroupName,
4444
HttpCredential = _httpCred
4545
};
46-
revokecmdlet = new RevokeAzureHDInsightHttpServicesAccessCommand
47-
{
48-
CommandRuntime = commandRuntimeMock.Object,
49-
HDInsightManagementClient = hdinsightManagementMock.Object,
50-
ClusterName = ClusterName,
51-
ResourceGroupName = ResourceGroupName
52-
};
5346
}
5447

5548
[Fact]
5649
[Trait(Category.AcceptanceType, Category.CheckIn)]
57-
public void CanGrantHttpAccess()
50+
public void CanSetGatewayCredentialSupportsShouldProcess()
5851
{
52+
commandRuntimeMock.Setup(c => c.ShouldProcess(ClusterName, It.IsAny<string>())).Returns(true);
53+
5954
hdinsightManagementMock.Setup(
6055
c =>
61-
c.ConfigureHttp(ResourceGroupName, ClusterName,
56+
c.UpdateGatewayCredential(ResourceGroupName, ClusterName,
6257
It.Is<HttpSettingsParameters>(
6358
param =>
6459
param.HttpUserEnabled && param.HttpUsername == _httpCred.UserName &&
@@ -71,58 +66,58 @@ public void CanGrantHttpAccess()
7166
})
7267
.Verifiable();
7368

74-
var connectivitysettings = new HttpConnectivitySettings
69+
var gatewayCredential = new HttpConnectivitySettings
7570
{
7671
HttpPassword = _httpCred.Password.ConvertToString(),
7772
HttpUserEnabled = true,
7873
HttpUsername = _httpCred.UserName,
7974
StatusCode = HttpStatusCode.OK
8075
};
8176

82-
hdinsightManagementMock.Setup(c => c.GetConnectivitySettings(ResourceGroupName, ClusterName))
83-
.Returns(connectivitysettings)
77+
hdinsightManagementMock.Setup(c => c.GetGatewaySettings(ResourceGroupName, ClusterName))
78+
.Returns(gatewayCredential)
8479
.Verifiable();
8580

86-
grantcmdlet.ExecuteCmdlet();
81+
setcmdlet.ExecuteCmdlet();
8782

8883
commandRuntimeMock.VerifyAll();
89-
commandRuntimeMock.Verify(f => f.WriteObject(connectivitysettings), Times.Once);
84+
commandRuntimeMock.Verify(f => f.WriteObject(gatewayCredential), Times.Once);
9085
}
9186

9287
[Fact]
9388
[Trait(Category.AcceptanceType, Category.CheckIn)]
94-
public void CanRevokeHttpAccess()
89+
public void CanWriteErrorWhenSetGatewayCredentialFailedSupportsProcess()
9590
{
91+
var result = new OperationResource
92+
{
93+
ErrorInfo = new ErrorInfo { Code = "Ambari Failed Code", Message = "GetAmbariUserFailed" },
94+
StatusCode = HttpStatusCode.OK,
95+
State = AsyncOperationState.Failed
96+
};
97+
98+
commandRuntimeMock.Setup(c => c.ShouldProcess(ClusterName, It.IsAny<string>())).Returns(true);
99+
96100
hdinsightManagementMock.Setup(
97101
c =>
98-
c.ConfigureHttp(ResourceGroupName, ClusterName,
102+
c.UpdateGatewayCredential(ResourceGroupName, ClusterName,
99103
It.Is<HttpSettingsParameters>(
100104
param =>
101-
!param.HttpUserEnabled &&
102-
string.IsNullOrEmpty(param.HttpPassword) &&
103-
string.IsNullOrEmpty(param.HttpUsername))))
104-
.Returns(new OperationResource
105-
{
106-
ErrorInfo = null,
107-
StatusCode = HttpStatusCode.OK,
108-
State = AsyncOperationState.Succeeded
109-
})
110-
.Verifiable();
111-
112-
var connectivitysettings = new HttpConnectivitySettings
113-
{
114-
HttpUserEnabled = false,
115-
StatusCode = HttpStatusCode.OK
116-
};
117-
118-
hdinsightManagementMock.Setup(c => c.GetConnectivitySettings(ResourceGroupName, ClusterName))
119-
.Returns(connectivitysettings)
105+
param.HttpUserEnabled && param.HttpUsername == _httpCred.UserName &&
106+
param.HttpPassword == _httpCred.Password.ConvertToString())))
107+
.Returns(result)
120108
.Verifiable();
121109

122-
revokecmdlet.ExecuteCmdlet();
110+
setcmdlet.ExecuteCmdlet();
123111

124112
commandRuntimeMock.VerifyAll();
125-
commandRuntimeMock.Verify(f => f.WriteObject(connectivitysettings), Times.Once);
113+
commandRuntimeMock.Verify(
114+
f =>
115+
f.WriteError(It.Is<ErrorRecord>(
116+
record =>
117+
record.Exception.Message == $"{result.ErrorInfo.Code}: {result.ErrorInfo.Message}" &&
118+
string.IsNullOrEmpty(record.FullyQualifiedErrorId) &&
119+
record.CategoryInfo.Category == ErrorCategory.InvalidArgument)),
120+
Times.Once);
126121
}
127122
}
128123
}

src/HDInsight/HDInsight/Az.HDInsight.psd1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ CmdletsToExport = 'Get-AzHDInsightJob', 'New-AzHDInsightSqoopJobDefinition',
8989
'Add-AzHDInsightScriptAction', 'Add-AzHDInsightMetastore',
9090
'Add-AzHDInsightConfigValue', 'Get-AzHDInsightProperty',
9191
'Revoke-AzHDInsightRdpServicesAccess',
92-
'Revoke-AzHDInsightHttpServicesAccess',
9392
'Grant-AzHDInsightRdpServicesAccess',
94-
'Grant-AzHDInsightHttpServicesAccess',
93+
'Set-AzHDInsightGatewayCredential',
9594
'New-AzHDInsightClusterConfig', 'Remove-AzHDInsightCluster',
9695
'Set-AzHDInsightClusterSize',
9796
'Get-AzHDInsightPersistedScriptAction',

src/HDInsight/HDInsight/ChangeLog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Removed two cmdlets:
22+
- Grant-AzHDInsightHttpServicesAccess
23+
- Revoke-AzHDInsightHttpServicesAccess
24+
* Added a new cmdlet Set-AzHDInsightGatewayCredential to replace Grant-AzHDInsightHttpServicesAccess
25+
* Update cmdlet Get-AzHDInsightJobOutput to distinguish reader role and hdinsight operator role:
26+
- Users with reader role need to specify `DefaultStorageAccountKey` parameter explicitly, otherwise error occurs.
27+
- Users with hdinsight operator role will not be affected.
28+
2129

2230
## Version 1.1.0
2331
* Updated cmdlets with plural nouns to singular, and deprecated plural names.

src/HDInsight/HDInsight/ClusterConfigurationUtils.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static AzureHDInsightDefaultStorageAccount GetDefaultStorageAccountDetail
5353
string defaultFSUrl;
5454
const string AdlPrefix = "adl://";
5555
const string WasbPrefix = "wasb://";
56+
const string SecureWasbPrefix = "wasbs://";
5657

5758
if (coreSiteConfiguration.TryGetValue(key, out defaultFSUrl))
5859
{
@@ -78,15 +79,26 @@ public static AzureHDInsightDefaultStorageAccount GetDefaultStorageAccountDetail
7879
resourceUri: resourceUri
7980
);
8081
}
81-
else if (defaultFSUrl.StartsWith(WasbPrefix))
82+
else if (defaultFSUrl.StartsWith(WasbPrefix) || defaultFSUrl.StartsWith(SecureWasbPrefix))
8283
{
83-
string[] accountAndContainer = defaultFSUrl.Substring(WasbPrefix.Length).Split('@');
84+
string[] accountAndContainer;
85+
if (defaultFSUrl.StartsWith(WasbPrefix))
86+
{
87+
accountAndContainer = defaultFSUrl.Substring(WasbPrefix.Length).Split('@');
88+
}
89+
else
90+
{
91+
accountAndContainer = defaultFSUrl.Substring(SecureWasbPrefix.Length).Split('@');
92+
}
93+
94+
string storageAccountKey;
95+
coreSiteConfiguration.TryGetValue(Constants.ClusterConfiguration.StorageAccountKeyPrefix + accountAndContainer[1], out storageAccountKey);
8496

8597
return new AzureHDInsightWASBDefaultStorageAccount
8698
(
8799
storageContainerName: accountAndContainer[0],
88100
storageAccountName: accountAndContainer[1],
89-
storageAccountKey: coreSiteConfiguration[Constants.ClusterConfiguration.StorageAccountKeyPrefix + accountAndContainer[1]]
101+
storageAccountKey: storageAccountKey
90102
);
91103
}
92104
else

src/HDInsight/HDInsight/HDInsight.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<PsModuleName>HDInsight</PsModuleName>
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.Azure.Management.HDInsight" Version="2.0.8" />
14+
<PackageReference Include="Microsoft.Azure.Management.HDInsight" Version="2.1.0" />
1515
<PackageReference Include="Microsoft.Azure.Management.HDInsight.Job" Version="2.0.7" />
1616
<PackageReference Include="WindowsAzure.Storage" Version="9.3.0" />
1717
</ItemGroup>

src/HDInsight/HDInsight/JobCommands/GetAzureHDInsightJobOutputCommand.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@
1717
using Microsoft.Azure.Commands.HDInsight.Models;
1818
using Microsoft.Azure.Commands.HDInsight.Models.Job;
1919
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
20+
using Microsoft.Azure.Management.HDInsight;
2021
using Microsoft.Azure.Management.HDInsight.Job.Models;
22+
using Microsoft.Azure.Management.HDInsight.Models;
2123
using Microsoft.WindowsAzure.Commands.Common;
24+
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
25+
using System;
2226
using System.IO;
27+
using System.Linq;
2328
using System.Management.Automation;
2429

2530
namespace Microsoft.Azure.Commands.HDInsight
2631
{
27-
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "HDInsightJobOutput"),OutputType(typeof(string))]
32+
[GenericBreakingChange("Users with reader role need to specify `DefaultStorageAccountKey` parameter explicitly, otherwise error occurs.", "2.0.0", "05/06/2019")]
33+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "HDInsightJobOutput"), OutputType(typeof(string))]
2834
public class GetAzureHDInsightJobOutputCommand : HDInsightCmdletBase
2935
{
3036
#region Input Parameter Definitions
@@ -131,20 +137,53 @@ private static string Convert(Stream stream)
131137
return text;
132138
}
133139

140+
private string GetStorageAccountKey(string resourceGroupName, string clusterName)
141+
{
142+
string storageAccountKey = null;
143+
string errorMessage = "Fails to retrieve storage account key. Please specify DefaultStorageAccountKey explicitly.";
144+
const string AuthorizationFailedCode = "AuthorizationFailed";
145+
146+
try
147+
{
148+
ClusterConfiguration coreSiteClusterConfiguration;
149+
HDInsightManagementClient.ListConfigurations(resourceGroupName, clusterName).Configurations.TryGetValue(ConfigurationKey.CoreSite, out coreSiteClusterConfiguration);
150+
coreSiteClusterConfiguration?.Configuration.TryGetValue(Constants.ClusterConfiguration.StorageAccountKeyPrefix + DefaultStorageAccountName, out storageAccountKey);
151+
}
152+
catch (CloudException cloudEx)
153+
{
154+
if (cloudEx.Error.Code == AuthorizationFailedCode)
155+
{
156+
errorMessage = "Insufficient permissions to retrieve storage account key. Please specify DefaultStorageAccountKey explicitly.";
157+
}
158+
}
159+
catch (Exception ex)
160+
{
161+
errorMessage = errorMessage + " Reason: " + ex.Message;
162+
}
163+
164+
if (storageAccountKey == null)
165+
{
166+
throw new CloudException(errorMessage);
167+
}
168+
169+
return storageAccountKey;
170+
}
171+
134172
internal IStorageAccess GetDefaultStorageAccess(string resourceGroupName, string clusterName)
135173
{
136174
var StorageAccountSuffix = "";
137-
175+
138176
if (DefaultContainer == null && DefaultStorageAccountName == null && DefaultStorageAccountKey == null)
139177
{
140178
var DefaultStorageAccount = GetDefaultStorageAccount(resourceGroupName, clusterName);
141179

142180
var wasbAccount = DefaultStorageAccount as AzureHDInsightWASBDefaultStorageAccount;
181+
143182
if (wasbAccount != null)
144183
{
145184
DefaultContainer = wasbAccount.StorageContainerName;
146185
DefaultStorageAccountName = wasbAccount.StorageAccountName;
147-
DefaultStorageAccountKey = wasbAccount.StorageAccountKey;
186+
DefaultStorageAccountKey = GetStorageAccountKey(resourceGroupName, clusterName);
148187
StorageAccountSuffix = DefaultContext.Environment.StorageEndpointSuffix;
149188
}
150189
else
@@ -154,7 +193,7 @@ internal IStorageAccess GetDefaultStorageAccess(string resourceGroupName, string
154193

155194
}
156195

157-
return new AzureStorageAccess(DefaultStorageAccountName, DefaultStorageAccountKey, DefaultContainer,StorageAccountSuffix);
196+
return new AzureStorageAccess(DefaultStorageAccountName, DefaultStorageAccountKey, DefaultContainer, StorageAccountSuffix);
158197
}
159198
}
160199
}

0 commit comments

Comments
 (0)