Chilkat Online Tools

CreateKey SQL Server Example

AWS Key Management Service

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(max)
    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    DECLARE @authAws int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.AuthAws', @authAws OUT

    EXEC sp_OASetProperty @authAws, 'AccessKey', 'AWS_ACCESS_KEY'
    EXEC sp_OASetProperty @authAws, 'SecretKey', 'AWS_SECRET_KEY'

    -- Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
    EXEC sp_OASetProperty @authAws, 'Region', 'us-west-2'
    EXEC sp_OASetProperty @authAws, 'ServiceName', 'kms'
    -- SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @authAws

    -- URL: https://kms.us-west-2.amazonaws.com/
    -- Use the same region as specified above.
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'kms.us-west-2.amazonaws.com', 443, 1, 1
    IF @success <> 1
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ConnectFailReason', @iTmp0 OUT
        PRINT 'ConnectFailReason: ' + @iTmp0
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        RETURN
      END

    -- The following code creates the JSON request body.
    -- The JSON created by this code is shown below.

    -- Use this online tool to generate code from sample JSON:
    -- Generate Code to Create JSON

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'BypassPolicyLockoutSafetyCheck', 123
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'CustomerMasterKeySpec', 'string'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'CustomKeyStoreId', 'string'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'Description', 'string'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'KeySpec', 'string'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'KeyUsage', 'string'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'MultiRegion', 123
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'Origin', 'string'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'Policy', 'string'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'Tags[0].TagKey', 'string'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'Tags[0].TagValue', 'string'

    -- The JSON request body created by the above code:

    -- {
    --   "BypassPolicyLockoutSafetyCheck": boolean,
    --   "CustomerMasterKeySpec": "string",
    --   "CustomKeyStoreId": "string",
    --   "Description": "string",
    --   "KeySpec": "string",
    --   "KeyUsage": "string",
    --   "MultiRegion": boolean,
    --   "Origin": "string",
    --   "Policy": "string",
    --   "Tags": [
    --     {
    --       "TagKey": "string",
    --       "TagValue": "string"
    --     }
    --   ]
    -- }

    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/x-amz-json-1.1'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', 'TrentService.CreateKey'

    DECLARE @sbRequestBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbRequestBody OUT

    EXEC sp_OAMethod @json, 'EmitSb', @success OUT, @sbRequestBody
    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/', @sbRequestBody, @sbResponseBody
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbRequestBody
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END
    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @respStatusCode OUT

    PRINT 'response status code = ' + @respStatusCode
    IF @respStatusCode <> 200
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Response Body:'
        EXEC sp_OAMethod @sbResponseBody, 'GetAsString', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @authAws
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbRequestBody
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody

    -- The following code parses the JSON response.
    -- A sample JSON response is shown below the sample code.

    -- Use this online tool to generate parsing code from sample JSON:
    -- Generate Parsing Code from JSON

    DECLARE @strVal nvarchar(max)

    DECLARE @Arn nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @Arn OUT, 'KeyMetadata.Arn'
    DECLARE @AWSAccountId nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @AWSAccountId OUT, 'KeyMetadata.AWSAccountId'
    DECLARE @CloudHsmClusterId nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @CloudHsmClusterId OUT, 'KeyMetadata.CloudHsmClusterId'
    DECLARE @CreationDate int
    EXEC sp_OAMethod @jResp, 'IntOf', @CreationDate OUT, 'KeyMetadata.CreationDate'
    DECLARE @CustomerMasterKeySpec nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @CustomerMasterKeySpec OUT, 'KeyMetadata.CustomerMasterKeySpec'
    DECLARE @CustomKeyStoreId nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @CustomKeyStoreId OUT, 'KeyMetadata.CustomKeyStoreId'
    DECLARE @DeletionDate int
    EXEC sp_OAMethod @jResp, 'IntOf', @DeletionDate OUT, 'KeyMetadata.DeletionDate'
    DECLARE @Description nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @Description OUT, 'KeyMetadata.Description'
    DECLARE @Enabled int
    EXEC sp_OAMethod @jResp, 'IntOf', @Enabled OUT, 'KeyMetadata.Enabled'
    DECLARE @ExpirationModel nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @ExpirationModel OUT, 'KeyMetadata.ExpirationModel'
    DECLARE @KeyId nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @KeyId OUT, 'KeyMetadata.KeyId'
    DECLARE @KeyManager nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @KeyManager OUT, 'KeyMetadata.KeyManager'
    DECLARE @KeySpec nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @KeySpec OUT, 'KeyMetadata.KeySpec'
    DECLARE @KeyState nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @KeyState OUT, 'KeyMetadata.KeyState'
    DECLARE @KeyUsage nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @KeyUsage OUT, 'KeyMetadata.KeyUsage'
    DECLARE @MultiRegion int
    EXEC sp_OAMethod @jResp, 'IntOf', @MultiRegion OUT, 'KeyMetadata.MultiRegion'
    DECLARE @MultiRegionKeyType nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @MultiRegionKeyType OUT, 'KeyMetadata.MultiRegionConfiguration.MultiRegionKeyType'
    DECLARE @PrimaryKeyArn nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @PrimaryKeyArn OUT, 'KeyMetadata.MultiRegionConfiguration.PrimaryKey.Arn'
    DECLARE @Region nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @Region OUT, 'KeyMetadata.MultiRegionConfiguration.PrimaryKey.Region'
    DECLARE @Origin nvarchar(max)
    EXEC sp_OAMethod @jResp, 'StringOf', @Origin OUT, 'KeyMetadata.Origin'
    DECLARE @PendingDeletionWindowInDays int
    EXEC sp_OAMethod @jResp, 'IntOf', @PendingDeletionWindowInDays OUT, 'KeyMetadata.PendingDeletionWindowInDays'
    DECLARE @ValidTo int
    EXEC sp_OAMethod @jResp, 'IntOf', @ValidTo OUT, 'KeyMetadata.ValidTo'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'KeyMetadata.EncryptionAlgorithms'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @strVal OUT, 'KeyMetadata.EncryptionAlgorithms[i]'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'KeyMetadata.MacAlgorithms'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @strVal OUT, 'KeyMetadata.MacAlgorithms[i]'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'KeyMetadata.MultiRegionConfiguration.ReplicaKeys'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @Arn OUT, 'KeyMetadata.MultiRegionConfiguration.ReplicaKeys[i].Arn'
        EXEC sp_OAMethod @jResp, 'StringOf', @Region OUT, 'KeyMetadata.MultiRegionConfiguration.ReplicaKeys[i].Region'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'KeyMetadata.SigningAlgorithms'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @strVal OUT, 'KeyMetadata.SigningAlgorithms[i]'
        SELECT @i = @i + 1
      END

    -- A sample JSON response body parsed by the above code:

    -- {
    --   "KeyMetadata": {
    --     "Arn": "string",
    --     "AWSAccountId": "string",
    --     "CloudHsmClusterId": "string",
    --     "CreationDate": number,
    --     "CustomerMasterKeySpec": "string",
    --     "CustomKeyStoreId": "string",
    --     "DeletionDate": number,
    --     "Description": "string",
    --     "Enabled": boolean,
    --     "EncryptionAlgorithms": [
    --       "string"
    --     ],
    --     "ExpirationModel": "string",
    --     "KeyId": "string",
    --     "KeyManager": "string",
    --     "KeySpec": "string",
    --     "KeyState": "string",
    --     "KeyUsage": "string",
    --     "MacAlgorithms": [
    --       "string"
    --     ],
    --     "MultiRegion": boolean,
    --     "MultiRegionConfiguration": {
    --       "MultiRegionKeyType": "string",
    --       "PrimaryKey": {
    --         "Arn": "string",
    --         "Region": "string"
    --       },
    --       "ReplicaKeys": [
    --         {
    --           "Arn": "string",
    --           "Region": "string"
    --         }
    --       ]
    --     },
    --     "Origin": "string",
    --     "PendingDeletionWindowInDays": number,
    --     "SigningAlgorithms": [
    --       "string"
    --     ],
    --     "ValidTo": number
    --   }
    -- }

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @authAws
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbRequestBody
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO