Skip to content

Commit 24eebc9

Browse files
author
Hovsep
committed
Merge pull request #2166 from yoreddy/dev
Support for CertificateFileContents in HDI ADL cmdlets
2 parents 71b7811 + 2087be4 commit 24eebc9

File tree

8 files changed

+224
-128
lines changed

8 files changed

+224
-128
lines changed

src/ResourceManager/HDInsight/Commands.HDInsight.Test/ScenarioTests/DataLakeStoreScenarioTests.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function Test-DataLakeStoreClusterCreate{
3434
$workernodeSize = "Standard_D3"
3535
$username = "admin"
3636
$passwd = " "
37-
$certPasswd = " "
37+
$certPasswd = ""
3838
$certPath = " "
3939
$certFile = "sp.pfx"
4040
$servPrincipal = "sp"
@@ -48,7 +48,11 @@ function Test-DataLakeStoreClusterCreate{
4848
$sshCred=New-Object System.Management.Automation.PSCredential($sshUser,$sshPasswd)
4949

5050
#execute
51-
$cluster = New-AzureRmHDInsightCluster -Location $locName -ResourceGroupName $rmGroup -ClusterType Hadoop -ClusterName $clusterName -ClusterSizeInNodes $clusterNodes -HttpCredential $credential -DefaultStorageAccountName "ypseastus.blob.core.windows.net" -DefaultStorageAccountKey $storageAccountKey -DefaultStorageContainer $clusterName -Version $version -SshCredential $sshCred -OSType Linux -ObjectId $objectId -CertificateFilePath $certFilePath -CertificatePassword $certPasswd
51+
$cluster = New-AzureRmHDInsightCluster -Location $locName -ResourceGroupName $rmGroup -ClusterType Hadoop `
52+
-ClusterName $clusterName -ClusterSizeInNodes $clusterNodes -HttpCredential $credential `
53+
-DefaultStorageAccountName "$saName.blob.core.windows.net" -DefaultStorageAccountKey $storageAccountKey `
54+
-DefaultStorageContainer $clusterName -Version $version -SshCredential $sshCred -OSType Linux `
55+
-ObjectId $objectId -CertificateFilePath $certFilePath -CertificatePassword $certPasswd
5256

5357
#assert
5458
Assert-NotNull $cluster

src/ResourceManager/HDInsight/Commands.HDInsight.Test/ScenarioTests/HDInsightScenarioTestsBase.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ protected HDInsightManagementClient GetHdInsightManagementClient()
5353
return TestBase.GetServiceClient<HDInsightManagementClient>(this.csmTestFactory);
5454
}
5555

56+
/// <summary>
57+
/// Runs the PowerShell test
58+
/// </summary>
59+
/// <param name="scripts">script to be executed</param>
5660
public void RunPsTest(params string[] scripts)
5761
{
5862
var callingClassType = TestUtilities.GetCallingClass(2);
@@ -68,6 +72,14 @@ public void RunPsTest(params string[] scripts)
6872
mockName);
6973
}
7074

75+
/// <summary>
76+
/// Runs the PowerShell test under mock undo context based on the test mode setting (Record|Playback)
77+
/// </summary>
78+
/// <param name="scriptBuilder">Script builder delegate</param>
79+
/// <param name="initialize">initialize action</param>
80+
/// <param name="cleanup">cleanup action</param>
81+
/// <param name="callingClassType">Calling class type</param>
82+
/// <param name="mockName">Mock Name</param>
7183
public void RunPsTestWorkflow(
7284
Func<string[]> scriptBuilder,
7385
Action<CSMTestEnvironmentFactory> initialize,

src/ResourceManager/HDInsight/Commands.HDInsight.Test/UnitTests/DataLakeStoreTests.cs

Lines changed: 64 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ public class DataLakeStoreTests : HDInsightTestBase
4242
private Guid AadTenantId = new Guid("11111111-1111-1111-1111-111111111111");
4343
private string Certificate = "";
4444
private string CertificatePassword = "";
45-
45+
private byte[] CertificateFileContents = { };
4646
private readonly PSCredential _httpCred;
47+
private Mock<AzureHDInsightConfig> AzureHDInsightconfigMock;
4748

4849
public DataLakeStoreTests()
4950
{
@@ -54,120 +55,52 @@ public DataLakeStoreTests()
5455
CommandRuntime = commandRuntimeMock.Object,
5556
HDInsightManagementClient = hdinsightManagementMock.Object
5657
};
58+
AzureHDInsightconfigMock = new Mock<AzureHDInsightConfig>();
5759
}
5860

