protected void ValidateThumbprint(bool uploadCert) { if (X509Certificate != null) { var operationDescription = string.Format(Resources.ServiceExtensionUploadingCertificate, CommandRuntime, X509Certificate.Thumbprint); if (uploadCert) { var parameters = new ServiceCertificateCreateParameters { Data = CertUtilsNewSM.GetCertificateData(X509Certificate), Password = null, CertificateFormat = CertificateFormat.Pfx }; ExecuteClientActionNewSM( null, CommandRuntime.ToString(), () => this.ComputeClient.ServiceCertificates.Create(this.ServiceName, parameters)); } CertificateThumbprint = X509Certificate.Thumbprint; } else { CertificateThumbprint = CertificateThumbprint ?? string.Empty; } ThumbprintAlgorithm = ThumbprintAlgorithm ?? string.Empty; }
protected PSArgument[] CreateServiceCertificateCreateParameters() { string serviceName = string.Empty; ServiceCertificateCreateParameters parameters = new ServiceCertificateCreateParameters(); return(ConvertFromObjectsToArguments(new string[] { "ServiceName", "Parameters" }, new object[] { serviceName, parameters })); }
protected void ExecuteServiceCertificateCreateMethod(object[] invokeMethodInputParameters) { string serviceName = (string)ParseParameter(invokeMethodInputParameters[0]); ServiceCertificateCreateParameters parameters = (ServiceCertificateCreateParameters)ParseParameter(invokeMethodInputParameters[1]); var result = ServiceCertificateClient.Create(serviceName, parameters); WriteObject(result); }
public static ServiceCertificateCreateParameters Create(X509Certificate2 certificate, bool dropPrivateKey) { if (dropPrivateKey) { certificate = DropPrivateKey(certificate); } var password = CertUtils.RandomBase64PasswordString(); var certificateData = GetCertificateData(certificate, password); var certificateFile = new ServiceCertificateCreateParameters { Data = certificateData, Password = password, CertificateFormat = CertificateFormat.Pfx }; return certificateFile; }
internal void ExecuteCommand() { Password = Password ?? string.Empty; var certData = GetCertificateData(); var parameters = new ServiceCertificateCreateParameters { Data = certData, Password = Password, CertificateFormat = CertificateFormat.Pfx }; ExecuteClientActionNewSM( null, CommandRuntime.ToString(), () => this.ComputeClient.ServiceCertificates.Create(this.ServiceName, parameters)); }
public static ServiceCertificateCreateParameters Create(X509Certificate2 certificate, bool dropPrivateKey) { if (dropPrivateKey) { certificate = DropPrivateKey(certificate); } var password = CertUtils.RandomBase64PasswordString(); var certificateData = GetCertificateData(certificate, password); var certificateFile = new ServiceCertificateCreateParameters { Data = certificateData, Password = password, CertificateFormat = CertificateFormat.Pfx }; return(certificateFile); }
private void UploadCertificate(X509Certificate2 cert, ConfigCertificate certElement, string name) { try { var createParams = new ServiceCertificateCreateParameters { Data = cert.Export(X509ContentType.Pfx, string.Empty), Password = string.Empty, CertificateFormat = CertificateFormat.Pfx }; TranslateException(() => ComputeClient.ServiceCertificates.Create(name, createParams)); } catch (CryptographicException ex) { throw new ArgumentException(string.Format( Resources.CertificatePrivateKeyAccessError, certElement.name), ex); } }
/// <summary> /// The Begin Creating Service Certificate operation adds a certificate /// to a hosted service. This operation is an asynchronous operation. /// To determine whether the management service has finished /// processing the request, call Get Operation Status. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460817.aspx /// for more information) /// </summary> /// <param name='serviceName'> /// Required. The DNS prefix name of your service. /// </param> /// <param name='parameters'> /// Required. Parameters supplied to the Begin Creating Service /// Certificate operation. /// </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> BeginCreatingAsync(string serviceName, ServiceCertificateCreateParameters parameters, CancellationToken cancellationToken) { // Validate if (serviceName == null) { throw new ArgumentNullException("serviceName"); } // TODO: Validate serviceName is a valid DNS name. if (parameters == null) { throw new ArgumentNullException("parameters"); } if (parameters.Data == null) { throw new ArgumentNullException("parameters.Data"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("serviceName", serviceName); tracingParameters.Add("parameters", parameters); TracingAdapter.Enter(invocationId, this, "BeginCreatingAsync", tracingParameters); } // Construct URL string url = "/" + (this.Client.Credentials.SubscriptionId == null ? "" : Uri.EscapeDataString(this.Client.Credentials.SubscriptionId)) + "/services/hostedservices/" + Uri.EscapeDataString(serviceName) + "/certificates"; 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.Post; httpRequest.RequestUri = new Uri(url); // Set Headers httpRequest.Headers.Add("x-ms-version", "2014-10-01"); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Serialize Request string requestContent = null; XDocument requestDoc = new XDocument(); XElement certificateFileElement = new XElement(XName.Get("CertificateFile", "http://schemas.microsoft.com/windowsazure")); requestDoc.Add(certificateFileElement); XElement dataElement = new XElement(XName.Get("Data", "http://schemas.microsoft.com/windowsazure")); dataElement.Value = Convert.ToBase64String(parameters.Data); certificateFileElement.Add(dataElement); XElement certificateFormatElement = new XElement(XName.Get("CertificateFormat", "http://schemas.microsoft.com/windowsazure")); certificateFormatElement.Value = ComputeManagementClient.CertificateFormatToString(parameters.CertificateFormat); certificateFileElement.Add(certificateFormatElement); if (parameters.Password != null) { XElement passwordElement = new XElement(XName.Get("Password", "http://schemas.microsoft.com/windowsazure")); passwordElement.Value = parameters.Password; certificateFileElement.Add(passwordElement); } requestContent = requestDoc.ToString(); httpRequest.Content = new StringContent(requestContent, Encoding.UTF8); httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/xml"); // 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.Accepted) { 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> /// The Create Service Certificate operation adds a certificate to a /// hosted service. This operation is an asynchronous operation. To /// determine whether the management service has finished processing /// the request, call Get Operation Status. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460817.aspx /// for more information) /// </summary> /// <param name='serviceName'> /// Required. The DNS prefix name of your service. /// </param> /// <param name='parameters'> /// Required. Parameters supplied to the Create Service Certificate /// operation. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The response body contains the status of the specified asynchronous /// operation, indicating whether it has succeeded, is inprogress, or /// has failed. Note that this status is distinct from the HTTP status /// code returned for the Get Operation Status operation itself. If /// the asynchronous operation succeeded, the response body includes /// the HTTP status code for the successful request. If the /// asynchronous operation failed, the response body includes the HTTP /// status code for the failed request and error information regarding /// the failure. /// </returns> public async Task <OperationStatusResponse> CreateAsync(string serviceName, ServiceCertificateCreateParameters parameters, CancellationToken cancellationToken) { ComputeManagementClient client = this.Client; bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("serviceName", serviceName); tracingParameters.Add("parameters", parameters); TracingAdapter.Enter(invocationId, this, "CreateAsync", tracingParameters); } cancellationToken.ThrowIfCancellationRequested(); AzureOperationResponse response = await client.ServiceCertificates.BeginCreatingAsync(serviceName, parameters, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); OperationStatusResponse result = await client.GetOperationStatusAsync(response.RequestId, cancellationToken).ConfigureAwait(false); int delayInSeconds = 30; if (client.LongRunningOperationInitialTimeout >= 0) { delayInSeconds = client.LongRunningOperationInitialTimeout; } while ((result.Status != OperationStatus.InProgress) == false) { cancellationToken.ThrowIfCancellationRequested(); await TaskEx.Delay(delayInSeconds * 1000, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); result = await client.GetOperationStatusAsync(response.RequestId, cancellationToken).ConfigureAwait(false); delayInSeconds = 30; if (client.LongRunningOperationRetryTimeout >= 0) { delayInSeconds = client.LongRunningOperationRetryTimeout; } } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } if (result.Status != OperationStatus.Succeeded) { if (result.Error != null) { CloudException ex = new CloudException(result.Error.Code + " : " + result.Error.Message); ex.Error = new CloudError(); ex.Error.Code = result.Error.Code; ex.Error.Message = result.Error.Message; if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } else { CloudException ex = new CloudException(""); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } } return(result); }
/// <summary> /// The Begin Creating Service Certificate operation adds a certificate /// to a hosted service. This operation is an asynchronous operation. /// To determine whether the management service has finished /// processing the request, call Get Operation Status. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460817.aspx /// for more information) /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.WindowsAzure.Management.Compute.IServiceCertificateOperations. /// </param> /// <param name='serviceName'> /// Required. The DNS prefix name of your service. /// </param> /// <param name='parameters'> /// Required. Parameters supplied to the Begin Creating Service /// Certificate operation. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static AzureOperationResponse BeginCreating(this IServiceCertificateOperations operations, string serviceName, ServiceCertificateCreateParameters parameters) { return(Task.Factory.StartNew((object s) => { return ((IServiceCertificateOperations)s).BeginCreatingAsync(serviceName, parameters); } , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult()); }
/// <summary> /// The Create Service Certificate operation adds a certificate to a /// hosted service. This operation is an asynchronous operation. To /// determine whether the management service has finished processing /// the request, call Get Operation Status. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460817.aspx /// for more information) /// </summary> /// <param name='serviceName'> /// Required. The DNS prefix name of your service. /// </param> /// <param name='parameters'> /// Required. Parameters supplied to the Create Service Certificate /// operation. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The response body contains the status of the specified asynchronous /// operation, indicating whether it has succeeded, is inprogress, or /// has failed. Note that this status is distinct from the HTTP status /// code returned for the Get Operation Status operation itself. If /// the asynchronous operation succeeded, the response body includes /// the HTTP status code for the successful request. If the /// asynchronous operation failed, the response body includes the HTTP /// status code for the failed request and error information regarding /// the failure. /// </returns> public async System.Threading.Tasks.Task <OperationStatusResponse> CreateAsync(string serviceName, ServiceCertificateCreateParameters parameters, CancellationToken cancellationToken) { ComputeManagementClient client = this.Client; bool shouldTrace = CloudContext.Configuration.Tracing.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = Tracing.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("serviceName", serviceName); tracingParameters.Add("parameters", parameters); Tracing.Enter(invocationId, this, "CreateAsync", tracingParameters); } try { if (shouldTrace) { client = this.Client.WithHandler(new ClientRequestTrackingHandler(invocationId)); } cancellationToken.ThrowIfCancellationRequested(); OperationResponse response = await client.ServiceCertificates.BeginCreatingAsync(serviceName, parameters, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); OperationStatusResponse result = await client.GetOperationStatusAsync(response.RequestId, cancellationToken).ConfigureAwait(false); int delayInSeconds = 30; while ((result.Status != OperationStatus.InProgress) == false) { cancellationToken.ThrowIfCancellationRequested(); await TaskEx.Delay(delayInSeconds * 1000, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); result = await client.GetOperationStatusAsync(response.RequestId, cancellationToken).ConfigureAwait(false); delayInSeconds = 30; } if (shouldTrace) { Tracing.Exit(invocationId, result); } if (result.Status != OperationStatus.Succeeded) { if (result.Error != null) { CloudException ex = new CloudException(result.Error.Code + " : " + result.Error.Message); ex.ErrorCode = result.Error.Code; ex.ErrorMessage = result.Error.Message; if (shouldTrace) { Tracing.Error(invocationId, ex); } throw ex; } else { CloudException ex = new CloudException(""); if (shouldTrace) { Tracing.Error(invocationId, ex); } throw ex; } } return(result); } finally { if (client != null && shouldTrace) { client.Dispose(); } } }
/// <summary> /// The Add Service Certificate operation adds a certificate to a /// hosted service. The Add Service Certificate operation is an /// asynchronous operation. To determine whether the management /// service has finished processing the request, call Get Operation /// Status. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460817.aspx /// for more information) /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.WindowsAzure.Management.Compute.IServiceCertificateOperations. /// </param> /// <param name='serviceName'> /// The DNS prefix name of your service. /// </param> /// <param name='parameters'> /// Parameters supplied to the Create Service Certificate operation. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static Task <OperationResponse> BeginCreatingAsync(this IServiceCertificateOperations operations, string serviceName, ServiceCertificateCreateParameters parameters) { return(operations.BeginCreatingAsync(serviceName, parameters, CancellationToken.None)); }
/// <summary> /// The Add Service Certificate operation adds a certificate to a /// hosted service. The Add Service Certificate operation is an /// asynchronous operation. To determine whether the management /// service has finished processing the request, call Get Operation /// Status. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460817.aspx /// for more information) /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.WindowsAzure.Management.Compute.IServiceCertificateOperations. /// </param> /// <param name='serviceName'> /// The DNS prefix name of your service. /// </param> /// <param name='parameters'> /// Parameters supplied to the Create Service Certificate operation. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static OperationResponse BeginCreating(this IServiceCertificateOperations operations, string serviceName, ServiceCertificateCreateParameters parameters) { try { return(operations.BeginCreatingAsync(serviceName, parameters).Result); } catch (AggregateException ex) { if (ex.InnerExceptions.Count > 1) { throw; } else { throw ex.InnerException; } } }
/// <summary> /// The Add Service Certificate operation adds a certificate to a /// hosted service. The Add Service Certificate operation is an /// asynchronous operation. To determine whether the management /// service has finished processing the request, call Get Operation /// Status. This overload will (see /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460817.aspx /// for more information) /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.WindowsAzure.Management.Compute.IServiceCertificateOperations. /// </param> /// <param name='serviceName'> /// The DNS prefix name of your service. /// </param> /// <param name='parameters'> /// Parameters supplied to the Create Service Certificate operation. /// </param> /// <returns> /// The response body contains the status of the specified asynchronous /// operation, indicating whether it has succeeded, is inprogress, or /// has failed. Note that this status is distinct from the HTTP status /// code returned for the Get Operation Status operation itself. If /// the asynchronous operation succeeded, the response body includes /// the HTTP status code for the successful request. If the /// asynchronous operation failed, the response body includes the HTTP /// status code for the failed request, and also includes error /// information regarding the failure. /// </returns> public static Task <ComputeOperationStatusResponse> CreateAsync(this IServiceCertificateOperations operations, string serviceName, ServiceCertificateCreateParameters parameters) { return(operations.CreateAsync(serviceName, parameters, CancellationToken.None)); }