
    d                         S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKrSr	Sr
S	rS
 rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rg)z,Utils for GKE Hub Identity Service commands.    )absolute_import)division)unicode_literalsN)
exceptions      i  c                 \   [        U R                  5      S:w  a  [        R                  " S5      eU R                  S   n[	        U5        [        U5      n[        U5      nUc  Uc  [        R                  " S5      eUR                  5       n[        XA5      Ul	        [        X15      Ul        U$ )a  Load FeatureSpec MemberConfig from the parsed ClientConfig CRD yaml file.

Args:
  loaded_config: YamlConfigFile, The data loaded from the ClientConfig CRD
    yaml file given by the user. YamlConfigFile is from
    googlecloudsdk.command_lib.anthos.common.file_parsers.
  msg: The gkehub messages package.

Returns:
  member_config: The MemberConfig configuration containing the AuthMethods for
    the IdentityServiceFeatureSpec.
   z1Input config file must contain one YAML document.r   zOA valid 'spec.identityServiceOptions' or 'spec.authentication' must be provided)lendatar   Errorvalidate_clientconfig_metaget_auth_methodsget_identity_service_optionsIdentityServiceMembershipSpec5validate_and_construct_identity_service_options_protoidentityServiceOptions)validate_and_construct_auth_methods_protoauthMethods)loaded_configmsgclientconfigauth_methodsidentity_service_optionsmember_configs         Hlib/googlecloudsdk/command_lib/container/fleet/identity_service/utils.pyparse_configr   #   s     			!


N
OO##A&,\*!,/,9,G6>


	 
 335-;
" &
 H- 
    c                 j    U S   nUb  SU;  a  g US   (       d  [         R                  " S5      eUS   $ )Nspecr   z?Must provide a valid option under 'spec.identityServiceOptions'r   r   r   r    s     r   r   r   K   sI    	f	$	\-T9	&	'


I  
&	''r   c           	          U c  g[         [        S.nUR                  5       nU  HB  nXB;  a%  [        R                  " SR                  U5      5      e[        UUX$   " X5      5        MD     U$ )a  Constructs an IdentityServiceMembershipSpec.IdentityServiceIdentityServiceOptions instance from the provided `identity_service_options_config` config.

Args:
  identity_service_options_config: a map of non-protocol-related configuration
    options from the applied configuration.
  msg: The gkehub message package

Returns:
  an instance of
  IdentityServiceMembershipSpec.IdentityServiceIdentityServiceOptions

Raises:
  exceptions.Error: if an unsupported option is found under
  `spec.IdentityServiceOptions`
N)sessionDurationdiagnosticInterfacez@Invalid option '{}' provided under 'spec.identityServiceOptions')parse_session_durationparse_diagnostic_interface%IdentityServiceIdentityServiceOptionsr   r   formatsetattr)identity_service_options_configr   supported_optionsidentity_service_options_protooptions        r   r   r   V   s~    $ %,/7 $'#L#L#N /f&
L6&>  &!#G 0 
('r   c                     US   n[        U[        5      (       a  U[        :  d
  U[        :  a.  [        R
                  " SR                  [        [        5      5      e[        US-  5      S-   $ )Nr$   zS'spec.identityServiceOptions.sessionDuration' must be an integer between {} and {}.<   s)
isinstanceintSESSION_DURATION_MINSESSION_DURATION_MAXr   r   r)   str)_r+   session_durations      r   r&   r&   }   sl    45FG	$c	*	*--	0	0


	$f%9;OP  
"	#c	))r   c                    US   n[        U[        5      (       a  SU;  d  SU;  a  [        R                  " S5      eU R	                  5       nU H|  nUS:X  a  US   Ul        M  US:X  a>   [        R                  R                  US   R                  5       S5      nUS   Ul	        MY  [        R                  " SR                  U5      5      e   U$ ! [         a    [        SR                  US   5      5      ef = f)a  Constructs an IdentityServiceDiagnosticInterface instance from the provided config `identity_service_options_config`.

Args:
  msg: The gkehub message package
  identity_service_options_config: a map of non-protocol-related configuration
    options from the applied configuration.

Returns:
  an instance of IdentityServiceDiagnosticInterface

Raises:
   ValueError: if the value provided in `diagnosticInterface.expirationTime`
   is not RFC3339-compliant.
r%   enabledexpirationTimezhRequired fields 'diagnosticInterface.enabled' and 'diagnosticInterface.expirationTime' must be provided.z%Y-%m-%dT%H:%M:%S%zz'{}' is invalid for the field 'diagnosticInterface.expirationTime'. Please, provide a valid date in the '%Y-%m-%dT%H:%M:%S%z' format.zPUnknown field '{}' found under 'spec.identityServiceOptions.diagnosticInterface')r2   dictr   r   "IdentityServiceDiagnosticInterfacer:   datetimestrptime__str__r;   
ValueErrorr)   )r   r+   diagnostic_interface_configdiagnostic_interface_protokeyr7   s         r   r'   r'      s2    !@! 0$
7
7	5	5	!<	<


	B   #EEG(c
