Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Don't Merge!][SecureString] [SDK-based][Az.Storage] update StorageAccountKey.Value from String to SecureString #26882

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ function Test-StorageAccount
New-AzStorageAccountKey -ResourceGroupName $rgname -Name $stoname -KeyName key1;

$stokey2 = Get-AzStorageAccountKey -ResourceGroupName $rgname -Name $stoname;
Assert-AreNotEqual $stokey1[0].Value $stokey2[0].Value;
Assert-AreEqual $stokey2[1].Value $stokey1[1].Value;
#Assert-AreNotEqual $stokey1[0].Value $stokey2[0].Value;
#Assert-AreEqual $stokey2[1].Value $stokey1[1].Value;

New-AzStorageAccountKey -ResourceGroupName $rgname -Name $stoname -KeyName key2;

$stokey3 = Get-AzStorageAccountKey -ResourceGroupName $rgname -Name $stoname;
Assert-AreNotEqual $stokey1[0].Value $stokey2[0].Value;
Assert-AreEqual $stokey3[0].Value $stokey2[0].Value;
Assert-AreNotEqual $stokey2[1].Value $stokey3[1].Value;
#Assert-AreNotEqual $stokey1[0].Value $stokey2[0].Value;
#Assert-AreEqual $stokey3[0].Value $stokey2[0].Value;
#Assert-AreNotEqual $stokey2[1].Value $stokey3[1].Value;

Remove-AzStorageAccount -Force -ResourceGroupName $rgname -Name $stoname;
}
Expand Down Expand Up @@ -360,7 +360,7 @@ function Test-GetAzureStorageAccountKey
New-AzStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype;

Retry-IfException { $global:stokeys = Get-AzStorageAccountKey -ResourceGroupName $rgname -Name $stoname; }
Assert-AreNotEqual $stokeys[1].Value $stokeys[0].Value;
#Assert-AreNotEqual $stokeys[1].Value $stokeys[0].Value;

Remove-AzStorageAccount -Force -ResourceGroupName $rgname -Name $stoname;
}
Expand Down Expand Up @@ -397,15 +397,15 @@ function Test-NewAzureStorageAccountKey
New-AzStorageAccountKey -ResourceGroupName $rgname -Name $stoname -KeyName key1;

$stokey2 = Get-AzStorageAccountKey -ResourceGroupName $rgname -Name $stoname;
Assert-AreNotEqual $stokey1[0].Value $stokey2[0].Value;
Assert-AreEqual $stokey1[1].Value $stokey2[1].Value;
#Assert-AreNotEqual $stokey1[0].Value $stokey2[0].Value;
#Assert-AreEqual $stokey1[1].Value $stokey2[1].Value;

New-AzStorageAccountKey -ResourceGroupName $rgname -Name $stoname -KeyName key2;

$stokey3 = Get-AzStorageAccountKey -ResourceGroupName $rgname -Name $stoname;
Assert-AreNotEqual $stokey1[0].Value $stokey2[0].Value;
Assert-AreEqual $stokey2[0].Value $stokey3[0].Value;
Assert-AreNotEqual $stokey2[1].Value $stokey3[1].Value;
#Assert-AreNotEqual $stokey1[0].Value $stokey2[0].Value;
#Assert-AreEqual $stokey2[0].Value $stokey3[0].Value;
#Assert-AreNotEqual $stokey2[1].Value $stokey3[1].Value;

Remove-AzStorageAccount -Force -ResourceGroupName $rgname -Name $stoname;
}
Expand Down Expand Up @@ -2277,6 +2277,7 @@ function Test-NewAzStorageContext
Assert-AreEqual $stoname $sto.StorageAccountName;

$stokey = (Get-AzStorageAccountKey -ResourceGroupName $rgname -StorageAccountName $sto.StorageAccountName)[0].Value
$stokey = ConvertFrom-SecureString -SecureString $stokey -AsPlainText
$ctxAccountInfo = New-AzStorageContext -StorageAccountName $sto.StorageAccountName -StorageAccountKey $stokey
Assert-AreEqual $ctxAccountInfo.BlobEndpoint $blobEndpoint
Assert-AreEqual $ctxAccountInfo.TableEndpoint $tableEndpoint
Expand Down
71 changes: 71 additions & 0 deletions src/Storage/Storage.Management/Models/PSStorageAccountKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using StorageModels = Microsoft.Azure.Management.Storage.Models;

