
    5                         d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	Z	d Z
d Zd Zd	 Zd
 Zd Zd Z	 ddZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!y)zBCode that's shared between multiple security policies subcommands.    N)
exceptions)yaml)resource_printerc           	         |dk(  rt        j                  |       }n	 t        j                  |       }|j                         }d|v r
|d   |_
        d|v r
|d   |_        d|v r,t        j                  |d   j                  d            |_        d|v r#|j                  j!                  |d         |_        d	|v r|j%                  |d	   d
         |_        d|v r|j)                  |j+                  |d   d   d               |_        |d   d   j/                  dg       D ].  }|j1                  |d         }d|v r
|d   |_        d|v r
|d   |_        d|v r
|d   |_        d|v r
|d   |_        d|v r
|d   |_        d|v r
|d   |_        d|v r
|d   |_        |j/                  dg       D ]p  }|jA                         }	d|v r
|d   |	_!        d|v r#|j@                  j!                  |d         |	_        d|v r
|d   |	_"        |jF                  jI                  |	       r |j,                  jJ                  jL                  jI                  |       1 d|d   v r|jO                         |j,                  _(        d|d   d   v r$|d   d   d   |j,                  jP                  _)        d |d   d   v r$|d   d   d    |j,                  jP                  _*        d!|d   d   v r$|d   d   d!   |j,                  jP                  _+        d"|d   d   v r$|d   d   d"   |j,                  jP                  _,        d#|d   d   v r=|j*                  j[                  |d   d   d#         |j,                  jJ                  _.        d$|v r|d$   }
|j_                         |_0        d%|
v r-|j^                  jc                  |
d%         |j`                  _2        d&|
v r4|jg                  |
d&   j/                  d'g       (      |j`                  _4        d)|
v r-|j^                  jk                  |
d)         |j`                  _6        d*|
v r|
d*   |j`                  _7        d+|
v r|
d+   |j`                  _8        d,|v r|js                  |jr                  ju                  |d,   d-         .      |_;        d/|d,   v r0|jr                  jy                  |d,   d/         |jv                  _=        d0|d,   v r|d,   d0   |jv                  _>        d1|v r3|j                         |_@        d2|d1   v r|d1   d2   |j                  _A        d3|v rg }|d3   D ]r  }|j                         }|d   |_C        |j                  j                  |d4         |_E        |d5   |_F        |d6   |_G        d7|v r
|d7   |_H        |jI                  |       t ||_I        g }d8|v r|d8   D ]  }|j                         }|d9   |_K        d|v r
|d   |_        d:|v r|j                         }d;|d:   v rt        |d:   d;   |      |_N        d<|d:   v r |j                  |d:   d<   d=   >      |_P        d?|d:   v rk|j                         }d@|d:   d?   v rJ|j                  |d:   d?   d@   j/                  dAg       |d:   d?   d@   j/                  dBg       C      |_S        ||_T        dD|d:   v r*dE|d:   dD   v r |j                  |d:   dD   dE   F      |_V        ||_W        dG|v r|j                         }d3|dG   v rKg }|dG   d3   D ]7  }|j                         }|d   |_C        |dH   |_Z        |jI                  |       9 ||_I        dE|dG   v r|dG   dE   |_[        dI|dG   v r|dG   dI   |_\        dJ|dG   v r|dG   dJ   |_]        dK|dG   v r|dG   dK   |_^        dL|dG   v r|dG   dL   |__        dM|dG   v r|dG   dM   |_`        dN|dG   v r|dG   dN   |_a        ||_b        t        |dO         |_d        dP|v r
|dP   |_e        |jI                  |       dQ|v r
|dQ   |_f        dR|v rt        |dR         |_g        dS|v rX|j                         }d|dS   v r&|j                  j!                  |dS   d         |_        dT|dS   v r|dS   dT   |_i        ||_j        dU|v rf|j                         }|dU   j/                  dVg       }g }|D ]*  }|jI                  |j                  |dW   |dX   Y             , |r||_m        ||_n        dZ|v r|dZ   }|j                  |j                  |d[   d\   |d[   d]   ^      |d_   |d`   a      |_q        db|v rI|j                         }dc|db   v r|db   dc   |_s        dd|db   v r|db   dd   |_t        ||j                  _u        de|v rb|j                         }d|de   v r&|j                  j!                  |de   d         |_        dT|de   v r|de   dT   |_i        ||j                  _v        df|v r.|j                  |df   d\   |df   d]   ^      |j                  _w        dg|v r|dg   |j                  _x        dh|v r-|j                  j                  |dh         |j                  _z        di|v r|di   |j                  _{        |j/                  djg       D ]l  }|j                         }dk|v r#|j                  j                  |dk         |_~        di|v r
|di   |_{        |j                  j                  jI                  |       n dl|v sX|j                         }|dl   j/                  dmg       D ]i  } |j                         }!dn| v r
| dn   |!_        | j/                  dog       D ]  }"|!j                  jI                  |"         | j/                  dpg       D ])  }#|!j                  jI                  t        |#|             + | j/                  dqg       D ])  }$|!j                  jI                  t        |$|             + | j/                  drg       D ])  }%|!j                  jI                  t        |%|             + | j/                  dsg       D ])  }&|!j                  jI                  t        |&|             + |j                  jI                  |!       l ||_         ||_        |S # t        $ r<}t	        j
                  dj                  t        j                  |                  d}~ww xY w)ta  Returns the security policy read from the given file.

  Args:
    input_file: file, A file with a security policy config.
    messages: messages, The set of available messages.
    file_format: string, the format of the file to read from

  Returns:
    A security policy resource.
  r   zError parsing JSON: {0}N	shortNamedescriptionfingerprintasciitypecloudArmorConfigenableMlr   adaptiveProtectionConfiglayer7DdosDefenseConfigenable)r   )r   thresholdConfigsname)r   autoDeployConfidenceThresholdautoDeployExpirationSec#autoDeployImpactedBaselineThresholdautoDeployLoadThresholddetectionAbsoluteQpsdetectionLoadThresholddetectionRelativeToBaselineQpstrafficGranularityConfigsenableEachUniqueValuevalueautoDeployConfigloadThresholdconfidenceThresholdimpactedBaselineThresholdexpirationSecruleVisibilityadvancedOptionsConfigjsonParsingjsonCustomConfigcontentTypesr'   logLevelrequestBodyInspectionSizeuserIpRequestHeadersddosProtectionConfigddosProtection)r-   ddosAdaptiveProtectionddosImpactedBaselineThresholdrecaptchaOptionsConfigredirectSiteKeyuserDefinedFieldsbaseoffsetsizemaskrulesactionmatchversionedExprexpr
expression)r<   exprOptionsrecaptchaOptionsactionTokenSiteKeyssessionTokenSiteKeys)r?   r@   configsrcIpRanges)rB   networkMatchvaluesdestIpRangesipProtocolssrcPorts	destPortssrcRegionCodessrcAsnsprioritypreviewredirectTarget
ruleNumberredirectOptionstargetheaderActionrequestHeadersToAdds
headerNameheaderValue)rS   rT   rateLimitOptionsrateLimitThresholdcountintervalSec)rW   rX   conformActionexceedAction)rV   rY   rZ   exceedActionRpcStatuscodemessageexceedRedirectOptionsbanThresholdbanDurationSecenforceOnKeyenforceOnKeyNameenforceOnKeyConfigsenforceOnKeyTypepreconfiguredWafConfig
exclusionstargetRuleSettargetRuleIdsrequestHeadersToExcluderequestCookiesToExcluderequestQueryParamsToExcluderequestUrisToExclude)r   loadjson
ValueErrorr   BadFileExceptionformatsix	text_typeSecurityPolicyr   r   base64urlsafe_b64decodeencoder	   TypeValueValuesEnumr   SecurityPolicyCloudArmorConfigr   &SecurityPolicyAdaptiveProtectionConfig=SecurityPolicyAdaptiveProtectionConfigLayer7DdosDefenseConfigr   getLSecurityPolicyAdaptiveProtectionConfigLayer7DdosDefenseConfigThresholdConfigr   r   r   r   r   r   r   dSecurityPolicyAdaptiveProtectionConfigLayer7DdosDefenseConfigThresholdConfigTrafficGranularityConfigr   r   r   appendr   r   6SecurityPolicyAdaptiveProtectionConfigAutoDeployConfigr   r   r    r!   r"   RuleVisibilityValueValuesEnumr#   #SecurityPolicyAdvancedOptionsConfigr$   JsonParsingValueValuesEnumr%   3SecurityPolicyAdvancedOptionsConfigJsonCustomConfigr&   LogLevelValueValuesEnumr)   r*   r+   "SecurityPolicyDdosProtectionConfigDdosProtectionValueValuesEnumr,   %DdosAdaptiveProtectionValueValuesEnumr.   r/   $SecurityPolicyRecaptchaOptionsConfigr0   r1   SecurityPolicyUserDefinedFieldr   BaseValueValuesEnumr3   r4   r5   r6   r2   SecurityPolicyRuler8   SecurityPolicyRuleMatcherConvertToEnumr:   Exprr;   $SecurityPolicyRuleMatcherExprOptions4SecurityPolicyRuleMatcherExprOptionsRecaptchaOptionsr>   r=   SecurityPolicyRuleMatcherConfigrA   r9    SecurityPolicyRuleNetworkMatcher5SecurityPolicyRuleNetworkMatcherUserDefinedFieldMatchrD   rB   rE   rF   rG   rH   rI   rJ   rC   intrK   rL   rM   rN   !SecurityPolicyRuleRedirectOptionsrP   rO   "SecurityPolicyRuleHttpHeaderAction2SecurityPolicyRuleHttpHeaderActionHttpHeaderOptionrR   rQ   "SecurityPolicyRuleRateLimitOptions+SecurityPolicyRuleRateLimitOptionsThresholdrU   +SecurityPolicyRuleRateLimitOptionsRpcStatusr\   r]   r[   r^   r_   r`   EnforceOnKeyValueValuesEnumra   rb   4SecurityPolicyRuleRateLimitOptionsEnforceOnKeyConfigEnforceOnKeyTypeValueValuesEnumrd   rc   (SecurityPolicyRulePreconfiguredWafConfig1SecurityPolicyRulePreconfiguredWafConfigExclusionrg   rh   ri   (ConvertPreconfigWafExclusionRequestFieldrj   rk   rl   rf   re   r7   )'
input_filemessagesfile_formatparsed_security_policyesecurity_policyparsed_threshold_configthreshold_config!parsed_traffic_granularity_configtraffic_granularity_configadvanced_options_configuser_defined_fieldsudfuser_defined_fieldr7   rulesecurity_policy_ruler9   expr_optionsnetwork_matchuser_defined_field_matchredirect_optionsheader_actionheaders_in_ruleheaders_to_addheader_to_addrate_limit_optionsexceed_action_rpc_statusexceed_redirect_optionsrA   enforce_on_key_configpreconfig_waf_config	exclusionexclusion_to_addtarget_rule_idrequest_headerrequest_cookierequest_query_paramrequest_uris'                                          Slib/googlecloudsdk/command_lib/compute/security_policies/security_policies_utils.pySecurityPolicyFromFiler      s    F!YYz2#yy4
 ++-/** 6{ CO,,"8"GO,,"(":":}-44W=#?O%%33"6*	,  11'/'N'N'(:;JG (O (IO$#9977$,JJ-.HI*,,46 K 7 	8 	9 , $:"$$!!$%7!<$= "nn&v. o  
),C	C#$CD 	6 
#&=	=3J%4
0 
/2I	I#$IJ 	< 
#&=	=3J%4
0 
 #:	:0G"1
- 
"%<	<2I$3
/ 
*-D	D#$DE 	7 0G/J/J
%r0
+ yy{ 	# #&GG/0GH %
: 66,4  -Z  -Z  -n  -n/7-
$
) 77-N.
$
* 	2299&	
%0, ..FFWW^^
q$=x 34NOO

I
I
K ..?	23MN
 
 ##=>!!02 	00AAO 
"8
$#&&8#: 
: ##=>!!68 	00AAU 
%(>
$)&&8): 
: ##=>!!<> 	00AA[ 
23MN
 
 ##=>!!02 	00AAO 12LM!# # 
P
P(($%?@(**:<= ..FFU
  6645LM446 )//

6
6%%%m46 ++7 44

F
F2!##&3~r#: G < ++< ,,

6
6
N
N%j13 ++4 #&==
!"=
> ++E !88
!"8
9 ++@5533#FF**&'=>!#$ 	4 	% ( 	!!"89	: EMDoDo  EV  EV
 !7
8&Eo**A 	(!"89	: !!7
8- **H
 !77557 *23KLL
 !9
:;L
M ,,< 22%&9:#BBD #F

1
1
E
E&k 
 #&h- #F	3"%f+  !34 ; )<O%
%&&&w/%88:$(N!	$	+/+>(	D224d7m+ -7mO,h!%
 T']"}}gv.|< % %* DM)!FFH,4=#??MM(,Wm(D*)c/4)-g})E**c0"5 N  ) +%
tG}$d7mH55#CC M(3MB D EL &+"	4	 AAC$~"66 "
.)*=>cNNP % -0K$).1(m$+&&'?@ ? -@-
)D00&*>&:=&I-
#T.11'+N';N'K-
$D00&*>&:=&I-
#n--#'#7
#C-
 $~..$($8$E-
!tN33)-n)=>N)O-
&^,,"&~"6y"A-
,9)&)$z*:&;#	d	'+I$ll'(	T	!.23C.D+		*-d<.@*A'	d	"#EEGT+,,88LL()&1 

 t-..$():$;H$E

!/?,	4	 CCE~.223I2N,M


II*<8+M: J  - /=-
,,9)	t	#!"45080[0['SS()=>wG./CD!  T   -_=+N; 1\ 	1
- #&88BBD # )*ABB,>'--$) ,-DEE/A'00$, ' 
/
/
E #&88$,$N$N$P
!)*ABB::NN&'>?G $(
 +,CDD-?'..#* & 
/
/
E //BB*>:7C0@O C  
/
/
< 11 !12 
/
/
> //?G?j?j  @G  @G 0@

/
/
< !33 !34 
/
/
@ ),,-BBGFKKM    6)5=5r5r  6S  6S)*6!2  6)5;<N5O!2

/
/
C
C
J
J# H 
"T	)==? 	 67;;L"MIHHJ  	)-6-G* )or Bn**11.A !C ).G Ln44;;8"H !M !*.G Ln44;;8"H !M &/]]+R&! 88??8'& ']]+A2Fk11888hO G 
)
)
0
01A
BA NB 7K3] 0`  /	e  ''(A(H(H
--
)  s   r9 9	s>7s99s>c                 8    |j                   j                  |       S )a  Converts a string version of a versioned expr to the enum representation.

  Args:
    versioned_expr: string, string version of versioned expr to convert.
    messages: messages, The set of available messages.

  Returns:
    A versioned expression enum.
  )r   VersionedExprValueValuesEnum)versioned_exprr   s     r   r   r     s      
	+	+	H	H
     c                     |j                         }d| v r#|j                   j                  | d         |_        d| v r
| d   |_        |S )aI  Converts the request field in preconfigured WAF exclusion configuration.

  Args:
    request_field_in_rule: a request field in preconfigured WAF exclusion
      configuration read from the security policy config file.
    messages: the set of available messages.

  Returns:
    The proto representation of the request field.
  opval)<SecurityPolicyRulePreconfiguredWafConfigExclusionFieldParamsOpValueValuesEnumr   r   )request_field_in_ruler   request_fields      r   r   r     s^     KKM 	""MM		06	7  ##-e4M	r   c                 4    t        j                  |||        y)a  Writes the given security policy in the given format to the given file.

  Args:
    output_file: file, File into which the security policy should be written.
    security_policy: resource, SecurityPolicy to be written out.
    file_format: string, the format of the file to write to.
  )print_formatoutN)r   Print)output_filer   r   s      r   WriteToFiler     s     K[Br   c                 r    | j                   }d}|j                  |j                  |j                        }|S )z1Returns a SecurityPolicyCloudArmorConfig message.Nr   )r   	enable_mlry   )clientargsr   cloud_armor_configs       r   CreateCloudArmorConfigr     sA     __(	^^!@@ A !	r   c                    | j                   }|	 |n|j                         }|j                  d      s|j                  d      r|j                  |j                  n|j	                         }|j                  d      r|j
                  |_        |j                  d      r*|j                  j                  |j                        |_	        ||_        |S )z9Returns a SecurityPolicyAdaptiveProtectionConfig message.enable_layer7_ddos_defense#layer7_ddos_defense_rule_visibility)
r   rz   IsSpecifiedr   r{   r   r   r   r   r#   )r   r   #existing_adaptive_protection_configr   adaptive_protection_configlayer7_ddos_defense_configs         r   CreateAdaptiveProtectionConfigr     s     __(-P.)FFH  34
<= &==I 
$	;	;SSU 
 45*.*I*I '=>

P
P((668 !/
 	# 6 
$#r   c                 .   | j                   }t        | ||      }|j                  d      s3|j                  d      s"|j                  d      s|j                  d      r|j                  |j                  n|j	                         }|j                  d      r|j
                  |_        |j                  d      r|j                  |_        |j                  d      r|j                  |_
        |j                  d      r|j                  |_        ||_        |S )zOReturns a SecurityPolicyAdaptiveProtectionConfig message with AutoDeployConfig..layer7_ddos_defense_auto_deploy_load_threshold4layer7_ddos_defense_auto_deploy_confidence_threshold;layer7_ddos_defense_auto_deploy_impacted_baseline_threshold.layer7_ddos_defense_auto_deploy_expiration_sec)r   r   r   r   r   r   r   r   r    r   r!   r   r"   )r   r   r   r   r   auto_deploy_configs         r   2CreateAdaptiveProtectionConfigWithAutoDeployConfigr     s'    __(=d7 9 
68;?;K;K
@<
GLM &66B 	#33GGI  HI

=
= &NO

C
C ,EG 
J
J 2HI

=
= & 3E/	##r   c                 $   | j                   }|	 |n|j                         }|j                  d      r*|j                  j                  |j                        |_        |j                  d      r!|j                  |j                        |_        |j                  d      r*|j                  j                  |j                        |_        |r"|j                  d      r|j                  |_        |j                  d      r|j                  |_        |S )z6Returns a SecurityPolicyAdvancedOptionsConfig message.json_parsingjson_custom_content_typesr(   	log_levelrequest_body_inspection_sizeuser_ip_request_headers)r   r   r   r   r   r%   r   r   r&   r   r   r)   r   r*   r   r+   )r   r    existing_advanced_options_configenable_large_body_sizer   r   s         r   CreateAdvancedOptionsConfigr   =  s   
 __(*J+&CCE  
n%44OO	 ' 
12DD77 	E 	9 , 
k"44LLNN	 $  0 0$! 	)) 5 
/0373O3O0	  r   c                     | j                   }|	 |n|j                         }|j                  d      r*|j                  j                  |j                        |_        |S )5Returns a SecurityPolicyDdosProtectionConfig message.network_ddos_protection)r   r   r   r   r   r-   r   r   existing_ddos_protection_configr   ddos_protection_configs        r   CreateDdosProtectionConfigr   c  sg     __()H*%BBD  
/033	&	&t'C'C	D ) 
 r   c                     | j                   }||n|j                         }|j                  d      r*|j                  j                  |j                        |_        |S )r    network_ddos_adaptive_protection)r   r   r   r   r   r.   r   s        r   4CreateDdosProtectionConfigWithDdosAdaptiveProtectionr   s  sm    
 __( 
)	4 &668 
 
894<4_4_  5F  5F--51 
 r   c                     | j                   }||n|j                         }|j                  d      r|j                  |_        |S )r   (network_ddos_impacted_baseline_threshold)r   r   r   r  r/   r   s        r   BCreateDdosProtectionConfigWithNetworkDdosImpactedBaselineThresholdr    sU    
 __( 
)	4 &668 
 
@A55 8 
 r   c                     | j                   }|	 |n|j                         }|j                  d      r*|j                  j                  |j                        |_        |S )r   ddos_protection)r   r   r   r   r  r-   r   s        r   CreateDdosProtectionConfigOldr    sg     __()H*%BBD  
'(33	&	&t';';	< ) 
 r   c                     | j                   }|	 |n|j                         }|j                  d      r|j                  |_        |S )z7Returns a SecurityPolicyRecaptchaOptionsConfig message.recaptcha_redirect_site_key)r   r   r   r  r1   )r   r   !existing_recaptcha_options_configr   recaptcha_options_configs        r   CreateRecaptchaOptionsConfigr    sR     __(+L,'DDF  
34/3/O/O,	!!r   c           
      f   | j                   }|j                         }d}|j                  d      s|j                  d      r]|j                         }|j                  d      r|j                  |_        |j                  d      r|j                  |_        ||_        d}|j                  d      rt        |j                        |_        d}|j                  d      rt        |j                        |_        d}|j                  d      s|j                  d      r|j                         }|j                  d      r3|j                  j!                  t#        |j$                              |_        |j                  d      r|j(                  |_        ||_        d}|j                  d	      r5|j                  j/                  t1        |j2                              |_        d}|j                  d
      r|j6                  |_        d}|j                  d      rug }|j:                  j=                         D ]M  \  }	}
|j?                  |jA                  |j@                  jC                  t1        |	            |
r|
nd             O ||_"        d}|j                  d      s|j                  d      r]|j                         }|j                  d      r|jF                  |_        |j                  d      r|jH                  |_        ||_%        d}|j                  d      r|jL                  |_'        d}|r|j                  d      s|j                  d      r]|jQ                         }|j                  d      r|jR                  |_*        |j                  d      r|jV                  |_,        ||_-        d}|r|S dS )z5Returns a SecurityPolicyRuleRateLimitOptions message.Frate_limit_threshold_count!rate_limit_threshold_interval_secTconform_actionexceed_actionexceed_redirect_typeexceed_redirect_targetenforce_on_keyenforce_on_key_nameenforce_on_key_configsN)rd   rb   ban_threshold_countban_threshold_interval_secban_duration_secexceed_action_rpc_status_code exceed_action_rpc_status_message).r   r   r   r   r  rW   r  rX   rV   _ConvertConformActionr  rY   _ConvertExceedActionr  rZ   r   rx   _ConvertRedirectTyper  r   r  rP   r^   r   ConvertEnforceOnKeyr  ra   r  rb   r  itemsr   r   r   rc   r  r  r_   r  r`   r   r  r\   r  r]   r[   )r   r   support_fairsharer   r   
is_updatedrate_limit_thresholdr   r  kvban_thresholdr   s                r   CreateRateLimitOptionsr&    s    __(BBD*
34
:;<<> 45#'#B#B ;<)-)O)O&,@)J	&''<($J	o&&:4;M;M&N#J
-.
/0&HHJ./

4
4
H
H"4#<#<=? " 01'+'B'B$/F,J	&'33OO 3 34	6 # J	+,*.*B*B'J	./++1131##

G
G'\\||%a(  %&q4	 H  4 .D*J
,-
34HHJM-. 44m45"&"A"Am&3#J	()(,(=(=%J
67
9:<<>  78&*&H&H#:;)-)N)N&/G,J)	3t3r   c                 *    ddij                  | |       S )Nallowr|   r8   s    r   r  r    s    
7				//r   c                 0    dddddj                  | |       S )Nz	deny(403)z	deny(404)z	deny(429)z	deny(502))zdeny-403zdeny-404zdeny-429zdeny-502r)  r*  s    r   r  r    s&    	

 Cr   c                 @    ddddddddd	d
dddj                  | |       S )NIPALL_IPSALLHTTP_HEADERXFF_IPHTTP_COOKIE	HTTP_PATHSNIREGION_CODETLS_JA3_FINGERPRINTUSER_IPTLS_JA4_FINGERPRINT)ipzall-ipsallzhttp-headerzxff-ipzhttp-cookiez	http-pathsnizregion-codeztls-ja3-fingerprintzuser-ipztls-ja4-fingerprintr)  )r  s    r   r  r    s>    """22
 C'(r   c                    | j                   }|j                         }d}|j                  d      r5|j                  j                  t	        |j
                              |_        d}|j                  d      rD|j                  |_        |j                  %|j                  j                  j                  |_        d}|r|S dS )z4Returns a SecurityPolicyRuleRedirectOptions message.Fredirect_typeTredirect_targetN)
r   r   r   rx   r  r=  r   r>  rP   EXTERNAL_302)r   r   r   r   r!  s        r   CreateRedirectOptionsr@  0  s     __(??A*	o&22FF !3!34	6  J 
'("22$

4
4
H
H<  J'	1T1r   c                 ,    dddj                  | |       S )NGOOGLE_RECAPTCHAr?  )zgoogle-recaptchazexternal-302r)  )r=  s    r   r  r  J  s     ,$
 C}%&r   c                 ,   |j                  d      s|j                  d      sy| j                  }|j                         }|j                  d      r|j                  |_        |j                  d      r|j
                  |_        |j                  |      S )z7Returns a SecurityPolicyRuleMatcherExprOptions message.recaptcha_action_site_keysrecaptcha_session_site_keysN)r>   )r   r   r   rD  r?   rE  r@   r   )r   r   r   recaptcha_optionss       r   CreateExpressionOptionsrG  Q  s    			"
:;__(CCE  
23,0,K,K)	34-1-M-M*		6	6( 
7 
 r   c                 H   | j                   }|j                         }g }d}t        |dd      yg }|j                  D ]N  }|j	                  d      }|d   }	|d   j	                  d      }
|j                  |j                  |	|
             P ||_        |j                  d	       d
}t        |dd      $|j                  |_	        |j                  d       d
}t        |dd      $|j                  |_        |j                  d       d
}t        |dd      $|j                  |_        |j                  d       d
}t        |dd      $|j                  |_        |j                  d       d
}t        |dd      $|j                   |_        |j                  d       d
}t        |dd      $|j$                  |_        |j                  d       d
}t        |dd      ;|j(                  D cg c]  }t+        |       c}|_        |j                  d       d
}|r||fS dg fS c c}w )z3Returns a SecurityPolicyRuleNetworkMatcher message.Fnetwork_user_defined_fieldsN;r      :)r   rD   z!network_match.user_defined_fieldsTnetwork_src_ip_rangesznetwork_match.src_ip_rangesnetwork_dest_ip_rangesznetwork_match.dest_ip_rangesnetwork_ip_protocolsznetwork_match.ip_protocolsnetwork_src_portsznetwork_match.src_portsnetwork_dest_portsznetwork_match.dest_portsnetwork_src_region_codesznetwork_match.src_region_codesnetwork_src_asnsznetwork_match.src_asns)r   r   getattrrI  splitr   r   r2   rM  rB   rN  rE   rO  rF   rP  rG   rQ  rH   rR  rI   rS  r   rJ   )r   r   r   network_matcherupdate_maskr!  r   r   parsedr   rD   asns               r   CreateNetworkMatcherrZ  g  sM    __(==?/+*T0$7C">> "'',fAYdays#f  

H
H I  ? )<O%:;JT*D1="&"<"<O45JT+T2>#'#>#>O 56JT)40<"&";";O34JT&-9#55O01JT'.: $ 7 7O12JT-t4@%)%B%BO"78JT%t,8373H3HI3HCs3x3HIO/0J+5/;	'ED":E	 Js   (Hc                 F   | j                   }|j                         }|j                  |_        |j                  j	                  t        |j                              |_        |j                  |_        |j                  |_        t        |dd      |j                  |_
        |S )z1Returns a SecurityPolicyUserDefinedField message.r6   N)r   r   user_defined_field_namer   r   _ConvertUserDefinedFieldBaser3   r4   r5   rT  r6   )r   r   r   r   s       r   CreateUserDefinedFieldr^    s     __(>>@ 88--AA
&tyy
1 
 #kk IIT64 ,"ii	r   c                 0    dddddj                  | |       S )NIPV4IPV6TCPUDP)ipv4ipv6tcpudpr)  )r3   s    r   r]  r]    s#    &u	E	I	I
D
 r   c                 ^   | j                   }|j                         }|j                  |_        |j	                  d      r|j
                  |_        |j	                  d      r|j                  |_        |j	                  d      r|j                  |_
        |j	                  d      r|j                  |_        |r|j	                  d      r|j                  |_        |j	                  d      r|j                  |_        |j	                  d      r|j"                  |_        |j	                  d      r~g }|j&                  D ]f  }|j)                         }d	|v r#|j(                  j+                  |d	         |_        d
|v r
|d
   |_        d|v r
|d   |_        |j3                  |       h ||_        |S )z_Returns a SecurityPolicyAdaptiveProtectionConfigLayer7DdosDefenseConfigThresholdConfig message.auto_deploy_load_threshold auto_deploy_confidence_threshold'auto_deploy_impacted_baseline_thresholdauto_deploy_expiration_secdetection_load_thresholddetection_absolute_qps"detection_relative_to_baseline_qpstraffic_granularity_configsr   r   r   )r   r}   threshold_config_namer   r   ri  r   rj  r   rk  r   rl  r   rm  r   rn  r   ro  r   rp  r~   rx   r   r   r   r   r   )r   r   support_granularity_configr   r   rp  arg_traffic_granularity_configr   s           r   &CreateLayer7DdosDefenseThresholdConfigrt    s   
 __([[]  44	23/3/N/N,	89-- 2 
?@44 8 
23/3/N/N,23040M0M-01.2.I.I+<=

1
1 5 56$&!,0,L,L
(yy{ 	# 33,4  -Z  -Z  -n  -n,V4-
$
) 44-K.
$
* #&DD,-DE %
: 	$**+EF! -M" 4O0	r   )F)"__doc__ru   rn   googlecloudsdk.callioper   googlecloudsdk.corer   googlecloudsdk.core.resourcer   rr   r   r   r   r   r   r   r   r   r   r   r  r  r  r&  r  r  r  r@  r  rG  rZ  r^  r]  rt   r   r   <module>rz     s    I   . $ 9 
cL,	B$:"$L LQ#!L   & & ""V4r0("24&,5Fp*7r   