Skip to content

Commit 127bc61

Browse files
committed
Use secure string for password fields
1 parent a2f7a0c commit 127bc61

File tree

7 files changed

+26
-25
lines changed

7 files changed

+26
-25
lines changed

src/ResourceManager/AzureBatch/Commands.Batch.Test/Certificates/NewBatchCertificateCommandTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System.Management.Automation;
2525
using System.Security.Cryptography.X509Certificates;
2626
using System.Threading.Tasks;
27+
using Microsoft.WindowsAzure.Commands.Common;
2728
using Xunit;
2829
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2930

@@ -119,7 +120,7 @@ public void NewBatchCertificateRequestBodyTest()
119120

120121
// Verify that when the raw data is specified with a password, the request body matches expectations
121122
cmdlet.RawData = cert.RawData;
122-
cmdlet.Password = BatchTestHelpers.TestCertificatePassword;
123+
cmdlet.Password = BatchTestHelpers.TestCertificatePassword.ConvertToSecureString();
123124
cmdlet.ExecuteCmdlet();
124125
Assert.Equal(CertificateFormat.Pfx, requestParameters.CertificateFormat);
125126
Assert.Equal(BatchTestHelpers.TestCertificateAlgorithm, requestParameters.ThumbprintAlgorithm);
@@ -139,7 +140,7 @@ public void NewBatchCertificateRequestBodyTest()
139140
Assert.Equal(certDataBase64String, requestParameters.Data);
140141

141142
// Verify that when a file path is specified with a password, the request body matches expectations
142-
cmdlet.Password = BatchTestHelpers.TestCertificatePassword;
143+
cmdlet.Password = BatchTestHelpers.TestCertificatePassword.ConvertToSecureString();
143144
cmdlet.ExecuteCmdlet();
144145
Assert.Equal(CertificateFormat.Pfx, requestParameters.CertificateFormat);
145146
Assert.Equal(BatchTestHelpers.TestCertificateAlgorithm, requestParameters.ThumbprintAlgorithm);

src/ResourceManager/AzureBatch/Commands.Batch.Test/ComputeNodeUsers/NewBatchComputeNodeUserCommandTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System;
2121
using System.Collections.Generic;
2222
using System.Management.Automation;
23+
using Microsoft.WindowsAzure.Commands.Common;
2324
using Xunit;
2425
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2526
using ProxyModels = Microsoft.Azure.Batch.Protocol.Models;
@@ -57,7 +58,7 @@ public void NewBatchComputeNodeUserParametersTest()
5758
cmdlet.PoolId = "testPool";
5859
cmdlet.ComputeNodeId = "computeNode1";
5960
cmdlet.Name = "testUser";
60-
cmdlet.Password = "Password1234";
61+
cmdlet.Password = "Password1234".ConvertToSecureString();
6162

6263
// Don't go to the service on an Add ComputeNodeUser call
6364
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
@@ -81,7 +82,7 @@ public void NewBatchComputeNodeUserParametersGetPassedToRequestTest()
8182
cmdlet.PoolId = "testPool";
8283
cmdlet.ComputeNodeId = "computeNode1";
8384
cmdlet.Name = "user";
84-
cmdlet.Password = "password";
85+
cmdlet.Password = "password".ConvertToSecureString();
8586
cmdlet.IsAdmin = true;
8687
cmdlet.ExpiryTime = DateTime.Now.AddDays(30);
8788

@@ -101,7 +102,7 @@ public void NewBatchComputeNodeUserParametersGetPassedToRequestTest()
101102

102103
// Verify the request parameters match the cmdlet parameters
103104
Assert.Equal(cmdlet.Name, requestParameters.Name);
104-
Assert.Equal(cmdlet.Password, requestParameters.Password);
105+
Assert.Equal(cmdlet.Password.ConvertToString(), requestParameters.Password);
105106
Assert.Equal(cmdlet.IsAdmin, requestParameters.IsAdmin);
106107
Assert.Equal(cmdlet.ExpiryTime, requestParameters.ExpiryTime);
107108
}