i+F
, ( 
 	 
&&'(89AAC!
 5P5
"1 ??Evc{ / )6 
$#  
99?+,<=:
 	

s   +;C(C9c                 j    U S   nUb  SU;  a  g US   (       d  [         R                  " S5      eUS   $ )Nr    authenticationz>Must provide a valid configuration under 'spec.authentication'r!   r"   s     r   r   r      sI    	f	$	\%T1		


H  
	r   c                    U c  / $ [        U 5      nU[        :  a,  SR                  U[        5      n[        R                  " U5      e[
        [        [        [        [        S.nSS1n/ nU  H  nUR                  5        Vs/ s H  oU;  d  M
  UPM     n	n[        U	5      S:w  a  [        R                  " S5      eU	S   n
X;  a%  [        R                  " SR                  U
5      5      eXJ   " Xq5      nUR                  U5        M     U$ s  snf )	at  Constructs a list of IdentityServiceMembershipSpec.IdentityServiceAuthMethod from the given auth methods config.

Args:
  auth_methods_config: A list of providers from the applied configuration
  msg: The GKE Hub message package

Returns:
  a list of
  IdentityServiceMembershipSpec.IdentityServiceAuthMethod

Raises:
  exceptions.Error: if the provided config is invalid
ziThe provided configuration contains {} identity providers. The maximum number that can be provided is {}.)oidcgoogleazureADsamlldapnameproxyr
   zIExactly one identity protocol can be configured per authentication methodr   zEUnsupported identity protocol [{}] found under 'spec.authentication'.)r   MAX_AUTH_PROVIDERSr)   r   r   provision_oidc_configprovision_google_configprovision_azuread_configprovision_saml_configprovision_ldap_configkeysappend)auth_methods_configr   auth_methods_counterr_msgsupported_protocolsauth_method_metaauth_methods_protoauth_method_configrD   	protocolsprotocolauth_methods               r   r   r      s4     I./,,	9f!34  

7
###')## g&/)..00?O4O0   9~  |H*$$*F8$4  &/0BHKk*! 0" 
!s    	D
D
c                 <    SU ;  a  [         R                  " S5      eg)zValidate the basics of the parsed clientconfig yaml for AIS Hub Feature Spec.

Args:
  clientconfig: The data field of the YamlConfigFile.
r    zMissing required field 'spec'.Nr!   )r   s    r   r   r     s#     <


;
<<  r   c                     UR                  5       nSU ;  a  [        R                  " S5      eU S   Ul        SU ;   a
  U S   Ul        SU ;   a  [        U S   S5      Ul        U$ )a  Provision FeatureSpec LdapConfig Server from the parsed yaml file.

Args:
  ldap_server_config: YamlConfigFile, The ldap server data loaded from the
    yaml file given by the user. YamlConfigFile is from
    googlecloudsdk.command_lib.anthos.common.file_parsers.
  msg: The gkehub messages package.

Returns:
  member_config: A MemberConfig configuration containing server details of a
  single LDAP auth method for the IdentityServiceFeatureSpec.
hostz4LDAP Authentication method must contain server host.connectionTypecertificateAuthorityDatazutf-8)IdentityServiceServerConfigr   r   rc   rd   bytesre   )ldap_server_configr   servers      r   provision_ldap_server_configrj     s     **,& %%


>  #6*&+ ++./?@F#55&+56'F# 
-r   c                 h   U c  [         R                  " S5      eUR                  5       nSU ;   an  UR                  5       Ul        U S   nUS   (       a
  US   (       d  [         R                  " S5      eUS   UR                  l        US   UR                  l        U$ [         R                  " S5      e)a  Provision FeatureSpec LdapConfig ServiceAccount from the parsed yaml file.

Args:
  ldap_service_account_config: YamlConfigFile, The ldap service account data
    loaded from the yaml file given by the user. YamlConfigFile is from
    googlecloudsdk.command_lib.anthos.common.file_parsers.
  msg: The gkehub messages package.

Returns:
  member_config: A MemberConfig configuration containing the service account
   details of a single LDAP auth method for the IdentityServiceFeatureSpec.
z@LDAP Authentication method must contain Service Account details.simpleBindCredentialsdnpasswordzNLDAP Authentication method must contain non-empty Service Account credentials.zHUnknown service account type. Supported types are: simpleBindCredentials)r   r   #IdentityServiceServiceAccountConfig$IdentityServiceSimpleBindCredentialsrl   rm   rn   )ldap_service_account_configr   service_accountldap_simple_bind_credentialss       r   %provision_ldap_service_account_configrt   /  s     !(


J  ;;=/  ;;002 ) $?$  ).+J7  0L0O)), 	%Z0 ))2 P	 r   c                     UR                  5       nSU ;  a  [        R                  " S5      eU S   Ul        SU ;   a
  U S   Ul        SU ;   a
  U S   Ul        SU ;   a
  U S   Ul        U$ )a  Provision FeatureSpec LdapConfig User from the parsed yaml file.

Args:
  ldap_user_config: YamlConfigFile, The ldap user data loaded from the yaml
    file given by the user. YamlConfigFile is from
    googlecloudsdk.command_lib.anthos.common.file_parsers.
  msg: The gkehub messages package.

Returns:
  member_config: A MemberConfig configuration containing the user details of a
  single LDAP auth method for the IdentityServiceFeatureSpec.
baseDnz4LDAP Authentication method must contain user baseDn.loginAttributeidAttributefilter)IdentityServiceUserConfigr   r   rv   rw   rx   ry   )ldap_user_configr   users      r   provision_ldap_user_configr}   ^  s     
	&	&	($ %%


>  !*$+ ))*+;<D&&'6D!!"8,DK	+r   c                     UR                  5       nSU ;  a  [        R                  " S5      eU S   Ul        SU ;   a
  U S   Ul        SU ;   a
  U S   Ul        U$ )a  Provision FeatureSpec LdapConfig Group from the parsed yaml file.

