
    G                     d    d Z ddlmZ ddlmZ ddlmZ ddlmZ  G d de      Z	 G d d	e      Z
y
)zSecurity policy.    )absolute_import)division)unicode_literals)
exceptionsc                   t    e Zd ZdZddZed        Zed        Zd Zd Z	d Z
dd	Zdd
ZddZddZ	 ddZy)SecurityPolicyz"Abstracts SecurityPolicy resource.Nc                      || _         || _        y Nref_compute_clientselfr   compute_clients      >lib/googlecloudsdk/api_lib/compute/security_policies/client.py__init__zSecurityPolicy.__init__       DH)D    c                 .    | j                   j                  S r
   r   apitools_clientr   s    r   _clientzSecurityPolicy._client       ///r   c                 .    | j                   j                  S r
   r   messagesr   s    r   	_messageszSecurityPolicy._messages#       (((r   c                    t        | j                  dd       }|a| j                  j                  d| j                  j                  | j                  j                  || j                  j                               fS | j                  j                  d| j                  j                  | j                  j                  | j                  j                               fS )NregionDeleteprojectr!   securityPolicyr$   r%   )
getattrr   r   regionSecurityPoliciesr   *ComputeRegionSecurityPoliciesDeleteRequestr$   NamesecurityPolicies$ComputeSecurityPoliciesDeleteRequestr   r!   s     r   _MakeDeleteRequestTuplez&SecurityPolicy._MakeDeleteRequestTuple'   s    TXXx.Fll118nnGG((**!% H 23 3
 LL))8NN??(( @ JK Kr   c                    t        | j                  dd       }|a| j                  j                  d| j                  j                  | j                  j                  || j                  j                               fS | j                  j                  d| j                  j                  | j                  j                  | j                  j                               fS )Nr!   Getr#   r&   )
r'   r   r   r(   r   'ComputeRegionSecurityPoliciesGetRequestr$   r*   r+   !ComputeSecurityPoliciesGetRequestr-   s     r   _MakeDescribeRequestTuplez(SecurityPolicy._MakeDescribeRequestTuple3   s    TXXx.Fll115nnDD((**!% E 23 3
 LL))5NN<<(( = JK Kr   c                 V   t        | j                  dd       }|I| j                  j                  d| j                  j                  | j                  j                  ||      fS | j                  j                  d| j                  j                  | j                  j                  |      fS )Nr!   Insertr#   r&   )	r'   r   r   r(   r   *ComputeRegionSecurityPoliciesInsertRequestr$   r+   $ComputeSecurityPoliciesInsertRequest)r   security_policyr!   s      r   _MakeCreateRequestTuplez&SecurityPolicy._MakeCreateRequestTuple?   s    TXXx.Fll118nnGG((**!0 H 23 3
 LL))8NN??(( @ JK Kr   c           	         t        | j                  dd      }||rc| j                  j                  d| j                  j                  | j                  j                  || j                  j                         ||      fS | j                  j                  d| j                  j                  | j                  j                  || j                  j                         |      fS | j                  j                  d| j                  j                  | j                  j                  | j                  j                         |      fS )zGenerates a SecurityPolicies Patch request.

    Args:
      security_policy: The updated security policy
      update_mask: Field mask for clearing fields

    Returns:
      A tuple containing the resource collection, verb, and request.
    r!   NPatch)r$   r!   r%   securityPolicyResource
