| | 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 | | /// Options for certificates to be merged into Azure Key Vault. |
| | 14 | | /// </summary> |
| | 15 | | public class MergeCertificateOptions : IJsonSerializable |
| | 16 | | { |
| 2 | 17 | | private static readonly JsonEncodedText s_attributesPropertyNameBytes = JsonEncodedText.Encode("attributes"); |
| 2 | 18 | | private static readonly JsonEncodedText s_enabledPropertyNameBytes = JsonEncodedText.Encode("enabled"); |
| 2 | 19 | | private static readonly JsonEncodedText s_tagsPropertyNameBytes = JsonEncodedText.Encode("tags"); |
| 2 | 20 | | private static readonly JsonEncodedText s_x5cPropertyNameBytes = JsonEncodedText.Encode("x5c"); |
| | 21 | |
|
| | 22 | | private Dictionary<string, string> _tags; |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Initializes a new instance of the <see cref="MergeCertificateOptions"/> class. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="name">The name of the certificate.</param> |
| | 28 | | /// <param name="x509Certificates">The certificate or certificate chain to merge.</param> |
| | 29 | | /// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception> |
| | 30 | | /// <exception cref="ArgumentNullException"><paramref name="name"/> or <paramref name="x509Certificates"/> is nu |
| 4 | 31 | | public MergeCertificateOptions(string name, IEnumerable<byte[]> x509Certificates) |
| | 32 | | { |
| 4 | 33 | | Argument.AssertNotNullOrEmpty(name, nameof(name)); |
| 4 | 34 | | Argument.AssertNotNull(x509Certificates, nameof(x509Certificates)); |
| | 35 | |
|
| 4 | 36 | | Name = name; |
| 4 | 37 | | X509Certificates = x509Certificates; |
| 4 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Gets the name of the certificate. |
| | 42 | | /// </summary> |
| 8 | 43 | | public string Name { get; } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Gets the certificate or certificate chain to merge. |
| | 47 | | /// </summary> |
| 8 | 48 | | public IEnumerable<byte[]> X509Certificates { get; } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Gets or sets a value indicating whether the merged certificate should be enabled. If null, the server defaul |
| | 52 | | /// </summary> |
| 0 | 53 | | public bool? Enabled { get; set; } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Gets the tags to be applied to the merged certificate. Although this collection cannot be set, it can be mod |
| | 57 | | /// or initialized with a <see href="https://docs.microsoft.com/dotnet/csharp/programming-guide/classes-and-str |
| | 58 | | /// </summary> |
| 0 | 59 | | public IDictionary<string, string> Tags => LazyInitializer.EnsureInitialized(ref _tags); |
| | 60 | |
|
| | 61 | | void IJsonSerializable.WriteProperties(Utf8JsonWriter json) |
| | 62 | | { |
| 4 | 63 | | if (Enabled.HasValue) |
| | 64 | | { |
| 0 | 65 | | json.WriteStartObject(s_attributesPropertyNameBytes); |
| | 66 | |
|
| 0 | 67 | | json.WriteBoolean(s_enabledPropertyNameBytes, Enabled.Value); |
| | 68 | |
|
| 0 | 69 | | json.WriteEndObject(); |
| | 70 | | } |
| | 71 | |
|
| 4 | 72 | | if (!_tags.IsNullOrEmpty()) |
| | 73 | | { |
| 0 | 74 | | json.WriteStartObject(s_tagsPropertyNameBytes); |
| | 75 | |
|
| 0 | 76 | | foreach (KeyValuePair<string, string> kvp in _tags) |
| | 77 | | { |
| 0 | 78 | | json.WriteString(kvp.Key, kvp.Value); |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | json.WriteEndObject(); |
| | 82 | | } |
| | 83 | |
|
| 4 | 84 | | if (X509Certificates != null) |
| | 85 | | { |
| 4 | 86 | | json.WriteStartArray(s_x5cPropertyNameBytes); |
| | 87 | |
|
| 24 | 88 | | foreach (byte[] x509certificate in X509Certificates) |
| | 89 | | { |
| 8 | 90 | | json.WriteBase64StringValue(x509certificate); |
| | 91 | | } |
| | 92 | |
|
| 4 | 93 | | json.WriteEndArray(); |
| | 94 | | } |
| 4 | 95 | | } |
| | 96 | | } |
| | 97 | | } |