59-
[Fact(Skip="Test currently failing. To be fixed in next release.")]
61+
[Fact]
6062
[Trait(Category.AcceptanceType, Category.CheckIn)]
61-
public void CanCreateNewHDInsightDataLakeStoreCluster()
63+
public void CanCreateClusterConfigWithDataLakeStoreParameters()
6264
{
63-
cmdlet.ClusterName = ClusterName;
64-
cmdlet.ResourceGroupName = ResourceGroupName;
65-
cmdlet.ClusterSizeInNodes = ClusterSize;
66-
cmdlet.Location = Location;
67-
cmdlet.HttpCredential = _httpCred;
68-
cmdlet.DefaultStorageAccountName = StorageName;
69-
cmdlet.DefaultStorageAccountKey = StorageKey;
70-
cmdlet.AadTenantId = AadTenantId;
71-
cmdlet.ObjectId = ObjectId;
72-
cmdlet.CertificateFilePath = Certificate;
73-
cmdlet.CertificatePassword = CertificatePassword;
74-
75-
var cluster = new Cluster
76-
{
77-
Id = "id",
78-
Name = ClusterName,
79-
Location = Location,
80-
Properties = new ClusterGetProperties
81-
{
82-
ClusterVersion = "3.2",
83-
ClusterState = "Running",
84-
ClusterDefinition = new ClusterDefinition
85-
{
86-
ClusterType = ClusterType
87-
},
88-
QuotaInfo = new QuotaInfo
89-
{
90-
CoresUsed = 24
91-
},
92-
OperatingSystemType = OSType.Windows
93-
}
94-
};
95-
var coreConfigs = new Dictionary<string, string>
96-
{
97-
{"fs.defaultFS", "wasb://dummycsmv2@" + StorageName},
98-
{
99-
"fs.azure.account.key." + StorageName,
100-
StorageKey
101-
}
102-
};
103-
var gatewayConfigs = new Dictionary<string, string>
104-
{
105-
{"restAuthCredential.isEnabled", "true"},
106-
{"restAuthCredential.username", _httpCred.UserName},
107-
{"restAuthCredential.password", _httpCred.Password.ConvertToString()}
108-
};
109-
var datalakeStoreConfigs = new Dictionary<string, string>
110-
{
111-
{"clusterIdentity.applicationId", ObjectId.ToString()},
112-
{"clusterIdentity.certificate", Certificate},
113-
{"clusterIdentity.certificatePassword", CertificatePassword},
114-
{"clusterIdentity.aadTenantId", AadTenantId.ToString()}
115-
};
116-
var configurations = new Dictionary<string, Dictionary<string, string>>
65+
var newclusteridentitycmdlet = new NewAzureHDInsightClusterConfigCommand()
11766
{
118-
{"core-site", coreConfigs},
119-
{"gateway", gatewayConfigs},
120-
{"clusterIdentity", datalakeStoreConfigs}
67+
CommandRuntime = commandRuntimeMock.Object,
68+
HDInsightManagementClient = hdinsightManagementMock.Object,
69+
ObjectId = ObjectId,
70+
CertificateFilePath = Certificate,
71+
AadTenantId = AadTenantId,
72+
CertificatePassword = CertificatePassword
12173
};
122-
var serializedConfig = JsonConvert.SerializeObject(configurations);
123-
cluster.Properties.ClusterDefinition.Configurations = serializedConfig;
124-
125-
var getresponse = new ClusterGetResponse {Cluster = cluster};
126-
127-
hdinsightManagementMock.Setup(
128-
c => c.CreateNewCluster(ResourceGroupName, ClusterName, It.Is<ClusterCreateParameters>(
129-
parameters =>
130-
parameters.ClusterSizeInNodes == ClusterSize &&
131-
parameters.DefaultStorageAccountName == StorageName &&
132-
parameters.DefaultStorageAccountKey == StorageKey &&
133-
parameters.Location == Location &&
134-
parameters.UserName == _httpCred.UserName &&
135-
parameters.Password == _httpCred.Password.ConvertToString() &&
136-
parameters.ClusterType == ClusterType &&
137-
parameters.OSType == OSType.Windows)))
138-
.Returns(getresponse)
139-
.Verifiable();
140-
141-
cmdlet.ExecuteCmdlet();
14274

143-
commandRuntimeMock.VerifyAll();
144-
commandRuntimeMock.Verify(f => f.WriteObject(It.Is<AzureHDInsightCluster>(
145-
clusterout =>
146-
clusterout.ClusterState == "Running" &&
147-
clusterout.ClusterType == ClusterType &&
148-
clusterout.ClusterVersion == "3.2" &&
149-
clusterout.CoresUsed == 24 &&
150-
clusterout.Location == Location &&
151-
clusterout.Name == ClusterName &&
152-
clusterout.OperatingSystemType == OSType.Windows)),
153-
Times.Once);
75+
newclusteridentitycmdlet.ExecuteCmdlet();
76+
commandRuntimeMock.Verify(
77+
f =>
78+
f.WriteObject(
79+
It.Is<AzureHDInsightConfig>(
80+
c =>
81+
c.AADTenantId == AadTenantId &&
82+
c.CertificatePassword == CertificatePassword &&
83+
c.ObjectId == ObjectId &&
84+
c.CertificateFilePath == Certificate
85+
)),
86+
Times.Once);
15487
}
15588

