/*******************************************************************************
* 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.SecretsManager;
using Amazon.SecretsManager.Model;
namespace Amazon.PowerShell.Cmdlets.SEC
{
///
/// Modifies the details of a secret, including metadata and the secret value. To change
/// the secret value, you can also use PutSecretValue.
///
///
///
/// To change the rotation configuration of a secret, use RotateSecret instead.
///
/// To change a secret so that it is managed by another service, you need to recreate
/// the secret in that service. See Secrets
/// Manager secrets managed by other Amazon Web Services services.
///
/// We recommend you avoid calling UpdateSecret
at a sustained rate of more
/// than once every 10 minutes. When you call UpdateSecret
to update the
/// secret value, Secrets Manager creates a new version of the secret. Secrets Manager
/// removes outdated versions when there are more than 100, but it does not remove versions
/// created less than 24 hours ago. If you update the secret value more than once every
/// 10 minutes, you create more versions than Secrets Manager removes, and you will reach
/// the quota for secret versions.
///
/// If you include SecretString
or SecretBinary
to create a
/// new secret version, Secrets Manager automatically moves the staging label AWSCURRENT
/// to the new version. Then it attaches the label AWSPREVIOUS
to the version
/// that AWSCURRENT
was removed from.
///
/// If you call this operation with a ClientRequestToken
that matches an
/// existing version's VersionId
, the operation results in an error. You
/// can't modify an existing version, you can only create a new version. To remove a version,
/// remove all staging labels from it. See UpdateSecretVersionStage.
///
/// Secrets Manager generates a CloudTrail log entry when you call this action. Do not
/// include sensitive information in request parameters except SecretBinary
/// or SecretString
because it might be logged. For more information, see
/// Logging
/// Secrets Manager events with CloudTrail.
/// Required permissions: secretsmanager:UpdateSecret
. For more
/// information, see
/// IAM policy actions for Secrets Manager and Authentication
/// and access control in Secrets Manager. If you use a customer managed key, you
/// must also have kms:GenerateDataKey
, kms:Encrypt
, and kms:Decrypt
/// permissions on the key. If you change the KMS key and you don't have kms:Encrypt
/// permission to the new key, Secrets Manager does not re-ecrypt existing secret versions
/// with the new key. For more information, see
/// Secret encryption and decryption.
///
///
[Cmdlet("Update", "SECSecret", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
[OutputType("Amazon.SecretsManager.Model.UpdateSecretResponse")]
[AWSCmdlet("Calls the AWS Secrets Manager UpdateSecret API operation.", Operation = new[] {"UpdateSecret"}, SelectReturnType = typeof(Amazon.SecretsManager.Model.UpdateSecretResponse))]
[AWSCmdletOutput("Amazon.SecretsManager.Model.UpdateSecretResponse",
"This cmdlet returns an Amazon.SecretsManager.Model.UpdateSecretResponse object containing multiple properties. The object can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class UpdateSECSecretCmdlet : AmazonSecretsManagerClientCmdlet, IExecutor
{
protected override bool IsSensitiveRequest { get; set; } = true;
#region Parameter ClientRequestToken
///
///
/// If you include SecretString
or SecretBinary
, then Secrets
/// Manager creates a new version for the secret, and this parameter specifies the unique
/// identifier for the new version.If you use the Amazon Web Services CLI or one of the Amazon Web Services SDKs to call
/// this operation, then you can leave this parameter empty. The CLI or SDK generates
/// a random UUID for you and includes it as the value for this parameter in the request.
/// If you don't use the SDK and instead generate a raw HTTP request to the Secrets Manager
/// service endpoint, then you must generate a ClientRequestToken
yourself
/// for the new version and include the value in the request.This value becomes the VersionId
of the new version.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String ClientRequestToken { get; set; }
#endregion
#region Parameter Description
///
///
/// The description of the secret.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String Description { get; set; }
#endregion
#region Parameter KmsKeyId
///
///
/// The ARN, key ID, or alias of the KMS key that Secrets Manager uses to encrypt new
/// secret versions as well as any existing versions with the staging labels AWSCURRENT
,
/// AWSPENDING
, or AWSPREVIOUS
. If you don't have kms:Encrypt
/// permission to the new key, Secrets Manager does not re-ecrypt existing secret versions
/// with the new key. For more information about versions and staging labels, see Concepts:
/// Version.A key alias is always prefixed by alias/
, for example alias/aws/secretsmanager
.
/// For more information, see About
/// aliases.If you set this to an empty string, Secrets Manager uses the Amazon Web Services managed
/// key aws/secretsmanager
. If this key doesn't already exist in your account,
/// then Secrets Manager creates it for you automatically. All users and roles in the
/// Amazon Web Services account automatically have access to use aws/secretsmanager
.
/// Creating aws/secretsmanager
can result in a one-time significant delay
/// in returning the result. You can only use the Amazon Web Services managed key aws/secretsmanager
/// if you call this operation using credentials from the same Amazon Web Services account
/// that owns the secret. If the secret is in a different account, then you must use a
/// customer managed key and provide the ARN of that KMS key in this field. The user making
/// the call must have permissions to both the secret and the KMS key in their respective
/// accounts.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public System.String KmsKeyId { get; set; }
#endregion
#region Parameter SecretBinary
///
///
/// The binary data to encrypt and store in the new version of the secret. We recommend
/// that you store your binary data in a file and then pass the contents of the file as
/// a parameter. Either SecretBinary
or SecretString
must have a value, but
/// not both.You can't access this parameter in the Secrets Manager console.
///
/// The cmdlet will automatically convert the supplied parameter of type string, string[], System.IO.FileInfo or System.IO.Stream to byte[] before supplying it to the service.
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Amazon.PowerShell.Common.MemoryStreamParameterConverter]
public byte[] SecretBinary { get; set; }
#endregion
#region Parameter SecretId
///
///
/// The ARN or name of the secret.For an ARN, we recommend that you specify a complete ARN rather than a partial ARN.
/// See Finding
/// a secret from a partial ARN.
///
///
#if !MODULAR
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
#else
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true)]
[System.Management.Automation.AllowEmptyString]
[System.Management.Automation.AllowNull]
#endif
[Amazon.PowerShell.Common.AWSRequiredParameter]
public System.String SecretId { get; set; }
#endregion
#region Parameter SecretString
///
///
/// The text data to encrypt and store in the new version of the secret. We recommend
/// you use a JSON structure of key/value pairs for your secret value. Either SecretBinary
or SecretString
must have a value, but
/// not both.
///
///
[System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true)]
public System.String SecretString { 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.SecretsManager.Model.UpdateSecretResponse).
/// Specifying the name of a property of type Amazon.SecretsManager.Model.UpdateSecretResponse 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 SecretString parameter.
/// The -PassThru parameter is deprecated, use -Select '^SecretString' instead. This parameter will be removed in a future version.
///
[System.Obsolete("The -PassThru parameter is deprecated, use -Select '^SecretString' instead. This parameter will be removed in a future version.")]
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter PassThru { get; set; }
#endregion
#region Parameter Force
///
/// This parameter overrides confirmation prompts to force
/// the cmdlet to continue its operation. This parameter should always
/// be used with caution.
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
public SwitchParameter Force { get; set; }
#endregion
protected override void ProcessRecord()
{
this._AWSSignerType = "v4";
base.ProcessRecord();
var resourceIdentifiersText = FormatParameterValuesForConfirmationMsg(nameof(this.SecretId), MyInvocation.BoundParameters);
if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "Update-SECSecret (UpdateSecret)"))
{
return;
}
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.SecretString;
}
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
context.ClientRequestToken = this.ClientRequestToken;
context.Description = this.Description;
context.KmsKeyId = this.KmsKeyId;
context.SecretBinary = this.SecretBinary;
context.SecretId = this.SecretId;
#if MODULAR
if (this.SecretId == null && ParameterWasBound(nameof(this.SecretId)))
{
WriteWarning("You are passing $null as a value for parameter SecretId 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.SecretString = this.SecretString;
// 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)
{
System.IO.MemoryStream _SecretBinaryStream = null;
try
{
var cmdletContext = context as CmdletContext;
// create request
var request = new Amazon.SecretsManager.Model.UpdateSecretRequest();
if (cmdletContext.ClientRequestToken != null)
{
request.ClientRequestToken = cmdletContext.ClientRequestToken;
}
if (cmdletContext.Description != null)
{
request.Description = cmdletContext.Description;
}
if (cmdletContext.KmsKeyId != null)
{
request.KmsKeyId = cmdletContext.KmsKeyId;
}
if (cmdletContext.SecretBinary != null)
{
_SecretBinaryStream = new System.IO.MemoryStream(cmdletContext.SecretBinary);
request.SecretBinary = _SecretBinaryStream;
}
if (cmdletContext.SecretId != null)
{
request.SecretId = cmdletContext.SecretId;
}
if (cmdletContext.SecretString != null)
{
request.SecretString = cmdletContext.SecretString;
}
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;
}
finally
{
if( _SecretBinaryStream != null)
{
_SecretBinaryStream.Dispose();
}
}
}
public ExecutorContext CreateContext()
{
return new CmdletContext();
}
#endregion
#region AWS Service Operation Call
private Amazon.SecretsManager.Model.UpdateSecretResponse CallAWSServiceOperation(IAmazonSecretsManager client, Amazon.SecretsManager.Model.UpdateSecretRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "AWS Secrets Manager", "UpdateSecret");
try
{
#if DESKTOP
return client.UpdateSecret(request);
#elif CORECLR
return client.UpdateSecretAsync(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 ClientRequestToken { get; set; }
public System.String Description { get; set; }
public System.String KmsKeyId { get; set; }
public byte[] SecretBinary { get; set; }
public System.String SecretId { get; set; }
public System.String SecretString { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response;
}
}
}