Args:
  ldap_group_config: YamlConfigFile, The ldap group data loaded from the yaml
    file given by the user. YamlConfigFile is from
    googlecloudsdk.command_lib.anthos.common.file_parsers.
  msg: The gkehub messages package.

Returns:
  member_config: A MemberConfig configuration containing the group details of
  a single LDAP auth method for the IdentityServiceFeatureSpec.
rv   z5LDAP Authentication method must contain group baseDn.rx   ry   )IdentityServiceGroupConfigr   r   rv   rx   ry   )ldap_group_configr   groups      r   provision_ldap_group_configr     ss     
(
(
*% &&


?  #8,%, '')-8E""$X.EL	,r   c                 :   SU ;  a  [         R                  " S5      eUR                  5       nU S   Ul        SU ;   a
  U S   Ul        U S   nSU;  d  SU;  d  SU;  a*  SR                  U S   5      n[         R                  " U5      eUR                  5       Ul        [        US   U5      UR                  l	        [        US   U5      UR                  l        [        US   U5      UR                  l        S	U;   a  [        US	   U5      UR                  l        U$ )
a  Provision FeatureSpec LdapConfig from the parsed yaml file.

Args:
  auth_method: YamlConfigFile, The data loaded from the yaml file given by the
    user. YamlConfigFile is from
    googlecloudsdk.command_lib.anthos.common.file_parsers.
  msg: The gkehub messages package.

Returns:
  member_config: A MemberConfig configuration containing a single
  LDAP auth method for the IdentityServiceFeatureSpec.
rM   z-LDAP Authentication method must contain name.rN   rL   ri   r|   serviceAccountzPAuthentication method [{}] must contain server, user and serviceAccount details.r   )r   r   IdentityServiceAuthMethodrM   rN   r)   IdentityServiceLdapConfig
ldapConfigrj   ri   rt   r   r}   r|   r   r   )r`   r   auth_method_protoldap_configrY   s        r   rT   rT     sB    ;


J
KK335&v.)'2F#+ k!	{	"		,	3f[ !  

7
##!$!>!>!@(D(S)% ,K8H,I3O - 'A&3'#
 )DGc*  & 
r   c                    SU ;  a  [         R                  " S5      eUR                  5       nU S   Ul        U S   nSU;  d  SU;  a  [         R                  " S5      eUR	                  5       Ul        US   UR
                  l        US   UR
                  l        [        UR
                  R                  U S   5        SU ;   a
  U S   Ul	        SU;   a  US   UR
                  l
        S	U;   a  US	   UR
                  l        S
U;   a  US
   UR
                  l        SU;   a  US   UR
                  l        SU;   a  US   UR
                  l        UR
                  R                  (       dC  UR
                  R                  (       a(  [         R                  " SR                  U S   5      5      eSU;   a  US   UR
                  l        SU;   a  US   UR
                  l        SU;   a  US   UR
                  l        SU;   a  US   UR
                  l        SU;   a  US   UR
                  l        SU;   a  US   UR
                  l        U$ )a  Provision FeatureSpec OIDCConfig from the parsed yaml file.

Args:
  auth_method: YamlConfigFile, The data loaded from the yaml file given by the
    user. YamlConfigFile is from
    googlecloudsdk.command_lib.anthos.common.file_parsers.
  msg: The gkehub messages package.

Returns:
  member_config: A MemberConfig configuration containing a single
    OIDC auth method for the IdentityServiceFeatureSpec.
rM   z-OIDC Authentication method must contain name.rH   	issuerURIclientIDzBinput config file OIDC Config must contain issuerURI and clientID.rN   re   deployCloudConsoleProxyextraParamsgroupPrefixgroupsClaimzIgroupPrefix should be empty for method [{}] because groupsClaim is empty.kubectlRedirectURIscopes	userClaim
userPrefixclientSecretenableAccessToken)r   r   r   rM   IdentityServiceOidcConfig
oidcConfig	issuerUriclientIdvalidate_issuer_urirN   re   r   r   r   r   r)   kubectlRedirectUrir   r   r   r   r   )r`   r   r   oidc_configs       r   rP   rP     sw    ;


