Skip to content

Commit

Permalink
Adding CMK changes for PS cmdlets
Browse files Browse the repository at this point in the history
  • Loading branch information
viparek committed May 26, 2021
1 parent cdb2ed1 commit 9b775f1
Show file tree
Hide file tree
Showing 26 changed files with 219 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public string ServerKeyType
get { return this._serverKeyType; }
set { this._serverKeyType = value; }
}

/// <summary>
/// Initializes a new instance of the
/// EncryptionProtectorCreateOrUpdateProperties class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,19 @@ public string Uri
get { return this._uri; }
set { this._uri = value; }
}


private bool? _isAutoRotationEnabled;

/// <summary>
/// Optional. Gets or sets the Azure Sql Server Encryption
/// Protector Key Rotation Status
/// </summary>
public bool? AutoRotationEnabled
{
get { return this._isAutoRotationEnabled; }
set { this._isAutoRotationEnabled = value; }
}

/// <summary>
/// Initializes a new instance of the EncryptionProtectorProperties
/// class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,12 @@ public void TestServerTransparentDataEncryptionProtectorSet()
{
RunPowerShellTest("Test-SetTransparentDataEncryptionProtector");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestServerTransparentDataEncryptionProtectorSetWithKeyRotation()
{
RunPowerShellTest("Test-SetTransparentDataEncryptionProtectorWithKeyRotation");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,41 @@ function Test-SetTransparentDataEncryptionProtector
Remove-ResourceGroupForTest $rg
}
}

