| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System.Text.Json; |
| | | 5 | | |
| | | 6 | | namespace Azure.Security.KeyVault.Certificates |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// An action to be executed at a prescribed time in a certificates lifecycle |
| | | 10 | | /// </summary> |
| | | 11 | | public class LifetimeAction : IJsonSerializable, IJsonDeserializable |
| | | 12 | | { |
| | | 13 | | private const string TriggerPropertyName = "trigger"; |
| | | 14 | | private const string ActionPropertyName = "action"; |
| | | 15 | | private const string LifetimePercentagePropertyName = "lifetime_percentage"; |
| | | 16 | | private const string DaysBeforeExpiryPropertyName = "days_before_expiry"; |
| | | 17 | | private const string ActionTypePropertyName = "action_type"; |
| | | 18 | | |
| | 2 | 19 | | private static readonly JsonEncodedText s_triggerPropertyNameBytes = JsonEncodedText.Encode(TriggerPropertyName) |
| | 2 | 20 | | private static readonly JsonEncodedText s_actionPropertyNameBytes = JsonEncodedText.Encode(ActionPropertyName); |
| | 2 | 21 | | private static readonly JsonEncodedText s_lifetimePercentagePropertyNameBytes = JsonEncodedText.Encode(LifetimeP |
| | 2 | 22 | | private static readonly JsonEncodedText s_daysBeforeExpiryPropertyNameBytes = JsonEncodedText.Encode(DaysBeforeE |
| | 2 | 23 | | private static readonly JsonEncodedText s_actionTypePropertyNameBytes = JsonEncodedText.Encode(ActionTypePropert |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes a new instance of the <see cref="LifetimeAction"/> class. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="action">The <see cref="CertificatePolicyAction"/> to be performed.</param> |
| | 0 | 29 | | public LifetimeAction(CertificatePolicyAction action) |
| | | 30 | | { |
| | 0 | 31 | | Action = action; |
| | 0 | 32 | | } |
| | | 33 | | |
| | 70 | 34 | | private LifetimeAction() |
| | | 35 | | { |
| | 70 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets or sets the action should be performed the specified number of days before the certificate will expire. |
| | | 40 | | /// </summary> |
| | 0 | 41 | | public int? DaysBeforeExpiry { get; set; } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets or sets the action should be performed when the certificate reaches the specified percentage of its lif |
| | | 45 | | /// </summary> |
| | 76 | 46 | | public int? LifetimePercentage { get; set; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets the <see cref="CertificatePolicyAction"/> to be performed. |
| | | 50 | | /// </summary> |
| | 74 | 51 | | public CertificatePolicyAction Action { get; private set; } |
| | | 52 | | |
| | | 53 | | internal static LifetimeAction FromJsonObject(JsonElement json) |
| | | 54 | | { |
| | 70 | 55 | | var action = new LifetimeAction(); |
| | | 56 | | |
| | 70 | 57 | | ((IJsonDeserializable)action).ReadProperties(json); |
| | | 58 | | |
| | 70 | 59 | | return action; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | void IJsonDeserializable.ReadProperties(JsonElement json) |
| | | 63 | | { |
| | 420 | 64 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | | 65 | | { |
| | 140 | 66 | | switch (prop.Name) |
| | | 67 | | { |
| | | 68 | | case TriggerPropertyName: |
| | 280 | 69 | | foreach (JsonProperty triggerProp in prop.Value.EnumerateObject()) |
| | | 70 | | { |
| | 70 | 71 | | switch (triggerProp.Name) |
| | | 72 | | { |
| | | 73 | | case LifetimePercentagePropertyName: |
| | 70 | 74 | | LifetimePercentage = triggerProp.Value.GetInt32(); |
| | 70 | 75 | | break; |
| | | 76 | | |
| | | 77 | | case DaysBeforeExpiryPropertyName: |
| | 0 | 78 | | DaysBeforeExpiry = triggerProp.Value.GetInt32(); |
| | | 79 | | break; |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | break; |
| | | 83 | | |
| | | 84 | | case ActionPropertyName: |
| | 70 | 85 | | Action = prop.Value.GetProperty(ActionTypePropertyName).GetString(); |
| | | 86 | | break; |
| | | 87 | | } |
| | | 88 | | } |
| | 70 | 89 | | } |
| | | 90 | | |
| | | 91 | | void IJsonSerializable.WriteProperties(Utf8JsonWriter json) |
| | | 92 | | { |
| | | 93 | | // trigger |
| | 2 | 94 | | json.WriteStartObject(s_triggerPropertyNameBytes); |
| | | 95 | | |
| | 2 | 96 | | if (DaysBeforeExpiry.HasValue) |
| | | 97 | | { |
| | 0 | 98 | | json.WriteNumber(s_daysBeforeExpiryPropertyNameBytes, DaysBeforeExpiry.Value); |
| | | 99 | | } |
| | | 100 | | |
| | 2 | 101 | | if (LifetimePercentage.HasValue) |
| | | 102 | | { |
| | 2 | 103 | | json.WriteNumber(s_lifetimePercentagePropertyNameBytes, LifetimePercentage.Value); |
| | | 104 | | } |
| | | 105 | | |
| | 2 | 106 | | json.WriteEndObject(); |
| | | 107 | | |
| | | 108 | | // action |
| | 2 | 109 | | json.WriteStartObject(s_actionPropertyNameBytes); |
| | | 110 | | |
| | 2 | 111 | | json.WriteString(s_actionTypePropertyNameBytes, Action.ToString()); |
| | | 112 | | |
| | 2 | 113 | | json.WriteEndObject(); |
| | | 114 | | |
| | 2 | 115 | | } |
| | | 116 | | } |
| | | 117 | | } |