J
KK335&v.F#+ #z'D


L  "%!>!>!@+6{+C(*5j*A'"",,k&.A
 )'2  ;.<G"=  9 +-;F!<  8 k!/:=/I  ,k!/:=/I  ,k!/:=/I  , 
&
&
2
2

&
&
2
2


	&V,- 
 [(6A7  3 *5h*?  'K-8-E  *[ .9,.G  +{"0;N0K  -K'5@6  2 
r   c                    SU ;  a  [         R                  " S5      eUR                  5       nU S   Ul        U S   nUR	                  5       Ul        / SQnU Vs/ s H  nXS;  d  M
  UPM     nnU(       a8  [         R                  " SR                  U S   SR                  U5      5      5      eUS   UR
                  l        US   UR
                  l	        US	   UR
                  l
        S
U;   a  US
   UR
                  l        SU;   a  US   UR
                  l        SU;   a  US   UR
                  l        SU;   a  US   UR
                  l        SU;   a  UR                  R                  5       UR
                  l        US   R#                  5        Hd  u  pxUR                  R                  R%                  5       n	Xyl        Xl        UR
                  R                   R*                  R-                  U	5        Mf     U$ s  snf )a  Provision FeatureSpec SamlConfig from the parsed configuration file.

Args:
  auth_method: YamlConfigFile, The data loaded from the yaml file given by the
    user. YamlConfigFile is from
    googlecloudsdk.command_lib.anthos.common.file_parsers.
  msg: The gkehub messages package.

Returns:
  member_config: A MemberConfig configuration containing a single SAML
  auth method for the IdentityServiceFeatureSpec.
rM   z-SAML Authentication method must contain name.rK   )idpEntityIDidpSingleSignOnURIidpCertificateDataListzFThe following fields are not set for the authentication method {} : {}z, r   r   r   userAttributegroupsAttributer   r   attributeMapping)r   r   r   rM   IdentityServiceSamlConfig
samlConfigr)   joinidentityProviderIdidentityProviderSsoUriidentityProviderCertificatesr   r   r   r   AttributeMappingValuer   itemsAdditionalPropertyrD   valueadditionalPropertiesrV   )
r`   r   r   saml_configrequired_fields
field_nameunset_required_fieldsattribute_keyattribute_valueattribute_map_items
             r   rS   rS   *  s$    ;


J
KK335&v.F#+!$!>!>!@/ ('*		& '  
 


P	F#TYY/D%E	F  5@4N18C95 ?J?;
 #1<_1M  .+%3>4  0 [ .9,.G  +k!/:=/I  ,;&%%;;=   1 +6+eg+& 
'
'
=
=
P
P
R   -!0""33HHOO
+ 
as   	G=&G=c                 J   SU ;  a  [         R                  " S5      eUR                  5       nU S   Ul        U S   nUR	                  5       Ul        SU ;   a
  U S   Ul        SU;  a(  [         R                  " SR                  U S   5      5      eUS   UR
                  l        U$ )a  Provision FeatureSpec GoogleConfig from the parsed configuration file.

Args:
  auth_method: YamlConfigFile, The data loaded from the yaml file given by the
    user. YamlConfigFile is from
    googlecloudsdk.command_lib.anthos.common.file_parsers.
  msg: The gkehub messages package.

Returns:
  member_config: A MemberConfig configuration containing a single Google
  auth method for the IdentityServiceFeatureSpec.
rM   z/Google Authentication method must contain name.rI   rN   disablezAThe "disable" field is not set for the authentication method "{}")	r   r   r   rM   IdentityServiceGoogleConfiggoogleConfigrN   r)   r   )r`   r   r   google_configs       r   rQ   rQ   z  s     ;


