Skip to content

Commit 41cfb46

Browse files
support versionless in helper
(cherry picked from commit e35f2a44f89d17a5dadc1433e92ef6ad15e42ac3)
1 parent e9a75c4 commit 41cfb46

File tree

6 files changed

+22
-13
lines changed

6 files changed

+22
-13
lines changed

src/Sql/Sql/Common/TdeKeyHelper.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ class TdeKeyHelper
3030
/// <summary>
3131
/// Creates the SQL Server Key Name from an Azure Key Vault KeyId
3232
/// Throws an exception if the provided KeyId is malformed.
33-
/// An example of a well formed Azure Key Vault KeyId is: https://YourVaultName.vault.azure.net/keys/YourKeyName/01234567890123456789012345678901
33+
/// Examples of well formed Azure Key Vault KeyIds are:
34+
/// https://YourVaultName.vault.azure.net/keys/YourKeyName/01234567890123456789012345678901 (versioned)
35+
/// https://YourVaultName.vault.azure.net/keys/YourKeyName (versionless)
3436
/// </summary>
3537
/// <param name="keyId">The full Azure Key Vault KeyId</param>
3638
/// <returns>The Server Key Name for the provided KeyId</returns>
@@ -41,8 +43,8 @@ public static string CreateServerKeyNameFromKeyId(string keyId)
4143
return ServerKeyType.ServiceManaged.ToString();
4244
}
4345

44-
// Validate that the url is a keyvault url and has a key and version
45-
Regex r = new Regex(@"https://(.)+\.(managedhsm.azure.net|managedhsm-preview.azure.net|vault.azure.net|vault-int.azure-int.net|vault.azure.cn|managedhsm.azure.cn|vault.usgovcloudapi.net|managedhsm.usgovcloudapi.net|vault.microsoftazure.de|managedhsm.microsoftazure.de|vault.cloudapi.eaglex.ic.gov|vault.cloudapi.microsoft.scloud)(:443)?\/keys/[^\/]+\/[0-9a-zA-Z]+$", RegexOptions.IgnoreCase);
46+
// Validate that the url is a keyvault url and has a key with an optional version
47+
Regex r = new Regex(@"^https://(.)+\.(managedhsm\.azure\.net|managedhsm-preview\.azure\.net|vault\.azure\.net|vault-int\.azure-int\.net|vault\.azure\.cn|managedhsm\.azure\.cn|vault\.usgovcloudapi\.net|managedhsm\.usgovcloudapi\.net|vault\.microsoftazure\.de|managedhsm\.microsoftazure\.de|vault\.cloudapi\.eaglex\.ic\.gov|vault\.cloudapi\.microsoft\.scloud|mdep\.azure\.net)(:443)?/keys/[^/]+(/([0-9a-zA-Z]+))?/?$", RegexOptions.IgnoreCase);
4648
if (!r.IsMatch(keyId))
4749
{
4850
// Throw an error here, since we don't want to use a non keyvault url
@@ -53,10 +55,17 @@ public static string CreateServerKeyNameFromKeyId(string keyId)
5355
var uri = new Uri(keyId);
5456

5557
string vault = uri.Host.Split('.').First();
56-
string key = uri.Segments[2].TrimEnd('/');
57-
string version = uri.Segments.Last();
58+
string[] pathSegments = uri.AbsolutePath.Trim('/').Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
59+
string key = pathSegments[1];
60+
bool hasVersion = pathSegments.Length >= 3 && !string.IsNullOrEmpty(pathSegments[2]);
5861

59-
return String.Format("{0}_{1}_{2}", vault, key, version);
62+
if (hasVersion)
63+
{
64+
string version = pathSegments[2];
65+
return String.Format("{0}_{1}_{2}", vault, key, version);
66+
}
67+
68+
return String.Format("{0}_{1}", vault, key);
6069
}
6170
}
6271
}