src/ResourceManager/AzureBatch/Commands.Batch.Test/ComputeNodeUsers/SetBatchComputeNoderUserCommandTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System;
2222
using System.Collections.Generic;
2323
using System.Management.Automation;
24+
using Microsoft.WindowsAzure.Commands.Common;
2425
using Xunit;
2526
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2627

@@ -82,7 +83,7 @@ public void SetBatchComputeNodeUserRequestTest()
8283
cmdlet.PoolId = "testPool";
8384
cmdlet.ComputeNodeId = "computeNode1";
8485
cmdlet.Name = "testUser";
85-
cmdlet.Password = "Password1234";
86+
cmdlet.Password = "Password1234".ConvertToSecureString();
8687
cmdlet.ExpiryTime = DateTime.Now.AddDays(1);
8788

8889
string requestPassword = null;
@@ -101,7 +102,7 @@ public void SetBatchComputeNodeUserRequestTest()
101102
cmdlet.ExecuteCmdlet();
102103

103104
// Verify the request parameters match expectations
104-
Assert.Equal(cmdlet.Password, requestPassword);
105+
Assert.Equal(cmdlet.Password.ConvertToString(), requestPassword);
105106
Assert.Equal(cmdlet.ExpiryTime, requestExpiryTime);
106107
}
107108
}

src/ResourceManager/AzureBatch/Commands.Batch.Test/ScenarioTests/ComputeNodeUserTests.ps1

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ function Test-ComputeNodeUserEndToEnd
2222

2323
$context = New-Object Microsoft.Azure.Commands.Batch.Test.ScenarioTests.ScenarioTestContext
2424
$userName = "userendtoend"
25-
$password = "Password1234!"
25+
$password1 = ConvertTo-SecureString "Password1234!" -AsPlainText -Force
2626

2727
# Create a user
28-
New-AzureBatchComputeNodeUser -PoolId $poolId -ComputeNodeId $computeNodeId -Name $userName -Password $password -BatchContext $context
28+
New-AzureBatchComputeNodeUser -PoolId $poolId -ComputeNodeId $computeNodeId -Name $userName -Password $password1 -BatchContext $context
2929

3030
# Update the user. Since there's no Get user API, this also validates that the create call worked (no 404 error).
3131
# Basically just validating that we can set the parameters and execute the cmdlet without error.
3232
# If a Get user API is added, we can validate that the properties were actually updated.
33-
Set-AzureBatchComputeNodeUser $poolId $computeNodeId $userName "Abcdefghijk1234!" -ExpiryTime ([DateTime]::Now.AddDays(5)) -BatchContext $context
33+
$password2 = ConvertTo-SecureString "Abcdefghijk1234!" -AsPlainText -Force
34+
Set-AzureBatchComputeNodeUser $poolId $computeNodeId $userName $password2 -ExpiryTime ([DateTime]::Now.AddDays(5)) -BatchContext $context
3435

3536
# Delete the user
3637
Remove-AzureBatchComputeNodeUser -PoolId $poolId -ComputeNodeId $computeNodeId -Name $userName -BatchContext $context

src/ResourceManager/AzureBatch/Commands.Batch/Certificates/NewBatchCertificateCommand.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
using Microsoft.Azure.Commands.Batch.Models;
1616
using System;
1717
using System.Management.Automation;
18+
using System.Security;
19+
using Microsoft.WindowsAzure.Commands.Common;
1820
using Constants = Microsoft.Azure.Commands.Batch.Utils.Constants;
1921

2022
namespace Microsoft.Azure.Commands.Batch
@@ -37,17 +39,14 @@ public class NewBatchCertificateCommand : BatchObjectModelCmdletBase
3739

3840
[Parameter]
3941
[ValidateNotNullOrEmpty]
40-
[Obsolete("New-AzureRmBatchCertificate: The parameter \"Password\" is being changed from a string to a SecureString in an upcoming breaking change release.")]
41-
public string Password { get; set; }
42+
public SecureString Password { get; set; }
4243

