| | 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.Keys |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// <see cref="KeyProperties"/> is the resource containing all the properties of the <see cref="KeyVaultKey"/> excep |
| | 15 | | /// </summary> |
| | 16 | | public class KeyProperties : IJsonDeserializable |
| | 17 | | { |
| | 18 | | private const string KeyIdPropertyName = "kid"; |
| | 19 | | private const string ManagedPropertyName = "managed"; |
| | 20 | | private const string AttributesPropertyName = "attributes"; |
| | 21 | | private const string TagsPropertyName = "tags"; |
| | 22 | |
|
| 0 | 23 | | private static readonly JsonEncodedText s_attributesPropertyNameBytes = JsonEncodedText.Encode(AttributesPropert |
| | 24 | |
|
| | 25 | | internal Dictionary<string, string> _tags; |
| | 26 | |
|
| 708 | 27 | | internal KeyProperties() { } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Initializes a new instance of the <see cref="KeyProperties"/> class. |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="name">The name of the key.</param> |
| | 33 | | /// <exception cref="ArgumentException"><paramref name="name"/> is an empty string.</exception> |
| | 34 | | /// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception> |
| 644 | 35 | | public KeyProperties(string name) |
| | 36 | | { |
| 644 | 37 | | Argument.AssertNotNullOrEmpty(name, nameof(name)); |
| | 38 | |
|
| 644 | 39 | | Name = name; |
| 644 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Initializes a new instance of the <see cref="KeyProperties"/> class. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="id">The identifier of the key.</param> |
| | 46 | | /// <exception cref="ArgumentNullException"><paramref name="id"/> is null.</exception> |
| 0 | 47 | | public KeyProperties(Uri id) |
| | 48 | | { |
| 0 | 49 | | Argument.AssertNotNull(id, nameof(id)); |
| | 50 | |
|
| 0 | 51 | | ParseId(id); |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | private KeyAttributes _attributes; |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Gets the name of the key. |
| | 58 | | /// </summary> |
| 2400 | 59 | | public string Name { get; internal set; } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Gets the key identifier. |
| | 63 | | /// </summary> |
| 1408 | 64 | | public Uri Id { get; internal set; } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Gets the Key Vault base <see cref="Uri"/>. |
| | 68 | | /// </summary> |
| 0 | 69 | | public Uri VaultUri { get; internal set; } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Gets the version of the key. |
| | 73 | | /// </summary> |
| 964 | 74 | | public string Version { get; internal set; } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Gets a value indicating whether the key's lifetime is managed by Key Vault. |
| | 78 | | /// If this key is backing a Key Vault certificate, the value will be true. |
| | 79 | | /// </summary> |
| 370 | 80 | | public bool Managed { get; internal set; } |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Gets a dictionary of tags with specific metadata about the key. |
| | 84 | | /// </summary> |
| 684 | 85 | | public IDictionary<string, string> Tags => LazyInitializer.EnsureInitialized(ref _tags); |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Gets or sets a value indicating whether the key is enabled and useable for cryptographic operations. |
| | 89 | | /// </summary> |
| 156 | 90 | | public bool? Enabled { get => _attributes.Enabled; set => _attributes.Enabled = value; } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Gets or sets a <see cref="DateTimeOffset"/> indicating when the key will be valid and can be used for crypto |
| | 94 | | /// </summary> |
| 632 | 95 | | public DateTimeOffset? NotBefore { get => _attributes.NotBefore; set => _attributes.NotBefore = value; } |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Gets or sets a <see cref="DateTimeOffset"/> indicating when the key will expire and cannot be used for crypt |
| | 99 | | /// </summary> |
| 682 | 100 | | public DateTimeOffset? ExpiresOn { get => _attributes.ExpiresOn; set => _attributes.ExpiresOn = value; } |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// Gets a <see cref="DateTimeOffset"/> indicating when the key was created. |
| | 104 | | /// </summary> |
| 0 | 105 | | public DateTimeOffset? CreatedOn { get => _attributes.CreatedOn; internal set => _attributes.CreatedOn = value; |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// Gets a <see cref="DateTimeOffset"/> indicating when the key was updated. |
| | 109 | | /// </summary> |
| 0 | 110 | | public DateTimeOffset? UpdatedOn { get => _attributes.UpdatedOn; internal set => _attributes.UpdatedOn = value; |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Gets the number of days a key is retained before being deleted for a soft delete-enabled Key Vault. |
| | 114 | | /// </summary> |
| 0 | 115 | | public int? RecoverableDays { get => _attributes.RecoverableDays; internal set => _attributes.RecoverableDays = |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// Gets the recovery level currently in effect for keys in the Key Vault. |
| | 119 | | /// If <c>Purgeable</c>, the key can be permanently deleted by an authorized user; |
| | 120 | | /// otherwise, only the service can purge the keys at the end of the retention interval. |
| | 121 | | /// </summary> |
| | 122 | | /// <value>Possible values include <c>Purgeable</c>, <c>Recoverable+Purgeable</c>, <c>Recoverable</c>, and <c>Re |
| 0 | 123 | | public string RecoveryLevel { get => _attributes.RecoveryLevel; internal set => _attributes.RecoveryLevel = valu |
| | 124 | |
|
| | 125 | | /// <summary> |
| | 126 | | /// Parses the key identifier into the <see cref="VaultUri"/>, <see cref="Name"/>, and <see cref="Version"/> of |
| | 127 | | /// </summary> |
| | 128 | | /// <param name="idToParse">The key vault object identifier.</param> |
| | 129 | | internal void ParseId(Uri idToParse) |
| | 130 | | { |
| | 131 | | // We expect an identifier with either 3 or 4 segments: host + collection + name [+ version] |
| 744 | 132 | | if (idToParse.Segments.Length != 3 && idToParse.Segments.Length != 4) |
| 0 | 133 | | throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ObjectIdentifier: {0}. |
| | 134 | |
|
| 744 | 135 | | if (!string.Equals(idToParse.Segments[1], "keys" + "/", StringComparison.OrdinalIgnoreCase)) |
| 0 | 136 | | throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ObjectIdentifier: {0}. |
| | 137 | |
|
| 744 | 138 | | Id = idToParse; |
| 744 | 139 | | VaultUri = new Uri($"{idToParse.Scheme}://{idToParse.Authority}"); |
| 744 | 140 | | Name = idToParse.Segments[2].Trim('/'); |
| 744 | 141 | | Version = (idToParse.Segments.Length == 4) ? idToParse.Segments[3].TrimEnd('/') : null; |
| 744 | 142 | | } |
| | 143 | |
|
| | 144 | | internal void ReadProperty(JsonProperty prop) |
| | 145 | | { |
| 800 | 146 | | switch (prop.Name) |
| | 147 | | { |
| | 148 | | case KeyIdPropertyName: |
| 68 | 149 | | string id = prop.Value.GetString(); |
| 68 | 150 | | Id = new Uri(id); |
| 68 | 151 | | ParseId(Id); |
| 68 | 152 | | break; |
| | 153 | | case ManagedPropertyName: |
| 4 | 154 | | Managed = prop.Value.GetBoolean(); |
| 4 | 155 | | break; |
| | 156 | | case AttributesPropertyName: |
| 692 | 157 | | _attributes.ReadProperties(prop.Value); |
| 692 | 158 | | break; |
| | 159 | | case TagsPropertyName: |
| 216 | 160 | | foreach (JsonProperty tagProp in prop.Value.EnumerateObject()) |
| | 161 | | { |
| 72 | 162 | | Tags[tagProp.Name] = tagProp.Value.GetString(); |
| | 163 | | } |
| | 164 | | break; |
| | 165 | | } |
| 36 | 166 | | } |
| | 167 | |
|
| | 168 | | internal void ReadProperties(JsonElement json) |
| | 169 | | { |
| 304 | 170 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | 171 | | { |
| 100 | 172 | | ReadProperty(prop); |
| | 173 | | } |
| 52 | 174 | | } |
| | 175 | |
|
| | 176 | | internal void WriteAttributes(Utf8JsonWriter json) |
| | 177 | | { |
| 36 | 178 | | if (_attributes.ShouldSerialize) |
| | 179 | | { |
| 0 | 180 | | json.WriteStartObject(s_attributesPropertyNameBytes); |
| | 181 | |
|
| 0 | 182 | | _attributes.WriteProperties(json); |
| | 183 | |
|
| 0 | 184 | | json.WriteEndObject(); |
| | 185 | | } |
| 36 | 186 | | } |
| | 187 | |
|
| 52 | 188 | | void IJsonDeserializable.ReadProperties(JsonElement json) => ReadProperties(json); |
| | 189 | | } |
| | 190 | | } |