<#
.SYNOPSIS
Tests Setting a server transparent data encryption protector
#>
function Test-SetTransparentDataEncryptionProtectorWithKeyRotation
{
# Setup
$params = Get-SqlServerKeyVaultKeyTestEnvironmentParameters
$rg = Create-ServerKeyVaultKeyTestEnvironment $params
$autoRotationEnabled = $true

try
{
# Encryption Protector should be set to Service Managed initially
$encProtector1 = Get-AzSqlServerTransparentDataEncryptionProtector -ResourceGroupName $params.rgName -ServerName $params.serverName
Assert-AreEqual ServiceManaged $encProtector1.Type
Assert-AreEqual ServiceManaged $encProtector1.ServerKeyVaultKeyName

# Add server key
$keyResult = Add-AzSqlServerKeyVaultKey -ServerName $params.serverName -ResourceGroupName $params.rgName -KeyId $params.keyId
Assert-AreEqual $params.keyId $keyResult.Uri

# Rotate to AKV
$job = Set-AzSqlServerTransparentDataEncryptionProtector -ResourceGroupName $params.rgName -ServerName $params.serverName `
-Type AzureKeyVault -KeyId $params.keyId -AutoRotationEnabled $autoRotationEnabled -Force -AsJob
$job | Wait-Job
$encProtector2 = $job.Output

Assert-AreEqual AzureKeyVault $encProtector2.Type
Assert-AreEqual $params.serverKeyName $encProtector2.ServerKeyVaultKeyName
Assert-AreEqual $autoRotationEnabled $encProtector2.AutoRotationEnabled
}
finally
{
Remove-ResourceGroupForTest $rg
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public class AzureSqlDatabaseLongTermRetentionPolicyModel
/// <summary>
/// Gets or sets the long term retention policy of the database
/// </summary>
public BackupLongTermRetentionPolicy Policy { get; set; }
public LongTermRetentionPolicy Policy { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ internal AzureSqlDatabaseBackupLongTermRetentionPolicyModel GetDatabaseBackupLon
string serverName,
string databaseName)
{
Management.Sql.Models.BackupLongTermRetentionPolicy response = Communicator.GetDatabaseLongTermRetentionPolicy(
Management.Sql.Models.LongTermRetentionPolicy response = Communicator.GetDatabaseLongTermRetentionPolicy(
resourceGroup,
serverName,
databaseName);
Expand Down Expand Up @@ -324,11 +324,11 @@ internal AzureSqlDatabaseBackupLongTermRetentionPolicyModel SetDatabaseBackupLon
string databaseName,
AzureSqlDatabaseBackupLongTermRetentionPolicyModel model)
{
Management.Sql.Models.BackupLongTermRetentionPolicy response = Communicator.SetDatabaseLongTermRetentionPolicy(
Management.Sql.Models.LongTermRetentionPolicy response = Communicator.SetDatabaseLongTermRetentionPolicy(
resourceGroup,
serverName,
databaseName,
new Management.Sql.Models.BackupLongTermRetentionPolicy()
new Management.Sql.Models.LongTermRetentionPolicy()
{
WeeklyRetention = model.WeeklyRetention,
MonthlyRetention = model.MonthlyRetention,
Expand Down Expand Up @@ -524,7 +524,7 @@ internal AzureSqlDatabaseModel RestoreDatabase(string resourceGroup, DateTime re
Capacity = model.Capacity
},
LicenseType = model.LicenseType,
StorageAccountType = MapExternalBackupStorageRedundancyToInternal(model.BackupStorageRedundancy),
RequestedBackupStorageRedundancy = MapExternalBackupStorageRedundancyToInternal(model.BackupStorageRedundancy),
};

if (model.CreateMode == Management.Sql.Models.CreateMode.Recovery)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ public Management.Sql.LegacySdk.Models.DatabaseBackupLongTermRetentionPolicy Set
/// <param name="resourceGroup">The resource group name.</param>
/// <param name="serverName">The server name.</param>
/// <param name="databaseName">The database name.</param>
public Management.Sql.Models.BackupLongTermRetentionPolicy GetDatabaseLongTermRetentionPolicy(
public Management.Sql.Models.LongTermRetentionPolicy GetDatabaseLongTermRetentionPolicy(
string resourceGroup,
string serverName,
string databaseName)
{
return GetCurrentSqlClient().BackupLongTermRetentionPolicies.Get(resourceGroup, serverName, databaseName);
return GetCurrentSqlClient().LongTermRetentionPolicies.Get(resourceGroup, serverName, databaseName);
}

/// <summary>
Expand All @@ -239,13 +239,13 @@ public Management.Sql.Models.BackupLongTermRetentionPolicy GetDatabaseLongTermRe
/// <param name="serverName">The server name.</param>
/// <param name="databaseName">The database name.</param>
/// <param name="policy">The Long Term Retention policy to apply.</param>
public Management.Sql.Models.BackupLongTermRetentionPolicy SetDatabaseLongTermRetentionPolicy(
public Management.Sql.Models.LongTermRetentionPolicy SetDatabaseLongTermRetentionPolicy(
string resourceGroup,
string serverName,
string databaseName,
Management.Sql.Models.BackupLongTermRetentionPolicy policy)
Management.Sql.Models.LongTermRetentionPolicy policy)
{
return GetCurrentSqlClient().BackupLongTermRetentionPolicies.CreateOrUpdate(resourceGroup, serverName, databaseName, policy);
return GetCurrentSqlClient().LongTermRetentionPolicies.CreateOrUpdate(resourceGroup, serverName, databaseName, policy);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Sql/Database/Model/AzureSqlDatabaseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public AzureSqlDatabaseModel(string resourceGroup, string serverName, Management
AutoPauseDelayInMinutes = database.AutoPauseDelay;
MinimumCapacity = database.MinCapacity;
HighAvailabilityReplicaCount = database.HighAvailabilityReplicaCount;
BackupStorageRedundancy = MapInternalBackupStorageRedundancyToExternal(database.StorageAccountType);
BackupStorageRedundancy = MapInternalBackupStorageRedundancyToExternal(database.RequestedBackupStorageRedundancy);
SecondaryType = database.SecondaryType;
MaintenanceConfigurationId = database.MaintenanceConfigurationId;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Sql/Database/Services/AzureSqlDatabaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ internal AzureSqlDatabaseModel UpsertDatabaseWithNewSdk(string resourceGroup, st
AutoPauseDelay = model.Database.AutoPauseDelayInMinutes,
MinCapacity = model.Database.MinimumCapacity,
HighAvailabilityReplicaCount = model.Database.HighAvailabilityReplicaCount,
StorageAccountType = MapExternalBackupStorageRedundancyToInternal(model.Database.BackupStorageRedundancy),
RequestedBackupStorageRedundancy = MapExternalBackupStorageRedundancyToInternal(model.Database.BackupStorageRedundancy),
SecondaryType = model.Database.SecondaryType,
MaintenanceConfigurationId = MaintenanceConfigurationHelper.ConvertMaintenanceConfigurationIdArgument(model.Database.MaintenanceConfigurationId, _subscription.Id),
});
Expand Down
18 changes: 17 additions & 1 deletion src/Sql/Sql/ManagedInstance/Cmdlet/NewAzureSqlManagedInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,20 @@ public class NewAzureSqlManagedInstance : ManagedInstanceCmdletBase
HelpMessage = "The Maintenance configuration id for the Sql Azure Managed Instance.")]
public string MaintenanceConfigurationId { get; set; }

/// <summary>
/// Id of the primary user assigned identity
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "The primary user assigned identity id")]
public string PrimaryUserAssignedIdentityId { get; set; }

/// <summary>
/// URI of the key to use for encryption
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "URI of the key to use for encryption")]
public string KeyId { get; set; }

/// <summary>
/// Gets or sets whether or not to run this cmdlet in the background as a job
/// </summary>
Expand Down Expand Up @@ -483,7 +497,9 @@ public override void ExecuteCmdlet()
InstancePoolName = this.InstancePoolName,
MinimalTlsVersion = this.MinimalTlsVersion,
BackupStorageRedundancy = this.BackupStorageRedundancy,
MaintenanceConfigurationId = this.MaintenanceConfigurationId
MaintenanceConfigurationId = this.MaintenanceConfigurationId,
PrimaryUserAssignedIdentityId = this.PrimaryUserAssignedIdentityId,
KeyId = this.KeyId
});
return newEntity;
}
Expand Down
18 changes: 17 additions & 1 deletion src/Sql/Sql/ManagedInstance/Cmdlet/SetAzureSqlManagedInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@ public class SetAzureSqlManagedInstance : ManagedInstanceCmdletBase
[PSArgumentCompleter("None", "1.0", "1.1", "1.2")]
public string MinimalTlsVersion { get; set; }

/// <summary>
/// Id of the primary user assigned identity
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "The primary user assigned identity id")]
public string PrimaryUserAssignedIdentityId { get; set; }

/// <summary>
/// URI of the key to use for encryption
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "URI of the key to use for encryption")]
public string KeyId { get; set; }

/// <summary>
/// Defines whether it is ok to skip the requesting of rule removal confirmation
/// </summary>
Expand Down Expand Up @@ -290,7 +304,9 @@ protected override IEnumerable<AzureSqlManagedInstanceModel> ApplyUserInputToMod
Identity = model.FirstOrDefault().Identity ?? ResourceIdentityHelper.GetIdentityObjectFromType(this.AssignIdentity.IsPresent),
InstancePoolName = this.InstancePoolName,
MinimalTlsVersion = this.MinimalTlsVersion,
MaintenanceConfigurationId = this.MaintenanceConfigurationId
MaintenanceConfigurationId = this.MaintenanceConfigurationId,
PrimaryUserAssignedIdentityId = this.PrimaryUserAssignedIdentityId,
KeyId = this.KeyId
});
return updateData;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Sql/Sql/ManagedInstance/Model/AzureSqlManagedInstanceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,15 @@ public class AzureSqlManagedInstanceModel
/// Gets or sets the managed instance maintenance configuration id
/// </summary>
public string MaintenanceConfigurationId { get; set; }

/// <summary>
/// Gets or sets the resource id of a user assigned identity to be used
/// </summary>
public string PrimaryUserAssignedIdentityId { get; set; }

/// <summary>
/// Gets or sets a CMK URI of the key to use for encryption.
/// </summary>
public string KeyId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ public AzureSqlManagedInstanceModel UpsertManagedInstance(AzureSqlManagedInstanc
Context.Subscription.Id, model.ResourceGroupName, model.InstancePoolName): null,
MinimalTlsVersion = model.MinimalTlsVersion,
StorageAccountType = MapExternalBackupStorageRedundancyToInternal(model.BackupStorageRedundancy),
MaintenanceConfigurationId = model.MaintenanceConfigurationId
MaintenanceConfigurationId = model.MaintenanceConfigurationId,
PrimaryUserAssignedIdentityId = model.PrimaryUserAssignedIdentityId,
KeyId = model.KeyId
});

return CreateManagedInstanceModelFromResponse(resp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ internal AzureSqlDatabaseCopyModel CopyDatabaseWithNewSdk(string copyResourceGro
Capacity = model.Capacity
},
LicenseType = model.LicenseType,
StorageAccountType = MapExternalBackupStorageRedundancyToInternal(model.BackupStorageRedundancy)
RequestedBackupStorageRedundancy = MapExternalBackupStorageRedundancyToInternal(model.BackupStorageRedundancy)
});

return CreateDatabaseCopyModelFromResponse(model.CopyResourceGroupName, model.CopyServerName, model.ResourceGroupName,
Expand Down Expand Up @@ -212,7 +212,7 @@ private AzureSqlDatabaseCopyModel CreateDatabaseCopyModelFromResponse(string cop
model.CopyLocation = database.Location;
model.CreationDate = database.CreationDate.Value;
model.LicenseType = database.LicenseType;
model.BackupStorageRedundancy = MapInternalBackupStorageRedundancyToExternal(database.StorageAccountType);
model.BackupStorageRedundancy = MapInternalBackupStorageRedundancyToExternal(database.RequestedBackupStorageRedundancy);

return model;
}
Expand Down Expand Up @@ -273,7 +273,7 @@ internal AzureReplicationLinkModel CreateLinkWithNewSdk(string resourceGroupName
Capacity = model.Capacity
},
LicenseType = model.LicenseType,
StorageAccountType = MapExternalBackupStorageRedundancyToInternal(model.BackupStorageRedundancy),
RequestedBackupStorageRedundancy = MapExternalBackupStorageRedundancyToInternal(model.BackupStorageRedundancy),
SecondaryType = model.SecondaryType,
});

Expand Down
16 changes: 16 additions & 0 deletions src/Sql/Sql/Server/Cmdlet/NewAzureSqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ public class NewAzureSqlServer : AzureSqlServerCmdletBase
[PSArgumentCompleter("1.0", "1.1", "1.2")]
public string MinimalTlsVersion { get; set; }

/// <summary>
/// Id of the primary user assigned identity
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "The primary user assigned identity id")]
public string PrimaryUserAssignedIdentityId { get; set; }

/// <summary>
/// URI of the key to use for encryption
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "URI of the key to use for encryption")]
public string KeyId { get; set; }

/// <summary>
/// Gets or sets whether or not to run this cmdlet in the background as a job
/// </summary>
Expand Down Expand Up @@ -156,6 +170,8 @@ public override void ExecuteCmdlet()
Identity = ResourceIdentityHelper.GetIdentityObjectFromType(this.AssignIdentity.IsPresent),
MinimalTlsVersion = this.MinimalTlsVersion,
PublicNetworkAccess = this.PublicNetworkAccess,
PrimaryUserAssignedIdentityId = this.PrimaryUserAssignedIdentityId,
KeyId = this.KeyId
});
return newEntity;
}
Expand Down
16 changes: 16 additions & 0 deletions src/Sql/Sql/Server/Cmdlet/SetAzureSqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ public class SetAzureSqlServer : AzureSqlServerCmdletBase
[PSArgumentCompleter("1.0", "1.1", "1.2")]
public string MinimalTlsVersion { get; set; }

/// <summary>
/// Id of the primary user assigned identity
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "The primary user assigned identity id")]
public string PrimaryUserAssignedIdentityId { get; set; }

/// <summary>
/// URI of the key to use for encryption
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "URI of the key to use for encryption")]
public string KeyId { get; set; }

/// <summary>
/// Defines whether it is ok to skip the requesting of rule removal confirmation
/// </summary>
Expand Down Expand Up @@ -123,6 +137,8 @@ public class SetAzureSqlServer : AzureSqlServerCmdletBase
Identity = model.FirstOrDefault().Identity ?? ResourceIdentityHelper.GetIdentityObjectFromType(this.AssignIdentity.IsPresent),
PublicNetworkAccess = this.PublicNetworkAccess,
MinimalTlsVersion = this.MinimalTlsVersion,
PrimaryUserAssignedIdentityId = this.PrimaryUserAssignedIdentityId,
KeyId = this.KeyId
});
return updateData;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Sql/Sql/Server/Model/AzureSqlServerModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,15 @@ public class AzureSqlServerModel
/// Gets or sets the flag to control enable/disable public network access
/// </summary>
public string PublicNetworkAccess { get; set; }

/// <summary>
/// Gets or sets the resource id of a user assigned identity to be used
/// </summary>
public string PrimaryUserAssignedIdentityId { get; set; }

/// <summary>
/// Gets or sets a CMK URI of the key to use for encryption.
/// </summary>
public string KeyId { get; set; }
}
}
6 changes: 5 additions & 1 deletion src/Sql/Sql/Server/Services/AzureSqlServerAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public AzureSqlServerModel UpsertServer(AzureSqlServerModel model)
Version = model.ServerVersion,
Identity = model.Identity,
MinimalTlsVersion = model.MinimalTlsVersion,
PublicNetworkAccess = model.PublicNetworkAccess
PublicNetworkAccess = model.PublicNetworkAccess,
PrimaryUserAssignedIdentityId = model.PrimaryUserAssignedIdentityId,
KeyId = model.KeyId
});

return CreateServerModelFromResponse(resp);
Expand Down Expand Up @@ -150,6 +152,8 @@ private static AzureSqlServerModel CreateServerModelFromResponse(Management.Sql.
server.ResourceId = resp.Id;
server.MinimalTlsVersion = resp.MinimalTlsVersion;
server.PublicNetworkAccess = resp.PublicNetworkAccess;
server.PrimaryUserAssignedIdentityId = resp.PrimaryUserAssignedIdentityId;
server.KeyId = resp.KeyId;

return server;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Sql/Sql/Sql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.48.0-preview" />
<PackageReference Include="System.Security.Permissions" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<Reference Include="D:\repos\azure-sdk-for-net\artifacts\bin\Microsoft.Azure.Management.Sql\Debug\net461\Microsoft.Azure.Management.Sql.dll" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Sql.LegacySdk\Sql.LegacySdk.csproj" />
Expand Down
Loading

0 comments on commit 9b775f1

Please sign in to comment.