15689
[Fact]
15790
[Trait(Category.AcceptanceType, Category.CheckIn)]
158-
public void CanCreateClusterConfigWithDataLakeStoreParameters()
91+
public void CanCreateDataLakeClusterWithCertificationFileContents()
15992
{
160-
var newclusteridentitycmdlet = new NewAzureHDInsightClusterConfigCommand()
93+
var clusterIdentityCmdlet = new NewAzureHDInsightClusterConfigCommand()
16194
{
16295
CommandRuntime = commandRuntimeMock.Object,
16396
HDInsightManagementClient = hdinsightManagementMock.Object,
164-
CertificateFilePath = Certificate,
16597
ObjectId = ObjectId,
98+
CertificateFileContents = CertificateFileContents,
16699
AadTenantId = AadTenantId,
167100
CertificatePassword = CertificatePassword
168101
};
169102

170-
newclusteridentitycmdlet.ExecuteCmdlet();
103+
clusterIdentityCmdlet.ExecuteCmdlet();
171104
commandRuntimeMock.Verify(
172105
f =>
173106
f.WriteObject(
@@ -176,9 +109,43 @@ public void CanCreateClusterConfigWithDataLakeStoreParameters()
176109
c.AADTenantId == AadTenantId &&
177110
c.CertificatePassword == CertificatePassword &&
178111
c.ObjectId == ObjectId &&
179-
c.CertificateFilePath == Certificate
112+
c.CertificateFileContents == CertificateFileContents
180113
)),
181-
Times.Once);
114+
Times.Once);
115+
}
116+
117+
[Fact]
118+
[Trait(Category.AcceptanceType, Category.CheckIn)]
119+
public void ShouldThrowIfCertificateOptionsAreNotPassed()
120+
{
121+
var clusterIdentityCmdlet = new AddAzureHDInsightClusterIdentity()
122+
{
123+
CommandRuntime = commandRuntimeMock.Object,
124+
HDInsightManagementClient = hdinsightManagementMock.Object,
125+
ObjectId = ObjectId,
126+
AadTenantId = AadTenantId,
127+
CertificatePassword = CertificatePassword
128+
};
129+
130+
Assert.Throws<ArgumentException>(() => clusterIdentityCmdlet.ExecuteCmdlet());
131+
}
132+
133+
[Fact]
134+
[Trait(Category.AcceptanceType, Category.CheckIn)]
135+
public void ShouldThrowIfBothCertificateOptionsArePassed()
136+
{
137+
var clusterIdentityCmdlet = new AddAzureHDInsightClusterIdentity()
138+
{
139+
CommandRuntime = commandRuntimeMock.Object,
140+
HDInsightManagementClient = hdinsightManagementMock.Object,
141+
ObjectId = ObjectId,
142+
AadTenantId = AadTenantId,
143+
CertificatePassword = CertificatePassword,
144+
CertificateFileContents = CertificateFileContents,
145+
CertificateFilePath = Certificate
146+
};
147+
148+
Assert.Throws<ArgumentException>(() => clusterIdentityCmdlet.ExecuteCmdlet());
182149
}
183150
}
184151
}

