< Summary

Class:Azure.Security.KeyVault.Certificates.CertificateIssuer
Assembly:Azure.Security.KeyVault.Certificates
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Certificates\src\CertificateIssuer.cs
Covered lines:93
Uncovered lines:5
Coverable lines:98
Total lines:293
Line coverage:94.8% (93 of 98)
Covered branches:49
Total branches:52
Branch coverage:94.2% (49 of 52)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-100%100%
.ctor(...)-100%100%
.ctor(...)-100%100%
.ctor(...)-100%100%
get_Id()-100%100%
get_Name()-100%100%
get_Provider()-100%100%
get_AccountId()-100%100%
get_Password()-100%100%
get_OrganizationId()-100%100%
get_AdministratorContacts()-100%100%
get_CreatedOn()-0%100%
get_UpdatedOn()-100%100%
get_Enabled()-100%100%
ReadProperty(...)-100%100%
ReadCredentialsProperties(...)-83.33%66.67%
ReadOrgDetailsProperties(...)-100%100%
ReadAttributeProperties(...)-100%100%
WriteProperties(...)-78.57%90%
WriteCredentialsProperties(...)-100%100%
WriteOrgDetailsProperties(...)-100%100%
Azure.Security.KeyVault.IJsonDeserializable.ReadProperties(...)-100%100%
Azure.Security.KeyVault.IJsonSerializable.WriteProperties(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Certificates\src\CertificateIssuer.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.Json;
 7using System.Threading;
 8using Azure.Core;
 9
 10namespace Azure.Security.KeyVault.Certificates
 11{
 12    /// <summary>
 13    /// A certificate issuer used to sign certificates managed by Azure Key Vault.
 14    /// </summary>
 15    public class CertificateIssuer : IJsonDeserializable, IJsonSerializable
 16    {
 17        private const string CredentialsPropertyName = "credentials";
 18        private const string OrgDetailsPropertyName = "org_details";
 19        private const string AttributesPropertyName = "attributes";
 20        private const string AccountIdPropertyName = "account_id";
 21        private const string PasswordPropertyName = "pwd";
 22        private const string OrganizationIdPropertyName = "id";
 23        private const string AdminDetailsPropertyName = "admin_details";
 24        private const string CreatedPropertyName = "created";
 25        private const string UpdatedPropertyName = "updated";
 26        private const string EnabledPropertyName = "enabled";
 27
 228        private static readonly JsonEncodedText s_credentialsPropertyNameBytes = JsonEncodedText.Encode(CredentialsPrope
 229        private static readonly JsonEncodedText s_orgDetailsPropertyNameBytes = JsonEncodedText.Encode(OrgDetailsPropert
 230        private static readonly JsonEncodedText s_attributesPropertyNameBytes = JsonEncodedText.Encode(AttributesPropert
 231        private static readonly JsonEncodedText s_enabledPropertyNameBytes = JsonEncodedText.Encode(EnabledPropertyName)
 232        private static readonly JsonEncodedText s_accountIdPropertyNameBytes = JsonEncodedText.Encode(AccountIdPropertyN
 233        private static readonly JsonEncodedText s_passwordPropertyNameBytes = JsonEncodedText.Encode(PasswordPropertyNam
 234        private static readonly JsonEncodedText s_organizationIdPropertyNameBytes = JsonEncodedText.Encode(OrganizationI
 235        private static readonly JsonEncodedText s_adminDetailsPropertyNameBytes = JsonEncodedText.Encode(AdminDetailsPro
 36
 37        private List<AdministratorContact> _administratorContacts;
 38        private IssuerProperties _properties;
 39
 4440        internal CertificateIssuer(IssuerProperties properties = null)
 41        {
 4442            _properties = properties ?? new IssuerProperties();
 4443        }
 44
 45        /// <summary>
 46        /// Initializes a new instance of the <see cref="CertificateIssuer"/> class.
 47        /// You can use this constructor to initialize a <see cref="CertificateIssuer"/> for
 48        /// <see cref="CertificateClient.UpdateIssuer(CertificateIssuer, CancellationToken)"/> or
 49        /// <see cref="CertificateClient.UpdateIssuerAsync(CertificateIssuer, CancellationToken)"/>.
 50        /// </summary>
 51        /// <param name="name">The name of the issuer, including values from <see cref="WellKnownIssuerNames"/>.</param>
 52        /// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
 53        /// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
 854        public CertificateIssuer(string name)
 55        {
 856            Argument.AssertNotNullOrEmpty(name, nameof(name));
 57
 458            _properties = new IssuerProperties(name);
 459        }
 60
 61        /// <summary>
 62        /// Initializes a new instance of the <see cref="CertificateIssuer"/> class.
 63        /// You can use this constructor to initialize a <see cref="CertificateIssuer"/> for
 64        /// <see cref="CertificateClient.CreateIssuer(CertificateIssuer, CancellationToken)"/> or
 65        /// <see cref="CertificateClient.CreateIssuerAsync(CertificateIssuer, CancellationToken)"/>.
 66        /// </summary>
 67        /// <param name="name">The name of the issuer, including values from <see cref="WellKnownIssuerNames"/>.</param>
 68        /// <param name="provider">The provider name of the certificate issuer.</param>
 69        /// <exception cref="ArgumentException"><paramref name="name"/> or <paramref name="provider"/> is empty.</except
 70        /// <exception cref="ArgumentNullException"><paramref name="name"/> or <paramref name="provider"/> is null.</exc
 3271        public CertificateIssuer(string name, string provider)
 72        {
 3273            Argument.AssertNotNullOrEmpty(name, nameof(name));
 3274            Argument.AssertNotNullOrEmpty(provider, nameof(provider));
 75
 2876            _properties = new IssuerProperties(name)
 2877            {
 2878                Provider = provider,
 2879            };
 2880        }
 81
 82        /// <summary>
 83        /// Gets the unique identifier of the certificate issuer.
 84        /// </summary>
 685        public Uri Id => _properties.Id;
 86
 87        /// <summary>
 88        /// Gets the name of the certificate issuer.
 89        /// </summary>
 8690        public string Name => _properties.Name;
 91
 92        /// <summary>
 93        /// Gets or sets the provider name of the certificate issuer.
 94        /// </summary>
 3695        public string Provider => _properties.Provider;
 96
 97        /// <summary>
 98        /// Gets or sets the account identifier or username used to authenticate to the certificate issuer.
 99        /// </summary>
 74100        public string AccountId { get; set; }
 101
 102        /// <summary>
 103        /// Gets or sets the password or key used to authenticate to the certificate issuer.
 104        /// </summary>
 36105        public string Password { get; set; }
 106
 107        /// <summary>
 108        /// Gets or sets the organizational identifier for the issuer.
 109        /// </summary>
 50110        public string OrganizationId { get; set; }
 111
 112        /// <summary>
 113        /// Gets a list of contacts who administer the certificate issuer account.
 114        /// </summary>
 42115        public IList<AdministratorContact> AdministratorContacts => LazyInitializer.EnsureInitialized(ref _administrator
 116
 117        /// <summary>
 118        /// Gets a <see cref="DateTimeOffset"/> indicating when the certificate was created.
 119        /// </summary>
 0120        public DateTimeOffset? CreatedOn { get; internal set; }
 121
 122        /// <summary>
 123        /// Gets a <see cref="DateTimeOffset"/> indicating when the certificate was updated.
 124        /// </summary>
 38125        public DateTimeOffset? UpdatedOn { get; internal set; }
 126
 127        /// <summary>
 128        /// Gets or sets a value indicating whether the issuer can currently be used to issue certificates. If null, the
 129        /// </summary>
 62130        public bool? Enabled { get; set; }
 131
 132        internal virtual void ReadProperty(JsonProperty prop)
 133        {
 136134            switch (prop.Name)
 135            {
 136                case CredentialsPropertyName:
 12137                    ReadCredentialsProperties(prop.Value);
 12138                    break;
 139
 140                case OrgDetailsPropertyName:
 20141                    ReadOrgDetailsProperties(prop.Value);
 20142                    break;
 143
 144                case AttributesPropertyName:
 34145                    ReadAttributeProperties(prop.Value);
 34146                    break;
 147
 148                default:
 70149                    _properties.ReadProperty(prop);
 150                    break;
 151            }
 70152        }
 153
 154        private void ReadCredentialsProperties(JsonElement json)
 155        {
 48156            foreach (JsonProperty prop in json.EnumerateObject())
 157            {
 12158                switch (prop.Name)
 159                {
 160                    case AccountIdPropertyName:
 12161                        AccountId = prop.Value.GetString();
 12162                        break;
 163
 164                    case PasswordPropertyName:
 0165                        Password = prop.Value.GetString();
 166                        break;
 167                }
 168            }
 12169        }
 170
 171        private void ReadOrgDetailsProperties(JsonElement json)
 172        {
 116173            foreach (JsonProperty prop in json.EnumerateObject())
 174            {
 38175                switch (prop.Name)
 176                {
 177                    case OrganizationIdPropertyName:
 8178                        OrganizationId = prop.Value.GetString();
 8179                        break;
 180
 181                    case AdminDetailsPropertyName:
 48182                        foreach (JsonElement elem in prop.Value.EnumerateArray())
 183                        {
 12184                            var admin = new AdministratorContact();
 12185                            admin.ReadProperties(elem);
 12186                            AdministratorContacts.Add(admin);
 187                        }
 188                        break;
 189                }
 190            }
 20191        }
 192
 193        private void ReadAttributeProperties(JsonElement json)
 194        {
 272195            foreach (JsonProperty prop in json.EnumerateObject())
 196            {
 102197                switch (prop.Name)
 198                {
 199                    case EnabledPropertyName:
 34200                        Enabled = prop.Value.GetBoolean();
 34201                        break;
 202
 203                    case CreatedPropertyName:
 34204                        CreatedOn = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64());
 34205                        break;
 206
 207                    case UpdatedPropertyName:
 34208                        UpdatedOn = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64());
 209                        break;
 210                }
 211            }
 34212        }
 213
 214        internal virtual void WriteProperties(Utf8JsonWriter json)
 215        {
 28216            _properties.WriteProperties(json);
 217
 28218            if (!string.IsNullOrEmpty(AccountId) || !string.IsNullOrEmpty(Password))
 219            {
 8220                json.WriteStartObject(s_credentialsPropertyNameBytes);
 221
 8222                WriteCredentialsProperties(json);
 223
 8224                json.WriteEndObject();
 225            }
 226
 28227            if (!string.IsNullOrEmpty(OrganizationId) || !_administratorContacts.IsNullOrEmpty())
 228            {
 10229                json.WriteStartObject(s_orgDetailsPropertyNameBytes);
 230
 10231                WriteOrgDetailsProperties(json);
 232
 10233                json.WriteEndObject();
 234            }
 235
 28236            if (Enabled.HasValue)
 237            {
 0238                json.WriteStartObject(s_attributesPropertyNameBytes);
 239
 0240                json.WriteBoolean(s_enabledPropertyNameBytes, Enabled.Value);
 241
 0242                json.WriteEndObject();
 243            }
 28244        }
 245
 246        private void WriteCredentialsProperties(Utf8JsonWriter json)
 247        {
 8248            if (!string.IsNullOrEmpty(AccountId))
 249            {
 8250                json.WriteString(s_accountIdPropertyNameBytes, AccountId);
 251            }
 252
 8253            if (!string.IsNullOrEmpty(Password))
 254            {
 4255                json.WriteString(s_passwordPropertyNameBytes, Password);
 256            }
 8257        }
 258
 259        private void WriteOrgDetailsProperties(Utf8JsonWriter json)
 260        {
 10261            if (!string.IsNullOrEmpty(OrganizationId))
 262            {
 4263                json.WriteString(s_organizationIdPropertyNameBytes, AccountId);
 264            }
 265
 10266            if (!_administratorContacts.IsNullOrEmpty())
 267            {
 6268                json.WriteStartArray(s_adminDetailsPropertyNameBytes);
 269
 24270                foreach (AdministratorContact admin in _administratorContacts)
 271                {
 6272                    json.WriteStartObject();
 273
 6274                    admin.WriteProperties(json);
 275
 6276                    json.WriteEndObject();
 277                }
 278
 6279                json.WriteEndArray();
 280            }
 10281        }
 282
 283        void IJsonDeserializable.ReadProperties(JsonElement json)
 284        {
 344285            foreach (JsonProperty prop in json.EnumerateObject())
 286            {
 136287                ReadProperty(prop);
 288            }
 36289        }
 290
 28291        void IJsonSerializable.WriteProperties(Utf8JsonWriter json) => WriteProperties(json);
 292    }
 293}