private static void ListGrant(KmsClient kmsClient)
        {
            try
            {
                var request = new ListGrantsRequest
                {
                    VersionId = "v1.0",
                    Body      = new ListGrantsRequestBody()
                    {
                        KeyId = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2")
                    }
                };
                var resp = kmsClient.ListGrants(request);

                Console.WriteLine(resp);
            }
            catch (RequestTimeoutException requestTimeoutException)
            {
                Console.WriteLine(requestTimeoutException.ErrorMessage);
            }
            catch (ServiceResponseException clientRequestException)
            {
                Console.WriteLine(clientRequestException.HttpStatusCode);
                Console.WriteLine(clientRequestException.ErrorCode);
                Console.WriteLine(clientRequestException.ErrorMsg);
            }
            catch (ConnectionException connectionException)
            {
                Console.WriteLine(connectionException.ErrorMessage);
            }
        }
예제 #2
0
 private static void GetKeyTag(KmsClient kmsClient)
 {
     try
     {
         var req = new ShowKmsTagsRequest
         {
             VersionId = "v1.0",
             KeyId     = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2")
         };
         var resp = kmsClient.ShowKmsTags(req);
         Console.WriteLine(resp.ToString());
     }
     catch (RequestTimeoutException requestTimeoutException)
     {
         Console.WriteLine(requestTimeoutException.ErrorMessage);
     }
     catch (ServiceResponseException clientRequestException)
     {
         Console.WriteLine(clientRequestException.HttpStatusCode);
         Console.WriteLine(clientRequestException.ErrorCode);
         Console.WriteLine(clientRequestException.ErrorMsg);
     }
     catch (ConnectionException connectionException)
     {
         Console.WriteLine(connectionException.ErrorMessage);
     }
 }
 private static void DeleteKey(KmsClient kmsClient)
 {
     try
     {
         var req = new DeleteKeyRequest
         {
             VersionId = "v1.0",
             Body      = new ScheduleKeyDeletionRequestBody
             {
                 KeyId       = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2"),
                 PendingDays = "365"
             }
         };
         var resp = kmsClient.DeleteKey(req);
         Console.WriteLine(resp.ToString());
     }
     catch (RequestTimeoutException requestTimeoutException)
     {
         Console.WriteLine(requestTimeoutException.ErrorMessage);
     }
     catch (ServiceResponseException clientRequestException)
     {
         Console.WriteLine(clientRequestException.HttpStatusCode);
         Console.WriteLine(clientRequestException.ErrorCode);
         Console.WriteLine(clientRequestException.ErrorMsg);
     }
     catch (ConnectionException connectionException)
     {
         Console.WriteLine(connectionException.ErrorMessage);
     }
 }
 private static void RetireGrant(KmsClient kmsClient)
 {
     try
     {
         var request = new CancelSelfGrantRequest
         {
             VersionId = "v1.0",
             Body      = new RevokeGrantRequestBody
             {
                 KeyId   = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2"),
                 GrantId = CreateGrant(kmsClient)
             }
         };
         var resp = kmsClient.CancelSelfGrant(request);
         Console.WriteLine(resp);
     }
     catch (RequestTimeoutException requestTimeoutException)
     {
         Console.WriteLine(requestTimeoutException.ErrorMessage);
     }
     catch (ServiceResponseException clientRequestException)
     {
         Console.WriteLine(clientRequestException.HttpStatusCode);
         Console.WriteLine(clientRequestException.ErrorCode);
         Console.WriteLine(clientRequestException.ErrorMsg);
     }
     catch (ConnectionException connectionException)
     {
         Console.WriteLine(connectionException.ErrorMessage);
     }
 }
 private static void EnableKey(KmsClient kmsClient)
 {
     try
     {
         var req = new EnableKeyRequest
         {
             VersionId = "v1.0",
             Body      = new OperateKeyRequestBody
             {
                 KeyId = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "3")
             }
         };
         var resp = kmsClient.EnableKey(req);
         Console.WriteLine(resp.ToString());
     }
     catch (RequestTimeoutException requestTimeoutException)
     {
         Console.WriteLine(requestTimeoutException.ErrorMessage);
     }
     catch (ServiceResponseException clientRequestException)
     {
         Console.WriteLine(clientRequestException.HttpStatusCode);
         Console.WriteLine(clientRequestException.ErrorCode);
         Console.WriteLine(clientRequestException.ErrorMsg);
     }
     catch (ConnectionException connectionException)
     {
         Console.WriteLine(connectionException.ErrorMessage);
     }
 }
 private static void UpdateKeyDescription(KmsClient kmsClient)
 {
     try
     {
         var req = new UpdateKeyDescriptionRequest
         {
             VersionId = "v1.0",
             Body      = new UpdateKeyDescriptionRequestBody
             {
                 KeyId          = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2"),
                 KeyDescription = "0000"
             }
         };
         var resp = kmsClient.UpdateKeyDescription(req);
         Console.WriteLine(resp.ToString());
     }
     catch (RequestTimeoutException requestTimeoutException)
     {
         Console.WriteLine(requestTimeoutException.ErrorMessage);
     }
     catch (ServiceResponseException clientRequestException)
     {
         Console.WriteLine(clientRequestException.HttpStatusCode);
         Console.WriteLine(clientRequestException.ErrorCode);
         Console.WriteLine(clientRequestException.ErrorMsg);
     }
     catch (ConnectionException connectionException)
     {
         Console.WriteLine(connectionException.ErrorMessage);
     }
 }
        private static ShowKeyRotationStatusResponse GetKeyRotationStatus(KmsClient kmsClient)
        {
            try
            {
                var request = new ShowKeyRotationStatusRequest
                {
                    VersionId = "v1.0",
                    Body      = new OperateKeyRequestBody()
                    {
                        KeyId = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2")
                    }
                };
                var resp = kmsClient.ShowKeyRotationStatus(request);
                Console.WriteLine(resp);
                return(resp);
            }
            catch (RequestTimeoutException requestTimeoutException)
            {
                Console.WriteLine(requestTimeoutException.ErrorMessage);
            }
            catch (ServiceResponseException clientRequestException)
            {
                Console.WriteLine(clientRequestException.HttpStatusCode);
                Console.WriteLine(clientRequestException.ErrorCode);
                Console.WriteLine(clientRequestException.ErrorMsg);
            }
            catch (ConnectionException connectionException)
            {
                Console.WriteLine(connectionException.ErrorMessage);
            }

            return(null);
        }
 private static void EnableKeyRotation(KmsClient kmsClient)
 {
     try
     {
         if (GetKeyRotationStatus(kmsClient).KeyRotationEnabled.GetValueOrDefault())
         {
             DisableKeyRotation(kmsClient);
         }
         var request = new EnableKeyRotationRequest
         {
             VersionId = "v1.0",
             Body      = new OperateKeyRequestBody()
             {
                 KeyId = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2")
             }
         };
         var resp = kmsClient.EnableKeyRotation(request);
         Console.WriteLine(resp);
     }
     catch (RequestTimeoutException requestTimeoutException)
     {
         Console.WriteLine(requestTimeoutException.ErrorMessage);
     }
     catch (ServiceResponseException clientRequestException)
     {
         Console.WriteLine(clientRequestException.HttpStatusCode);
         Console.WriteLine(clientRequestException.ErrorCode);
         Console.WriteLine(clientRequestException.ErrorMsg);
     }
     catch (ConnectionException connectionException)
     {
         Console.WriteLine(connectionException.ErrorMessage);
     }
 }
