Skip to content

Commit c1f3188

Browse files
[Az.Batch] Adds encryption configuration (#20687)
* Add BatchAccount encryption Update help Updated changelog * Added unit test * whitespace * Add BatchAccount encryption Update help Updated changelog * Added unit test * whitespace * Flattened encryption * Fixed identity type, unit test, and context Fixed unit test and context Updated help * typo --------- Co-authored-by: Yabo Hu <[email protected]>
1 parent 05cc743 commit c1f3188

File tree

8 files changed

+120
-4
lines changed

8 files changed

+120
-4
lines changed

src/Batch/Batch.Test/BatchAccounts/NewBatchAccountCommandTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,44 @@ public void CanCreateUserSubscriptionBatchAccount()
144144
Assert.Equal(keyVaultId, actualCreateParameters.KeyVaultId);
145145
Assert.Equal(keyVaultUrl, actualCreateParameters.KeyVaultUrl);
146146
}
147+
148+
[Fact]
149+
[Trait(Category.AcceptanceType, Category.CheckIn)]
150+
public void NewBatchWithCustomerEncryptionKeyAccountTest()
151+
{
152+
string accountName = "account01";
153+
string resourceGroup = "resourceGroup";
154+
string location = "location";
155+
string identityId = "subscriptions/0000/resourceGroups/resourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/encryptionIdentity";
156+
string encryptionKeyVaultId = "subscriptions/0000/resourceGroups/resourceGroup/providers/Microsoft.KeyVault/vaults/encryptionVault";
157+
AccountCreateParameters actualCreateParameters = null;
158+
159+
// Setup the mock client to return a fake response and capture the account create parameters
160+
BatchAccount accountResource = BatchTestHelpers.CreateAccountResource(accountName, resourceGroup, location);
161+
BatchAccountContext fakeResponse = BatchAccountContext.ConvertAccountResourceToNewAccountContext(accountResource, null);
162+
163+
batchClientMock.Setup(b => b.CreateAccount(It.IsAny<AccountCreateParameters>()))
164+
.Returns(fakeResponse)
165+
.Callback((AccountCreateParameters p) => actualCreateParameters = p);
166+
167+
// Setup and run the cmdlet
168+
cmdlet.AccountName = accountName;
169+
cmdlet.ResourceGroupName = resourceGroup;
170+
cmdlet.Location = location;
171+
cmdlet.IdentityType = ResourceIdentityType.UserAssigned;
172+
cmdlet.IdentityId = new string[] { identityId };
173+
cmdlet.EncryptionKeySource = KeySource.MicrosoftKeyVault;
174+
cmdlet.EncryptionKeyIdentifier = encryptionKeyVaultId;
175+
cmdlet.ExecuteCmdlet();
176+
177+
// Verify the fake response was written to the pipeline and that the captured account create
178+
// parameters matched expectations.
179+
commandRuntimeMock.Verify(r => r.WriteObject(fakeResponse), Times.Once());
180+
Assert.Equal(accountName, actualCreateParameters.BatchAccount);
181+
Assert.Equal(resourceGroup, actualCreateParameters.ResourceGroup);
182+
Assert.Equal(location, actualCreateParameters.Location);
183+
Assert.Equal(KeySource.MicrosoftKeyVault, actualCreateParameters.Encryption.KeySource);
184+
Assert.Equal(encryptionKeyVaultId, actualCreateParameters.Encryption.KeyVaultProperties.KeyIdentifier);
185+
}
147186
}
148187
}

src/Batch/Batch/BatchAccounts/NewBatchAccountCommand.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ public class NewBatchAccountCommand : BatchCmdletBase
6969
[Parameter(Mandatory = false, HelpMessage = "An array containing user assigned identities associated with the BatchAccount. This parameter is only used when IdentityType is set to UserAssigned.")]
7070
public string[] IdentityId { get; set; }
7171

