Skip to content

Commit 0740a8c

Browse files
Support inhering TD policy from server cmdlet
1 parent 2d75872 commit 0740a8c

File tree

9 files changed

+77
-8
lines changed

9 files changed

+77
-8
lines changed

src/ResourceManager/Sql/Commands.Sql/Auditing/Model/DatabaseAuditingPolicyModel.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Commands.Sql.Common;
16+
1517
namespace Microsoft.Azure.Commands.Sql.Auditing.Model
1618
{
17-
/// <summary>
18-
/// The possible states in which the user server's policy property may be in
19-
/// </summary>
20-
public enum UseServerDefaultOptions { Enabled, Disabled }
21-
2219
/// <summary>
2320
/// A class representing a database auditing policy
2421
/// </summary>
@@ -32,6 +29,6 @@ public class DatabaseAuditingPolicyModel : BaseTableAuditingPolicyModel
3229
/// <summary>
3330
/// Gets or sets the use server default property
3431
/// </summary>
35-
public UseServerDefaultOptions UseServerDefault { get; set; }
32+
public SecurityConstants.UseServerDefaultOptions UseServerDefault { get; set; }
3633
}
3734
}

src/ResourceManager/Sql/Commands.Sql/Auditing/Services/SqlAuditAdapter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private DatabaseAuditingPolicyModel ModelizeDatabaseAuditPolicy(DatabaseAuditing
180180
var dbPolicyModel = new DatabaseAuditingPolicyModel();
181181
var properties = policy.Properties;
182182
dbPolicyModel.AuditState = ModelizeAuditState(properties.AuditingState);
183-
dbPolicyModel.UseServerDefault = properties.UseServerDefault == SecurityConstants.AuditingEndpoint.Enabled ? UseServerDefaultOptions.Enabled : UseServerDefaultOptions.Disabled;
183+
dbPolicyModel.UseServerDefault = properties.UseServerDefault == SecurityConstants.AuditingEndpoint.Enabled ? SecurityConstants.UseServerDefaultOptions.Enabled : SecurityConstants.UseServerDefaultOptions.Disabled;
184184
ModelizeStorageInfo(dbPolicyModel, properties.StorageAccountName, properties.StorageAccountKey, properties.StorageAccountSecondaryKey);
185185
ModelizeEventTypesInfo(dbPolicyModel, properties.EventTypesToAudit);
186186
ModelizeRetentionInfo(dbPolicyModel, properties.RetentionDays, properties.AuditLogsTableName, properties.FullAuditLogsTableName);
@@ -406,7 +406,7 @@ private DatabaseAuditingPolicyCreateOrUpdateParameters PolicizeDatabaseAuditingM
406406
var properties = new DatabaseAuditingPolicyProperties();
407407
updateParameters.Properties = properties;
408408
properties.AuditingState = model.AuditState.ToString();
409-
properties.UseServerDefault = (model.UseServerDefault == UseServerDefaultOptions.Enabled) ? SecurityConstants.AuditingEndpoint.Enabled : SecurityConstants.AuditingEndpoint.Disabled;
409+
properties.UseServerDefault = (model.UseServerDefault == SecurityConstants.UseServerDefaultOptions.Enabled) ? SecurityConstants.AuditingEndpoint.Enabled : SecurityConstants.AuditingEndpoint.Disabled;
410410
properties.StorageAccountName = ExtractStorageAccountName(model);
411411
properties.StorageAccountResourceGroupName = ExtractStorageAccountResourceGroup(properties.StorageAccountName);
412412
properties.StorageAccountSubscriptionId = ExtractStorageAccountSubscriptionId(properties.StorageAccountName);

src/ResourceManager/Sql/Commands.Sql/Commands.Sql.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
<Compile Include="Auditing\Cmdlet\SqlDatabaseAuditingCmdletBase.cs" />
174174
<Compile Include="Auditing\Cmdlet\SqlDatabaseServerAuditingCmdletBase.cs" />
175175
<Compile Include="Auditing\Cmdlet\UseAzureSqlServerAuditingPolicy.cs" />
176+
<Compile Include="ThreatDetection\Cmdlet\UseAzureSqlServerThreatDetection.cs" />
176177
<Compile Include="ThreatDetection\Model\BaseThreatDetectionPolicyModel.cs" />
177178
<Compile Include="ThreatDetection\Model\ServerThreatDetectionPolicyModel.cs" />
178179
<Compile Include="ThreatDetection\Model\DatabaseThreatDetectionPolicyModel .cs" />

src/ResourceManager/Sql/Commands.Sql/Common/SecurityConstants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ namespace Microsoft.Azure.Commands.Sql.Common
2323
/// </summary>
2424
public class SecurityConstants
2525
{
26+
/// <summary>
27+
/// The possible states in which the user server's policy property may be in
28+
/// </summary>
29+
public enum UseServerDefaultOptions { Enabled, Disabled }
30+
2631
// Audit Events:
2732
public const string PlainSQL_Success = "PlainSQL_Success";
2833
public const string PlainSQL_Failure = "PlainSQL_Failure";

src/ResourceManager/Sql/Commands.Sql/ThreatDetection/Cmdlet/RemoveSqlDatabaseThreatDetection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ protected override DatabaseThreatDetectionPolicyModel ApplyUserInputToModel(
4646
{
4747
model = base.ApplyUserInputToModel(model);
4848
model.ThreatDetectionState = ThreatDetectionStateType.Disabled;
49+
model.UseServerDefault = UseServerDefaultOptions.Disabled;
4950
return model;
5051
}
5152
}

src/ResourceManager/Sql/Commands.Sql/ThreatDetection/Cmdlet/SetAzureSqlDatabaseThreatDetection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ protected override DatabaseThreatDetectionPolicyModel ApplyUserInputToModel(Data
8484
base.ApplyUserInputToModel(model);
8585

8686
model.ThreatDetectionState = ThreatDetectionStateType.Enabled;
87+
model.UseServerDefault = UseServerDefaultOptions.Disabled;
8788

8889
if (NotificationRecipientsEmails != null)
8990
{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Sql.ThreatDetection.Model;
16+
using System.Management.Automation;
17+
using Microsoft.Azure.Commands.Sql.Common;
18+
19+
namespace Microsoft.Azure.Commands.Sql.ThreatDetection.Cmdlet
20+
{
21+
/// <summary>
22+
/// Marks the given database as using its server's default policy instead of its own policy.
23+
/// </summary>
24+
[Cmdlet(VerbsOther.Use, "AzureRmSqlDatabaseThreatDetectionPolicy", SupportsShouldProcess = true),
25+
OutputType(typeof(DatabaseThreatDetectionPolicyModel))]
26+
public class UseAzureSqlServerAuditingPolicy : SqlDatabaseThreatDetectionCmdletBase
27+
{
28+
/// <summary>
29+
/// Defines whether the cmdlets will output the model object at the end of its execution
30+
/// </summary>
31+
[Parameter(Mandatory = false)]
32+
public SwitchParameter PassThru { get; set; }
33+
34+
/// <summary>
35+
/// Returns true if the model object that was constructed by this cmdlet should be written out
36+
/// </summary>
37+
/// <returns>True if the model object should be written out, False otherwise</returns>
38+
protected override bool WriteResult() { return PassThru; }
39+
40+
/// <summary>
41+
/// Updates the given model element with the cmdlet specific operation
42+
/// </summary>
43+
/// <param name="model">A model object</param>
44+
protected override DatabaseThreatDetectionPolicyModel ApplyUserInputToModel(DatabaseThreatDetectionPolicyModel model)
45+
{
46+
if (model.ThreatDetectionState == ThreatDetectionStateType.New)
47+
{
48+
model.ThreatDetectionState = ThreatDetectionStateType.Enabled;
49+
}
50+
model.UseServerDefault = SecurityConstants.UseServerDefaultOptions.Enabled;
51+
return model;
52+
}
53+
}
54+
}

src/ResourceManager/Sql/Commands.Sql/ThreatDetection/Model/DatabaseThreatDetectionPolicyModel .cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Commands.Sql.Common;
16+
1517
namespace Microsoft.Azure.Commands.Sql.ThreatDetection.Model
1618
{
19+
20+
1721
/// <summary>
1822
/// A class representing a database threat detection policy
1923
/// </summary>
@@ -23,5 +27,10 @@ public class DatabaseThreatDetectionPolicyModel : BaseThreatDetectionPolicyModel
2327
/// Gets or sets the database name
2428
/// </summary>
2529
public string DatabaseName { get; set; }
30+
31+
/// <summary>
32+
/// Gets or sets the use server default property
33+
/// </summary>
34+
public SecurityConstants.UseServerDefaultOptions UseServerDefault { get; set; }
2635
}
2736
}

src/ResourceManager/Sql/Commands.Sql/ThreatDetection/Services/SqlThreatDetectionAdapter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public ServerThreatDetectionPolicyModel GetServerThreatDetectionPolicy(string re
117117
private static BaseThreatDetectionPolicyModel ModelizeThreatDetectionPolicy(BaseSecurityAlertPolicyProperties threatDetectionProperties, BaseThreatDetectionPolicyModel model)
118118
{
119119
model.ThreatDetectionState = ModelizeThreatDetectionState(threatDetectionProperties.State);
120+
model.UseServerDefault = properties.UseServerDefault == SecurityConstants.AuditingEndpoint.Enabled ? SecurityConstants.UseServerDefaultOptions.Enabled : SecurityConstants.UseServerDefaultOptions.Disabled;
120121
model.NotificationRecipientsEmails = threatDetectionProperties.EmailAddresses;
121122
model.EmailAdmins = ModelizeThreatDetectionEmailAdmins(threatDetectionProperties.EmailAccountAdmins);
122123
ModelizeStorageAccount(model, threatDetectionProperties.StorageEndpoint);

0 commit comments

Comments
 (0)