예제 #9
0
 private static void DeleteTag(KmsClient kmsClient)
 {
     try
     {
         var req = new DeleteTagRequest
         {
             VersionId = "v1.0",
             KeyId     = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2"),
             Key       = "key"
         };
         var resp = kmsClient.DeleteTag(req);
         Console.WriteLine(resp.HttpStatusCode);
     }
     catch (RequestTimeoutException requestTimeoutException)
     {
         Console.WriteLine(requestTimeoutException.ErrorMessage);
     }
     catch (ServiceResponseException clientRequestException)
     {
         Console.WriteLine(clientRequestException.HttpStatusCode);
         Console.WriteLine(clientRequestException.ErrorCode);
         Console.WriteLine(clientRequestException.ErrorMsg);
     }
     catch (ConnectionException connectionException)
     {
         Console.WriteLine(connectionException.ErrorMessage);
     }
 }
예제 #10
0
        private static EncryptDataResponse EncryptData(KmsClient kmsClient)
        {
            try
            {
                var request = new EncryptDataRequest
                {
                    VersionId = "v1.0",
                    Body      = new EncryptDataRequestBody()
                    {
                        KeyId     = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2"),
                        PlainText = "hello world"
                    }
                };
                var resp = kmsClient.EncryptData(request);
                Console.WriteLine(resp.ToString());
                return(resp);
            }
            catch (RequestTimeoutException requestTimeoutException)
            {
                Console.WriteLine(requestTimeoutException.ErrorMessage);
            }
            catch (ServiceResponseException clientRequestException)
            {
                Console.WriteLine(clientRequestException.HttpStatusCode);
                Console.WriteLine(clientRequestException.ErrorCode);
                Console.WriteLine(clientRequestException.ErrorMsg);
            }
            catch (ConnectionException connectionException)
            {
                Console.WriteLine(connectionException.ErrorMessage);
            }

            return(null);
        }
예제 #11
0
 private static void CreateDataKeyWithoutPlaintext(KmsClient kmsClient)
 {
     try
     {
         var request = new CreateDatakeyWithoutPlaintextRequest
         {
             VersionId = "v1.0",
             Body      = new CreateDatakeyRequestBody()
             {
                 KeyId         = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2"),
                 DatakeyLength = "512"
             }
         };
         var resp = kmsClient.CreateDatakeyWithoutPlaintext(request);
         Console.WriteLine(resp.ToString());
     }
     catch (RequestTimeoutException requestTimeoutException)
     {
         Console.WriteLine(requestTimeoutException.ErrorMessage);
     }
     catch (ServiceResponseException clientRequestException)
     {
         Console.WriteLine(clientRequestException.HttpStatusCode);
         Console.WriteLine(clientRequestException.ErrorCode);
         Console.WriteLine(clientRequestException.ErrorMsg);
     }
     catch (ConnectionException connectionException)
     {
         Console.WriteLine(connectionException.ErrorMessage);
     }
 }
예제 #12
0
        private static void CreateTag(KmsClient kmsClient)
        {
            var tagItem = new TagItem();

            tagItem.Key   = "key";
            tagItem.Value = "value";
            try
            {
                var req = new CreateKmsTagRequest
                {
                    VersionId = "v1.0",
                    KeyId     = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2"),
                    Body      = new CreateKmsTagRequestBody
                    {
                        Tag = tagItem
                    }
                };
                var resp = kmsClient.CreateKmsTag(req);
                Console.WriteLine(resp.HttpStatusCode);
            }
            catch (RequestTimeoutException requestTimeoutException)
            {
                Console.WriteLine(requestTimeoutException.ErrorMessage);
            }
            catch (ServiceResponseException clientRequestException)
            {
                Console.WriteLine(clientRequestException.HttpStatusCode);
                Console.WriteLine(clientRequestException.ErrorCode);
                Console.WriteLine(clientRequestException.ErrorMsg);
            }
            catch (ConnectionException connectionException)
            {
                Console.WriteLine(connectionException.ErrorMessage);
            }
        }
예제 #13
0
 private static void EncryptDataKey(KmsClient kmsClient)
 {
     try
     {
         var data      = DecryptDataKey(kmsClient);
         var plainText = data.DataKey + data.DatakeyDgst;
         var request   = new EncryptDatakeyRequest
         {
             VersionId = "v1.0",
             Body      = new EncryptDatakeyRequestBody()
             {
                 KeyId = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2"),
                 DatakeyPlainLength = "64",
                 PlainText          = plainText
             }
         };
         var resp = kmsClient.EncryptDatakey(request);
         Console.WriteLine(resp.ToString());
     }
     catch (RequestTimeoutException requestTimeoutException)
     {
         Console.WriteLine(requestTimeoutException.ErrorMessage);
     }
     catch (ServiceResponseException clientRequestException)
     {
         Console.WriteLine(clientRequestException.HttpStatusCode);
         Console.WriteLine(clientRequestException.ErrorCode);
         Console.WriteLine(clientRequestException.ErrorMsg);
     }
     catch (ConnectionException connectionException)
     {
         Console.WriteLine(connectionException.ErrorMessage);
     }
 }
