/*******************************************************************************
* Copyright 2012-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file.
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* *****************************************************************************
*
* AWS Tools for Windows (TM) PowerShell (TM)
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using Amazon.PowerShell.Common;
using Amazon.Runtime;
using Amazon.KeyManagementService;
using Amazon.KeyManagementService.Model;
namespace Amazon.PowerShell.Cmdlets.KMS
{
///
/// Returns the public key and an import token you need to import or reimport key material
/// for a KMS key.
///
///
///
/// By default, KMS keys are created with key material that KMS generates. This operation
/// supports Importing
/// key material, an advanced feature that lets you generate and import the cryptographic
/// key material for a KMS key. For more information about importing key material into
/// KMS, see Importing
/// key material in the Key Management Service Developer Guide.
///
/// Before calling GetParametersForImport
, use the CreateKey operation
/// with an Origin
value of EXTERNAL
to create a KMS key with
/// no key material. You can import key material for a symmetric encryption KMS key, HMAC
/// KMS key, asymmetric encryption KMS key, or asymmetric signing KMS key. You can also
/// import key material into a multi-Region
/// key of any supported type. However, you can't import key material into a KMS key
/// in a custom key
/// store. You can also use GetParametersForImport
to get a public key
/// and import token to reimport
/// the original key material into a KMS key whose key material expired or was deleted.
/// GetParametersForImport
returns the items that you need to import your
/// key material.
/// -
/// The public key (or "wrapping key") of an RSA key pair that KMS generates.
///
/// You will use this public key to encrypt ("wrap") your key material while it's in transit
/// to KMS.
///
-
/// A import token that ensures that KMS can decrypt your key material and associate it
/// with the correct KMS key.
///
/// The public key and its import token are permanently linked and must be used together.
/// Each public key and import token set is valid for 24 hours. The expiration date and
/// time appear in the ParametersValidTo
field in the GetParametersForImport
/// response. You cannot use an expired public key or import token in an ImportKeyMaterial
/// request. If your key and token expire, send another GetParametersForImport
/// request.
/// GetParametersForImport
requires the following information:
/// -
/// The key ID of the KMS key for which you are importing the key material.
///
-
/// The key spec of the public key ("wrapping key") that you will use to encrypt your
/// key material during import.
///
-
/// The wrapping algorithm that you will use with the public key to encrypt your key material.
///
/// You can use the same or a different public key spec and wrapping algorithm each time
/// you import or reimport the same key material.
///
/// The KMS key that you use for this operation must be in a compatible key state. For
/// details, see Key
/// states of KMS keys in the Key Management Service Developer Guide.
/// Cross-account use: No. You cannot perform this operation on a KMS key in a
/// different Amazon Web Services account.
/// Required permissions: kms:GetParametersForImport
/// (key policy)
/// Related operations:
///
[Cmdlet("Get", "KMSParametersForImport")]
[OutputType("Amazon.KeyManagementService.Model.GetParametersForImportResponse")]
[AWSCmdlet("Calls the AWS Key Management Service GetParametersForImport API operation.", Operation = new[] {"GetParametersForImport"}, SelectReturnType = typeof(Amazon.KeyManagementService.Model.GetParametersForImportResponse))]
[AWSCmdletOutput("Amazon.KeyManagementService.Model.GetParametersForImportResponse",
"This cmdlet returns an Amazon.KeyManagementService.Model.GetParametersForImportResponse object containing multiple properties. The object can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class GetKMSParametersForImportCmdlet : AmazonKeyManagementServiceClientCmdlet, IExecutor
{
protected override bool IsSensitiveResponse { get; set; } = true;
#region Parameter KeyId
///
///
/// The identifier of the KMS key that will be associated with the imported key material.
/// The Origin
of the KMS key must be EXTERNAL
.All KMS key types are supported, including multi-Region keys. However, you cannot
/// import key material into a KMS key in a custom key store.Specify the key ID or key ARN of the KMS key.For example:- Key ID:
1234abcd-12ab-34cd-56ef-1234567890ab
- Key ARN:
arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab
To get the key ID and key ARN for a KMS key, use ListKeys or DescribeKey.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true)]
#else
[System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true, Mandatory = true)]
[System.Management.Automation.AllowEmptyString]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
public System.String KeyId { get; set; }
#endregion
#region Parameter WrappingAlgorithm
///
///
/// The algorithm you will use with the RSA public key (PublicKey
) in the
/// response to protect your key material during import. For more information, see Select
/// a wrapping algorithm in the Key Management Service Developer Guide.For RSA_AES wrapping algorithms, you encrypt your key material with an AES key that
/// you generate, then encrypt your AES key with the RSA public key from KMS. For RSAES
/// wrapping algorithms, you encrypt your key material directly with the RSA public key
/// from KMS.The wrapping algorithms that you can use depend on the type of key material that you
/// are importing. To import an RSA private key, you must use an RSA_AES wrapping algorithm.- RSA_AES_KEY_WRAP_SHA_256 — Supported for wrapping RSA and ECC key material.
- RSA_AES_KEY_WRAP_SHA_1 — Supported for wrapping RSA and ECC key material.
- RSAES_OAEP_SHA_256 — Supported for all types of key material, except RSA key
/// material (private key).You cannot use the RSAES_OAEP_SHA_256 wrapping algorithm with the RSA_2048 wrapping
/// key spec to wrap ECC_NIST_P521 key material.
- RSAES_OAEP_SHA_1 — Supported for all types of key material, except RSA key
/// material (private key).You cannot use the RSAES_OAEP_SHA_1 wrapping algorithm with the RSA_2048 wrapping
/// key spec to wrap ECC_NIST_P521 key material.
- RSAES_PKCS1_V1_5 (Deprecated) — Supported only for symmetric encryption key
/// material (and only in legacy mode).
///
///
#if !MODULAR
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
#else
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true)]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
[AWSConstantClassSource("Amazon.KeyManagementService.AlgorithmSpec")]
public Amazon.KeyManagementService.AlgorithmSpec WrappingAlgorithm { get; set; }
#endregion
#region Parameter WrappingKeySpec
///
///
/// The type of RSA public key to return in the response. You will use this wrapping key
/// with the specified wrapping algorithm to protect your key material during import.
/// Use the longest RSA wrapping key that is practical. You cannot use an RSA_2048 public key to directly wrap an ECC_NIST_P521 private key.
/// Instead, use an RSA_AES wrapping algorithm or choose a longer RSA public key.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
#else
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true)]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
[AWSConstantClassSource("Amazon.KeyManagementService.WrappingKeySpec")]
public Amazon.KeyManagementService.WrappingKeySpec WrappingKeySpec { get; set; }
#endregion
#region Parameter Select
///
/// Use the -Select parameter to control the cmdlet output. The default value is '*'.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.KeyManagementService.Model.GetParametersForImportResponse).
/// Specifying the name of a property of type Amazon.KeyManagementService.Model.GetParametersForImportResponse will result in that property being returned.
/// Specifying -Select '^ParameterName' will result in the cmdlet returning the selected cmdlet parameter value.
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public string Select { get; set; } = "*";
#endregion
#region Parameter PassThru
///
/// Changes the cmdlet behavior to return the value passed to the KeyId parameter.
/// The -PassThru parameter is deprecated, use -Select '^KeyId' instead. This parameter will be removed in a future version.
///
[System.Obsolete("The -PassThru parameter is deprecated, use -Select '^KeyId' instead. This parameter will be removed in a future version.")]
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter PassThru { get; set; }
#endregion
protected override void ProcessRecord()
{
this._AWSSignerType = "v4";
base.ProcessRecord();
var context = new CmdletContext();
// allow for manipulation of parameters prior to loading into context
PreExecutionContextLoad(context);
#pragma warning disable CS0618, CS0612 //A class member was marked with the Obsolete attribute
if (ParameterWasBound(nameof(this.Select)))
{
context.Select = CreateSelectDelegate(Select) ??
throw new System.ArgumentException("Invalid value for -Select parameter.", nameof(this.Select));
if (this.PassThru.IsPresent)
{
throw new System.ArgumentException("-PassThru cannot be used when -Select is specified.", nameof(this.Select));
}
}
else if (this.PassThru.IsPresent)
{
context.Select = (response, cmdlet) => this.KeyId;
}
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
context.KeyId = this.KeyId;
#if MODULAR
if (this.KeyId == null && ParameterWasBound(nameof(this.KeyId)))
{
WriteWarning("You are passing $null as a value for parameter KeyId which is marked as required. In case you believe this parameter was incorrectly marked as required, report this by opening an issue at https://github.com/aws/aws-tools-for-powershell/issues.");
}
#endif
context.WrappingAlgorithm = this.WrappingAlgorithm;
#if MODULAR
if (this.WrappingAlgorithm == null && ParameterWasBound(nameof(this.WrappingAlgorithm)))
{
WriteWarning("You are passing $null as a value for parameter WrappingAlgorithm which is marked as required. In case you believe this parameter was incorrectly marked as required, report this by opening an issue at https://github.com/aws/aws-tools-for-powershell/issues.");
}
#endif
context.WrappingKeySpec = this.WrappingKeySpec;
#if MODULAR
if (this.WrappingKeySpec == null && ParameterWasBound(nameof(this.WrappingKeySpec)))
{
WriteWarning("You are passing $null as a value for parameter WrappingKeySpec which is marked as required. In case you believe this parameter was incorrectly marked as required, report this by opening an issue at https://github.com/aws/aws-tools-for-powershell/issues.");
}
#endif
// allow further manipulation of loaded context prior to processing
PostExecutionContextLoad(context);
var output = Execute(context) as CmdletOutput;
ProcessOutput(output);
}
#region IExecutor Members
public object Execute(ExecutorContext context)
{
var cmdletContext = context as CmdletContext;
// create request
var request = new Amazon.KeyManagementService.Model.GetParametersForImportRequest();
if (cmdletContext.KeyId != null)
{
request.KeyId = cmdletContext.KeyId;
}
if (cmdletContext.WrappingAlgorithm != null)
{
request.WrappingAlgorithm = cmdletContext.WrappingAlgorithm;
}
if (cmdletContext.WrappingKeySpec != null)
{
request.WrappingKeySpec = cmdletContext.WrappingKeySpec;
}
CmdletOutput output;
// issue call
var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint);
try
{
var response = CallAWSServiceOperation(client, request);
object pipelineOutput = null;
pipelineOutput = cmdletContext.Select(response, this);
output = new CmdletOutput
{
PipelineOutput = pipelineOutput,
ServiceResponse = response
};
}
catch (Exception e)
{
output = new CmdletOutput { ErrorResponse = e };
}
return output;
}
public ExecutorContext CreateContext()
{
return new CmdletContext();
}
#endregion
#region AWS Service Operation Call
private Amazon.KeyManagementService.Model.GetParametersForImportResponse CallAWSServiceOperation(IAmazonKeyManagementService client, Amazon.KeyManagementService.Model.GetParametersForImportRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "AWS Key Management Service", "GetParametersForImport");
try
{
#if DESKTOP
return client.GetParametersForImport(request);
#elif CORECLR
return client.GetParametersForImportAsync(request).GetAwaiter().GetResult();
#else
#error "Unknown build edition"
#endif
}
catch (AmazonServiceException exc)
{
var webException = exc.InnerException as System.Net.WebException;
if (webException != null)
{
throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException);
}
throw;
}
}
#endregion
internal partial class CmdletContext : ExecutorContext
{
public System.String KeyId { get; set; }
public Amazon.KeyManagementService.AlgorithmSpec WrappingAlgorithm { get; set; }
public Amazon.KeyManagementService.WrappingKeySpec WrappingKeySpec { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response;
}
}
}