src/Sql/Sql/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/Sql/Sql/Properties/Resources.resx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@
454454
<value>KeyId parameter is required for encryption protector type AzureKeyVault</value>
455455
</data>
456456
<data name="InvalidKeyId" xml:space="preserve">
457-
<value>Invalid parameter format for keyId: '{0}'. It should be a well formed Azure Key Vault KeyId like: https://YourVaultName.vault.azure.net/keys/YourKeyName/01234567890123456789012345678901</value>
457+
<value>Invalid parameter format for keyId: '{0}'. It should be a well formed Azure Key Vault KeyId such as https://YourVaultName.vault.azure.net/keys/YourKeyName/01234567890123456789012345678901 (versioned) or https://YourVaultName.vault.azure.net/keys/YourKeyName (versionless).</value>
458458
</data>
459459
<data name="SetAzureSqlInstanceDescription" xml:space="preserve">
460460
<value>Setting Azure Sql Database Managed Instance '{0}'.</value>
@@ -778,4 +778,4 @@
778778
<data name="InvalidSoftDeleteRetentionDaysRange" xml:space="preserve">
779779
<value>SoftDeleteRetentionDays must be between 1 and 35 when EnableSoftDelete is true.</value>
780780
</data>
781-
</root>
781+
</root>

src/Sql/Sql/help/Add-AzSqlServerKeyVaultKey.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Thumbprint : 1122334455667788990011223344556677889900
3838
CreationDate : 1/1/2017 12:00:00 AM
3939
```
4040

41-
This command adds the Key Vault key with Id 'https://contoso.vault.azure.net/keys/contosokey/01234567890123456789012345678901' to the SQL server named 'ContosoServer' in the resource group 'ContosoResourceGroup'.
41+
This command adds the Key Vault key with Id 'https://contoso.vault.azure.net/keys/contosokey/01234567890123456789012345678901' to the SQL server named 'ContosoServer' in the resource group 'ContosoResourceGroup'. Versionless key IDs, for example 'https://contoso.vault.azure.net/keys/contosokey', are also supported.
4242

4343
## PARAMETERS
4444

src/Sql/Sql/help/Get-AzSqlServerKeyVaultKey.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ $MyServerKeyVaultKey = Get-AzSqlServerKeyVaultKey -KeyId 'https://contoso.vault.
5353
```
5454

5555
This command gets the Key Vault key with Id 'https://contoso.vault.azure.net/keys/contosokey/01234567890123456789012345678901', and then stores it in the $MyServerKeyVaultKey variable.
56-
You can inspect the properties of $MyServerKeyVaultKey to get details about the key vault.
56+
You can inspect the properties of $MyServerKeyVaultKey to get details about the key vault. Versionless key IDs, for example 'https://contoso.vault.azure.net/keys/contosokey', are also supported.
5757

5858
## PARAMETERS
5959

src/Sql/Sql/help/Set-AzSqlServerTransparentDataEncryptionProtector.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ResourceGroupName ServerName Type ServerKeyVaultKeyName
4848
ContosoResourceGroup ContosoServer AzureKeyVault contoso_contosokey_01234567890123456789012345678901
4949
```
5050

51-
This command updates a server to use the Server Key Vault Key with Id 'https://contoso.vault.azure.net/keys/contosokey/01234567890123456789012345678901' as the TDE protector.
51+
This command updates a server to use the Server Key Vault Key with Id 'https://contoso.vault.azure.net/keys/contosokey/01234567890123456789012345678901' as the TDE protector. You can also specify a versionless key, for example 'https://contoso.vault.azure.net/keys/contosokey'.
5252

5353
### Example 3
5454

@@ -123,7 +123,7 @@ Accept wildcard characters: False
123123
```
124124
125125
### -KeyId
126-
The Azure Key Vault KeyId.
126+
The Azure Key Vault KeyId. Supports versioned and versionless key IDs.
127127
128128
```yaml
129129
Type: System.String

0 commit comments

Comments
 (0)