72+
[Parameter(Mandatory = false, HelpMessage = "Configures how customer data is encrypted inside the Batch account.\r\nBy default, accounts are encrypted using a Microsoft managed key.\r\nFor additional control, a customer-managed key can be used instead.")]
73+
public KeySource EncryptionKeySource { get; set; }
74+
75+
[Parameter(Mandatory = false, HelpMessage = "The Key Identifier for customer-based encryption.")]
76+
public string EncryptionKeyIdentifier { get; set; }
77+
7278
protected override void ExecuteCmdletImpl()
7379
{
7480
Dictionary<string, UserAssignedIdentities> identityDictionary = null;
@@ -82,6 +88,22 @@ protected override void ExecuteCmdletImpl()
8288
identityDictionary = IdentityId.ToDictionary(i => i, i => new UserAssignedIdentities());
8389
}
8490

91+
EncryptionProperties encryption = null;
92+
if (EncryptionKeySource == KeySource.MicrosoftKeyVault)
93+
{
94+
if (IdentityType != ResourceIdentityType.UserAssigned)
95+
{
96+
throw new PSArgumentException("If EncryptionKeySource is set to 'MicrosoftKeyVault', the Batch Account identity must be set to `UserAssigned`.");
97+
}
98+
99+
if (EncryptionKeyIdentifier == null)
100+
{
101+
throw new PSArgumentNullException("EncryptionKeyIdentifier", "If EncryptionKeySource is set to 'MicrosoftKeyVault', a valid Key Identifier must also be supplied as parameter 'EncryptionKeyIdentifier'.");
102+
}
103+
104+
encryption = new EncryptionProperties(EncryptionKeySource, new KeyVaultProperties(EncryptionKeyIdentifier));
105+
}
106+
85107
AccountCreateParameters parameters = new AccountCreateParameters(this.ResourceGroupName, this.AccountName, this.Location)
86108
{
87109
AutoStorageAccountId = this.AutoStorageAccountId,
@@ -90,7 +112,8 @@ protected override void ExecuteCmdletImpl()
90112
KeyVaultUrl = this.KeyVaultUrl,
91113
Tags = this.Tag,
92114
PublicNetworkAccess = this.PublicNetworkAccess,
93-
Identity = new BatchAccountIdentity(IdentityType, null, null, identityDictionary)
115+
Identity = new BatchAccountIdentity(IdentityType, null, null, identityDictionary),
116+
Encryption = encryption
94117
};
95118

96119
BatchAccountContext context = BatchClient.CreateAccount(parameters);

src/Batch/Batch/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Added new property `Encryption` of type `EncryptionProperties` to `AccountCreateParameters`.
22+
- Configures how customer data is encrypted inside the Batch account.
2123

2224
## Version 3.3.0
2325
* Added new properties `CurrentNodeCommunicationMode` (read only) and `TargetCommunicationMode` of type `NodeCommunicationMode` to `PSCloudPool`.

src/Batch/Batch/Models/AccountCreateParameters.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,10 @@ public AccountCreateParameters(string resourceGroup, string batchAccount, string
9191
/// The identity of the Batch account.
9292
/// </summary>
9393
public BatchAccountIdentity Identity { get; set; }
94+
95+
/// <summary>
96+
/// Gets the encryption configuration for the Batch account.
97+
/// </summary>
98+
public EncryptionProperties Encryption { get; set; }
9499
}
95100
}

src/Batch/Batch/Models/BatchAccountContext.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,16 @@ public AccountKeyType KeyInUse
179179
}
180180
}
181181

