
    @                        d Z ddlmZ ddlmZ ddlmZ ddl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mZ ddlZ G d
 dej                         Z G d de      Z G d de      Z G d de      ZdZd Zd Z G d d ej2                  ej4                  e            Z G d de      Z G d de      Z G d de      Z G d de      Z G d d e      Z  G d! d"e      Z! G d# d$e      Z" G d% d&e#      Z$y)'z'Generators for Credential Config Files.    )absolute_import)division)unicode_literalsN)enterprise_certificate_config)log)
properties)filesc                       e Zd ZdZdZy)
ConfigType      N)__name__
__module____qualname__WORKLOAD_IDENTITY_POOLSWORKFORCE_POOLS     Alib/googlecloudsdk/command_lib/iam/byoid_utilities/cred_config.pyr   r       s    /r   r   c                   .    e Zd ZdZ	 	 	 ddZed        Zy)ByoidEndpointszBase class for BYOID endpoints.c                     |r|r|dk7  rt        d      d| _        d| _        || _        |rdnd| _        || _        || _        y )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__zByoidEndpoints.__init__(   s\     |(@  !ED9 	! DM'RDJ+D%Dr   c                 (   | j                   r| j                   dk(  r<| j                  j                  | j                  | j                  | j
                        S | j                  j                  | j                  | j                   | j
                        S )Nr   )r#   mtlsuniverse)r#   r&   r*   )r!   r   formatr   r   r    r   )r"   s    r   	_base_urlzByoidEndpoints._base_url?   s    !3!3x!?&&----djj4;P;P .   ((//''&& 0  r   N)Fzgoogleapis.comr   )r   r   r   __doc__r'   propertyr,   r   r   r   r   r   %   s)    '
 &&. 	 	r   r   c                   R     e Zd ZdZ fdZed        Zed        Zed        Z xZ	S )StsEndpointsz$Simple class to build STS endpoints.c                 ,    t        t        | 
  di | y )N)sts)superr0   r'   )r"   kwargs	__class__s     r   r'   zStsEndpoints.__init__O   s    	,&77r   c                 >    d}dj                  | j                  |      S )Nzv1/token{}/{}r+   r,   r"   apis     r   	token_urlzStsEndpoints.token_urlR   s    
C>>$..#..r   c                 >    d}dj                  | j                  |      S )Nzv1/oauthtokenr7   r8   r9   s     r   oauth_token_urlzStsEndpoints.oauth_token_urlW       
C>>$..#..r   c                 >    d}dj                  | j                  |      S )Nzv1/introspectr7   r8   r9   s     r   token_info_urlzStsEndpoints.token_info_url\   r>   r   )
r   r   r   r-   r'   r.   r;   r=   r@   __classcell__r5   s   @r   r0   r0   L   sG    ,8 / / / / / /r   r0   c                   2     e Zd ZdZ fdZed        Z xZS )IamEndpointsz/Simple class to build IAM Credential endpoints.c                 :    || _         t        t        |   di | y )N)iamcredentials)_service_accountr3   rD   r'   )r"   service_accountr4   r5   s      r   r'   zIamEndpoints.__init__e   s    +D	,&B6Br   c                 p    dj                  | j                        }dj                  | j                  |      S )Nz4v1/projects/-/serviceAccounts/{}:generateAccessTokenr7   )r+   rG   r,   r9   s     r   impersonation_urlzIamEndpoints.impersonation_urli   s2    
@
G
GC >>$..#..r   )r   r   r   r-   r'   r.   rJ   rA   rB   s   @r   rD   rD   b   s     7C / /r   rD   zcredential configuration filec                    t        | dd      du}t        | dd      }t        | dd      }|r|st        | d      rt        d      d}t        j                  j
                  j                  }t        | d	d      r| j                  }nO|j                         r|j                         }n.t        j                  j
                  j                  j                  }t        |||
      }	 t        | |      }|dd| j                  z   |j                  | j                        |j                  |j!                  |       d}	|t"        j$                  u r| j&                  |	d<   | j(                  rJt+        | j(                  ||      }