4344
public override void ExecuteCmdlet()
4445
{
4546
NewCertificateParameters parameters = new NewCertificateParameters(this.BatchContext, this.FilePath, this.RawData,
4647
this.AdditionalBehaviors)
4748
{
48-
#pragma warning disable 0618
49-
Password = this.Password
50-
#pragma warning restore 0618
49+
Password = this.Password?.ConvertToString()
5150
};
5251

5352
BatchClient.AddCertificate(parameters);

src/ResourceManager/AzureBatch/Commands.Batch/ComputeNodeUsers/NewBatchComputeNodeUserCommand.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
using Microsoft.Azure.Commands.Batch.Models;
1717
using System;
1818
using System.Management.Automation;
19+
using System.Security;
20+
using Microsoft.WindowsAzure.Commands.Common;
1921
using Constants = Microsoft.Azure.Commands.Batch.Utils.Constants;
2022

2123
namespace Microsoft.Azure.Commands.Batch
@@ -44,8 +46,7 @@ public class NewBatchComputeNodeUserCommand : BatchObjectModelCmdletBase
4446

4547
[Parameter(Mandatory = true, HelpMessage = "The account password.")]
4648
[ValidateNotNullOrEmpty]
47-
[Obsolete("New-AzureRmBatchComputeNodeUser: The parameter \"Password\" is being changed from a string to a SecureString in an upcoming breaking change release.")]
48-
public string Password { get; set; }
49+
public SecureString Password { get; set; }
4950

5051
[Parameter]
5152
[ValidateNotNullOrEmpty]
@@ -60,9 +61,7 @@ public override void ExecuteCmdlet()
6061
this.ComputeNode, this.AdditionalBehaviors)
6162
{
6263
ComputeNodeUserName = this.Name,
63-
#pragma warning disable 0618
64-
Password = this.Password,
65-
#pragma warning restore 0618
64+
Password = this.Password?.ConvertToString(),
6665
ExpiryTime = this.ExpiryTime,
6766
IsAdmin = this.IsAdmin.IsPresent
6867
};

src/ResourceManager/AzureBatch/Commands.Batch/ComputeNodeUsers/SetBatchComputeNodeUserCommand.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
using Microsoft.Azure.Commands.Batch.Models;
1616
using System;
1717
using System.Management.Automation;
18+
using System.Security;
19+
using Microsoft.WindowsAzure.Commands.Common;
1820
using Constants = Microsoft.Azure.Commands.Batch.Utils.Constants;
1921

2022
namespace Microsoft.Azure.Commands.Batch
@@ -39,8 +41,7 @@ public class SetBatchComputeNodeUserCommand : BatchObjectModelCmdletBase
3941

4042
[Parameter(Position = 3, Mandatory = true, HelpMessage = "The account password.")]
4143
[ValidateNotNullOrEmpty]
42-
[Obsolete("Set-AzureRmBatchComputeNodeUser: The parameter \"Password\" is being changed from a string to a SecureString in an upcoming breaking change release.")]
43-
public string Password { get; set; }
44+
public SecureString Password { get; set; }
4445

4546
[Parameter]
4647
[ValidateNotNullOrEmpty]
@@ -51,9 +52,7 @@ public override void ExecuteCmdlet()
5152
UpdateComputeNodeUserParameters parameters = new UpdateComputeNodeUserParameters(this.BatchContext,
5253
this.PoolId, this.ComputeNodeId, this.Name, this.AdditionalBehaviors)
5354
{
54-
#pragma warning disable 0618
55-
Password = this.Password,
56-
#pragma warning restore 0618
55+
Password = this.Password?.ConvertToString(),
5756
ExpiryTime = this.ExpiryTime
5857
};
5958
this.BatchClient.UpdateComputeNodeUser(parameters);

0 commit comments

Comments
 (0)