| | | 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.Secrets |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// <see cref="SecretProperties"/> is the resource containing all the properties of the secret except its value. |
| | | 14 | | /// </summary> |
| | | 15 | | public class SecretProperties : IJsonDeserializable, IJsonSerializable |
| | | 16 | | { |
| | | 17 | | private const string IdPropertyName = "id"; |
| | | 18 | | private const string ContentTypePropertyName = "contentType"; |
| | | 19 | | private const string KidPropertyName = "kid"; |
| | | 20 | | private const string ManagedPropertyName = "managed"; |
| | | 21 | | private const string AttributesPropertyName = "attributes"; |
| | | 22 | | private const string TagsPropertyName = "tags"; |
| | | 23 | | |
| | 4 | 24 | | private static readonly JsonEncodedText s_contentTypePropertyNameBytes = JsonEncodedText.Encode(ContentTypePrope |
| | 4 | 25 | | private static readonly JsonEncodedText s_attributesPropertyNameBytes = JsonEncodedText.Encode(AttributesPropert |
| | 4 | 26 | | private static readonly JsonEncodedText s_tagsPropertyNameBytes = JsonEncodedText.Encode(TagsPropertyName); |
| | | 27 | | |
| | | 28 | | private ObjectId _identifier; |
| | | 29 | | private SecretAttributes _attributes; |
| | | 30 | | private Dictionary<string, string> _tags; |
| | | 31 | | private string _keyId; |
| | | 32 | | |
| | 1620 | 33 | | internal SecretProperties() |
| | | 34 | | { |
| | 1620 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Initializes a new instance of the <see cref="SecretProperties"/> class. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="name">The name of the secret.</param> |
| | | 41 | | /// <exception cref="ArgumentException"><paramref name="name"/> is an empty string.</exception> |
| | | 42 | | /// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception> |
| | 744 | 43 | | public SecretProperties(string name) |
| | | 44 | | { |
| | 744 | 45 | | Argument.AssertNotNullOrEmpty(name, nameof(name)); |
| | | 46 | | |
| | 736 | 47 | | _identifier.Name = name; |
| | 736 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Initializes a new instance of the <see cref="SecretProperties"/> class. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="id">The identifier of the secret.</param> |
| | | 54 | | /// <exception cref="ArgumentNullException"><paramref name="id"/> is null.</exception> |
| | 0 | 55 | | public SecretProperties(Uri id) |
| | | 56 | | { |
| | 0 | 57 | | Argument.AssertNotNull(id, nameof(id)); |
| | | 58 | | |
| | 0 | 59 | | _identifier.ParseId("secrets", id); |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets the secret identifier. |
| | | 64 | | /// </summary> |
| | 0 | 65 | | public Uri Id { get => _identifier.Id; internal set => _identifier.Id = value; } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets the Key Vault base <see cref="Uri"/>. |
| | | 69 | | /// </summary> |
| | 0 | 70 | | public Uri VaultUri { get => _identifier.VaultUri; internal set => _identifier.VaultUri = value; } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Gets the name of the secret. |
| | | 74 | | /// </summary> |
| | 0 | 75 | | public string Name { get => _identifier.Name; internal set => _identifier.Name = value; } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets the version of the secret. |
| | | 79 | | /// </summary> |
| | 0 | 80 | | public string Version { get => _identifier.Version; internal set => _identifier.Version = value; } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets or sets the content type of the secret value such as "text/plain" for a password. |
| | | 84 | | /// </summary> |
| | 2072 | 85 | | public string ContentType { get; set; } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Gets a value indicating whether the secret's lifetime is managed by Key Vault. |
| | | 89 | | /// If this secret is backing a Key Vault certificate, the value will be true. |
| | | 90 | | /// </summary> |
| | 1298 | 91 | | public bool Managed { get; internal set; } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets the key identifier of a key backing a Key Vault certificate if this secret is backing a Key Vault certi |
| | | 95 | | /// </summary> |
| | | 96 | | public Uri KeyId |
| | | 97 | | { |
| | 1288 | 98 | | get => _keyId is null ? null : new Uri(_keyId); |
| | 0 | 99 | | internal set => _keyId = value?.ToString(); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Gets or sets a value indicating whether the secret is enabled and useable. |
| | | 104 | | /// </summary> |
| | 1296 | 105 | | public bool? Enabled { get => _attributes.Enabled; set => _attributes.Enabled = value; } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Gets or sets a <see cref="DateTimeOffset"/> indicating when the secret will be valid and can be used. |
| | | 109 | | /// </summary> |
| | 1296 | 110 | | public DateTimeOffset? NotBefore { get => _attributes.NotBefore; set => _attributes.NotBefore = value; } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Gets or sets a <see cref="DateTimeOffset"/> indicating when the secret will expire and cannot be used. |
| | | 114 | | /// </summary> |
| | 1300 | 115 | | public DateTimeOffset? ExpiresOn { get => _attributes.ExpiresOn; set => _attributes.ExpiresOn = value; } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Gets a <see cref="DateTimeOffset"/> indicating when the secret was created. |
| | | 119 | | /// </summary> |
| | 0 | 120 | | public DateTimeOffset? CreatedOn { get => _attributes.CreatedOn; internal set => _attributes.CreatedOn = value; |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Gets a <see cref="DateTimeOffset"/> indicating when the secret was updated. |
| | | 124 | | /// </summary> |
| | 0 | 125 | | public DateTimeOffset? UpdatedOn { get => _attributes.UpdatedOn; internal set => _attributes.UpdatedOn = value; |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Gets the number of days a secret is retained before being deleted for a soft delete-enabled Key Vault. |
| | | 129 | | /// </summary> |
| | 0 | 130 | | public int? RecoverableDays { get => _attributes.RecoverableDays; internal set => _attributes.RecoverableDays = |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Gets the recovery level currently in effect for secrets in the Key Vault. |
| | | 134 | | /// If <c>Purgeable</c>, the secret can be permanently deleted by an authorized user; |
| | | 135 | | /// otherwise, only the service can purge the secrets at the end of the retention interval. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <value>Possible values include <c>Purgeable</c>, <c>Recoverable+Purgeable</c>, <c>Recoverable</c>, and <c>Re |
| | 0 | 138 | | public string RecoveryLevel { get => _attributes.RecoveryLevel; internal set => _attributes.RecoveryLevel = valu |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Gets a dictionary of tags with specific metadata about the secret. |
| | | 142 | | /// </summary> |
| | 104 | 143 | | public IDictionary<string, string> Tags => LazyInitializer.EnsureInitialized(ref _tags); |
| | | 144 | | |
| | | 145 | | internal void ReadProperties(JsonElement json) |
| | | 146 | | { |
| | 2624 | 147 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | | 148 | | { |
| | 876 | 149 | | ReadProperty(prop); |
| | | 150 | | } |
| | 436 | 151 | | } |
| | | 152 | | |
| | | 153 | | internal void ReadProperty(JsonProperty prop) |
| | | 154 | | { |
| | 3148 | 155 | | switch (prop.Name) |
| | | 156 | | { |
| | | 157 | | case IdPropertyName: |
| | 1544 | 158 | | _identifier.ParseId("secrets", prop.Value.GetString()); |
| | 1544 | 159 | | break; |
| | | 160 | | |
| | | 161 | | case ContentTypePropertyName: |
| | 16 | 162 | | ContentType = prop.Value.GetString(); |
| | 16 | 163 | | break; |
| | | 164 | | |
| | | 165 | | case KidPropertyName: |
| | 12 | 166 | | _keyId = prop.Value.GetString(); |
| | 12 | 167 | | break; |
| | | 168 | | |
| | | 169 | | case ManagedPropertyName: |
| | 4 | 170 | | Managed = prop.Value.GetBoolean(); |
| | 4 | 171 | | break; |
| | | 172 | | |
| | | 173 | | case AttributesPropertyName: |
| | 1548 | 174 | | _attributes.ReadProperties(prop.Value); |
| | 1548 | 175 | | break; |
| | | 176 | | |
| | | 177 | | case TagsPropertyName: |
| | 144 | 178 | | foreach (JsonProperty tag in prop.Value.EnumerateObject()) |
| | | 179 | | { |
| | 48 | 180 | | Tags[tag.Name] = tag.Value.GetString(); |
| | | 181 | | } |
| | | 182 | | break; |
| | | 183 | | } |
| | 24 | 184 | | } |
| | | 185 | | |
| | | 186 | | internal void WriteProperties(Utf8JsonWriter json) |
| | | 187 | | { |
| | 748 | 188 | | if (ContentType != null) |
| | | 189 | | { |
| | 8 | 190 | | json.WriteString(s_contentTypePropertyNameBytes, ContentType); |
| | | 191 | | } |
| | | 192 | | |
| | 748 | 193 | | if (_attributes.Enabled.HasValue || _attributes.NotBefore.HasValue || _attributes.ExpiresOn.HasValue) |
| | | 194 | | { |
| | 24 | 195 | | json.WriteStartObject(s_attributesPropertyNameBytes); |
| | | 196 | | |
| | 24 | 197 | | _attributes.WriteProperties(json); |
| | | 198 | | |
| | 24 | 199 | | json.WriteEndObject(); |
| | | 200 | | } |
| | | 201 | | |
| | 748 | 202 | | if (!_tags.IsNullOrEmpty()) |
| | | 203 | | { |
| | 16 | 204 | | json.WriteStartObject(s_tagsPropertyNameBytes); |
| | | 205 | | |
| | 96 | 206 | | foreach (KeyValuePair<string, string> kvp in _tags) |
| | | 207 | | { |
| | 32 | 208 | | json.WriteString(kvp.Key, kvp.Value); |
| | | 209 | | } |
| | | 210 | | |
| | 16 | 211 | | json.WriteEndObject(); |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | // KeyId is read-only don't serialize |
| | | 215 | | |
| | | 216 | | // Managed is read-only don't serialize |
| | 748 | 217 | | } |
| | | 218 | | |
| | 436 | 219 | | void IJsonDeserializable.ReadProperties(JsonElement json) => ReadProperties(json); |
| | | 220 | | |
| | 20 | 221 | | void IJsonSerializable.WriteProperties(Utf8JsonWriter json) => WriteProperties(json); |
| | | 222 | | } |
| | | 223 | | } |