| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Globalization; |
| | 6 | | using System.Text.Json; |
| | 7 | | using Azure.Core; |
| | 8 | |
|
| | 9 | | namespace Azure.Security.KeyVault.Certificates |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Properties pertaining to the status of a certificate operation. |
| | 13 | | /// </summary> |
| | 14 | | public class CertificateOperationProperties : IJsonDeserializable |
| | 15 | | { |
| | 16 | | private const string IdPropertyName = "id"; |
| | 17 | | private const string IssuerProperyName = "issuer"; |
| | 18 | | private const string CsrPropertyName = "csr"; |
| | 19 | | private const string CancellationRequestedPropertyName = "cancellation_requested"; |
| | 20 | | private const string RequestIdPropertyName = "request_id"; |
| | 21 | | private const string StatusPropertyName = "status"; |
| | 22 | | private const string StatusDetailsPropertyName = "status_details"; |
| | 23 | | private const string TargetPropertyName = "target"; |
| | 24 | | private const string ErrorPropertyName = "error"; |
| | 25 | | private const string Collection = "certificates/"; |
| | 26 | |
|
| | 27 | | private IssuerParameters _issuer; |
| | 28 | |
|
| 742 | 29 | | internal CertificateOperationProperties() |
| | 30 | | { |
| 742 | 31 | | } |
| | 32 | |
|
| 16 | 33 | | internal CertificateOperationProperties(Uri vaultUri, string name) |
| | 34 | | { |
| 16 | 35 | | VaultUri = vaultUri; |
| 16 | 36 | | Name = name; |
| | 37 | |
|
| 16 | 38 | | RequestUriBuilder builder = new RequestUriBuilder |
| 16 | 39 | | { |
| 16 | 40 | | Scheme = vaultUri.Scheme, |
| 16 | 41 | | Host = vaultUri.Host, |
| 16 | 42 | | Port = vaultUri.Port, |
| 16 | 43 | | Path = Collection + name, |
| 16 | 44 | | }; |
| | 45 | |
|
| 16 | 46 | | Id = builder.ToUri(); |
| 16 | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Gets the identifier of the certificate operation. |
| | 51 | | /// </summary> |
| 1576 | 52 | | public Uri Id { get; internal set; } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Gets the name of the certificate to which the operation applies. |
| | 56 | | /// </summary> |
| 1480 | 57 | | public string Name { get; internal set; } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Gets the <see cref="Uri"/> of the vault executing the certificate operation. |
| | 61 | | /// </summary> |
| 0 | 62 | | public Uri VaultUri { get; internal set; } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Gets the name of the <see cref="CertificateIssuer"/> for the certificate to create. |
| | 66 | | /// </summary> |
| | 67 | | public string IssuerName |
| | 68 | | { |
| 0 | 69 | | get => _issuer.IssuerName; |
| 0 | 70 | | internal set => _issuer.IssuerName = value; |
| | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Gets the type of the certificate to create. |
| | 75 | | /// </summary> |
| | 76 | | public string CertificateType |
| | 77 | | { |
| 0 | 78 | | get => _issuer.CertificateType; |
| 0 | 79 | | internal set => _issuer.CertificateType = value; |
| | 80 | | } |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Gets a value indicating whether the certificate will be published to the certificate transparency list when |
| | 84 | | /// </summary> |
| | 85 | | public bool? CertificateTransparency |
| | 86 | | { |
| 0 | 87 | | get => _issuer.CertificateTransparency; |
| 0 | 88 | | internal set => _issuer.CertificateTransparency = value; |
| | 89 | | } |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Gets the certificate signing request (CSR) that is being used in the certificate operation. |
| | 93 | | /// </summary> |
| 746 | 94 | | public byte[] Csr { get; internal set; } |
| | 95 | |
|
| | 96 | | /// <summary> |
| | 97 | | /// Gets a value indicating whether a cancellation has been requested for the operation. |
| | 98 | | /// </summary> |
| 0 | 99 | | public bool CancellationRequested { get; internal set; } |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Gets the request identifier of the certificate operation. |
| | 103 | | /// </summary> |
| 0 | 104 | | public string RequestId { get; internal set; } |
| | 105 | |
|
| | 106 | | /// <summary> |
| | 107 | | /// Gets the current status of the operation. |
| | 108 | | /// </summary> |
| 2106 | 109 | | public string Status { get; internal set; } |
| | 110 | |
|
| | 111 | | /// <summary> |
| | 112 | | /// Gets extended details on the status of the operation. |
| | 113 | | /// </summary> |
| 0 | 114 | | public string StatusDetails { get; internal set; } |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Gets the location which will contain the result of the certificate operation. |
| | 118 | | /// </summary> |
| 0 | 119 | | public string Target { get; internal set; } |
| | 120 | |
|
| | 121 | | /// <summary> |
| | 122 | | /// Gets any errors encountered during the processing of the certificate operation. |
| | 123 | | /// </summary> |
| 690 | 124 | | public CertificateOperationError Error { get; internal set; } |
| | 125 | |
|
| | 126 | | void IJsonDeserializable.ReadProperties(JsonElement json) |
| | 127 | | { |
| 11856 | 128 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | 129 | | { |
| 5186 | 130 | | switch (prop.Name) |
| | 131 | | { |
| | 132 | | case IdPropertyName: |
| 742 | 133 | | var id = prop.Value.GetString(); |
| 742 | 134 | | Id = new Uri(id); |
| 742 | 135 | | ParseId(Id); |
| 742 | 136 | | break; |
| | 137 | |
|
| | 138 | | case IssuerProperyName: |
| 742 | 139 | | _issuer.ReadProperties(prop.Value); |
| 742 | 140 | | break; |
| | 141 | |
|
| | 142 | | case CsrPropertyName: |
| 742 | 143 | | Csr = prop.Value.GetBytesFromBase64(); |
| 742 | 144 | | break; |
| | 145 | |
|
| | 146 | | case CancellationRequestedPropertyName: |
| 742 | 147 | | CancellationRequested = prop.Value.GetBoolean(); |
| 742 | 148 | | break; |
| | 149 | |
|
| | 150 | | case RequestIdPropertyName: |
| 742 | 151 | | RequestId = prop.Value.GetString(); |
| 742 | 152 | | break; |
| | 153 | |
|
| | 154 | | case StatusPropertyName: |
| 742 | 155 | | Status = prop.Value.GetString(); |
| 742 | 156 | | break; |
| | 157 | |
|
| | 158 | | case StatusDetailsPropertyName: |
| 702 | 159 | | StatusDetails = prop.Value.GetString(); |
| 702 | 160 | | break; |
| | 161 | |
|
| | 162 | | case TargetPropertyName: |
| 28 | 163 | | Target = prop.Value.GetString(); |
| 28 | 164 | | break; |
| | 165 | |
|
| | 166 | | case ErrorPropertyName: |
| 4 | 167 | | Error = new CertificateOperationError(); |
| 4 | 168 | | ((IJsonDeserializable)Error).ReadProperties(prop.Value); |
| | 169 | | break; |
| | 170 | | } |
| | 171 | | } |
| 742 | 172 | | } |
| | 173 | |
|
| | 174 | | private void ParseId(Uri idToParse) |
| | 175 | | { |
| | 176 | | // We expect an identifier with either 3 or 4 segments: host + collection + name [+ version] |
| 742 | 177 | | if (idToParse.Segments.Length != 3 && idToParse.Segments.Length != 4) |
| 0 | 178 | | throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ObjectIdentifier: {0}. |
| | 179 | |
|
| 742 | 180 | | if (!string.Equals(idToParse.Segments[1], Collection, StringComparison.OrdinalIgnoreCase)) |
| 0 | 181 | | throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ObjectIdentifier: {0}. |
| | 182 | |
|
| 742 | 183 | | VaultUri = new Uri($"{idToParse.Scheme}://{idToParse.Authority}"); |
| 742 | 184 | | Name = idToParse.Segments[2].Trim('/'); |
| 742 | 185 | | } |
| | 186 | | } |
| | 187 | | } |