
    @                        S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKJ	r	  SSK
Jr  SSK
Jr  SS	KJr  SSKr " S
 S\R                   5      r " S S\5      r " S S\5      r " S S\5      rSrS rS r " S S\R2                  " \R4                  \5      5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S \5      r  " S! S"\5      r! " S# S$\5      r" " S% S&\#5      r$g)'z'Generators for Credential Config Files.    )absolute_import)division)unicode_literalsN)enterprise_certificate_config)log)
properties)filesc                       \ rS rSrSrSrSrg)
ConfigType           N)__name__
__module____qualname____firstlineno__WORKLOAD_IDENTITY_POOLSWORKFORCE_POOLS__static_attributes__r       Alib/googlecloudsdk/command_lib/iam/byoid_utilities/cred_config.pyr   r       s    /r   r   c                   8    \ rS rSrSr   SS jr\S 5       rSrg)ByoidEndpoints%   zBase class for BYOID endpoints.c                     U(       a  U(       a  US:w  a  [        S5      eSU l        SU l        Xl        U(       a  SOSU l        X0l        X@l        g )NglobalzGmTLS is not supported with locational Security Token Service endpoints.z"https://{service}.{mtls}{universe}z/https://{service}.{sts_location}.rep.{universe}zmtls. )GeneratorError_sts_global_template_sts_locational_template_service_mtls_universe_domain_sts_location)selfserviceenable_mtlsuniverse_domainsts_locations        r   __init__ByoidEndpoints.__init__(   sV     |(@  !ED9 	! M'RDJ+%r   c                 (   U R                   (       a  U R                   S:X  a9  U R                  R                  U R                  U R                  U R
                  S9$ U R                  R                  U R                  U R                   U R
                  S9$ )Nr   )r'   mtlsuniverse)r'   r*   r/   )r%   r    formatr"   r#   r$   r!   )r&   s    r   	_base_urlByoidEndpoints._base_url?   s    !3!3x!?&&----djj4;P;P .   ((//''&& 0  r   )r#   r"   r    r%   r!   r$   N)Fzgoogleapis.comr   )	r   r   r   r   __doc__r+   propertyr1   r   r   r   r   r   r   %   s)    '
 &&. 	 	r   r   c                   \   ^  \ rS rSrSrU 4S jr\S 5       r\S 5       r\S 5       r	Sr
