| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Text.Json; |
| | 7 | | using System.Threading; |
| | 8 | | using Azure.Core; |
| | 9 | |
|
| | 10 | | namespace Azure.Security.KeyVault.Certificates |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// A certificate issuer used to sign certificates managed by Azure Key Vault. |
| | 14 | | /// </summary> |
| | 15 | | public class CertificateIssuer : IJsonDeserializable, IJsonSerializable |
| | 16 | | { |
| | 17 | | private const string CredentialsPropertyName = "credentials"; |
| | 18 | | private const string OrgDetailsPropertyName = "org_details"; |
| | 19 | | private const string AttributesPropertyName = "attributes"; |
| | 20 | | private const string AccountIdPropertyName = "account_id"; |
| | 21 | | private const string PasswordPropertyName = "pwd"; |
| | 22 | | private const string OrganizationIdPropertyName = "id"; |
| | 23 | | private const string AdminDetailsPropertyName = "admin_details"; |
| | 24 | | private const string CreatedPropertyName = "created"; |
| | 25 | | private const string UpdatedPropertyName = "updated"; |
| | 26 | | private const string EnabledPropertyName = "enabled"; |
| | 27 | |
|
| 2 | 28 | | private static readonly JsonEncodedText s_credentialsPropertyNameBytes = JsonEncodedText.Encode(CredentialsPrope |
| 2 | 29 | | private static readonly JsonEncodedText s_orgDetailsPropertyNameBytes = JsonEncodedText.Encode(OrgDetailsPropert |
| 2 | 30 | | private static readonly JsonEncodedText s_attributesPropertyNameBytes = JsonEncodedText.Encode(AttributesPropert |
| 2 | 31 | | private static readonly JsonEncodedText s_enabledPropertyNameBytes = JsonEncodedText.Encode(EnabledPropertyName) |
| 2 | 32 | | private static readonly JsonEncodedText s_accountIdPropertyNameBytes = JsonEncodedText.Encode(AccountIdPropertyN |
| 2 | 33 | | private static readonly JsonEncodedText s_passwordPropertyNameBytes = JsonEncodedText.Encode(PasswordPropertyNam |
| 2 | 34 | | private static readonly JsonEncodedText s_organizationIdPropertyNameBytes = JsonEncodedText.Encode(OrganizationI |
| 2 | 35 | | private static readonly JsonEncodedText s_adminDetailsPropertyNameBytes = JsonEncodedText.Encode(AdminDetailsPro |
| | 36 | |
|
| | 37 | | private List<AdministratorContact> _administratorContacts; |
| | 38 | | private IssuerProperties _properties; |
| | 39 | |
|
| 44 | 40 | | internal CertificateIssuer(IssuerProperties properties = null) |
| | 41 | | { |
| 44 | 42 | | _properties = properties ?? new IssuerProperties(); |
| 44 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Initializes a new instance of the <see cref="CertificateIssuer"/> class. |
| | 47 | | /// You can use this constructor to initialize a <see cref="CertificateIssuer"/> for |
| | 48 | | /// <see cref="CertificateClient.UpdateIssuer(CertificateIssuer, CancellationToken)"/> or |
| | 49 | | /// <see cref="CertificateClient.UpdateIssuerAsync(CertificateIssuer, CancellationToken)"/>. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="name">The name of the issuer, including values from <see cref="WellKnownIssuerNames"/>.</param> |
| | 52 | | /// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception> |
| | 53 | | /// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception> |
| 8 | 54 | | public CertificateIssuer(string name) |
| | 55 | | { |
| 8 | 56 | | Argument.AssertNotNullOrEmpty(name, nameof(name)); |
| | 57 | |
|
| 4 | 58 | | _properties = new IssuerProperties(name); |
| 4 | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Initializes a new instance of the <see cref="CertificateIssuer"/> class. |
| | 63 | | /// You can use this constructor to initialize a <see cref="CertificateIssuer"/> for |
| | 64 | | /// <see cref="CertificateClient.CreateIssuer(CertificateIssuer, CancellationToken)"/> or |
| | 65 | | /// <see cref="CertificateClient.CreateIssuerAsync(CertificateIssuer, CancellationToken)"/>. |
| | 66 | | /// </summary> |
| | 67 | | /// <param name="name">The name of the issuer, including values from <see cref="WellKnownIssuerNames"/>.</param> |
| | 68 | | /// <param name="provider">The provider name of the certificate issuer.</param> |
| | 69 | | /// <exception cref="ArgumentException"><paramref name="name"/> or <paramref name="provider"/> is empty.</except |
| | 70 | | /// <exception cref="ArgumentNullException"><paramref name="name"/> or <paramref name="provider"/> is null.</exc |
| 32 | 71 | | public CertificateIssuer(string name, string provider) |
| | 72 | | { |
| 32 | 73 | | Argument.AssertNotNullOrEmpty(name, nameof(name)); |
| 32 | 74 | | Argument.AssertNotNullOrEmpty(provider, nameof(provider)); |
| | 75 | |
|
| 28 | 76 | | _properties = new IssuerProperties(name) |
| 28 | 77 | | { |
| 28 | 78 | | Provider = provider, |
| 28 | 79 | | }; |
| 28 | 80 | | } |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Gets the unique identifier of the certificate issuer. |
| | 84 | | /// </summary> |
| 6 | 85 | | public Uri Id => _properties.Id; |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Gets the name of the certificate issuer. |
| | 89 | | /// </summary> |
| 86 | 90 | | public string Name => _properties.Name; |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Gets or sets the provider name of the certificate issuer. |
| | 94 | | /// </summary> |
| 36 | 95 | | public string Provider => _properties.Provider; |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Gets or sets the account identifier or username used to authenticate to the certificate issuer. |
| | 99 | | /// </summary> |
| 74 | 100 | | public string AccountId { get; set; } |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// Gets or sets the password or key used to authenticate to the certificate issuer. |
| | 104 | | /// </summary> |
| 36 | 105 | | public string Password { get; set; } |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// Gets or sets the organizational identifier for the issuer. |
| | 109 | | /// </summary> |
| 50 | 110 | | public string OrganizationId { get; set; } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Gets a list of contacts who administer the certificate issuer account. |
| | 114 | | /// </summary> |
| 42 | 115 | | public IList<AdministratorContact> AdministratorContacts => LazyInitializer.EnsureInitialized(ref _administrator |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// Gets a <see cref="DateTimeOffset"/> indicating when the certificate was created. |
| | 119 | | /// </summary> |
| 0 | 120 | | public DateTimeOffset? CreatedOn { get; internal set; } |
| | 121 | |
|
| | 122 | | /// <summary> |
| | 123 | | /// Gets a <see cref="DateTimeOffset"/> indicating when the certificate was updated. |
| | 124 | | /// </summary> |
| 38 | 125 | | public DateTimeOffset? UpdatedOn { get; internal set; } |
| | 126 | |
|
| | 127 | | /// <summary> |
| | 128 | | /// Gets or sets a value indicating whether the issuer can currently be used to issue certificates. If null, the |
| | 129 | | /// </summary> |
| 62 | 130 | | public bool? Enabled { get; set; } |
| | 131 | |
|
| | 132 | | internal virtual void ReadProperty(JsonProperty prop) |
| | 133 | | { |
| 136 | 134 | | switch (prop.Name) |
| | 135 | | { |
| | 136 | | case CredentialsPropertyName: |
| 12 | 137 | | ReadCredentialsProperties(prop.Value); |
| 12 | 138 | | break; |
| | 139 | |
|
| | 140 | | case OrgDetailsPropertyName: |
| 20 | 141 | | ReadOrgDetailsProperties(prop.Value); |
| 20 | 142 | | break; |
| | 143 | |
|
| | 144 | | case AttributesPropertyName: |
| 34 | 145 | | ReadAttributeProperties(prop.Value); |
| 34 | 146 | | break; |
| | 147 | |
|
| | 148 | | default: |
| 70 | 149 | | _properties.ReadProperty(prop); |
| | 150 | | break; |
| | 151 | | } |
| 70 | 152 | | } |
| | 153 | |
|
| | 154 | | private void ReadCredentialsProperties(JsonElement json) |
| | 155 | | { |
| 48 | 156 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | 157 | | { |
| 12 | 158 | | switch (prop.Name) |
| | 159 | | { |
| | 160 | | case AccountIdPropertyName: |
| 12 | 161 | | AccountId = prop.Value.GetString(); |
| 12 | 162 | | break; |
| | 163 | |
|
| | 164 | | case PasswordPropertyName: |
| 0 | 165 | | Password = prop.Value.GetString(); |
| | 166 | | break; |
| | 167 | | } |
| | 168 | | } |
| 12 | 169 | | } |
| | 170 | |
|
| | 171 | | private void ReadOrgDetailsProperties(JsonElement json) |
| | 172 | | { |
| 116 | 173 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | 174 | | { |
| 38 | 175 | | switch (prop.Name) |
| | 176 | | { |
| | 177 | | case OrganizationIdPropertyName: |
| 8 | 178 | | OrganizationId = prop.Value.GetString(); |
| 8 | 179 | | break; |
| | 180 | |
|
| | 181 | | case AdminDetailsPropertyName: |
| 48 | 182 | | foreach (JsonElement elem in prop.Value.EnumerateArray()) |
| | 183 | | { |
| 12 | 184 | | var admin = new AdministratorContact(); |
| 12 | 185 | | admin.ReadProperties(elem); |
| 12 | 186 | | AdministratorContacts.Add(admin); |
| | 187 | | } |
| | 188 | | break; |
| | 189 | | } |
| | 190 | | } |
| 20 | 191 | | } |
| | 192 | |
|
| | 193 | | private void ReadAttributeProperties(JsonElement json) |
| | 194 | | { |
| 272 | 195 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | 196 | | { |
| 102 | 197 | | switch (prop.Name) |
| | 198 | | { |
| | 199 | | case EnabledPropertyName: |
| 34 | 200 | | Enabled = prop.Value.GetBoolean(); |
| 34 | 201 | | break; |
| | 202 | |
|
| | 203 | | case CreatedPropertyName: |
| 34 | 204 | | CreatedOn = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| 34 | 205 | | break; |
| | 206 | |
|
| | 207 | | case UpdatedPropertyName: |
| 34 | 208 | | UpdatedOn = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| | 209 | | break; |
| | 210 | | } |
| | 211 | | } |
| 34 | 212 | | } |
| | 213 | |
|
| | 214 | | internal virtual void WriteProperties(Utf8JsonWriter json) |
| | 215 | | { |
| 28 | 216 | | _properties.WriteProperties(json); |
| | 217 | |
|
| 28 | 218 | | if (!string.IsNullOrEmpty(AccountId) || !string.IsNullOrEmpty(Password)) |
| | 219 | | { |
| 8 | 220 | | json.WriteStartObject(s_credentialsPropertyNameBytes); |
| | 221 | |
|
| 8 | 222 | | WriteCredentialsProperties(json); |
| | 223 | |
|
| 8 | 224 | | json.WriteEndObject(); |
| | 225 | | } |
| | 226 | |
|
| 28 | 227 | | if (!string.IsNullOrEmpty(OrganizationId) || !_administratorContacts.IsNullOrEmpty()) |
| | 228 | | { |
| 10 | 229 | | json.WriteStartObject(s_orgDetailsPropertyNameBytes); |
| | 230 | |
|
| 10 | 231 | | WriteOrgDetailsProperties(json); |
| | 232 | |
|
| 10 | 233 | | json.WriteEndObject(); |
| | 234 | | } |
| | 235 | |
|
| 28 | 236 | | if (Enabled.HasValue) |
| | 237 | | { |
| 0 | 238 | | json.WriteStartObject(s_attributesPropertyNameBytes); |
| | 239 | |
|
| 0 | 240 | | json.WriteBoolean(s_enabledPropertyNameBytes, Enabled.Value); |
| | 241 | |
|
| 0 | 242 | | json.WriteEndObject(); |
| | 243 | | } |
| 28 | 244 | | } |
| | 245 | |
|
| | 246 | | private void WriteCredentialsProperties(Utf8JsonWriter json) |
| | 247 | | { |
| 8 | 248 | | if (!string.IsNullOrEmpty(AccountId)) |
| | 249 | | { |
| 8 | 250 | | json.WriteString(s_accountIdPropertyNameBytes, AccountId); |
| | 251 | | } |
| | 252 | |
|
| 8 | 253 | | if (!string.IsNullOrEmpty(Password)) |
| | 254 | | { |
| 4 | 255 | | json.WriteString(s_passwordPropertyNameBytes, Password); |
| | 256 | | } |
| 8 | 257 | | } |
| | 258 | |
|
| | 259 | | private void WriteOrgDetailsProperties(Utf8JsonWriter json) |
| | 260 | | { |
| 10 | 261 | | if (!string.IsNullOrEmpty(OrganizationId)) |
| | 262 | | { |
| 4 | 263 | | json.WriteString(s_organizationIdPropertyNameBytes, AccountId); |
| | 264 | | } |
| | 265 | |
|
| 10 | 266 | | if (!_administratorContacts.IsNullOrEmpty()) |
| | 267 | | { |
| 6 | 268 | | json.WriteStartArray(s_adminDetailsPropertyNameBytes); |
| | 269 | |
|
| 24 | 270 | | foreach (AdministratorContact admin in _administratorContacts) |
| | 271 | | { |
| 6 | 272 | | json.WriteStartObject(); |
| | 273 | |
|
| 6 | 274 | | admin.WriteProperties(json); |
| | 275 | |
|
| 6 | 276 | | json.WriteEndObject(); |
| | 277 | | } |
| | 278 | |
|
| 6 | 279 | | json.WriteEndArray(); |
| | 280 | | } |
| 10 | 281 | | } |
| | 282 | |
|
| | 283 | | void IJsonDeserializable.ReadProperties(JsonElement json) |
| | 284 | | { |
| 344 | 285 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | 286 | | { |
| 136 | 287 | | ReadProperty(prop); |
| | 288 | | } |
| 36 | 289 | | } |
| | 290 | |
|
| 28 | 291 | | void IJsonSerializable.WriteProperties(Utf8JsonWriter json) => WriteProperties(json); |
| | 292 | | } |
| | 293 | | } |