182+
/// <summary>
183+
/// Gets the encryption configuration for the Batch account.
184+
/// </summary>
185+
/// <remarks>
186+
/// Configures how customer data is encrypted inside the Batch account.
187+
/// By default, accounts are encrypted using a Microsoft managed key.
188+
/// For additional control, a customer-managed key can be used instead.
189+
/// </remarks>
190+
public EncryptionProperties Encryption { get; private set; }
191+
182192
internal bool HasKeys
183193
{
184194
get { return !string.IsNullOrEmpty(PrimaryAccountKey) || !string.IsNullOrEmpty(SecondaryAccountKey); }
@@ -242,6 +252,7 @@ internal void ConvertAccountResourceToAccountContext(BatchAccount resource)
242252
this.PoolAllocationMode = resource.PoolAllocationMode;
243253
this.PublicNetworkAccess = resource.PublicNetworkAccess;
244254
this.Identity = resource.Identity;
255+
this.Encryption = resource.Encryption;
245256

246257
if (resource.AutoStorage != null)
247258
{

src/Batch/Batch/Models/BatchClient.BatchAccounts.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public virtual BatchAccountContext CreateAccount(AccountCreateParameters paramet
6464
PoolAllocationMode = parameters.PoolAllocationMode,
6565
KeyVaultReference = keyVaultRef,
6666
PublicNetworkAccess = parameters.PublicNetworkAccess,
67-
Identity = parameters.Identity
67+
Identity = parameters.Identity,
68+
Encryption = parameters.Encryption
6869
});
6970

7071
var context = BatchAccountContext.ConvertAccountResourceToNewAccountContext(response, this.azureContext);

src/Batch/Batch/help/Get-AzBatchAccountKey.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ This command gets the account details and stores it in a `$Context` object for u
3232

3333
### Example 2: Get batch account keys and display them
3434
<!-- Skip: Output cannot be splitted from code -->
35+
36+
3537
```powershell
3638
$Context = Get-AzBatchAccountKey -AccountName myaccount
3739
$Context.PrimaryAccountKey

src/Batch/Batch/help/New-AzBatchAccount.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ Creates a Batch account.
1717
New-AzBatchAccount [-AccountName] <String> [-Location] <String> [-ResourceGroupName] <String>
1818
[[-AutoStorageAccountId] <String>] [-PoolAllocationMode <PoolAllocationMode>] [-KeyVaultId <String>]
1919
[-KeyVaultUrl <String>] [-Tag <Hashtable>] [-PublicNetworkAccess <PublicNetworkAccessType>]
20-
[-IdentityType <ResourceIdentityType>] [-IdentityId <String[]>] [-DefaultProfile <IAzureContextContainer>]
21-
[<CommonParameters>]
20+
[-IdentityType <ResourceIdentityType>] [-IdentityId <String[]>] [-EncryptionKeySource <KeySource>]
21+
[-EncryptionKeyIdentifier <String>] [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
2222
```
2323

2424
## DESCRIPTION
@@ -104,6 +104,39 @@ Accept pipeline input: False
104104
Accept wildcard characters: False
105105
```
106106
107+
### -EncryptionKeyIdentifier
108+
The Key Identifier for customer-based encryption.
109+
110+
```yaml
111+
Type: System.String
112+
Parameter Sets: (All)
113+
Aliases:
114+
115+
Required: False
116+
Position: Named
117+
Default value: None
118+
Accept pipeline input: False
119+
Accept wildcard characters: False
120+
```
121+
122+
### -EncryptionKeySource
123+
Configures how customer data is encrypted inside the Batch account.
124+
By default, accounts are encrypted using a Microsoft managed key.
125+
For additional control, a customer-managed key can be used instead.
126+
127+
```yaml
128+
Type: Microsoft.Azure.Management.Batch.Models.KeySource
129+
Parameter Sets: (All)
130+
Aliases:
131+
Accepted values: MicrosoftBatch, MicrosoftKeyVault
132+
133+
Required: False
134+
Position: Named
135+
Default value: None
136+
Accept pipeline input: False
137+
Accept wildcard characters: False
138+
```
139+
107140
### -IdentityId
108141
The list of user assigned identities associated with the BatchAccount. This parameter is only used when IdentityType is set to UserAssigned.
109142

0 commit comments

Comments
 (0)