/*******************************************************************************
* 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.WAFRegional;
using Amazon.WAFRegional.Model;
namespace Amazon.PowerShell.Cmdlets.WAFR
{
///
///
/// This is AWS WAF Classic documentation. For more information, see AWS
/// WAF Classic in the developer guide.
/// For the latest version of AWS WAF, use the AWS WAFV2 API and see the AWS
/// WAF Developer Guide. With the latest version, AWS WAF has a single set of endpoints
/// for regional and global use.
///
/// Inserts or deletes ActivatedRule objects in a WebACL
. Each Rule
/// identifies web requests that you want to allow, block, or count. When you update a
/// WebACL
, you specify the following values:
/// -
/// A default action for the
WebACL
, either ALLOW
or BLOCK
.
/// AWS WAF performs the default action if a request doesn't match the criteria in any
/// of the Rules
in a WebACL
.
/// -
/// The
Rules
that you want to add or delete. If you want to replace one
/// Rule
with another, you delete the existing Rule
and add
/// the new one.
/// -
/// For each
Rule
, whether you want AWS WAF to allow requests, block requests,
/// or count requests that match the conditions in the Rule
.
/// -
/// The order in which you want AWS WAF to evaluate the
Rules
in a WebACL
.
/// If you add more than one Rule
to a WebACL
, AWS WAF evaluates
/// each request against the Rules
in order based on the value of Priority
.
/// (The Rule
that has the lowest value for Priority
is evaluated
/// first.) When a web request matches all the predicates (such as ByteMatchSets
/// and IPSets
) in a Rule
, AWS WAF immediately takes the corresponding
/// action, allow or block, and doesn't evaluate the request against the remaining Rules
/// in the WebACL
, if any.
///
/// To create and configure a WebACL
, perform the following steps:
/// -
/// Create and update the predicates that you want to include in
Rules
. For
/// more information, see CreateByteMatchSet, UpdateByteMatchSet, CreateIPSet,
/// UpdateIPSet, CreateSqlInjectionMatchSet, and UpdateSqlInjectionMatchSet.
/// -
/// Create and update the
Rules
that you want to include in the WebACL
.
/// For more information, see CreateRule and UpdateRule.
/// -
/// Create a
WebACL
. See CreateWebACL.
/// -
/// Use
GetChangeToken
to get the change token that you provide in the ChangeToken
/// parameter of an UpdateWebACL request.
/// -
/// Submit an
UpdateWebACL
request to specify the Rules
that
/// you want to include in the WebACL
, to specify the default action, and
/// to associate the WebACL
with a CloudFront distribution.
///
/// The ActivatedRule
can be a rule group. If you specify a rule group as
/// your ActivatedRule
, you can exclude specific rules from that rule group.
///
/// If you already have a rule group associated with a web ACL and want to submit an UpdateWebACL
/// request to exclude certain rules from that rule group, you must first remove the rule
/// group from the web ACL, the re-insert it again, specifying the excluded rules. For
/// details, see ActivatedRule$ExcludedRules .
///
/// Be aware that if you try to add a RATE_BASED rule to a web ACL without setting the
/// rule type when first creating the rule, the UpdateWebACL request will fail
/// because the request tries to add a REGULAR rule (the default rule type) with the specified
/// ID, which does not exist.
///
/// For more information about how to use the AWS WAF API to allow or block HTTP requests,
/// see the AWS WAF Developer
/// Guide.
///
///
[Cmdlet("Update", "WAFRWebACL", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
[OutputType("System.String")]
[AWSCmdlet("Calls the AWS WAF Regional UpdateWebACL API operation.", Operation = new[] {"UpdateWebACL"}, SelectReturnType = typeof(Amazon.WAFRegional.Model.UpdateWebACLResponse))]
[AWSCmdletOutput("System.String or Amazon.WAFRegional.Model.UpdateWebACLResponse",
"This cmdlet returns a System.String object.",
"The service call response (type Amazon.WAFRegional.Model.UpdateWebACLResponse) can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
)]
public partial class UpdateWAFRWebACLCmdlet : AmazonWAFRegionalClientCmdlet, IExecutor
{
#region Parameter ChangeToken
///
///
/// The value returned by the most recent call to GetChangeToken.
///
///
#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 ChangeToken { get; set; }
#endregion
#region Parameter DefaultAction_Type
///
///
/// Specifies how you want AWS WAF to respond to requests that match the settings in a
/// Rule
. Valid settings include the following:ALLOW
: AWS WAF allows requestsBLOCK
: AWS WAF blocks requestsCOUNT
: AWS WAF increments a counter of the requests that match all of
/// the conditions in the rule. AWS WAF then continues to inspect the web request based
/// on the remaining rules in the web ACL. You can't specify COUNT
for the
/// default action for a WebACL
.
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[AWSConstantClassSource("Amazon.WAFRegional.WafActionType")]
public Amazon.WAFRegional.WafActionType DefaultAction_Type { get; set; }
#endregion
#region Parameter Update
///
///
/// An array of updates to make to the WebACL.An array of WebACLUpdate
objects that you want to insert into or delete
/// from a WebACL. For more information, see the applicable data types:- WebACLUpdate: Contains
Action
and ActivatedRule
- ActivatedRule: Contains
Action
, OverrideAction
,
/// Priority
, RuleId
, and Type
. ActivatedRule|OverrideAction
/// applies only when updating or adding a RuleGroup
to a WebACL
.
/// In this case, you do not use ActivatedRule|Action
. For all other update
/// requests, ActivatedRule|Action
is used instead of ActivatedRule|OverrideAction
.
/// - WafAction: Contains
Type
///
///
[System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
[Alias("Updates")]
public Amazon.WAFRegional.Model.WebACLUpdate[] Update { get; set; }
#endregion
#region Parameter WebACLId
///
///
/// The WebACLId
of the WebACL that you want to update. WebACLId
/// is returned by CreateWebACL and by ListWebACLs.
///
///
#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 WebACLId { get; set; }
#endregion
#region Parameter Select
///
/// Use the -Select parameter to control the cmdlet output. The default value is 'ChangeToken'.
/// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.WAFRegional.Model.UpdateWebACLResponse).
/// Specifying the name of a property of type Amazon.WAFRegional.Model.UpdateWebACLResponse 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; } = "ChangeToken";
#endregion
#region Parameter PassThru
///
/// Changes the cmdlet behavior to return the value passed to the WebACLId parameter.
/// The -PassThru parameter is deprecated, use -Select '^WebACLId' instead. This parameter will be removed in a future version.
///
[System.Obsolete("The -PassThru parameter is deprecated, use -Select '^WebACLId' 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.WebACLId), MyInvocation.BoundParameters);
if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "Update-WAFRWebACL (UpdateWebACL)"))
{
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.WebACLId;
}
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
context.ChangeToken = this.ChangeToken;
#if MODULAR
if (this.ChangeToken == null && ParameterWasBound(nameof(this.ChangeToken)))
{
WriteWarning("You are passing $null as a value for parameter ChangeToken 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.DefaultAction_Type = this.DefaultAction_Type;
if (this.Update != null)
{
context.Update = new List(this.Update);
}
context.WebACLId = this.WebACLId;
#if MODULAR
if (this.WebACLId == null && ParameterWasBound(nameof(this.WebACLId)))
{
WriteWarning("You are passing $null as a value for parameter WebACLId 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.WAFRegional.Model.UpdateWebACLRequest();
if (cmdletContext.ChangeToken != null)
{
request.ChangeToken = cmdletContext.ChangeToken;
}
// populate DefaultAction
var requestDefaultActionIsNull = true;
request.DefaultAction = new Amazon.WAFRegional.Model.WafAction();
Amazon.WAFRegional.WafActionType requestDefaultAction_defaultAction_Type = null;
if (cmdletContext.DefaultAction_Type != null)
{
requestDefaultAction_defaultAction_Type = cmdletContext.DefaultAction_Type;
}
if (requestDefaultAction_defaultAction_Type != null)
{
request.DefaultAction.Type = requestDefaultAction_defaultAction_Type;
requestDefaultActionIsNull = false;
}
// determine if request.DefaultAction should be set to null
if (requestDefaultActionIsNull)
{
request.DefaultAction = null;
}
if (cmdletContext.Update != null)
{
request.Updates = cmdletContext.Update;
}
if (cmdletContext.WebACLId != null)
{
request.WebACLId = cmdletContext.WebACLId;
}
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.WAFRegional.Model.UpdateWebACLResponse CallAWSServiceOperation(IAmazonWAFRegional client, Amazon.WAFRegional.Model.UpdateWebACLRequest request)
{
Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "AWS WAF Regional", "UpdateWebACL");
try
{
#if DESKTOP
return client.UpdateWebACL(request);
#elif CORECLR
return client.UpdateWebACLAsync(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 ChangeToken { get; set; }
public Amazon.WAFRegional.WafActionType DefaultAction_Type { get; set; }
public List Update { get; set; }
public System.String WebACLId { get; set; }
public System.Func Select { get; set; } =
(response, cmdlet) => response.ChangeToken;
}
}
}