예제 #14
0
        private static void BatchDeleteTags(KmsClient kmsClient)
        {
            var tagItems = new List <TagItem>();
            var tagItem1 = new TagItem();

            tagItem1.Key   = "key1";
            tagItem1.Value = "value1";
            var tagItem2 = new TagItem();

            tagItem2.Key   = "key2";
            tagItem2.Value = "value2";
            tagItems.Add(tagItem1);
            tagItems.Add(tagItem2);
            try
            {
                var req = new BatchCreateKmsTagsRequest
                {
                    VersionId = "v1.0",
                    KeyId     = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2"),
                    Body      = new BatchCreateKmsTagsRequestBody
                    {
                        Action = "delete",
                        Tags   = tagItems
                    }
                };
                var resp = kmsClient.BatchCreateKmsTags(req);
                Console.WriteLine(resp.HttpStatusCode);
            }
            catch (RequestTimeoutException requestTimeoutException)
            {
                Console.WriteLine(requestTimeoutException.ErrorMessage);
            }
            catch (ServiceResponseException clientRequestException)
            {
                Console.WriteLine(clientRequestException.HttpStatusCode);
                Console.WriteLine(clientRequestException.ErrorCode);
                Console.WriteLine(clientRequestException.ErrorMsg);
            }
            catch (ConnectionException connectionException)
            {
                Console.WriteLine(connectionException.ErrorMessage);
            }
        }
        private static string CreateGrant(KmsClient kmsClient)
        {
            var operations = new List <string> {
                "describe-key", "create-datakey"
            };

            try
            {
                var request = new CreateGrantRequest
                {
                    VersionId = "v1.0",
                    Body      = new CreateGrantRequestBody()
                    {
                        KeyId                = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2"),
                        GranteePrincipal     = "99104bff46109531b9ac70e606d3ffdf",
                        GranteePrincipalType = CreateGrantRequestBody.GranteePrincipalTypeEnum.DOMAIN,
                        Operations           = operations
                    }
                };
                var resp = kmsClient.CreateGrant(request);
                Console.WriteLine(resp.GrantId);
                return(resp.GrantId);
            }
            catch (RequestTimeoutException requestTimeoutException)
            {
                Console.WriteLine(requestTimeoutException.ErrorMessage);
            }
            catch (ServiceResponseException clientRequestException)
            {
                Console.WriteLine(clientRequestException.HttpStatusCode);
                Console.WriteLine(clientRequestException.ErrorCode);
                Console.WriteLine(clientRequestException.ErrorMsg);
            }
            catch (ConnectionException connectionException)
            {
                Console.WriteLine(connectionException.ErrorMessage);
            }

            return(null);
        }
        static void Main1(string[] args)
        {
            const string ak        = "{your ak string}";
            const string sk        = "{your sk string}";
            const string endpoint  = "{your endpoint string}";
            const string projectId = "{your projectID string}";

            var config = HttpConfig.GetDefaultConfig();

            config.IgnoreSslVerification = true;
            var auth = new BasicCredentials(ak, sk, projectId);

            var kmsClient = KmsClient.NewBuilder()
                            .WithCredential(auth)
                            .WithEndPoint(endpoint)
                            .WithHttpConfig(config).Build();

            var keyId = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "5");

            GetParametersForImport(kmsClient, keyId);
            ImportKeyMaterial(kmsClient, keyId);
            DeleteKeyMaterial(kmsClient, keyId);
        }
예제 #17
0
        private static DecryptDatakeyResponse DecryptDataKey(KmsClient kmsClient)
        {
            try
            {
                var cipherText = CreateDataKey(kmsClient).CipherText;
                var request    = new DecryptDatakeyRequest
                {
                    VersionId = "v1.0",
                    Body      = new DecryptDatakeyRequestBody()
                    {
                        KeyId               = GetKeyByStatus.GetKeyByKeyStatus(kmsClient, "2"),
                        CipherText          = cipherText,
                        DatakeyCipherLength = "64"
                    }
                };
                var resp = kmsClient.DecryptDatakey(request);
                Console.WriteLine(resp.ToString());
                return(resp);
            }
            catch (RequestTimeoutException requestTimeoutException)
            {
                Console.WriteLine(requestTimeoutException.ErrorMessage);
            }
            catch (ServiceResponseException clientRequestException)
            {
                Console.WriteLine(clientRequestException.HttpStatusCode);
                Console.WriteLine(clientRequestException.ErrorCode);
                Console.WriteLine(clientRequestException.ErrorMsg);
            }
            catch (ConnectionException connectionException)
            {
                Console.WriteLine(connectionException.ErrorMessage);
            }

            return(null);
        }