updateMask)r$   r!   r%   r<   )r$   r%   r<   )
r'   r   r   r(   r   )ComputeRegionSecurityPoliciesPatchRequestr$   r*   r+   #ComputeSecurityPoliciesPatchRequest)r   r8   update_maskr!   s       r   _MakePatchRequestTuplez%SecurityPolicy._MakePatchRequestTupleK   s    TXXx.F	LL//NNDD((#xx}}'6& E 

 
	
 ll117nnFF((**!%)8	 G :; ; LL))7NN>>((#xx}}'6 ? 89 9r   c                 b    | j                         g}|s| j                  j                  |      S |S r
   r.   r   MakeRequestsr   only_generate_requestrequestss      r   r"   zSecurityPolicy.Deletep   2    ,,./H !!..x88Or   c                 b    | j                         g}|s| j                  j                  |      S |S r
   r3   r   rD   rE   s      r   DescribezSecurityPolicy.Describev   2    ..01H !!..x88Or   c                 d    | j                  |      g}|s| j                  j                  |      S |S r
   r9   r   rD   )r   r8   rF   rG   s       r   CreatezSecurityPolicy.Create|   s4    ,,_=>H !!..x88Or   c                 f    | j                  ||      g}|s| j                  j                  |      S |S r
   rA   r   rD   )r   r8   
field_maskrF   rG   s        r   r;   zSecurityPolicy.Patch   s8     ++OZHIH !!..x88Or   r
   F)NF)NNF)__name__
__module____qualname____doc__r   propertyr   r   r.   r3   r9   rA   r"   rK   rO   r;    r   r   r   r      se    ** 0 0 ) )
K
K
K#9J JOr   r   c                       e Zd ZdZddZed        Zed        Zd Zd Z	d Z
d	 Zd
 Zd Zd ZddZddZ	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)SecurityPolicyRulez&Abstracts SecurityPolicyRule resource.Nc                      || _         || _        y r
   r   r   s      r   r   zSecurityPolicyRule.__init__   r   r   c                 .    | j                   j                  S r
   r   r   s    r   r   zSecurityPolicyRule._client   r   r   c                 .    | j                   j                  S r
   r   r   s    r   r   zSecurityPolicyRule._messages   r   r   c                     	 t        |      }|dk  rt        j                  dd      |S # t        $ r t        j                  dd      w xY w)Npriorityz.priority must be a valid non-negative integer.r   )int
ValueErrorcalliope_exceptionsInvalidArgumentException)r   r`   int_prioritys      r   _ConvertPriorityToIntz(SecurityPolicyRule._ConvertPriorityToInt   sj    H]l a88
FH H  H88
FH HHs	   *  A
c                 2    ddddddj                  ||      S )Nz	deny(403)z	deny(404)z	deny(502)redirect_to_recaptcharate_based_ban)zdeny-403zdeny-404zdeny-502zredirect-to-recaptchazrate-based-ban)get)r   actions     r   _ConvertActionz!SecurityPolicyRule._ConvertAction   s)    !8* 
c&&r   c           	      j   t        | j                  dd       | j                  j                  d| j                  j                  | j                  j                  | j                  | j                  j                               | j                  j                  | j                  j                        fS | j                  j                  d| j                  j                  | j                  j                  | j                  | j                  j                               | j                  j                        fS )Nr!   
RemoveRuler$   r`   r!   r%   r$   r`   r%   )r'   r   r   r(   r   .ComputeRegionSecurityPoliciesRemoveRuleRequestr$   rf   r*   r!   r%   r+   (ComputeSecurityPoliciesRemoveRuleRequestr   s    r   r.   z*SecurityPolicyRule._MakeDeleteRequestTuple   s    txx4(4ll11<nnKK((**55dhhmmoF!%!8!8	 L :; ; LL))<NNCC((33DHHMMOD#xx66 D 89 9r   c           	      j   t        | j                  dd       | j                  j                  d| j                  j                  | j                  j                  | j                  | j                  j                               | j                  j                  | j                  j                        fS | j                  j                  d| j                  j                  | j                  j                  | j                  | j                  j                               | j                  j                        fS )Nr!   GetRulero   rp   )r'   r   r   r(   r   +ComputeRegionSecurityPoliciesGetRuleRequestr$   rf   r*   r!   r%   r+   %ComputeSecurityPoliciesGetRuleRequestr   s    r   r3   z,SecurityPolicyRule._MakeDescribeRequestTuple   s    txx4(4ll119nnHH((**55dhhmmoF!%!8!8	 I :; ; LL))9NN@@((33DHHMMOD#xx66 A 89 9r   c                    |rW| j                   j                  | j                  | j                  j	                               || j                  |      ||      }n|r[| j                   j                  | j                   j                  j                  d      | j                   j                  |            }n:|J | j                   j                  | j                   j                  |            }|r||_
        | j                   j                  | j                  | j                  j	                               || j                  |      ||      }|||_        |
| j                  |
      |_        |	|	|_        t        | j                  d	d      r| j                   j"                  d
| j                   j%                  | j                  j&                  || j                  j(                  | j                  j*                        fS | j                   j,                  d
| j                   j/                  | j                  j&                  || j                  j*                        fS )a5  Generates a SecurityPolicies AddRule request.

    Args:
      src_ip_ranges: The list of IP ranges to match.
      expression: The CEVAL expression to match.
      expression_options: The configuration options when specifying a CEVAL
        expression.
      network_matcher: Net LB fields to match.
      action: The action to enforce on match.
      description: The description of the rule.
      preview: If true, the action will not be enforced.
      redirect_options: Parameters defining the redirect action, such as
        redirect type and redirect target.
      rate_limit_options: The rate limiting behavior for this rule.
      request_headers_to_add: A list of headers to add to requests that match
        this rule.

    Returns:
      A tuple containing the resource collection, verb, and request.
    r`   descriptionrk   networkMatchpreview
