| | 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.Globalization; |
| | 7 | | using System.Text.Json; |
| | 8 | | using System.Threading; |
| | 9 | | using Azure.Core; |
| | 10 | |
|
| | 11 | | namespace Azure.Security.KeyVault.Certificates |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// <see cref="CertificateProperties"/> contains identity and other basic properties of a <see cref="KeyVaultCertifi |
| | 15 | | /// </summary> |
| | 16 | | public class CertificateProperties : IJsonDeserializable |
| | 17 | | { |
| | 18 | | private const string IdPropertyName = "id"; |
| | 19 | | private const string X509ThumprintPropertyName = "x5t"; |
| | 20 | | private const string TagsPropertyName = "tags"; |
| | 21 | | private const string AttributesPropertyName = "attributes"; |
| | 22 | |
|
| | 23 | | private CertificateAttributes _attributes; |
| | 24 | | private Dictionary<string, string> _tags; |
| | 25 | |
|
| 82 | 26 | | internal CertificateProperties() |
| | 27 | | { |
| 82 | 28 | | } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Initializes a new instance of the <see cref="CertificateProperties"/> class. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="name">The name of the certificate.</param> |
| | 34 | | /// <exception cref="ArgumentException"><paramref name="name"/> is an empty string.</exception> |
| | 35 | | /// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception> |
| 0 | 36 | | public CertificateProperties(string name) |
| | 37 | | { |
| 0 | 38 | | Argument.AssertNotNullOrEmpty(name, nameof(name)); |
| | 39 | |
|
| 0 | 40 | | Name = name; |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Initializes a new instance of the <see cref="CertificateProperties"/> class. |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="id">The identifier of the certificate.</param> |
| | 47 | | /// <exception cref="ArgumentNullException"><paramref name="id"/> is null.</exception> |
| 0 | 48 | | public CertificateProperties(Uri id) |
| | 49 | | { |
| 0 | 50 | | Argument.AssertNotNull(id, nameof(id)); |
| | 51 | |
|
| 0 | 52 | | Id = id; |
| 0 | 53 | | ParseId(id); |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Gets the identifier of the certificate. |
| | 58 | | /// </summary> |
| 152 | 59 | | public Uri Id { get; internal set; } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Gets the name of the certificate. |
| | 63 | | /// </summary> |
| 124 | 64 | | public string Name { get; internal set; } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Gets the <see cref="Uri"/> of the vault in which the certificate is stored. |
| | 68 | | /// </summary> |
| 0 | 69 | | public Uri VaultUri { get; internal set; } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Gets the version of the certificate. |
| | 73 | | /// </summary> |
| 104 | 74 | | public string Version { get; internal set; } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Gets the digital thumbprint of the certificate which can be used to uniquely identify it. |
| | 78 | | /// </summary> |
| 0 | 79 | | public byte[] X509Thumbprint { get; internal set; } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Gets the tags applied to the certificate. |
| | 83 | | /// </summary> |
| 40 | 84 | | public IDictionary<string, string> Tags => LazyInitializer.EnsureInitialized(ref _tags); |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Gets or sets a value indicating whether the certificate is currently enabled. If null, the server default wi |
| | 88 | | /// </summary> |
| 32 | 89 | | public bool? Enabled { get => _attributes.Enabled; set => _attributes.Enabled = value; } |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Gets a <see cref="DateTimeOffset"/> indicating when the certificate will be valid. |
| | 93 | | /// </summary> |
| 0 | 94 | | public DateTimeOffset? NotBefore { get => _attributes.NotBefore; internal set => _attributes.NotBefore = value; |
| | 95 | |
|
| | 96 | | /// <summary> |
| | 97 | | /// Gets a <see cref="DateTimeOffset"/> indicating when the certificate will expire. |
| | 98 | | /// </summary> |
| 0 | 99 | | public DateTimeOffset? ExpiresOn { get => _attributes.ExpiresOn; internal set => _attributes.ExpiresOn = value; |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Gets a <see cref="DateTimeOffset"/> indicating when the certificate was created. |
| | 103 | | /// </summary> |
| 0 | 104 | | public DateTimeOffset? CreatedOn { get => _attributes.CreatedOn; internal set => _attributes.CreatedOn = value; |
| | 105 | |
|
| | 106 | | /// <summary> |
| | 107 | | /// Gets a <see cref="DateTimeOffset"/> indicating when the certificate was updated. |
| | 108 | | /// </summary> |
| 0 | 109 | | public DateTimeOffset? UpdatedOn { get => _attributes.UpdatedOn; internal set => _attributes.UpdatedOn = value; |
| | 110 | |
|
| | 111 | | /// <summary> |
| | 112 | | /// Gets the number of days a certificate is retained before being deleted for a soft delete-enabled Key Vault. |
| | 113 | | /// </summary> |
| 0 | 114 | | public int? RecoverableDays { get => _attributes.RecoverableDays; internal set => _attributes.RecoverableDays = |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Gets the recovery level currently in effect for certificates in the Key Vault. |
| | 118 | | /// If <c>Purgeable</c>, the certificates can be permanently deleted by an authorized user; |
| | 119 | | /// otherwise, only the service can purge the certificates at the end of the retention interval. |
| | 120 | | /// </summary> |
| | 121 | | /// <value>Possible values include <c>Purgeable</c>, <c>Recoverable+Purgeable</c>, <c>Recoverable</c>, and <c>Re |
| 0 | 122 | | public string RecoveryLevel { get => _attributes.RecoveryLevel; internal set => _attributes.RecoveryLevel = valu |
| | 123 | |
|
| 8 | 124 | | internal bool HasTags => _tags != null; |
| | 125 | |
|
| | 126 | | void IJsonDeserializable.ReadProperties(JsonElement json) |
| | 127 | | { |
| 32 | 128 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | 129 | | { |
| 10 | 130 | | ReadProperty(prop); |
| | 131 | | } |
| 6 | 132 | | } |
| | 133 | |
|
| | 134 | | internal void ReadProperty(JsonProperty prop) |
| | 135 | | { |
| 298 | 136 | | switch (prop.Name) |
| | 137 | | { |
| | 138 | | case IdPropertyName: |
| 76 | 139 | | var id = prop.Value.GetString(); |
| 76 | 140 | | Id = new Uri(id); |
| 76 | 141 | | ParseId(Id); |
| 76 | 142 | | break; |
| | 143 | |
|
| | 144 | | case X509ThumprintPropertyName: |
| 68 | 145 | | X509Thumbprint = Base64Url.Decode(prop.Value.GetString()); |
| 68 | 146 | | break; |
| | 147 | |
|
| | 148 | | case TagsPropertyName: |
| 32 | 149 | | foreach (JsonProperty tagProp in prop.Value.EnumerateObject()) |
| | 150 | | { |
| 8 | 151 | | Tags[tagProp.Name] = tagProp.Value.GetString(); |
| | 152 | | } |
| | 153 | | break; |
| | 154 | |
|
| | 155 | | case AttributesPropertyName: |
| 80 | 156 | | _attributes.ReadProperties(prop.Value); |
| | 157 | | break; |
| | 158 | | } |
| 88 | 159 | | } |
| | 160 | |
|
| | 161 | | private void ParseId(Uri idToParse) |
| | 162 | | { |
| | 163 | | // We expect an identifier with either 3 or 4 segments: host + collection + name [+ version] |
| 76 | 164 | | if (idToParse.Segments.Length != 3 && idToParse.Segments.Length != 4) |
| 0 | 165 | | throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ObjectIdentifier: {0}. |
| | 166 | |
|
| 76 | 167 | | if (!string.Equals(idToParse.Segments[1], "certificates" + "/", StringComparison.OrdinalIgnoreCase)) |
| 0 | 168 | | throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ObjectIdentifier: {0}. |
| | 169 | |
|
| 76 | 170 | | VaultUri = new Uri($"{idToParse.Scheme}://{idToParse.Authority}"); |
| 76 | 171 | | Name = idToParse.Segments[2].Trim('/'); |
| 76 | 172 | | Version = (idToParse.Segments.Length == 4) ? idToParse.Segments[3].TrimEnd('/') : null; |
| 76 | 173 | | } |
| | 174 | |
|
| | 175 | | } |
| | 176 | | } |