U =r$ )StsEndpointsL   z$Simple class to build STS endpoints.c                 .   > [         [        U ]
  " S0 UD6  g )N)sts)superr6   r+   )r&   kwargs	__class__s     r   r+   StsEndpoints.__init__O   s    	,&77r   c                 >    SnSR                  U R                  U5      $ )Nzv1/token{}/{}r0   r1   r&   apis     r   	token_urlStsEndpoints.token_urlR   s    
C>>$..#..r   c                 >    SnSR                  U R                  U5      $ )Nzv1/oauthtokenr?   r@   rA   s     r   oauth_token_urlStsEndpoints.oauth_token_urlW       
C>>$..#..r   c                 >    SnSR                  U R                  U5      $ )Nzv1/introspectr?   r@   rA   s     r   token_info_urlStsEndpoints.token_info_url\   rH   r   r   )r   r   r   r   r3   r+   r4   rC   rF   rJ   r   __classcell__r<   s   @r   r6   r6   L   sG    ,8 / / / / / /r   r6   c                   <   ^  \ rS rSrSrU 4S jr\S 5       rSrU =r	$ )IamEndpointsb   z/Simple class to build IAM Credential endpoints.c                 :   > Xl         [        [        U ]  " S0 UD6  g )N)iamcredentials)_service_accountr:   rO   r+   )r&   service_accountr;   r<   s      r   r+   IamEndpoints.__init__e   s    +	,&B6Br   c                 p    SR                  U R                  5      nSR                  U R                  U5      $ )Nz4v1/projects/-/serviceAccounts/{}:generateAccessTokenr?   )r0   rS   r1   rA   s     r   impersonation_urlIamEndpoints.impersonation_urli   s2    
@
G
GC >>$..#..r   )rS   )
r   r   r   r   r3   r+   r4   rW   r   rL   rM   s   @r   rO   rO   b   s     7C / /r   rO   zcredential configuration filec                    [        U SS5      SLn[        U SS5      n[        U SS5      nU(       a%  U(       d  [        U S5      (       a  [        S5      eSn[        R                  R
                  R                  n[        U S	S5      (       a  U R                  nOTUR                  5       (       a  UR                  5       nO.[        R                  R
                  R                  R                  n[        UUUS
9n [        X5      nUSSU R                  -   UR                  U R                  5      UR                  UR!                  U 5      S.n	U["        R$                  L a  U R&                  U	S'   U R(                  (       aK  [+        U R(                  UUS9n
U
R,                  U	S'   0 nU R.                  (       a  U R.                  US'   XS'   OUR0                  U	S'   [2        R4                  " U R6                  [8        R:                  " U	SS95        [<        R>                  " U R6                  [@        5        [C        U[D        5      (       aY  [F        RH                  " [F        R"                  RJ                  U RL                  U RN                  U RP                  U RR                  S9  gg! [         a8  n[<        R>                  " U R6                  [@        URT                  S9   SnAgSnAff = f)z;Creates the byoid credential config based on CLI arguments.credential_cert_pathNr(   Fr*   r   z8Cannot disable mTLS when a certificate path is provided.Tr)   )r(   r)   r*   external_accountz//iam.googleapis.com/)r)   typeaudiencesubject_token_typerC   credential_sourceworkforce_pool_user_project)r(   r)   !service_account_impersonation_urltoken_lifetime_secondsservice_account_impersonationrJ   r   )indent)	cert_pathkey_pathoutput_filetrust_chain_path)failed)+getattrhasattrr   r   VALUEScorer)   IsExplicitlySetGetdefaultr6   get_generatorr]   get_token_typer^   rC   
get_sourcer   r   r`   rT   rO   rW   &service_account_token_lifetime_secondsrJ   r	   WriteFileContentsrg   jsondumpsr   CreatedResourceRESOURCE_TYPE
isinstanceX509CredConfigGeneratorr   create_configWORKLOADrZ    credential_cert_private_key_path)credential_cert_configuration_output_file credential_cert_trust_chain_pathmessage)argsconfig_typeis_certr(   r*   universe_domain_propertyr)   token_endpoint_builder	generatoroutputsa_endpoint_builderrc   cces                r   create_credential_configr   t   s    D0$7tC'mU3+~r2, 7477
D  K (..33CCT$d++**O//11.224O '',,<<DDO'%1Md0I*"+dmm;'66t7N7NO+55&11$7F j000.2.N.Nf*+(


!) 
/
/ 01 ')#		4	477 	&&>? 3P./!7!F!Ff	D,,djj.JK((-8 )455#11
'
2
2
;
;--88DD@@ 6 
 M((-LMs   9F&J! !
K#+.KK#c                     U R                   (       a  [        XR                   5      $ U R                  (       a   [        XR                  U R                  5      $ U R
                  (       a  [        U S5      (       aG  U R                  (       a6  [        XR
                  U R                  U R                  U R                  5      $ [        XR
                  U R                  U R                  5      $ U R                  (       a
  [        5       $ U R                  (       a   [        U R                   U R"                  5      $ U R$                  (       a6  ['        U R$                  U R(                  U R*                  U R,                  5      $ g)z@Determines the type of credential output based on CLI arguments.%executable_interactive_timeout_millisN)credential_source_fileFileCredConfigGeneratorcredential_source_urlUrlCredConfigGeneratorcredential_source_headersexecutable_commandrk   r   (InteractiveExecutableCredConfigGeneratorexecutable_timeout_millisexecutable_output_fileExecutableCredConfigGeneratorawsAwsCredConfigGeneratorazureAzureCredConfigGenerator
app_id_urir]   rZ   r{   r~   r   r   )r   r   s     r   rq   rq      s1   	  ";0K0KLL	!+/I/I"&"@"@B B	t<  >>5
..0N0N

%
%

4
46 6
 )6M6M)-)G)G)-)D)DF F 
XX!##	ZZ#DOOT]]CC	"!!--66--	  r   c                   T    \ rS rSrSrS rS rS rS r\	R                  S 5       rSrg	)
CredConfigGenerator   z2Base class for generating Credential Config files.c                     Xl         g Nr   )r&   r   s     r   r+   CredConfigGenerator.__init__   s    "r   c                 Z    SnU R                   [        R                  L a  SnU=(       d    U$ )z;Returns the type of token that this credential config uses.$urn:ietf:params:oauth:token-type:jwtz)urn:ietf:params:oauth:token-type:id_token)r   r   r   )r&   r^   default_token_types      r   rr   "CredConfigGenerator.get_token_type   s1     @:555F3!33r   c                     U(       d  gUR                  5       nUS;  a  [        S5      eSU0nUS:X  a  U(       d  [        S5      eX#S'   U$ )a  Returns an optional dictionary indicating the format of the token.

This is a shared method, that several different token types need access to.

Args:
  credential_source_type: The format of the token, either 'json' or 'text'.
  credential_source_field_name: The field name of a JSON object containing
    the text version of the token.

Raises:
  GeneratorError: if an invalid token format is specified, or no field name
  is specified for a json token.

N)rv   textz8--credential-source-type must be either "json" or "text"r\   rv   zA--credential-source-field-name required for JSON formatted tokenssubject_token_field_name)lowerr   )r&   credential_source_typecredential_source_field_nametoken_formats       r   _get_formatCredConfigGenerator._get_format   sm     "399;%55
DF F 23L')OQ 	Q1M-.r   c                 (    U(       a  [        S5      eg )Nz?--credential-source-type is not supported with --azure or --aws)r   )r&   r   s     r   _format_already_defined+CredConfigGenerator._format_already_defined  s    
KM M r   c                     g)z@Gets the credential source info used for this credential config.Nr   r&   r   s     r   rs   CredConfigGenerator.get_source  s     	r   r   N)r   r   r   r   r3   r+   rr   r   r   abcabstractmethodrs   r   r   r   r   r   r      s5    :#4@M
 	 	r   r   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )r   i!  z0The generator for File-based credential configs.c                 8   > [         [        U ]  U5        X l        g r   )r:   r   r+   r   )r&   r   r   r<   s      r   r+    FileCredConfigGenerator.__init__$  s    	
!41+>"8r   c                     SU R                   0nU R                  UR                  UR                  5      nU(       a  X2S'   U$ )Nfiler0   )r   r   r   r   r&   r   r_   r   s       r   rs   "FileCredConfigGenerator.get_source(  sF    !<!<=##D$?$?$($E$EGL$0!r   )r   	r   r   r   r   r3   r+   rs   r   rL   rM   s   @r   r   r   !  s    89 r   r   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )r   i1  z/The generator for Url-based credential configs.c                 D   > [         [        U ]  U5        X l        X0l        g r   )r:   r   r+   r   r   )r&   r   r   r   r<   s       r   r+   UrlCredConfigGenerator.__init__4  s    	
 $0=!6%>"r   c                     SU R                   0nU R                  (       a  U R                  US'   U R                  UR                  UR                  5      nU(       a  X2S'   U$ )Nurlheadersr0   )r   r   r   r   r   r   s       r   rs   !UrlCredConfigGenerator.get_source:  s`     : :;%%%)%C%C	"##D$?$?$($E$EGL$0!r   )r   r   r   rM   s   @r   r   r   1  s    7? r   r   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )r   iE  z?The generator for executable-command-based credentials configs.c                    > U(       a  [        U5      n[        [        U ]  U5        X l        U=(       d    SU l        X@l        g )Ni0u  )intr:   r   r+   commandtimeout_millisrg   )r&   r   r   r   rg   r<   s        r   r+   &ExecutableCredConfigGenerator.__init__H  s8    >*n	
'7DL(1ED"r   c                 |    U R                   U R                  S.nU R                  (       a  U R                  US'   SU0$ )N)r   r   rg   
executable)r   r   rg   r&   r   executable_configs      r   rs   (ExecutableCredConfigGenerator.get_sourceQ  sB    <<--
 )-)9)9&+,,r   )r   rg   r   r   rM   s   @r   r   r   E  s    G#	- 	-r   r   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )r   i]  zUThe generator for executable-command-based credentials configs with interactive mode.c                 N   > [         [        U ]  XX45        [        U5      U l        g r   )r:   r   r+   r   interactive_timeout_millis)r&   r   r   r   rg   r   r<   s         r   r+   1InteractiveExecutableCredConfigGenerator.__init__`  s'    	
2
$~K&)*D&ED#r   c                     U R                   (       d  [        S5      eU R                  U R                  U R                   U R                  S.nSU0$ )NzW--executable-output-file must be specified if --interactive-timeout-millis is provided.)r   r   rg   r   r   )rg   r   r   r   r   r   s      r   rs   3InteractiveExecutableCredConfigGenerator.get_sourcef  sZ     G H H <<--''&*&E&E	 +,,r   )r   r   rM   s   @r   r   r   ]  s    ]F- -r   r   c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )r   iu  z/The generator for AWS-based credential configs.c                 H   > [         [        U ]  [        R                  5        g r   )r:   r   r+   r   r   )r&   r<   s    r   r+   AwsCredConfigGenerator.__init__x  s    	
 
#;;<r   c                     g)Nz+urn:ietf:params:aws:token-type:aws4_requestr   r&   r^   s     r   rr   %AwsCredConfigGenerator.get_token_type|  s    8r   c                 v    U R                  UR                  5        SSSSS.nUR                  (       a  SUS'   U$ )Naws1zChttp://169.254.169.254/latest/meta-data/placement/availability-zonez@http://169.254.169.254/latest/meta-data/iam/security-credentialszNhttps://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15)environment_id
region_urlr   regional_cred_verification_urlz'http://169.254.169.254/latest/api/tokenimdsv2_session_token_url)r   r   enable_imdsv2)r&   r   r_   s      r   rs   !AwsCredConfigGenerator.get_source  sM      !<!<= QN\	 
3 23 r   r   
r   r   r   r   r3   r+   rr   rs   r   rL   rM   s   @r   r   r   u  s    7=9 r   r   c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )r   i  z1The generator for Azure-based credential configs.c                 `   > [         [        U ]  [        R                  5        Xl        X l        g r   )r:   r   r+   r   r   r   r]   )r&   r   r]   r<   s      r   r+   !AzureCredConfigGenerator.__init__  s'    	
"
#;;< OMr   c                     g)Nr   r   r   s     r   rr   'AzureCredConfigGenerator.get_token_type  s    1r   c                     U R                  UR                  5        SU R                  =(       d    SU R                  -   -   SS0SSS.S.$ )	NzVhttp://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=zhttps://iam.googleapis.com/MetadataTruerv   access_token)r\   r   )r   r   r0   )r   r   r   r]   r   s     r   rs   #AzureCredConfigGenerator.get_source  s[      !<!<= e__M = MO 
 (6
 r   )r   r]   r   rM   s   @r   r   r     s    92 r   r   c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )r{   i  z1The generator for X.509-based credential configs.c                 x   > [         [        U ]  [        R                  5        Xl        X l        X0l        X@l        g r   )	r:   r{   r+   r   r   certificate_pathrf   cert_config_pathrh   )r&   r   rf   r   rh   r<   s        r   r+    X509CredConfigGenerator.__init__  s6    
 

!
#;;<,M,,r   c                     g)Nz%urn:ietf:params:oauth:token-type:mtlsr   r   s     r   rr   &X509CredConfigGenerator.get_token_type  s    2r   c                     0 nU R                   c  [        S5      eU R                  b  U R                  US'   OSUS'   U R                  b  U R                  US'   SU0$ )Nz[--credential-cert-private-key-path must be specified if --credential-cert-path is provided.certificate_config_locationTuse_default_certificate_configrh   certificate)rf   r   r   rh   )r&   r   certificate_configs      r   rs   "X509CredConfigGenerator.get_source  sz    }}  (:>:O:O67=A9:(/3/D/D+,-..r   )r   r   rf   rh   r   rM   s   @r   r{   r{     s    9-3/ /r   r{   c                   (   ^  \ rS rSrU 4S jrSrU =r$ )r   i  c                 6   > [         [        U ]  5         Xl        g r   )r:   r   r+   r   )r&   r   r<   s     r   r+   GeneratorError.__init__  s    	.$(*Lr   )r   )r   r   r   r   r+   r   rL   rM   s   @r   r   r     s     r   r   )%r3   
__future__r   r   r   r   enumrv   googlecloudsdk.command_lib.authr   googlecloudsdk.corer   r   googlecloudsdk.core.utilr	   sixEnumr   objectr   r6   rO   ry   r   rq   with_metaclassABCMetar   r   r   r   r   r   r   r{   	Exceptionr   r   r   r   <module>r     s    . &  ' 
   I # * * 
 
$V $N/> /,/> / 0QMh>7	#,,S[[&A 7	t1  0 (-$7 -0-/L -00 >2 :%/1 %/PY r   