Esempio n. 1
0
        /// <summary>
        /// Gets the specified vault&#39;s configuration information.
        /// &lt;br/&gt;
        /// As a provisioning operation, this call is subject to a Key Management limit that applies to
        /// the total number of requests across all provisioning read operations. Key Management might
        /// throttle this call to reject an otherwise valid request when the total rate of provisioning
        /// read operations exceeds 10 requests per second for a given tenancy.
        ///
        /// </summary>
        /// <param name="request">The request object containing the details to send. Required.</param>
        /// <param name="retryConfiguration">The retry configuration that will be used by to send this request. Optional.</param>
        /// <param name="cancellationToken">The cancellation token to cancel this operation. Optional.</param>
        /// <returns>A response object containing details about the completed operation</returns>
        /// <example>Click <a href="https://docs.cloud.oracle.com/en-us/iaas/tools/dot-net-examples/latest/keymanagement/GetVault.cs.html">here</a> to see an example of how to use GetVault API.</example>
        public async Task <GetVaultResponse> GetVault(GetVaultRequest request, RetryConfiguration retryConfiguration = null, CancellationToken cancellationToken = default)
        {
            logger.Trace("Called getVault");
            Uri                uri            = new Uri(this.restClient.GetEndpoint(), System.IO.Path.Combine(basePathWithoutHost, "/20180608/vaults/{vaultId}".Trim('/')));
            HttpMethod         method         = new HttpMethod("GET");
            HttpRequestMessage requestMessage = Converter.ToHttpRequestMessage(uri, method, request);

            requestMessage.Headers.Add("Accept", "application/json");
            GenericRetrier      retryingClient = Retrier.GetPreferredRetrier(retryConfiguration, this.retryConfiguration);
            HttpResponseMessage responseMessage;

            try
            {
                if (retryingClient != null)
                {
                    responseMessage = await retryingClient.MakeRetryingCall(this.restClient.HttpSend, requestMessage, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    responseMessage = await this.restClient.HttpSend(requestMessage).ConfigureAwait(false);
                }
                this.restClient.CheckHttpResponseMessage(requestMessage, responseMessage);

                return(Converter.FromHttpResponseMessage <GetVaultResponse>(responseMessage));
            }
            catch (Exception e)
            {
                logger.Error($"GetVault failed with error: {e.Message}");
                throw;
            }
        }
        /// <summary>
        /// Creates a waiter using the provided configuration.
        /// </summary>
        /// <param name="request">Request to send.</param>
        /// <param name="config">Wait Configuration</param>
        /// <param name="targetStates">Desired resource states. If multiple states are provided then the waiter will return once the resource reaches any of the provided states</param>
        /// <returns>a new Oci.common.Waiter instance</returns>
        public Waiter <GetVaultRequest, GetVaultResponse> ForVault(GetVaultRequest request, WaiterConfiguration config, params Vault.LifecycleStateEnum[] targetStates)
        {
            var agent = new WaiterAgent <GetVaultRequest, GetVaultResponse>(
                request,
                request => client.GetVault(request),
                response => targetStates.Contains(response.Vault.LifecycleState.Value),
                targetStates.Contains(Vault.LifecycleStateEnum.Deleted)
                );

            return(new Waiter <GetVaultRequest, GetVaultResponse>(config, agent));
        }
Esempio n. 3
0
        private static async Task <Vault> GetVault(KmsVaultClient kmsVaultClient, string vaultId)
        {
            logger.Info("Get Vault");

            GetVaultRequest getVaultRequest = new GetVaultRequest
            {
                VaultId = vaultId
            };
            GetVaultResponse getVaultResponse = await kmsVaultClient.GetVault(getVaultRequest);

            logger.Info($"Retrieved vault: {getVaultResponse.Vault.DisplayName}");
            return(getVaultResponse.Vault);
        }
        private void HandleOutput(GetVaultRequest request)
        {
            var waiterConfig = new WaiterConfiguration
            {
                MaxAttempts           = MaxWaitAttempts,
                GetNextDelayInSeconds = (_) => WaitIntervalSeconds
            };

            switch (ParameterSetName)
            {
            case LifecycleStateParamSet:
                response = client.Waiters.ForVault(request, waiterConfig, WaitForLifecycleState).Execute();
                break;

            case Default:
                response = client.GetVault(request).GetAwaiter().GetResult();
                break;
            }
            WriteOutput(response, response.Vault);
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            GetVaultRequest request;

            try
            {
                request = new GetVaultRequest
                {
                    VaultId      = VaultId,
                    OpcRequestId = OpcRequestId
                };

                HandleOutput(request);
                FinishProcessing(response);
            }
            catch (Exception ex)
            {
                TerminatingErrorDuringExecution(ex);
            }
        }
 /// <summary>
 /// Creates a waiter using default wait configuration.
 /// </summary>
 /// <param name="request">Request to send.</param>
 /// <param name="targetStates">Desired resource states. If multiple states are provided then the waiter will return once the resource reaches any of the provided states</param>
 /// <returns>a new Oci.common.Waiter instance</returns>
 public Waiter <GetVaultRequest, GetVaultResponse> ForVault(GetVaultRequest request, params Vault.LifecycleStateEnum[] targetStates)
 {
     return(this.ForVault(request, WaiterConfiguration.DefaultWaiterConfiguration, targetStates));
 }
Esempio n. 7
0
        public static async Task MainKeyManagement()
        {
            logger.Info("Starting example");

            var provider      = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
            var compartmentId = Environment.GetEnvironmentVariable("OCI_COMPARTMENT_ID");
            var vaultId       = Environment.GetEnvironmentVariable("VAULT_ID");

            KmsVaultClient      kmsVaultClient      = null;
            KmsManagementClient kmsManagementClient = null;
            KmsCryptoClient     kmsCryptoClient     = null;

            try
            {
                // Initialize the KMS Clients. KMS has three clients as following:
                //      * KmsVaultClient: The client for Vault management
                //      * KmsManagementClient: The client for Key management (ControlPlane)
                //      * KmsCryptoClient: The client for data encryption and decryption (DataPlane)
                kmsVaultClient      = new KmsVaultClient(provider);
                kmsManagementClient = new KmsManagementClient(provider);
                kmsCryptoClient     = new KmsCryptoClient(provider);

                Vault vault = await GetVault(kmsVaultClient, vaultId);

                // The ManagementClient and CryptoClient use Vault specific endpoints; Set them now.
                kmsManagementClient.SetEndpoint(vault.ManagementEndpoint);
                kmsCryptoClient.SetEndpoint(vault.CryptoEndpoint);

                // Vault Operations
                await UpdateVaultResetTags(kmsVaultClient, vault.Id);
                await UpdateVault(kmsVaultClient, vault.Id);
                await ListVaults(kmsVaultClient, compartmentId);
                await ScheduleVaultDeletion(kmsVaultClient, vault.Id);

                var waiterConfiguration = new WaiterConfiguration
                {
                    MaxAttempts           = 10,
                    GetNextDelayInSeconds = DelayStrategy.GetExponentialDelayInSeconds
                };

                // After scheduling deletion, the Vault will stay in SCHEDULING_DELETION state shortly and then
                // transit to PENDING_DELETION state. Wait a bit for the transition to happen.
                logger.Info("Wait a bit for the deletion scheduling to finish");
                GetVaultRequest getVaultRequest = new GetVaultRequest
                {
                    VaultId = vault.Id
                };
                kmsVaultClient.Waiters.ForVault(getVaultRequest, waiterConfiguration, Vault.LifecycleStateEnum.PendingDeletion).Execute();

                await CancelVaultDeletion(kmsVaultClient, vault.Id);

                // After cancelling deletion, the Vault will stay in CANCELLING_DELETION state shortly and then
                // transit to ACTIVE state. Wait a bit for the transition to happen.
                logger.Info("Wait a bit for the deletion cancelling to finish");
                kmsVaultClient.Waiters.ForVault(getVaultRequest, waiterConfiguration, Vault.LifecycleStateEnum.Active).Execute();

                // Management / Key Operations
                string keyId = await CreateKey(kmsManagementClient, compartmentId);

                // After creating a Key, the Key will stay in CREATING state shortly and then
                // transit to ENABLED state. Wait a bit for the transition to happen.
                logger.Info("Wait a bit for Key creation to finish");
                GetKeyRequest getKeyRequest = new GetKeyRequest
                {
                    KeyId = keyId
                };
                kmsManagementClient.Waiters.ForKey(getKeyRequest, waiterConfiguration, Key.LifecycleStateEnum.Enabled).Execute();

                await GetKey(kmsManagementClient, keyId);
                await UpdateKeyResetTags(kmsManagementClient, keyId);

                kmsManagementClient.Waiters.ForKey(getKeyRequest, waiterConfiguration, Key.LifecycleStateEnum.Enabled).Execute();

                await UpdateKey(kmsManagementClient, keyId);

                kmsManagementClient.Waiters.ForKey(getKeyRequest, waiterConfiguration, Key.LifecycleStateEnum.Enabled).Execute();

                await ListKeys(kmsManagementClient, compartmentId);
                await DisableKey(kmsManagementClient, keyId);

                // After disabling a Key, the Key will stay in DISABLING state shortly and then
                // transit to DISABLED state. Wait a bit for the transition to happen.
                logger.Info("Wait a bit for Key disabling to finish");
                kmsManagementClient.Waiters.ForKey(getKeyRequest, waiterConfiguration, Key.LifecycleStateEnum.Disabled).Execute();

                await EnableKey(kmsManagementClient, keyId);

                // After enabling a Key, the Key will stay in ENABLING state shortly and then
                // transit to ENABLED state. Wait a bit for the transition to happen.
                logger.Info("Wait a bit for Key enabling to finish");
                kmsManagementClient.Waiters.ForKey(getKeyRequest, waiterConfiguration, Key.LifecycleStateEnum.Enabled).Execute();

                await ScheduleKeyDeletion(kmsManagementClient, keyId);

                // After scheduling deletion, the Key will stay in SCHEDULING_DELETION state shortly and then
                // transit to PENDING_DELETION state. Wait a bit for the transition to happen.
                logger.Info("Wait a bit for deletion scheduling to finish");
                kmsManagementClient.Waiters.ForKey(getKeyRequest, waiterConfiguration, Key.LifecycleStateEnum.PendingDeletion).Execute();

                await CancelKeyDeletion(kmsManagementClient, keyId);

                // After cancelling deletion, the Key will stay in CANCELLING_DELETION state shortly and then
                // transit to Enabled state. Wait a bit for the transition to happen.
                logger.Info("Wait a bit for deletion cancelling to finish");
                kmsManagementClient.Waiters.ForKey(getKeyRequest, waiterConfiguration, Key.LifecycleStateEnum.Enabled).Execute();

                await CreateKeyVersion(kmsManagementClient, keyId);
                await ListKeyVersions(kmsManagementClient, keyId);

                // Crypto Operations
                string cipherText = await Encrypt(kmsCryptoClient, keyId);
                await Decrypt(kmsCryptoClient, keyId, cipherText);
                await GenerateDataEncryptionKey(kmsCryptoClient, keyId);

                kmsManagementClient.Waiters.ForKey(getKeyRequest, waiterConfiguration, Key.LifecycleStateEnum.Enabled).Execute();
            }
            catch (Exception e)
            {
                logger.Error($"Failed to perform operations on Vault: {e}");
            }
            finally
            {
                if (kmsVaultClient != null)
                {
                    kmsVaultClient.Dispose();
                }

                if (kmsManagementClient != null)
                {
                    kmsManagementClient.Dispose();
                }

                if (kmsCryptoClient != null)
                {
                    kmsCryptoClient.Dispose();
                }
            }

            logger.Info("End example");
        }