
    W1                         S r SSKrSSK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  \R                  " SS9 " S S	5      5       r   SS
 jrS\4S jrSrSrSrSrS rS rS rg)z:Utility for validating Firestore Mongo connection strings.    N)List)log)console_attrT)frozenc                   p    \ rS rSr% Sr\\   \S'   \\   \S'   \\   \S'   \\   \S'   \\   \S'   S rS	r	g
)ValidationResults   z>Container class for results of validating a connection string.headersinfowarningserrorsfootersc                     SR                  U R                  U R                  -   U R                  -   U R                  -   U R
                  -   5      $ )N
)joinr
   r   r   r   r   )selfs    ;lib/googlecloudsdk/command_lib/firestore/connection_util.py__str__ValidationResults.__str__&   s>    99tyy 4==04;;>M      N)
__name__
__module____qualname____firstlineno____doc__r   str__annotations__r   __static_attributes__r   r   r   r   r      s;    F9S	/I
s)9r   r   c           	      
  ^^^ SSU  3S/n/ m/ n/ mS/nSnSnSIUUU4S jjn	 U R                  S5      (       d  TR                  S5        GOU [        S5      S n [        R                  " SU 5      n
U
(       ab  U
R                  S5      nS	[        U
R                  S
5      5      -  nTR                  SU SU 35        U [        U5      [        U5      -   S
-   S n [        R                  " SU 5      n
U
(       d*  TR                  SU SU 35        TR                  S5        GOU
R                  S5      U:w  a+  TR                  SU
R                  S5       SU SU S35        OTR                  SU S35        U SS n [        R                  " SU 5      n
U
(       d  TR                  S5        GO U
R                  S5      U:w  a+  TR                  SU
R                  S5       SU SU S35        OTR                  SU S35        U [        U
R                  S5      5      S-   S n U R                  S5      (       d  TR                  S5        GOU [        S5      S n [        R                  " SU 5      n
U
(       d  TR                  SU S U S!35        GO<U
R                  S5      U:w  aW  U
R                  S5      (       a(  TR                  S"U
R                  S5       S#U S35        O.TR                  S$U S%U S!35        OTR                  S"U S35        U [        U
R                  S5      5      S-   S n 0 mU R                  S&5      nU HN  nU(       d  M  UR                  S'5      n[        U5      S
:w  a  TR                  S(U S)35        MC  US   TUS*   '   MP     U	" S+S,5        U	" S-S,5        U	" S.S/5        S0T;   a  TS0   nTS0	 US1:X  a3  U	" S2S3S4S59  U(       d  TR                  S65        OTR                  S75        OUS8:X  a+  U(       d  TR                  S95        OTR                  S:5        OnUS;:X  a!  U(       a  TR                  S<5        U	" S=S>S?S59  OGTR                  S@U S35        O1U(       a  TR                  SAU SU SB35        OTR                  SC5        TR                  5        H  u  nnUR                  SDU S'U S35        M       T(       d  UR                  SE5        OUR                  SFU SG35        [        UTUTUSH9$ )JzDValidate the specified connection_string for the specified database.zP--------------------------------------------------------------------------------zEvaluating connection string: Nc                    > U T;  a  TR                  U SU  SU S35        g TU    nTU 	 X1:w  a!  TR                  U SU  SU SU  SU S3
5        g TR                  U  SU S35        g )Nz#The connection string must specify =.The parameter z is set to z%. The connection string must specify )append)
param_nameexpected_valueerror_descriptionactual_valuer   extra_paramsr   s       r   
CheckParam,ValidateConnectionString.<locals>.CheckParamA   s    %mm  \>*!-
 "*-l
z
"		' !
|;nAl!N+1.	
 	zl!N#3156r   z
mongodb://z0The connection string must start with mongodb://z^([^:]*):([^@]*)@   *   z&The connection string specifies user: z and password: zA^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.z@The database address must start with a valid UUID. For database z, use the value ztNOTE: for password based authentication, the connection string can also start with mongodb://username:password@UUID.z	the UUID z2 in the connection string does not match the UUID z for the current database r#   z	The UUID z is correct.%   z^([^\.]+)\.zIThe database address must have the form: UUID.location.firestore.goog:443zThe location z6 in the connection string does not match the location zfirestore.goog:443/zRThe database address must end with firestore.goog:443 as the domain name and port.z^([^\?]*)\?zMThe connection string must specify the database id. For the current database z: it should have the form UUID.location.firestore.goog:443/?zThe database name z> in the connection string does not match the current database zoThe database name in the connection string is empty. It is recommended to explicitly specify the database name z, e.g. firestore.goog:443/&r"   r$   z= appears malformed. Expected something in the form key=value.r   loadBalancedtruetlsretryWritesfalseauthMechanismPLAIN
authSourcez	$externalzUsing PLAIN authentication. )r(   zsThe username and an access token should be specified in the connection string when PLAIN authentication is enabled.z=username and access token specified for PLAIN authentication.zSCRAM-SHA-256zeThe username and password should be specified in the connection string when SCRAM-SHA-256 is enabled.z2username and password specified for SCRAM-SHA-256.zMONGODB-OIDCzZThe username should not be specified when using the MONGODB-OIDC authentication mechanism.authMechanismPropertiesz(ENVIRONMENT:gcp,TOKEN_RESOURCE:FIRESTOREz#Using MONGODB-OIDC authentication. z%Unsupported authentication mechanism z,Since the connection string specified user: zw, the connection must also be configured with an appropriate authentication mechanism, e.g. authMechanism=SCRAM-SHA-256znNo authMechanism specified. The connection string must specify one of the supported authentication mechanisms.zUnknown parameter z4Did not detect any errors in this connection string.zJTIP: You can use 'gcloud firestore databases connection-string --database=z(' to construct valid connection strings.)r
   r   r   r   r   ) )	
startswithr%   lenrematchgroupsplititemsr   )connection_stringdb_uiddb_location_iddatabase_idr
   r   r   userpasswordr+   r@   entriesentrypartsauth_mechanismkvr   r*   r   s                    @@@r   ValidateConnectionStringrP   ,   s    &'8&9:'
 
$(&J'	$(7 7& 	''55mmFG)#l*;*=> HH)+<=E[[^ds5;;q>**h
kk24& 9#*&
 ,CIH,E,I,KL HHLE mm%&6vh@	 mmB {{1~mmekk!n% &"8#=k]!M
 kkIfX\23)"#. HHE mm- {{1~'mm%++a.) *$$2#3 4)]!- kkM.!1>?)#ekk!n*=*A*CD ''(=>>mm& )#.C*D*FG HH^%67Emm&&1] 3..9]!=
 {{1~$	Q Q 0 1;;F-qJ	

 	Im5k]!E	
 kk&{m<@A)#ekk!n*=*A*CD L%%c*Gkk#e	UqUG $0 0	

 "'qU1X  ~v&uf}g& ,&#O4n

'	7	"<	

 
--L
 ++M _,
--A
 ++J
K^+
--7 	%6C	
 	=n=MQOP	:4& A" $++	
 	F	
 ""$1oo*1#Qqc34 % 
	NNIJNN	!]"J	L 

 r   validation_resultsc                    [         R                  " 5       nU R                   H"  n[        R                  R                  U5        M$     U R                   H7  n[        R                  R                  UR                  SS5       SU 35        M9     U R                   H7  n[        R                  R                  UR                  SS5       SU 35        M9     U R                   H7  n[        R                  R                  UR                  SS5       SU 35        M9     U R                   H"  n[        R                  R                  U5        M$     g)	z@Renders the connection string validation results to the console.zINFO:green zWARNING:yellowzERROR:redN)r   GetConsoleAttrr
   r   statusPrintr   Colorizer   r   r   )rQ   conheaderr   warningerrorfooters          r   PrettyPrintValidationResultsr`     s    	##%#"**fJJV + %%dJJWg67q?@ &#,,gJJZ:;1WIFG -!((eJJXu56aw?@ )"**fJJV +r   h340000000100000000000000DD07000000000000001F0000001068656C6C6F0001000000086C6F616442616C616E636564000100H240000000000000000000000dd07000000000000000f0000001070696e67000100000000g      4@g      @c                    Sn[         R                  " 5       nU R                  [        R	                  [
        5      5         U R                  S5      n[         R                  " 5       U-
  nU(       d  O%UR                  S5      S:w  a  SnOU[        :  a  OMV  U(       a  [        US S3SS	9  O
[        S
SS	9  U(       a  U$ S$ )z0Sends a Mongo ping message via specified socket.FT   s   ok.3fzs r<   endzN/A    N)
timeperf_countersendallbytesfromhex	_PING_HEXrecvfind_MAX_PING_WAIT_TIMEprint)ssockping_complete
ping_startdata	ping_times        r   Pingrx   9  s    -  "*--i()::dD!!#j0Iyy2m&& 	 	YsO2
B'	)#--r   c                 d   Sn[         R                  " 5       nU R                  [        R	                  [
        5      5         U R                  S5      n[         R                  " 5       U-
  nU(       d  O5UR                  S5      S:w  a  [        SUS S35        SnOU[        :  a  OMf  U(       a  U$ S	$ )
z1Sends a Mongo hello message via specified socket.FTrd   s   isWritablePrimaryre   zConnection established in rf   z secondsN)
ri   rj   rk   rl   rm   
_HELLO_HEXro   rp   rr   _MAX_CONNECTION_WAIT_TIME)rs   handshake_completeconnection_startrv   connect_times        r   Hellor   P  s    &&(--j)*::dD$$&)99Lyy%&",(c(:(CD// 	 ,55r   c                    [         R                  " 5       n/ n[        R                  " U S45       nUR	                  X@S9 n[        U5      n[        SU S3SS9  [        U5       H  nUR                  [        U5      5        M     [        5         Xc4sSSS5        sSSS5        $ ! , (       d  f       O= f SSS5        g! , (       d  f       g= f)zEOpens an SSL connection and sends timed Mongo commands to the server.i  )server_hostnamezSending z pings ...: r<   rg   N)
sslcreate_default_contextsocketcreate_connectionwrap_socketr   rr   ranger%   rx   )hostname	num_pingscontext
ping_timessockrs   r~   _s           r   ConnectAndPingr   d  s    &&('*30D			T		<5\lhyk.B7Y!$u+&  g' 
=	< 10	<	<	< 100s$   C  AB%	C %
B3	/C  
C)NNN)r   dataclassesr?   r   r   ri   typingr   googlecloudsdk.corer   googlecloudsdk.core.consoler   	dataclassr   rP   r`   rz   rn   r{   rq   rx   r   r   r   r   r   <module>r      s    A  	  
   # 4 d#  $" 	m`5F $2  O 
 !  ..6((r   