src/ResourceManager/HDInsight/Commands.HDInsight/ManagementCommands/AddAzureHDInsightClusterIdentity.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ namespace Microsoft.Azure.Commands.HDInsight.ManagementCommands
2121
{
2222
[Cmdlet(
2323
VerbsCommon.Add,
24-
Constants.CommandNames.AzureHDInsightClusterIdentity),
24+
Constants.CommandNames.AzureHDInsightClusterIdentity,
25+
DefaultParameterSetName = CertificateFilePathSet),
2526
OutputType(typeof(AzureHDInsightConfig))]
2627
public class AddAzureHDInsightClusterIdentity : HDInsightCmdletBase
2728
{
29+
private const string CertificateFilePathSet = "CertificateFilePath";
30+
private const string CertificateFileContentsSet = "CertificateFileContents";
31+
2832
#region Input Parameter Definitions
2933

3034
[Parameter(Position = 0,
@@ -41,9 +45,16 @@ public class AddAzureHDInsightClusterIdentity : HDInsightCmdletBase
4145

4246
[Parameter(Position = 2,
4347
Mandatory = true,
44-
HelpMessage = "The Service Principal certificate for accessing Azure Data Lake.")]
48+
HelpMessage = "The Service Principal certificate file path for accessing Azure Data Lake.",
49+
ParameterSetName = CertificateFilePathSet)]
4550
public string CertificateFilePath { get; set; }
4651

52+
[Parameter(Position = 2,
53+
Mandatory = true,
54+
HelpMessage = "The Service Principal certificate file contents for accessing Azure Data Lake.",
55+
ParameterSetName = CertificateFileContentsSet)]
56+
public byte[] CertificateFileContents { get; set; }
57+
4758
[Parameter(Position = 3,
4859
Mandatory = true,
4960
HelpMessage = "The Service Principal certificate password for accessing Azure Data Lake.")]
@@ -52,16 +63,32 @@ public class AddAzureHDInsightClusterIdentity : HDInsightCmdletBase
5263
[Parameter(Position = 4,
5364
Mandatory = false,
5465
HelpMessage = "The Service Principal AAD Tenant Id for accessing Azure Data Lake.")]
55-
public Guid AadTenantId { get; set; }
66+
public Guid AadTenantId { get; set; }
5667

5768
#endregion
5869

5970
public override void ExecuteCmdlet()
6071
{
72+
switch (ParameterSetName)
73+
{
74+
case CertificateFilePathSet:
75+
{
76+
Config.CertificateFilePath = CertificateFilePath;
77+
}
78+
break;
79+
case CertificateFileContentsSet:
80+
{
81+
Config.CertificateFileContents = CertificateFileContents;
82+
}
83+
break;
84+
default:
85+
throw new ArgumentException("Please specify CertificateFilePath or CertificateFileContent");
86+
}
87+
6188
Config.ObjectId = ObjectId;
6289
Config.AADTenantId = AadTenantId;
63-
Config.CertificateFilePath = CertificateFilePath;
6490
Config.CertificatePassword = CertificatePassword;
91+
6592
WriteObject(Config);
6693
}
6794
}