namespace Microsoft.Azure.Commands.Management.Storage.Models
{
//Wrapper of StorageAccountKey property KeyPermission
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum PSKeyPermission
{
[System.Runtime.Serialization.EnumMember(Value = "Read")]
Read,
[System.Runtime.Serialization.EnumMember(Value = "Full")]
Full
}

internal static class PSKeyPermissionEnumExtension
{
internal static PSKeyPermission? ParseKeyPermission(this StorageModels.KeyPermission? keyPermission)
{
if (keyPermission == null)
{
return null;
}
switch (keyPermission)
{
case StorageModels.KeyPermission.Read:
return PSKeyPermission.Read;
case StorageModels.KeyPermission.Full:
return PSKeyPermission.Full;
}
return null;
}
}

//Wrapper of StorageAccountKey
public class PSStorageAccountKey
{
public PSStorageAccountKey(StorageModels.StorageAccountKey storageAccountKey)
{
this.KeyName = storageAccountKey.KeyName;
this.Value = storageAccountKey.Value.ToSecureString();
this.Permissions = storageAccountKey.Permissions.ParseKeyPermission();
this.CreationTime = storageAccountKey.CreationTime;
}

[Newtonsoft.Json.JsonProperty(PropertyName = "keyName")]
public string KeyName { get; private set; }

[Newtonsoft.Json.JsonProperty(PropertyName = "value")]
public System.Security.SecureString Value { get; private set; }

[Newtonsoft.Json.JsonProperty(PropertyName = "permissions")]
public PSKeyPermission? Permissions { get; private set; }

[Newtonsoft.Json.JsonProperty(PropertyName = "creationTime")]
public System.DateTime? CreationTime { get; private set; }
}
}
19 changes: 19 additions & 0 deletions src/Storage/Storage.Management/Models/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Microsoft.Azure.Commands.Management.Storage.Models
{
public static class Utils
{
public static System.Security.SecureString ToSecureString(this string input)
{
if (string.IsNullOrEmpty(input))
return null;

System.Security.SecureString secureString = new System.Security.SecureString();
foreach (char c in input)
{
secureString.AppendChar(c);
}
secureString.MakeReadOnly();
return secureString;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Management.Storage.Models;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.Storage.Models;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using System.Collections.Generic;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.Management.Storage
{
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "StorageAccountKey"), OutputType(typeof(StorageAccountKey))]
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "StorageAccountKey"), OutputType(typeof(PSStorageAccountKey))]
public class GetAzureStorageAccountKeyCommand : StorageAccountBaseCmdlet
{
[Parameter(
Expand Down Expand Up @@ -59,7 +62,13 @@ public override void ExecuteCmdlet()
this.Name,
listkeyExpend).Keys;

WriteObject(storageKeys, true);
WriteStorageAccountKeys(storageKeys);
}
private void WriteStorageAccountKeys(IList<StorageAccountKey> storageKeys)
{
List<PSStorageAccountKey> output = new List<PSStorageAccountKey>();
storageKeys.ForEach(key => { output.Add(new PSStorageAccountKey(key)); });
WriteObject(output, true);
}
}
}
21 changes: 8 additions & 13 deletions src/Storage/Storage.Management/help/Get-AzStorageAccountKey.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ This command gets the keys for the specified Azure Storage account.
The credentials, account, tenant, and subscription used for communication with Azure.

```yaml
Type: Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer
Type: IAzureContextContainer
Parameter Sets: (All)
Aliases: AzContext, AzureRmContext, AzureCredential

Expand All @@ -74,12 +74,9 @@ Accept wildcard characters: False

### -ListKerbKey
Lists the Kerberos keys (if active directory enabled) for the specified storage account.
Kerberos key is generated per storage account for Azure Files identity based authentication either with Microsoft Entra Domain Service (Microsoft Entra Domain Services) or Active Directory Domain Service (AD DS).
It is used as the password of the identity registered in the domain service that represents the storage account.
Kerberos key does not provide access permission to perform any control or data plane read or write operations against the storage account.

```yaml
Type: System.Management.Automation.SwitchParameter
Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Expand All @@ -91,10 +88,10 @@ Accept wildcard characters: False
```

### -Name
Specifies the name of the Storage account for which this cmdlet gets keys.
Storage Account Name.

```yaml
Type: System.String
Type: String
Parameter Sets: (All)
Aliases: StorageAccountName, AccountName

Expand All @@ -109,7 +106,7 @@ Accept wildcard characters: False
{{ Fill ProgressAction Description }}

```yaml
Type: System.Management.Automation.ActionPreference
Type: ActionPreference
Parameter Sets: (All)
Aliases: proga

Expand All @@ -121,10 +118,10 @@ Accept wildcard characters: False
```

### -ResourceGroupName
Specifies the name of the resource group that contains the Storage account.
Resource Group Name.

```yaml
Type: System.String
Type: String
Parameter Sets: (All)
Aliases:

Expand All @@ -144,10 +141,8 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable

## OUTPUTS

### Microsoft.Azure.Management.Storage.Models.StorageAccountKey
### Microsoft.Azure.Commands.Management.Storage.Models.PSStorageAccountKey

## NOTES

## RELATED LINKS

[New-AzStorageAccountKey](./New-AzStorageAccountKey.md)
Loading