SRC_IPS_V1srcIpRangesversionedExprconfigN
expressionexprr`   ry   rk   matchr{   r!   AddRule)r$   securityPolicyRuler!   r%   )r$   r   r%   )r   r[   rf   r   r*   rl   SecurityPolicyRuleMatcherVersionedExprValueValuesEnumSecurityPolicyRuleMatcherConfigExprexprOptionsredirectOptions_ConvertRequestHeadersToAddheaderActionrateLimitOptionsr'   r   r(   +ComputeRegionSecurityPoliciesAddRuleRequestr$   r!   r%   r+   %ComputeSecurityPoliciesAddRuleRequest)r   src_ip_rangesr   expression_optionsnetwork_matcherrk   ry   r{   redirect_optionsrate_limit_optionsrequest_headers_to_addsecurity_policy_rulematchers                r   r9   z*SecurityPolicyRule._MakeCreateRequestTuple   s4   B !^^>>--dhhmmo>!$$V,& ?  
..::..BB__ >>AA) B 	 ; 
 %%%..::$$
$; ; 
 
0!^^>>--dhhmmo>!$$V, ?  #-=*)*.*J*J
 +"' %.@+txx4(4ll119nnHH((**%9!%!8!8	 I :; ; LL))9NN@@((#7#xx66 A 89 9r   c           
         |rX| j                   j                  | j                  | j                  j	                               || j                  |      ||      }nd}|r[| j                   j                  | j                   j                  j                  d      | j                   j                  |            }n8|r6| j                   j                  | j                   j                  |            }|r#|| j                   j                         }||_
        | j                   j                  | j                  | j                  j	                               || j                  |      ||      }|||_        |
| j                  |
      |_        |	|	|_        |||_        t!        | j                  d	d      | j"                  j$                  d
| j                   j'                  | j                  j(                  | j                  | j                  j	                               || j                  j*                  | j                  j,                  |      fS | j"                  j.                  d
| j                   j1                  | j                  j(                  | j                  | j                  j	                               || j                  j,                  |      fS )a  Generates a SecurityPolicies PatchRule request.

    Args:
      src_ip_ranges: The list of IP ranges to match.
      expression: The CEVAL expression to match.
      expression_options: The configuration options when specifying a CEVAL
        expression.
      network_matcher: Net LB fields to match.
      action: The action to enforce on match.
      description: The description of the rule.
      preview: If true, the action will not be enforced.
      redirect_options: Parameters defining the redirect action, such as
        redirect type and redirect target.
      rate_limit_options: The rate limiting behavior for this rule.
      request_headers_to_add: A list of headers to add to requests that match
        this rule.
      preconfig_waf_config: preconfigured WAF configuration to be applied for
        this rule.
      update_mask: Field mask for clearing fields

    Returns:
      A tuple containing the resource collection, verb, and request.
    rx   Nr|   r}   r   r   r   r   r!   	PatchRule)r$   r`   r   r!   r%   r=   )r$   r`   r   r%   r=   )r   r[   rf   r   r*   rl   r   r   r   r   r   r   r   r   r   preconfiguredWafConfigr'   r   r(   -ComputeRegionSecurityPoliciesPatchRuleRequestr$   r!   r%   r+   'ComputeSecurityPoliciesPatchRuleRequest)r   r   r   r   r   rk   ry   r{   r   r   r   preconfig_waf_configr@   r   r   s                  r   rA   z)SecurityPolicyRule._MakePatchRequestTuple"  s   L !^^>>--dhhmmo>!$$V,& ?  g	..::..BB__ >>AA) B 	 ; 
 ..::$$
$; ; 
 
?NN<<>'0!^^>>--dhhmmo>!$$V, ?  #-=*)*.*J*J
 +"' %.@+'4H1txx4(4
,,
-
-

..
F
Fhh&&11$((--/B!5XX__!XX44$ G   LL));NNBB((33DHHMMOD#7#xx66& C () )r   c                 (   | j                   j                         }|j                         D ]d  \  }}| j                   j                         }|j	                         |_        |j                         |_        |j                  j                  |       f |S )aT  Converts a request-headers-to-add string list into an HttpHeaderAction.

    Args:
      request_headers_to_add: A dict of headers to add to requests that match
        this rule. Leading whitespace in each header name and value is stripped.

    Returns:
      An HttpHeaderAction object with a populated request_headers_to_add field.
    )
r   "SecurityPolicyRuleHttpHeaderActionitems2SecurityPolicyRuleHttpHeaderActionHttpHeaderOptionstrip
headerNamelstripheaderValuerequestHeadersToAddsappend)r   r   header_actionhdr_namehdr_valheader_to_adds         r   r   z.SecurityPolicyRule._ConvertRequestHeadersToAdd  s}     NNEEGM399;'
..
K
K
M !)!1m").."2m((//> < r   c                 b    | j                         g}|s| j                  j                  |      S |S r
   rC   rE   s      r   r"   zSecurityPolicyRule.Delete  rH   r   c                 b    | j                         g}|s| j                  j                  |      S |S r
   rJ   rE   s      r   rK   zSecurityPolicyRule.Describe  rL   r   c                 v    | j                  |||||||||	|

      g}|s| j                  j                  |      S |S )zDMake and optionally send a request to Create a security policy rule.rN   )r   r   r   r   r   rk   ry   r{   r   r   r   rF   rG   s                r   rO   zSecurityPolicyRule.Create  sX      	$$"	
H !!!..x88Or   c                 z    | j                  |||||||||	|
||      g}|s| j                  j                  |      S |S )zCMake and optionally send a request to Patch a security policy rule.rQ   )r   r   r   r   r   rk   ry   r{   r   r   r   r   r@   rF   rG   s                  r   r;   zSecurityPolicyRule.Patch  s^    $ 	##" 	
H  !!!..x88Or   r
   rS   )NNNNNNFNNNF)NNNNNNNNNNNNF)rT   rU   rV   rW   r   rX   r   r   rf   rl   r.   r3   r9   rA   r   r"   rK   rO   r;   rY   r   r   r[   r[      s    .* 0 0 ) )	99V9pg)R& !!F !!#r   r[   N)rW   
__future__r   r   r   googlecloudsdk.callioper   rc   objectr   r[   rY   r   r   <module>r      s3     &  ' EpV pfc cr   