AWS KMS: Import New Symmetric Key Material for Rotation
See more AWS KMS Examples
Uses the current AWS KMS API. Configure the region and keep credentials in private qa_data/aws/kms.json, with nonempty string fields access_key and secret_key. The examples do not obtain or refresh credentials. Keep diagnostics private; they may contain credentials or sensitive key data. Create the output directory before running file-writing examples.
Uses the AWS JSON 1.1 protocol with TrentService targets, service kms, HTTPS certificate verification, and Signature Version 4. When using temporary credentials, also provide a nonempty session_token; this Rest example includes it in the signed query on each request. Never log that URL. The endpoint shown is for the commercial AWS partition; use the appropriate endpoint for other partitions. Failures stop the sample; production retry policies should use bounded backoff with jitter and reconcile uncertain writes.
Where a key is required, set string key_id to its fixed key ARN in the configured region. Additional named fields and input files are described below. Keep output files private when they contain plaintext or policies. The code does not automatically retry mutations, refresh credentials, or create output directories. A successful request can precede propagation of a state change. Discard sensitive objects promptly; clearing one buffer does not erase copies held in strings, JSON, or runtime memory.
Use an existing EXTERNAL symmetric ENCRYPT_DECRYPT key and a protected, durably backed-up file qa_data/kms/import_aes256.bin containing exactly 32 raw bytes. This imports an existing file rather than generating unrecoverable material at runtime. Uses RSAES_OAEP_SHA_256 with RSA_4096. For multi-Region keys, run on the primary first. Sends NEW_KEY_MATERIAL; for an existing enabled key, the new material must reach PENDING_ROTATION. Record the returned material ID; import those same bytes into every replica before rotation. This does not trigger rotation. Nonexpiring material is an explicit policy choice; use KEY_MATERIAL_EXPIRES/ValidTo if expiration is required.
API actions used: kms:DescribeKey, kms:GetParametersForImport, kms:ImportKeyMaterial. IAM permissions and key policies must allow the actual resources; grant and multi-Region workflows can need permissions in more than one region/account.
DescribeKey | GetParametersForImport | ImportKeyMaterial
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
DECLARE @stringCompare int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @stringCompare OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Keep this credentials file outside public web content and source control.
DECLARE @credentials int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @credentials OUT
EXEC sp_OASetProperty @credentials, 'Strict', 1
EXEC sp_OAMethod @credentials, 'LoadFile', @success OUT, 'qa_data/aws/kms.json'
IF @success <> 1
BEGIN
PRINT 'Could not load private KMS configuration.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
RETURN
END
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch1 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch1 OUT, '', 1
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'access_key'
IF (@iTmp0 <> 1) or (@stringMatch1 = 1)
BEGIN
PRINT 'Missing or invalid access_key.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
RETURN
END
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch2 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch2 OUT, '', 1
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'secret_key'
IF (@iTmp0 <> 1) or (@stringMatch2 = 1)
BEGIN
PRINT 'Missing or invalid secret_key.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
RETURN
END
DECLARE @region nvarchar(4000)
SELECT @region = 'us-west-2'
DECLARE @endpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'kms.'
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @region
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.amazonaws.com'
DECLARE @aws int
EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @aws OUT
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
EXEC sp_OASetProperty @aws, 'AccessKey', @sTmp0
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
EXEC sp_OASetProperty @aws, 'SecretKey', @sTmp0
EXEC sp_OASetProperty @aws, 'Region', @region
EXEC sp_OASetProperty @aws, 'ServiceName', 'kms'
EXEC sp_OASetProperty @aws, 'SignatureVersion', 4
DECLARE @rest int
EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
EXEC sp_OASetProperty @rest, 'ConnectTimeoutMs', 20000
EXEC sp_OASetProperty @rest, 'IdleTimeoutMs', 60000
EXEC sp_OASetProperty @rest, 'VerboseLogging', 0
EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @aws
IF @success <> 1
BEGIN
PRINT 'Could not configure AWS signing.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
RETURN
END
EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch3 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch3 OUT, '', 1
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'session_token'
IF (@iTmp0 <> 1) or (@stringMatch3 = 1)
BEGIN
PRINT 'Missing or invalid session_token.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
RETURN
END
END
DECLARE @connection int
EXEC @hr = sp_OACreate 'Chilkat.Socket', @connection OUT
EXEC sp_OASetProperty @connection, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @connection, 'MaxReadIdleMs', 60000
EXEC sp_OASetProperty @connection, 'MaxSendIdleMs', 60000
EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
IF @success <> 1
BEGIN
PRINT 'TLS connection failed; inspect diagnostics privately.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
RETURN
END
EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 1
IF @success <> 1
BEGIN
PRINT 'Could not attach TLS connection.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
RETURN
END
-- Keep connection alive while Rest uses it.
DECLARE @body int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT
EXEC sp_OASetProperty @result, 'Strict', 1
DECLARE @responseJson nvarchar(4000)
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'key_id'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch4 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch4 OUT, '', 1
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'key_id'
IF (@iTmp0 <> 1) or (@stringMatch4 = 1)
BEGIN
PRINT 'Missing or invalid key_id.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
DECLARE @keyId nvarchar(4000)
EXEC sp_OAMethod @credentials, 'StringOf', @keyId OUT, 'key_id'
EXEC sp_OAMethod @body, 'Clear', NULL
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'KeyId', @keyId
IF @success <> 1
BEGIN
PRINT 'Could not set KeyId.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @rest, 'ClearAllQueryParams', @success OUT
IF @success <> 1
BEGIN
PRINT 'Could not clear previous query parameters.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
-- Keep the JSON operation body separate from authentication parameters.
-- Include STS credentials in the signed query; never log the request URL.
EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'X-Amz-Security-Token', @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not set session token.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
END
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', 'TrentService.DescribeKey'
IF @success <> 1
BEGIN
PRINT 'Could not set operation.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/x-amz-json-1.1'
IF @success <> 1
BEGIN
PRINT 'Could not set content type.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @body, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @rest, 'FullRequestString', @responseJson OUT, 'POST', '/', @sTmp0
EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
PRINT 'KMS HTTP status: ' + @iTmp0
EXEC sp_OAMethod @result, 'Load', @success OUT, @responseJson
IF @success = 1
BEGIN
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, '__type'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
PRINT 'AWS error code: ' + @sTmp0
END
END
EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-amzn-RequestId'
PRINT 'AWS request ID: ' + @sTmp0
-- Do not print full bodies, headers, credentials, key material, or tokens.
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'Load', @success OUT, @responseJson
IF @success <> 1
BEGIN
PRINT 'Expected a well-formed JSON response.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyMetadata.Arn'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch5 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch5 OUT, '', 1
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'KeyMetadata.Arn'
IF (@iTmp0 <> 1) or (@stringMatch5 = 1)
BEGIN
PRINT 'Missing or invalid KeyMetadata.Arn.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyMetadata.KeyId'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch6 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch6 OUT, '', 1
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'KeyMetadata.KeyId'
IF (@iTmp0 <> 1) or (@stringMatch6 = 1)
BEGIN
PRINT 'Missing or invalid KeyMetadata.KeyId.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyMetadata.KeySpec'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch7 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch7 OUT, '', 1
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'KeyMetadata.KeySpec'
IF (@iTmp0 <> 1) or (@stringMatch7 = 1)
BEGIN
PRINT 'Missing or invalid KeyMetadata.KeySpec.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyMetadata.KeySpec'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch8 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch8 OUT, 'SYMMETRIC_DEFAULT', 1
IF @stringMatch8 <> 1
BEGIN
PRINT 'Unexpected KeyMetadata.KeySpec.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyMetadata.KeyUsage'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch9 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch9 OUT, '', 1
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'KeyMetadata.KeyUsage'
IF (@iTmp0 <> 1) or (@stringMatch9 = 1)
BEGIN
PRINT 'Missing or invalid KeyMetadata.KeyUsage.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyMetadata.KeyUsage'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch10 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch10 OUT, 'ENCRYPT_DECRYPT', 1
IF @stringMatch10 <> 1
BEGIN
PRINT 'Unexpected KeyMetadata.KeyUsage.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyMetadata.Origin'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch11 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch11 OUT, '', 1
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'KeyMetadata.Origin'
IF (@iTmp0 <> 1) or (@stringMatch11 = 1)
BEGIN
PRINT 'Missing or invalid KeyMetadata.Origin.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyMetadata.Origin'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch12 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch12 OUT, 'EXTERNAL', 1
IF @stringMatch12 <> 1
BEGIN
PRINT 'Unexpected KeyMetadata.Origin.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyMetadata.KeyManager'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch13 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch13 OUT, '', 1
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'KeyMetadata.KeyManager'
IF (@iTmp0 <> 1) or (@stringMatch13 = 1)
BEGIN
PRINT 'Missing or invalid KeyMetadata.KeyManager.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyMetadata.KeyManager'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch14 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch14 OUT, 'CUSTOMER', 1
IF @stringMatch14 <> 1
BEGIN
PRINT 'Unexpected KeyMetadata.KeyManager.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @keyId OUT, 'KeyMetadata.Arn'
DECLARE @material int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @material OUT
EXEC sp_OAMethod @material, 'LoadFile', @success OUT, 'qa_data/kms/import_aes256.bin'
IF @success <> 1
BEGIN
PRINT 'Could not load qa_data/kms/import_aes256.bin.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAGetProperty @material, 'NumBytes', @iTmp0 OUT
IF @iTmp0 <> 32
BEGIN
PRINT 'Provide exactly 32 bytes of backed-up AES-256 key material.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAMethod @result, 'BoolOf', @iTmp0 OUT, 'KeyMetadata.MultiRegion'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyMetadata.MultiRegionConfiguration.MultiRegionKeyType'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch15 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch15 OUT, '', 1
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'KeyMetadata.MultiRegionConfiguration.MultiRegionKeyType'
IF (@iTmp0 <> 1) or (@stringMatch15 = 1)
BEGIN
PRINT 'Missing or invalid KeyMetadata.MultiRegionConfiguration.MultiRegionKeyType.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyMetadata.MultiRegionConfiguration.MultiRegionKeyType'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch16 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch16 OUT, 'PRIMARY', 1
IF @stringMatch16 <> 1
BEGIN
PRINT 'Unexpected KeyMetadata.MultiRegionConfiguration.MultiRegionKeyType.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
END
EXEC sp_OAMethod @body, 'Clear', NULL
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'KeyId', @keyId
IF @success <> 1
BEGIN
PRINT 'Could not set KeyId.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'WrappingAlgorithm', 'RSAES_OAEP_SHA_256'
IF @success <> 1
BEGIN
PRINT 'Could not set WrappingAlgorithm.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'WrappingKeySpec', 'RSA_4096'
IF @success <> 1
BEGIN
PRINT 'Could not set WrappingKeySpec.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAMethod @rest, 'ClearAllQueryParams', @success OUT
IF @success <> 1
BEGIN
PRINT 'Could not clear previous query parameters.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
-- Keep the JSON operation body separate from authentication parameters.
-- Include STS credentials in the signed query; never log the request URL.
EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'X-Amz-Security-Token', @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not set session token.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
END
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', 'TrentService.GetParametersForImport'
IF @success <> 1
BEGIN
PRINT 'Could not set operation.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/x-amz-json-1.1'
IF @success <> 1
BEGIN
PRINT 'Could not set content type.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAMethod @body, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @rest, 'FullRequestString', @responseJson OUT, 'POST', '/', @sTmp0
EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
PRINT 'KMS HTTP status: ' + @iTmp0
EXEC sp_OAMethod @result, 'Load', @success OUT, @responseJson
IF @success = 1
BEGIN
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, '__type'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
PRINT 'AWS error code: ' + @sTmp0
END
END
EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-amzn-RequestId'
PRINT 'AWS request ID: ' + @sTmp0
-- Do not print full bodies, headers, credentials, key material, or tokens.
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAMethod @result, 'Load', @success OUT, @responseJson
IF @success <> 1
BEGIN
PRINT 'Expected a well-formed JSON response.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyId'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch17 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch17 OUT, '', 1
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'KeyId'
IF (@iTmp0 <> 1) or (@stringMatch17 = 1)
BEGIN
PRINT 'Missing or invalid KeyId.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyId'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch18 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch18 OUT, @keyId, 1
IF @stringMatch18 <> 1
BEGIN
PRINT 'Response belongs to another key; use a fixed key ARN in configuration.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'ImportToken'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch19 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch19 OUT, '', 1
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'ImportToken'
IF (@iTmp0 <> 1) or (@stringMatch19 = 1)
BEGIN
PRINT 'Missing or invalid ImportToken.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'PublicKey'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch20 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch20 OUT, '', 1
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'PublicKey'
IF (@iTmp0 <> 1) or (@stringMatch20 = 1)
BEGIN
PRINT 'Missing or invalid PublicKey.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'ParametersValidTo'
IF @iTmp0 <> 2
BEGIN
PRINT 'Missing or invalid ParametersValidTo.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
RETURN
END
DECLARE @now int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @now OUT
DECLARE @expires int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @expires OUT
EXEC sp_OAMethod @now, 'SetFromCurrentSystemTime', @success OUT
IF @success <> 1
BEGIN
PRINT 'Could not read current time.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
RETURN
END
EXEC sp_OAMethod @result, 'DateOf', @success OUT, 'ParametersValidTo', @expires
IF @success <> 1
BEGIN
PRINT 'Invalid parameter expiration.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
RETURN
END
EXEC sp_OAMethod @expires, 'DiffSeconds', @iTmp0 OUT, @now
IF @iTmp0 <= 0
BEGIN
PRINT 'Wrapping parameters expired.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
RETURN
END
DECLARE @importToken nvarchar(4000)
EXEC sp_OAMethod @result, 'StringOf', @importToken OUT, 'ImportToken'
DECLARE @wrappingKey int
EXEC @hr = sp_OACreate 'Chilkat.PublicKey', @wrappingKey OUT
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'PublicKey'
EXEC sp_OAMethod @wrappingKey, 'LoadBase64', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Invalid RSA wrapping key.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
RETURN
END
EXEC sp_OAGetProperty @wrappingKey, 'KeySize', @iTmp0 OUT
IF @iTmp0 <> 4096
BEGIN
PRINT 'Expected RSA-4096 wrapping key.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
RETURN
END
DECLARE @rsa int
EXEC @hr = sp_OACreate 'Chilkat.Rsa', @rsa OUT
EXEC sp_OASetProperty @rsa, 'PkcsPadding', 0
EXEC sp_OASetProperty @rsa, 'OaepHash', 'sha256'
EXEC sp_OASetProperty @rsa, 'OaepMgfHash', 'sha256'
EXEC sp_OAMethod @rsa, 'UsePublicKey', @success OUT, @wrappingKey
IF @success <> 1
BEGIN
PRINT 'Could not select wrapping key.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
EXEC sp_OAMethod @rsa, 'EncryptBd', @success OUT, @material, 0
IF @success <> 1
BEGIN
PRINT 'Could not wrap AES key material.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
EXEC sp_OAMethod @body, 'Clear', NULL
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'KeyId', @keyId
IF @success <> 1
BEGIN
PRINT 'Could not set KeyId.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ImportToken', @importToken
IF @success <> 1
BEGIN
PRINT 'Could not set ImportToken.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
EXEC sp_OAMethod @material, 'GetEncoded', @sTmp0 OUT, 'base64'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'EncryptedKeyMaterial', @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not set EncryptedKeyMaterial.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ExpirationModel', 'KEY_MATERIAL_DOES_NOT_EXPIRE'
IF @success <> 1
BEGIN
PRINT 'Could not set ExpirationModel.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ImportType', 'NEW_KEY_MATERIAL'
IF @success <> 1
BEGIN
PRINT 'Could not set ImportType.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'KeyMaterialDescription', 'Application rotation material'
IF @success <> 1
BEGIN
PRINT 'Could not set KeyMaterialDescription.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
EXEC sp_OAMethod @rest, 'ClearAllQueryParams', @success OUT
IF @success <> 1
BEGIN
PRINT 'Could not clear previous query parameters.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
-- Keep the JSON operation body separate from authentication parameters.
-- Include STS credentials in the signed query; never log the request URL.
EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'X-Amz-Security-Token', @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not set session token.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
END
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', 'TrentService.ImportKeyMaterial'
IF @success <> 1
BEGIN
PRINT 'Could not set operation.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/x-amz-json-1.1'
IF @success <> 1
BEGIN
PRINT 'Could not set content type.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
EXEC sp_OAMethod @body, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @rest, 'FullRequestString', @responseJson OUT, 'POST', '/', @sTmp0
EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
PRINT 'KMS HTTP status: ' + @iTmp0
EXEC sp_OAMethod @result, 'Load', @success OUT, @responseJson
IF @success = 1
BEGIN
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, '__type'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
PRINT 'AWS error code: ' + @sTmp0
END
END
EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-amzn-RequestId'
PRINT 'AWS request ID: ' + @sTmp0
-- Do not print full bodies, headers, credentials, key material, or tokens.
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
-- ImportKeyMaterial acceptance does not mean rotation has completed.
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @responseJson
DECLARE @stringMatch21 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch21 OUT, '', 1
IF @stringMatch21 <> 1
BEGIN
EXEC sp_OAMethod @result, 'Load', @success OUT, @responseJson
IF @success <> 1
BEGIN
PRINT 'Import accepted, but the response could not be parsed; reconcile before retrying.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyMaterialId'
EXEC sp_OAMethod @stringCompare, 'SetString', @success OUT, @sTmp0
DECLARE @stringMatch22 int
EXEC sp_OAMethod @stringCompare, 'ContentsEqual', @stringMatch22 OUT, '', 1
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'KeyMaterialId'
IF (@iTmp0 <> 1) or (@stringMatch22 = 1)
BEGIN
PRINT 'Missing or invalid KeyMaterialId.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'KeyMaterialId'
PRINT 'Imported material ID: ' + @sTmp0
END
ELSE
BEGIN
PRINT 'Import accepted; retrieve the material identifier/state with ListKeyRotations.'
END
PRINT 'Symmetric material import accepted; inspect ListKeyRotations before rotation.'
EXEC @hr = sp_OADestroy @stringCompare
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @material
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @expires
EXEC @hr = sp_OADestroy @wrappingKey
EXEC @hr = sp_OADestroy @rsa
END
GO