Skip to content

Commit 2153f70

Browse files
committed
Merge pull request #158 from divyakgupta/ignite
Key Vault cmdlet bug fixes
2 parents f2e268f + 2a22073 commit 2153f70

File tree

10 files changed

+543
-146
lines changed

10 files changed

+543
-146
lines changed

src/ResourceManager/KeyVault/Commands.KeyVault.Test/ScenarioTests/KeyVaultManagementTests.ps1

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,14 @@ function Test-ModifyAccessPolicy
289289
{
290290
Param($existingVaultName, $rgName, $upn)
291291

292-
# Add perms to start off
292+
# Adding nothing should not change the vault
293+
$PermToKeys = @()
294+
$PermToSecrets = @()
295+
$vault = Set-AzureKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -UserPrincipalName $upn -PermissionsToKeys $PermToKeys -PassThru
296+
Assert-NotNull $vault
297+
Assert-AreEqual 0 $vault.AccessPolicies.Count
298+
299+
# Add some perms now
293300
$PermToKeys = @("encrypt", "decrypt", "unwrapKey", "wrapKey", "verify", "sign", "get", "list", "update", "create", "import", "delete", "backup", "restore")
294301
$PermToSecrets = @("get", "list", "set", "delete")
295302
$vault = Set-AzureKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -UPN $upn -PermissionsToKeys $PermToKeys -PermissionsToSecrets $PermToSecrets -PassThru
@@ -311,10 +318,16 @@ function Test-ModifyAccessPolicy
311318
$vault = Set-AzureKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -ObjectId $objId -PermissionsToSecrets $PermToSecrets -PassThru
312319
CheckVaultAccessPolicy $vault $PermToKeys $PermToSecrets
313320

314-
# Remove just the secrets perms
321+
# Remove just the keys perms
315322
$PermToKeys = @()
316323
$vault = Set-AzureKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -UserPrincipalName $upn -PermissionsToKeys $PermToKeys -PassThru
317-
CheckVaultAccessPolicy $vault $PermToKeys $PermToSecrets
324+
CheckVaultAccessPolicy $vault $PermToKeys $PermToSecrets
325+
326+
# Remove secret perms too
327+
$PermToSecrets = @()
328+
$vault = Set-AzureKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -UserPrincipalName $upn -PermissionsToKeys $PermToKeys -PermissionsToSecrets $PermToSecrets -PassThru
329+
Assert-NotNull $vault
330+
Assert-AreEqual 0 $vault.AccessPolicies.Count
318331
}
319332

320333
function Test-SetAccessPolicyNegativeCases
@@ -327,9 +340,6 @@ function Test-SetAccessPolicyNegativeCases
327340

328341
# random string in perms
329342
Assert-Throws { Set-AzureKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -UserPrincipalName $upn -PermissionsToSecrets blah, get }
330-
331-
# empty perms
332-
Assert-Throws { Set-AzureKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -UserPrincipalName $upn -PermissionsToSecrets @() -PermissionsToKeys @() }
333343
}
334344

335345
function Test-RemoveNonExistentAccessPolicyDoesNotThrow

src/ResourceManager/KeyVault/Commands.KeyVault.Test/Scripts/VaultKeyTests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ function Test_GetKeyVersions
612612
} while ($i -le $run)
613613

614614
$keys=Get-AzureKeyVaultKey -VaultName $keyVault -Name $keyname -IncludeVersions
615-
Assert-True { $keys.Count -ge $total*$run+1 }
615+
Assert-True { $keys.Count -ge $total*$run }
616616
}
617617

618618
<#

src/ResourceManager/KeyVault/Commands.KeyVault.Test/Scripts/VaultSecretTests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ function Test_GetSecretVersions
479479
BulkCreateSecretVersions $keyVault $secretname $total
480480

481481
$secs=Get-AzureKeyVaultSecret -VaultName $keyVault -Name $secretname -IncludeVersions
482-
Assert-True { $secs.Count -ge $total+1 }
482+
Assert-True { $secs.Count -ge $total }
483483
}
484484

485485
<#

src/ResourceManager/KeyVault/Commands.KeyVault.Test/SessionRecords/Microsoft.Azure.Commands.KeyVault.Test.ScenarioTests.KeyVaultManagementTests/TestModifyAccessPolicy.json

Lines changed: 500 additions & 114 deletions
Large diffs are not rendered by default.

src/ResourceManager/KeyVault/Commands.KeyVault/Commands/GetAzureKeyVaultKey.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public override void ExecuteCmdlet()
108108
keyBundle = DataServiceClient.GetKey(VaultName, Name, null);
109109
if (keyBundle != null)
110110
WriteObject(new KeyIdentityItem(keyBundle));
111-
GetAndWriteKeyVersions(VaultName, Name);
111+
GetAndWriteKeyVersions(VaultName, Name, keyBundle.Version);
112112
break;
113113
case ByVaultNameParameterSet:
114114
GetAndWriteKeys(VaultName);
@@ -134,7 +134,7 @@ private void GetAndWriteKeys(string vaultName)
134134
} while (!string.IsNullOrEmpty(options.NextLink));
135135
}
136136

137-
private void GetAndWriteKeyVersions(string vaultName, string name)
137+
private void GetAndWriteKeyVersions(string vaultName, string name, string currentKeyVersion)
138138
{
139139
KeyVaultObjectFilterOptions options = new KeyVaultObjectFilterOptions
140140
{
@@ -145,7 +145,7 @@ private void GetAndWriteKeyVersions(string vaultName, string name)
145145

146146
do
147147
{
148-
var pageResults = DataServiceClient.GetKeyVersions(options);
148+
var pageResults = DataServiceClient.GetKeyVersions(options).Where(k => k.Version != currentKeyVersion);
149149
WriteObject(pageResults, true);
150150
} while (!string.IsNullOrEmpty(options.NextLink));
151151
}

src/ResourceManager/KeyVault/Commands.KeyVault/Commands/GetAzureKeyVaultSecret.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public override void ExecuteCmdlet()
106106
secret = DataServiceClient.GetSecret(VaultName, Name, null);
107107
if (secret != null)
108108
WriteObject(new SecretIdentityItem(secret));
109-
GetAndWriteSecretVersions(VaultName, Name);
109+
GetAndWriteSecretVersions(VaultName, Name, secret.Version);
110110
break;
111111
case ByVaultNameParameterSet:
112112
GetAndWriteSecrets(VaultName);
@@ -130,7 +130,7 @@ private void GetAndWriteSecrets(string vaultName)
130130
} while (!string.IsNullOrEmpty(options.NextLink));
131131
}
132132

133-
private void GetAndWriteSecretVersions(string vaultName, string name)
133+
private void GetAndWriteSecretVersions(string vaultName, string name, string currentSecretVersion)
134134
{
135135
KeyVaultObjectFilterOptions options = new KeyVaultObjectFilterOptions
136136
{
@@ -141,7 +141,8 @@ private void GetAndWriteSecretVersions(string vaultName, string name)
141141

142142
do
143143
{
144-
WriteObject(DataServiceClient.GetSecretVersions(options), true);
144+
var secrets = DataServiceClient.GetSecretVersions(options).Where(s => s.Version != currentSecretVersion);
145+
WriteObject(secrets, true);
145146
} while (!string.IsNullOrEmpty(options.NextLink));
146147
}
147148
}

src/ResourceManager/KeyVault/Commands.KeyVault/Commands/SetAzureKeyVaultAccessPolicy.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,7 @@ public override void ExecuteCmdlet()
170170

