public EncryptionCosmosClient(
            CosmosClient cosmosClient,
            IKeyEncryptionKeyResolver keyEncryptionKeyResolver,
            string keyEncryptionKeyResolverName,
            TimeSpan?keyCacheTimeToLive)
        {
            this.cosmosClient                 = cosmosClient ?? throw new ArgumentNullException(nameof(cosmosClient));
            this.KeyEncryptionKeyResolver     = keyEncryptionKeyResolver ?? throw new ArgumentNullException(nameof(keyEncryptionKeyResolver));
            this.KeyEncryptionKeyResolverName = keyEncryptionKeyResolverName ?? throw new ArgumentNullException(nameof(keyEncryptionKeyResolverName));
            this.clientEncryptionKeyPropertiesCacheByKeyId = new AsyncCache <string, ClientEncryptionKeyProperties>();
            this.EncryptionKeyStoreProviderImpl            = new EncryptionKeyStoreProviderImpl(keyEncryptionKeyResolver, keyEncryptionKeyResolverName);

            keyCacheTimeToLive ??= TimeSpan.FromHours(1);

            if (EncryptionCosmosClient.EncryptionKeyCacheSemaphore.Wait(-1))
            {
                try
                {
                    // We pick the minimum between the existing and passed in value given this is a static cache.
                    // This also means that the maximum cache duration is the originally initialized value for ProtectedDataEncryptionKey.TimeToLive which is 2 hours.
                    if (keyCacheTimeToLive < ProtectedDataEncryptionKey.TimeToLive)
                    {
                        ProtectedDataEncryptionKey.TimeToLive = keyCacheTimeToLive.Value;
                    }
                }
                finally
                {
                    EncryptionCosmosClient.EncryptionKeyCacheSemaphore.Release(1);
                }
            }
        }
 public EncryptionCosmosClient(CosmosClient cosmosClient, EncryptionKeyStoreProvider encryptionKeyStoreProvider)
 {
     this.cosmosClient = cosmosClient ?? throw new ArgumentNullException(nameof(cosmosClient));
     this.EncryptionKeyStoreProvider = encryptionKeyStoreProvider ?? throw new ArgumentNullException(nameof(encryptionKeyStoreProvider));
     this.clientEncryptionPolicyCacheByContainerId  = new AsyncCache <string, ClientEncryptionPolicy>();
     this.clientEncryptionKeyPropertiesCacheByKeyId = new AsyncCache <string, ClientEncryptionKeyProperties>();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="KeyVaultAccessClient"/> class.
 /// Invokes internal Facotory Methods.
 /// </summary>
 /// <param name="keyVaultTokenCredentialFactory"> TokenCredential </param>
 /// <param name="keyClientFactory"> KeyClient Factory </param>
 /// <param name="cryptographyClientFactory"> CryptoClient Factory </param>
 internal KeyVaultAccessClient(KeyVaultTokenCredentialFactory keyVaultTokenCredentialFactory, KeyClientFactory keyClientFactory, CryptographyClientFactory cryptographyClientFactory)
 {
     this.keyVaultTokenCredentialFactory = keyVaultTokenCredentialFactory;
     this.akvClientCache            = new AsyncCache <Uri, KeyClient>();
     this.akvCryptoClientCache      = new AsyncCache <Uri, CryptographyClient>();
     this.keyClientFactory          = keyClientFactory;
     this.cryptographyClientFactory = cryptographyClientFactory;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="KeyVaultAccessClient"/> class.
 /// </summary>
 /// <param name="keyVaultTokenCredentialFactory"> TokenCredentials </param>
 public KeyVaultAccessClient(KeyVaultTokenCredentialFactory keyVaultTokenCredentialFactory)
 {
     this.keyVaultTokenCredentialFactory = keyVaultTokenCredentialFactory;
     this.akvClientCache            = new AsyncCache <Uri, KeyClient>();
     this.akvCryptoClientCache      = new AsyncCache <Uri, CryptographyClient>();
     this.keyClientFactory          = new KeyClientFactory();
     this.cryptographyClientFactory = new CryptographyClientFactory();
 }
 /// <summary>
 /// All the operations / requests for exercising client-side encryption functionality need to be made using this EncryptionContainer instance.
 /// </summary>
 /// <param name="container">Regular cosmos container.</param>
 /// <param name="encryptionCosmosClient"> Cosmos Client configured with Encryption.</param>
 public EncryptionContainer(
     Container container,
     EncryptionCosmosClient encryptionCosmosClient)
 {
     this.container = container ?? throw new ArgumentNullException(nameof(container));
     this.EncryptionCosmosClient            = encryptionCosmosClient ?? throw new ArgumentNullException(nameof(container));
     this.ResponseFactory                   = this.Database.Client.ResponseFactory;
     this.CosmosSerializer                  = this.Database.Client.ClientOptions.Serializer;
     this.encryptionSettingsByContainerName = new AsyncCache <string, EncryptionSettings>();
 }