src/ResourceManager/HDInsight/Commands.HDInsight/ManagementCommands/NewAzureHDInsightClusterCommand.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ namespace Microsoft.Azure.Commands.HDInsight
3535
{
3636
[Cmdlet(
3737
VerbsCommon.New,
38-
Constants.CommandNames.AzureHDInsightCluster),
38+
Constants.CommandNames.AzureHDInsightCluster,
39+
DefaultParameterSetName = DefaultParameterSet),
3940
OutputType(
4041
typeof(AzureHDInsightCluster))]
4142
public class NewAzureHDInsightClusterCommand : HDInsightCmdletBase
4243
{
4344
private ClusterCreateParameters parameters;
45+
private const string CertificateFilePathSet = "CertificateFilePath";
46+
private const string CertificateFileContentsSet = "CertificateFileContents";
47+
private const string DefaultParameterSet = "Default";
48+
4449
#region Input Parameter Definitions
4550

4651
[Parameter(
@@ -118,6 +123,7 @@ public AzureHDInsightConfig Config
118123
OozieMetastore = OozieMetastore,
119124
ObjectId = ObjectId,
120125
AADTenantId = AadTenantId,
126+
CertificateFileContents = CertificateFileContents,
121127
CertificateFilePath = CertificateFilePath,
122128
CertificatePassword = CertificatePassword
123129
};
@@ -155,6 +161,7 @@ var storageAccount in
155161
parameters.ZookeeperNodeSize = value.ZookeeperNodeSize;
156162
HiveMetastore = value.HiveMetastore;
157163
OozieMetastore = value.OozieMetastore;
164+
CertificateFileContents = value.CertificateFileContents;
158165
CertificateFilePath = value.CertificateFilePath;
159166
AadTenantId = value.AADTenantId;
160167
ObjectId = value.ObjectId;
@@ -282,13 +289,18 @@ public DateTime RdpAccessExpiry
282289
[Parameter(HelpMessage = "Gets or sets the Service Principal Object Id for accessing Azure Data Lake.")]
283290
public Guid ObjectId { get; set; }
284291

285-
[Parameter(HelpMessage = "Gets or sets the Service Principal Certificate for accessing Azure Data Lake.")]
292+
[Parameter(HelpMessage = "Gets or sets the Service Principal Certificate file path for accessing Azure Data Lake.",
293+
ParameterSetName = CertificateFilePathSet)]
286294
public string CertificateFilePath { get; set; }
287295

296+
[Parameter(HelpMessage = "Gets or sets the Service Principal Certificate file contents for accessing Azure Data Lake.",
297+
ParameterSetName = CertificateFileContentsSet)]
298+
public byte[] CertificateFileContents { get; set; }
299+
288300
[Parameter(HelpMessage = "Gets or sets the Service Principal Certificate Password for accessing Azure Data Lake.")]
289301
public string CertificatePassword { get; set; }
290302

291-
[Parameter(HelpMessage = "Gets or sets the Service Principal AAD Tenant Id for accessing Azure Data Lake.", ParameterSetName = "ServicePrincipal")]
303+
[Parameter(HelpMessage = "Gets or sets the Service Principal AAD Tenant Id for accessing Azure Data Lake.")]
292304
public Guid AadTenantId { get; set; }
293305

294306
#endregion
@@ -352,14 +364,18 @@ var storageAccount in
352364
var metastore = HiveMetastore;
353365
parameters.HiveMetastore = new Metastore(metastore.SqlAzureServerName, metastore.DatabaseName, metastore.Credential.UserName, metastore.Credential.Password.ConvertToString());
354366
}
355-
if (CertificateFilePath != null && CertificatePassword != null)
367+
if(!string.IsNullOrEmpty(CertificatePassword))
356368
{
369+
if (!string.IsNullOrEmpty(CertificateFilePath)){
370+
CertificateFileContents = File.ReadAllBytes(CertificateFilePath);
371+
}
357372
var servicePrincipal = new Management.HDInsight.Models.ServicePrincipal(
358-
GetApplicationId(), GetTenantId(AadTenantId), File.ReadAllBytes(CertificateFilePath),
373+
GetApplicationId(), GetTenantId(AadTenantId), CertificateFileContents,
359374
CertificatePassword);
375+
360376
parameters.Principal = servicePrincipal;
361377
}
362-
378+
363379
var cluster = HDInsightManagementClient.CreateNewCluster(ResourceGroupName, ClusterName, parameters);
364380

365381
if (cluster != null)

0 commit comments

Comments
 (0)