171171
//Both arrays cannot be null
172172
if (PermissionsToKeys == null && PermissionsToSecrets == null)
173-
throw new ArgumentException(PSKeyVaultProperties.Resources.PermissionsNotSpecified);
174-
//Both arrays cannot be empty
175-
else if ((PermissionsToSecrets != null && PermissionsToSecrets.Length == 0) && (PermissionsToKeys != null && PermissionsToKeys.Length == 0))
176-
throw new ArgumentException(PSKeyVaultProperties.Resources.PermissionsNotSpecified);
173+
throw new ArgumentException(PSKeyVaultProperties.Resources.PermissionsNotSpecified);
177174
else
178175
{
179176
//Validate
@@ -193,12 +190,15 @@ public override void ExecuteCmdlet()
193190

194191
var secrets = PermissionsToSecrets != null ? PermissionsToSecrets :
195192
(existingPolicy != null && existingPolicy.PermissionsToSecrets != null ?
196-
existingPolicy.PermissionsToSecrets.ToArray() : null);
197-
198-
var policy = new PSKeyVaultModels.PSVaultAccessPolicy(vault.TenantId, objId, keys, secrets);
199-
200-
//Remove old policies for this object ID and add a new one with the right permission arrays
201-
updatedListOfAccessPolicies = vault.AccessPolicies.Where(ap => ap.ObjectId != objId).Concat(new[] { policy }).ToArray();
193+
existingPolicy.PermissionsToSecrets.ToArray() : null);
194+
195+
//Remove old policies for this object ID and add a new one with the right permissions, iff there were some non-empty permissions
196+
updatedListOfAccessPolicies = vault.AccessPolicies.Where(ap => ap.ObjectId != objId).ToArray();
197+
if ((keys != null && keys.Length > 0) || (secrets != null && secrets.Length > 0))
198+
{
199+
var policy = new PSKeyVaultModels.PSVaultAccessPolicy(vault.TenantId, objId, keys, secrets);
200+
updatedListOfAccessPolicies = updatedListOfAccessPolicies.Concat(new[] { policy }).ToArray();
201+
}
202202

203203
}
204204
}

src/ResourceManager/KeyVault/Commands.KeyVault/Microsoft.Azure.Commands.KeyVault.format.ps1xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@
114114
</ListControl>
115115
</View>
116116
<View>
117-
<Name>Microsoft.Azure.Commands.KeyVault.Models.VaultIdentityItem</Name>
117+
<Name>Microsoft.Azure.Commands.KeyVault.Models.PSVaultIdentityItem</Name>
118118
<ViewSelectedBy>
119-
<TypeName>Microsoft.Azure.Commands.KeyVault.Models.VaultIdentityItem</TypeName>
119+
<TypeName>Microsoft.Azure.Commands.KeyVault.Models.PSVaultIdentityItem</TypeName>
120120
</ViewSelectedBy>
121121
<ListControl>
122122
<ListEntries>
@@ -148,9 +148,9 @@
148148
</ListControl>
149149
</View>
150150
<View>
151-
<Name>Microsoft.Azure.Commands.KeyVault.Models.Vault</Name>
151+
<Name>Microsoft.Azure.Commands.KeyVault.Models.PSVault</Name>
152152
<ViewSelectedBy>
153-
<TypeName>Microsoft.Azure.Commands.KeyVault.Models.Vault</TypeName>
153+
<TypeName>Microsoft.Azure.Commands.KeyVault.Models.PSVault</TypeName>
154154
</ViewSelectedBy>
155155
<ListControl>
156156
<ListEntries>
@@ -202,9 +202,9 @@
202202
</ListControl>
203203
</View>
204204
<View>
205-
<Name>Microsoft.Azure.Commands.KeyVault.Models.VaultAccessPolicy</Name>
205+
<Name>Microsoft.Azure.Commands.KeyVault.Models.PSVaultAccessPolicy</Name>
206206
<ViewSelectedBy>
207-
<TypeName>Microsoft.Azure.Commands.KeyVault.Models.VaultAccessPolicy</TypeName>
207+
<TypeName>Microsoft.Azure.Commands.KeyVault.Models.PSVaultAccessPolicy</TypeName>
208208
</ViewSelectedBy>
209209
<ListControl>
210210
<ListEntries>

src/ResourceManager/KeyVault/Commands.KeyVault/Properties/Resources.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/KeyVault/Commands.KeyVault/Properties/Resources.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120120
<data name="ADObjectNotFound" xml:space="preserve">
121-
<value>Cannot find the Active Directory object '{0}' in tenant '{1}'</value>
121+
<value>Cannot find the Active Directory object '{0}' in tenant '{1}'. Please make sure that the user or application service principal you are authorizing is registered in the current subscription's Azure Active directory. The TenantID displayed by the cmdlet 'get-AzureSubscription -current' is the current subscription's Azure Active directory.</value>
122122
</data>
123123
<data name="BackupKeyFileNotFound" xml:space="preserve">
124124
<value>Cannot find backup key file '{0}'</value>

0 commit comments

Comments
 (0)