|
j,                  |	d<   i }| j.                  r$| j.                  |d<   ||	d<   n|j0                  |	d<   t3        j4                  | j6                  t9        j:                  |	d             t=        j>                  | j6                  t@               tC        |tD              r[tG        jH                  tF        j"                  jJ                  | jL                  | jN                  | jP                  | jR                         yy# t        $ r:}t=        j>                  | j6                  t@        |jT                         Y d}~yd}~ww xY w)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_typer;   credential_sourceworkforce_pool_user_project)r$   r%   !service_account_impersonation_urltoken_lifetime_secondsservice_account_impersonationr@   r   )indent)	cert_pathkey_pathoutput_filetrust_chain_path)failed)+getattrhasattrr   r   VALUEScorer%   IsExplicitlySetGetdefaultr0   get_generatorrO   get_token_typerP   r;   
get_sourcer   r   rR   rH   rD   rJ   &service_account_token_lifetime_secondsr@   r	   WriteFileContentsrY   jsondumpsr   CreatedResourceRESOURCE_TYPE
isinstanceX509CredConfigGeneratorr   create_configWORKLOADrL    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_builderrU   cces                r   create_credential_configr}   t   s    D0$7tC'mU3+~r2, 747
D  K (..33CCT$d+**O//1.224O '',,<<DDO'%1MdK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#11
'
2
2
;
;--88DD@@ 6 
 M((-LLMs   #FJ 	K0K  Kc                    | j                   rt        || j                         S | j                  r!t        || j                  | j                        S | j
                  r{t        | d      rC| j                  r7t        || j
                  | j                  | j                  | j                        S t        || j
                  | j                  | j                        S | j                  r
t               S | j                  r t        | j                   | j"                        S | j$                  r6t'        | j$                  | j(                  | j*                  | j,                        S y)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_commandr]   r   (InteractiveExecutableCredConfigGeneratorexecutable_timeout_millisexecutable_output_fileExecutableCredConfigGeneratorawsAwsCredConfigGeneratorazureAzureCredConfigGenerator
app_id_urirO   rL   rm   rp   rq   rr   )rt   ru   s     r   rc   rc      s4   	  ";0K0KLL	!+t/I/I"&"@"@B B	t< >>5
t..0N0N

%
%

4
46 6
 )d6M6M)-)G)G)-)D)DF F 
XX!##	ZZ#DOOT]]CC	"!!--66--	  r   c                   L    e Zd ZdZd Zd Zd Zd Zej                  d        Z
y)CredConfigGeneratorz2Base class for generating Credential Config files.c                     || _         y N)ru   )r"   ru   s     r   r'   zCredConfigGenerator.__init__   s
    "Dr   c                 N    d}| j                   t        j                  u rd}|xs |S )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)ru   r   r   )r"   rP   default_token_types      r   rd   z"CredConfigGenerator.get_token_type   s1     @:555F3!33r   c                     |sy|j                         }|dvrt        d      d|i}|dk(  r|st        d      ||d<   |S )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)rh   textz8--credential-source-type must be either "json" or "text"rN   rh   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_formatzCredConfigGenerator._get_format   so     "399;%55
DF F 23L')OQ 	Q1Ml-.r   c                     |rt        d      y )Nz?--credential-source-type is not supported with --azure or --aws)r   )r"   r   s     r   _format_already_definedz+CredConfigGenerator._format_already_defined  s    
KM M r   c                      y)z@Gets the credential source info used for this credential config.Nr   r"   rt   s     r   re   zCredConfigGenerator.get_source  s     	r   N)r   r   r   r-   r'   rd   r   r   abcabstractmethodre   r   r   r   r   r      s5    :#4@M
 	 	r   r   c                   (     e Zd ZdZ fdZd Z xZS )r   z0The generator for File-based credential configs.c                 :    t         t        |   |       || _        y r   )r3   r   r'   r   )r"   ru   r   r5   s      r   r'   z FileCredConfigGenerator.__init__$  s    	
!41+>"8Dr   c                 |    d| j                   i}| j                  |j                  |j                        }|r||d<   |S )Nfiler+   )r   r   r   r   r"   rt   rQ   r   s       r   re   z"FileCredConfigGenerator.get_source(  sI    !<!<=##D$?$?$($E$EGL$0!r   r   r   r   r-   r'   re   rA   rB   s   @r   r   r   !  s    89r   r   c                   (     e Zd ZdZ fdZd Z xZS )r   z/The generator for Url-based credential configs.c                 H    t         t        |   |       || _        || _        y r   )r3   r   r'   r   r   )r"   ru   r   r   r5   s       r   r'   zUrlCredConfigGenerator.__init__4  s#    	
 $0=!6D%>D"r   c                     d| j                   i}| j                  r| j                  |d<   | j                  |j                  |j                        }|r||d<   |S )Nurlheadersr+   )r   r   r   r   r   r   s       r   re   z!UrlCredConfigGenerator.get_source:  sc     : :;%%%)%C%C	"##D$?$?$($E$EGL$0!r   r   rB   s   @r   r   r   1  s    7?r   r   c                   (     e Zd ZdZ fdZd Z xZS )r   z?The generator for executable-command-based credentials configs.c                 x    |rt        |      }t        t        |   |       || _        |xs d| _        || _        y )Ni0u  )intr3   r   r'   commandtimeout_millisrY   )r"   ru   r   r   rY   r5   s        r   r'   z&ExecutableCredConfigGenerator.__init__H  s<    >*n	