L
MM335&v.h'-#&#B#B#D  )'2 m#


K	F#	$& & ,9+C  (	r   c                 R   SU ;  a  [         R                  " S5      eUR                  5       nU S   Ul        UR	                  5       Ul        SU ;   a
  U S   Ul        U S   nSU;  d  SU;  d  SU;  a*  SR                  U S   5      n[         R                  " U5      eUS   UR
                  l        US   UR
                  l	        US   UR
                  l
        S	U;   a  US	   UR
                  l        S
U;   a  US
   UR
                  l        SU;   a  US   UR
                  l        U$ )a  Provision FeatureSpec AzureADConfig from the parsed yaml file.

Args:
  auth_method: YamlConfigFile, The data loaded from the yaml file given by the
    user. YamlConfigFile is from
    googlecloudsdk.command_lib.anthos.common.file_parsers.
  msg: The gkehub messages package.

Returns:
  member_config: A MemberConfig configuration containing a single
  Azure AD auth method for the IdentityServiceFeatureSpec.
rM   z0AzureAD Authentication method must contain name.rN   rJ   r   r   tenantzQAuthentication method [{}] must contain clientID, kubectlRedirectURI, and tenant.r   r   groupFormat)r   r   r   rM   IdentityServiceAzureADConfigazureadConfigrN   r)   r   r   r   r   r   r   )r`   r   r   azuread_configrY   s        r   rR   rR     sU    ;


M
NN335&v.$'$D$D$F!)'2y). &	^	3		'	4f[ !  

7
##-;J-G!!*7E8!!4 ,:(+C!!( ~%3A4##0 N"0>{0K##-n$2@2O##/	r   c                 4   [         R                  R                  U 5      nUR                  S:w  a%  [        R
                  " SR                  U5      5      eUR                  b7  SUR                  ;   a&  [        R
                  " SR                  US5      5      egg)zValidates Issuer URI field of OIDC config.

Args:
  issuer_uri: issuer uri to be validated
  auth_method_name: auth method name that has this field
httpsz:issuerURI is invalid for method [{}]. Scheme is not https.Nz .well-known/openid-configurationzHissuerURI is invalid for method [{}]. issuerURI should not contain [{}].)urllib3util	parse_urlschemer   r   r)   path)
issuer_uriauth_method_nameurls      r   r   r     s     	z*#ZZ7


DKK	
 
 	XX@CHHL


	(*LM  Mr   )__doc__
__future__r   r   r   r>   googlecloudsdk.corer   r   rO   r4   r5   r   r   r   r&   r'   r   r   r   rj   rt   r}   r   rT   rP   rS   rQ   rR   r    r   r   <module>r      s    3 &  '  *      %P($(N
*7$t 2j=B,^D@5pOdM` F3lr   