private DatabaseSecurityPolicyUpdateParameters UnwrapPolicy(AuditingPolicy policy) { DatabaseSecurityPolicyUpdateParameters updateParameters = new DatabaseSecurityPolicyUpdateParameters(); DatabaseSecurityPolicyProperties properties = new DatabaseSecurityPolicyProperties(); updateParameters.Properties = properties; properties.RetentionDays = 90; properties.IsAuditingEnabled = policy.IsEnabled; properties.UseServerDefault = policy.UseServerDefault; UpdateEventTypes(policy, properties); UpdateStorage(policy.StorageAccountName, properties); return(updateParameters); }
public void SetDatabaseAuditingPolicy(AuditingPolicy policy, String clientId) { DatabaseSecurityPolicyUpdateParameters parameters = UnwrapPolicy(policy); Communicator.SetDatabaseSecurityPolicy(policy.ResourceGroupName, policy.ServerName, policy.DatabaseName, clientId, parameters); }
/// <summary> /// Updates an Azure SQL Database security policy object. /// </summary> /// <param name='resourceGroupName'> /// Required. The name of the Resource Group to which the Azure SQL /// Database Server belongs. /// </param> /// <param name='serverName'> /// Required. The name of the Azure SQL Database Server to which the /// Azure SQL Database belongs. /// </param> /// <param name='databaseName'> /// Required. The name of the Azure SQL Database to which the security /// policy is applied. /// </param> /// <param name='parameters'> /// Required. The required parameters for updating a security policy. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public async Task <AzureOperationResponse> UpdateAsync(string resourceGroupName, string serverName, string databaseName, DatabaseSecurityPolicyUpdateParameters parameters, CancellationToken cancellationToken) { // Validate if (resourceGroupName == null) { throw new ArgumentNullException("resourceGroupName"); } if (serverName == null) { throw new ArgumentNullException("serverName"); } if (databaseName == null) { throw new ArgumentNullException("databaseName"); } if (parameters == null) { throw new ArgumentNullException("parameters"); } if (parameters.Properties == null) { throw new ArgumentNullException("parameters.Properties"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("resourceGroupName", resourceGroupName); tracingParameters.Add("serverName", serverName); tracingParameters.Add("databaseName", databaseName); tracingParameters.Add("parameters", parameters); TracingAdapter.Enter(invocationId, this, "UpdateAsync", tracingParameters); } // Construct URL string url = ""; url = url + "/subscriptions/"; if (this.Client.Credentials.SubscriptionId != null) { url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId); } url = url + "/resourceGroups/"; url = url + Uri.EscapeDataString(resourceGroupName); url = url + "/providers/"; url = url + "Microsoft.Sql"; url = url + "/servers/"; url = url + Uri.EscapeDataString(serverName); url = url + "/databaseSecurityPolicies/"; url = url + Uri.EscapeDataString(databaseName); List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=2014-04-01"); if (queryParameters.Count > 0) { url = url + "?" + string.Join("&", queryParameters); } string baseUrl = this.Client.BaseUri.AbsoluteUri; // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl[baseUrl.Length - 1] == '/') { baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } if (url[0] == '/') { url = url.Substring(1); } url = baseUrl + "/" + url; url = url.Replace(" ", "%20"); // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Put; httpRequest.RequestUri = new Uri(url); // Set Headers // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Serialize Request string requestContent = null; JToken requestDoc = null; JObject databaseSecurityPolicyUpdateParametersValue = new JObject(); requestDoc = databaseSecurityPolicyUpdateParametersValue; JObject propertiesValue = new JObject(); databaseSecurityPolicyUpdateParametersValue["properties"] = propertiesValue; propertiesValue["isAuditingEnabled"] = parameters.Properties.IsAuditingEnabled; propertiesValue["retentionDays"] = parameters.Properties.RetentionDays; propertiesValue["isEventTypeDataAccessEnabled"] = parameters.Properties.IsEventTypeDataAccessEnabled; propertiesValue["isEventTypeSchemaChangeEnabled"] = parameters.Properties.IsEventTypeSchemaChangeEnabled; propertiesValue["isEventTypeDataChangesEnabled"] = parameters.Properties.IsEventTypeDataChangesEnabled; propertiesValue["isEventTypeSecurityExceptionsEnabled"] = parameters.Properties.IsEventTypeSecurityExceptionsEnabled; propertiesValue["isEventTypeGrantRevokePermissionsEnabled"] = parameters.Properties.IsEventTypeGrantRevokePermissionsEnabled; if (parameters.Properties.StorageAccountName != null) { propertiesValue["storageAccountName"] = parameters.Properties.StorageAccountName; } if (parameters.Properties.StorageAccountKey != null) { propertiesValue["storageAccountKey"] = parameters.Properties.StorageAccountKey; } if (parameters.Properties.SecondaryStorageAccountKey != null) { propertiesValue["secondaryStorageAccountKey"] = parameters.Properties.SecondaryStorageAccountKey; } if (parameters.Properties.StorageTableEndpoint != null) { propertiesValue["storageTableEndpoint"] = parameters.Properties.StorageTableEndpoint; } if (parameters.Properties.StorageAccountResourceGroupName != null) { propertiesValue["storageAccountResourceGroupName"] = parameters.Properties.StorageAccountResourceGroupName; } if (parameters.Properties.StorageAccountSubscriptionId != null) { propertiesValue["storageAccountSubscriptionId"] = parameters.Properties.StorageAccountSubscriptionId; } if (parameters.Properties.ProxyDnsName != null) { propertiesValue["proxyDnsName"] = parameters.Properties.ProxyDnsName; } if (parameters.Properties.ProxyPort != null) { propertiesValue["proxyPort"] = parameters.Properties.ProxyPort; } propertiesValue["useServerDefault"] = parameters.Properties.UseServerDefault; propertiesValue["isBlockDirectAccessEnabled"] = parameters.Properties.IsBlockDirectAccessEnabled; requestContent = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented); httpRequest.Content = new StringContent(requestContent, Encoding.UTF8); httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { TracingAdapter.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { TracingAdapter.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result AzureOperationResponse result = null; // Deserialize Response result = new AzureOperationResponse(); result.StatusCode = statusCode; if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }
/// <summary> /// Sets the database server security policy of the given database server in the given resource group /// </summary> public void SetServerSecurityPolicy(string resourceGroupName, string serverName, string clientRequestId, DatabaseSecurityPolicyUpdateParameters parameters) { ISecurityOperations operations = GetCurrentSqlClient(clientRequestId).DatabaseSecurity; operations.Update(resourceGroupName, serverName, Constants.ServerPolicyId, parameters); }
/// <summary> /// Updates an Azure SQL Database security policy object. /// </summary> /// <param name='operations'> /// Reference to the Microsoft.Azure.Management.Sql.ISecurityOperations. /// </param> /// <param name='resourceGroupName'> /// Required. The name of the Resource Group to which the Azure SQL /// Database Server belongs. /// </param> /// <param name='serverName'> /// Required. The name of the Azure SQL Database Server to which the /// Azure SQL Database belongs. /// </param> /// <param name='databaseName'> /// Required. The name of the Azure SQL Database to which the security /// policy is applied. /// </param> /// <param name='parameters'> /// Required. The required parameters for updating a security policy. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static Task <AzureOperationResponse> UpdateAsync(this ISecurityOperations operations, string resourceGroupName, string serverName, string databaseName, DatabaseSecurityPolicyUpdateParameters parameters) { return(operations.UpdateAsync(resourceGroupName, serverName, databaseName, parameters, CancellationToken.None)); }
/// <summary> /// Updates an Azure SQL Database security policy object. /// </summary> /// <param name='operations'> /// Reference to the Microsoft.Azure.Management.Sql.ISecurityOperations. /// </param> /// <param name='resourceGroupName'> /// Required. The name of the Resource Group to which the Azure SQL /// Database Server belongs. /// </param> /// <param name='serverName'> /// Required. The name of the Azure SQL Database Server to which the /// Azure SQL Database belongs. /// </param> /// <param name='databaseName'> /// Required. The name of the Azure SQL Database to which the security /// policy is applied. /// </param> /// <param name='parameters'> /// Required. The required parameters for updating a security policy. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static AzureOperationResponse Update(this ISecurityOperations operations, string resourceGroupName, string serverName, string databaseName, DatabaseSecurityPolicyUpdateParameters parameters) { return(Task.Factory.StartNew((object s) => { return ((ISecurityOperations)s).UpdateAsync(resourceGroupName, serverName, databaseName, parameters); } , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult()); }