< Summary

Class:Azure.Security.KeyVault.Certificates.ImportCertificateOptions
Assembly:Azure.Security.KeyVault.Certificates
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Certificates\src\ImportCertificateOptions.cs
Covered lines:28
Uncovered lines:11
Coverable lines:39
Total lines:125
Line coverage:71.7% (28 of 39)
Covered branches:13
Total branches:20
Branch coverage:65% (13 of 20)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-100%100%
.ctor(...)-100%100%
get_Name()-100%100%
get_Certificate()-100%100%
get_Policy()-100%100%
get_Password()-0%100%
get_Enabled()-0%100%
get_Tags()-0%100%
Azure.Security.KeyVault.IJsonSerializable.WriteProperties(...)-61.9%65%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Certificates\src\ImportCertificateOptions.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Text;
 7using System.Text.Json;
 8using System.Threading;
 9using Azure.Core;
 10
 11namespace Azure.Security.KeyVault.Certificates
 12{
 13    /// <summary>
 14    /// Options for a certificate to be imported into Azure Key Vault.
 15    /// </summary>
 16    public class ImportCertificateOptions : IJsonSerializable
 17    {
 218        private static readonly JsonEncodedText s_valuePropertyNameBytes = JsonEncodedText.Encode("value");
 219        private static readonly JsonEncodedText s_policyPropertyNameBytes = JsonEncodedText.Encode("policy");
 220        private static readonly JsonEncodedText s_passwordPropertyNameBytes = JsonEncodedText.Encode("pwd");
 221        private static readonly JsonEncodedText s_attributesPropertyNameBytes = JsonEncodedText.Encode("attributes");
 222        private static readonly JsonEncodedText s_enabledPropertyNameBytes = JsonEncodedText.Encode("enabled");
 223        private static readonly JsonEncodedText s_tagsPropertyNameBytes = JsonEncodedText.Encode("tags");
 24
 25        private Dictionary<string, string> _tags;
 26
 27        /// <summary>
 28        /// Initializes a new instance of the <see cref="ImportCertificateOptions"/> class.
 29        /// </summary>
 30        /// <param name="name">A name for the imported certificate.</param>
 31        /// <param name="certificate">The PFX or ASCII PEM formatted value of the certificate containing both the X.509 
 32        /// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
 33        /// <exception cref="ArgumentNullException"><paramref name="name"/> or <paramref name="certificate"/> is null.</
 834        public ImportCertificateOptions(string name, byte[] certificate)
 35        {
 836            Argument.AssertNotNullOrEmpty(name, nameof(name));
 837            Argument.AssertNotNull(certificate, nameof(certificate));
 38
 839            Name = name;
 840            Certificate = certificate;
 841        }
 42
 43        /// <summary>
 44        /// Gets the name of the certificate to import.
 45        /// </summary>
 2446        public string Name { get; }
 47
 48        /// <summary>
 49        /// Gets the PFX or PEM formatted value of the certificate containing both the X.509 certificates and the privat
 50        /// </summary>
 1651        public byte[] Certificate { get; }
 52
 53        /// <summary>
 54        /// Gets the policy which governs the lifecycle of the imported certificate and its properties when it is rotate
 55        /// </summary>
 4056        public CertificatePolicy Policy { get; set; }
 57
 58        /// <summary>
 59        /// Gets or sets the password protecting the certificate specified in the Value.
 60        /// </summary>
 061        public string Password { get; set; }
 62
 63        /// <summary>
 64        /// Gets or sets a value indicating whether the merged certificate should be enabled. If null, the server defaul
 65        /// </summary>
 066        public bool? Enabled { get; set; }
 67
 68        /// <summary>
 69        /// Gets the tags to be applied to the imported certificate. Although this collection cannot be set, it can be m
 70        ///  or initialized with a <see href="https://docs.microsoft.com/dotnet/csharp/programming-guide/classes-and-str
 71        /// </summary>
 072        public IDictionary<string, string> Tags => LazyInitializer.EnsureInitialized(ref _tags);
 73
 74        void IJsonSerializable.WriteProperties(Utf8JsonWriter json)
 75        {
 876            if (Certificate != null)
 77            {
 878                if (Policy != null && Policy.ContentType == CertificateContentType.Pem)
 79                {
 480                    string value = Encoding.ASCII.GetString(Certificate);
 481                    json.WriteString(s_valuePropertyNameBytes, value);
 82                }
 83                else
 84                {
 485                    json.WriteBase64String(s_valuePropertyNameBytes, Certificate);
 86                }
 87            }
 88
 889            if (!string.IsNullOrEmpty(Password))
 90            {
 091                json.WriteString(s_passwordPropertyNameBytes, Password);
 92            }
 93
 894            if (Policy != null)
 95            {
 896                json.WriteStartObject(s_policyPropertyNameBytes);
 97
 898                ((IJsonSerializable)Policy).WriteProperties(json);
 99
 8100                json.WriteEndObject();
 101            }
 102
 8103            if (Enabled.HasValue)
 104            {
 0105                json.WriteStartObject(s_attributesPropertyNameBytes);
 106
 0107                json.WriteBoolean(s_enabledPropertyNameBytes, Enabled.Value);
 108
 0109                json.WriteEndObject();
 110            }
 111
 8112            if (!_tags.IsNullOrEmpty())
 113            {
 0114                json.WriteStartObject(s_tagsPropertyNameBytes);
 115
 0116                foreach (KeyValuePair<string, string> kvp in _tags)
 117                {
 0118                    json.WriteString(kvp.Key, kvp.Value);
 119                }
 120
 0121                json.WriteEndObject();
 122            }
 8123        }
 124    }
 125}