'7DDL(1ED"Dr   c                 r    | j                   | j                  d}| j                  r| j                  |d<   d|iS )N)r   r   rY   
executable)r   r   rY   r"   rt   executable_configs      r   re   z(ExecutableCredConfigGenerator.get_sourceQ  sB    <<--
 )-)9)9&+,,r   r   rB   s   @r   r   r   E  s    G#	-r   r   c                   (     e Zd ZdZ fdZd Z xZS )r   zUThe generator for executable-command-based credentials configs with interactive mode.c                 R    t         t        |   ||||       t        |      | _        y r   )r3   r   r'   r   interactive_timeout_millis)r"   ru   r   r   rY   r   r5   s         r   r'   z1InteractiveExecutableCredConfigGenerator.__init__`  s+    	
2
$g~{K&)*D&ED#r   c                     | j                   st        d      | j                  | j                  | j                   | j                  d}d|iS )NzW--executable-output-file must be specified if --interactive-timeout-millis is provided.)r   r   rY   r   r   )rY   r   r   r   r   r   s      r   re   z3InteractiveExecutableCredConfigGenerator.get_sourcef  sZ     G H H <<--''&*&E&E	 +,,r   r   rB   s   @r   r   r   ]  s    ]F-r   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )r   z/The generator for AWS-based credential configs.c                 H    t         t        |   t        j                         y r   )r3   r   r'   r   r   )r"   r5   s    r   r'   zAwsCredConfigGenerator.__init__x  s    	
 
#;;<r   c                      y)Nz+urn:ietf:params:aws:token-type:aws4_requestr   r"   rP   s     r   rd   z%AwsCredConfigGenerator.get_token_type|  s    8r   c                 l    | j                  |j                         ddddd}|j                  rd|d<   |S )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"   rt   rQ   s      r   re   z!AwsCredConfigGenerator.get_source  sM      !<!<= QN\	 
3 23 r   r   r   r   r-   r'   rd   re   rA   rB   s   @r   r   r   u  s    7=9r   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )r   z1The generator for Azure-based credential configs.c                 d    t         t        |   t        j                         || _        || _        y r   )r3   r   r'   r   r   r   rO   )r"   r   rO   r5   s      r   r'   z!AzureCredConfigGenerator.__init__  s+    	
"
#;;< DODMr   c                      y)Nr   r   r   s     r   rd   z'AzureCredConfigGenerator.get_token_type  s    1r   c                     | j                  |j                         d| j                  xs d| j                  z   z   ddiddddS )	NzVhttp://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=zhttps://iam.googleapis.com/MetadataTruerh   access_token)rN   r   )r   r   r+   )r   r   r   rO   r   s     r   re   z#AzureCredConfigGenerator.get_source  s[      !<!<= e__M = MO 
 (6
 r   r   rB   s   @r   r   r     s    92r   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )rm   z1The generator for X.509-based credential configs.c                     t         t        |   t        j                         || _        || _        || _        || _        y r   )	r3   rm   r'   r   r   certificate_pathrX   cert_config_pathrZ   )r"   r   rX   r   rZ   r5   s        r   r'   z X509CredConfigGenerator.__init__  s>    
 

!
#;;<,DDM,D,Dr   c                      y)Nz%urn:ietf:params:oauth:token-type:mtlsr   r   s     r   rd   z&X509CredConfigGenerator.get_token_type  s    2r   c                     i }| j                   t        d      | j                  | j                  |d<   nd|d<   | j                  | j                  |d<   d|iS )Nz[--credential-cert-private-key-path must be specified if --credential-cert-path is provided.certificate_config_locationTuse_default_certificate_configrZ   certificate)rX   r   r   rZ   )r"   rt   certificate_configs      r   re   z"X509CredConfigGenerator.get_source  sz    }}  (:>:O:O67=A9:(/3/D/D+,-..r   r   rB   s   @r   rm   rm     s    9-3/r   rm   c                        e Zd Z fdZ xZS )r   c                 8    t         t        |           || _        y r   )r3   r   r'   rs   )r"   rs   r5   s     r   r'   zGeneratorError.__init__  s    	.$(*DLr   )r   r   r   r'   rA   rB   s   @r   r   r     s     r   r   )%r-   
__future__r   r   r   r   enumrh   googlecloudsdk.command_lib.authr   googlecloudsdk.corer   r   googlecloudsdk.core.utilr	   sixEnumr   objectr   r0   rD   rk   r}   rc   with_metaclassABCMetar   r   r   r   r   r   r   rm   	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   