
    z(                         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  ej                  d       G d d	             Z
	 	 	 dd
Zde
fdZy)z:Utility for validating Firestore Mongo connection strings.    N)List)log)console_attrT)frozenc                   h    e Zd ZU dZee   ed<   ee   ed<   ee   ed<   ee   ed<   ee   ed<   d Zy)	ValidationResultsz>Container class for results of validating a connection string.headersinfowarningserrorsfootersc                     dj                  | j                  | j                  z   | j                  z   | j                  z   | j
                  z         S )N
)joinr	   r
   r   r   r   )selfs    ;lib/googlecloudsdk/command_lib/firestore/connection_util.py__str__zValidationResults.__str__#   s>    99tyy 4==04;;>M     N)__name__
__module____qualname____doc__r   str__annotations__r    r   r   r   r      s;    F9S	/I
s)9r   r   c           	      x	   dd|  dg}g g }g dg}d}d}dIfd	}		 | j                  d      sj                  d       n=| t        d      d } t        j                  d|       }
|
rb|
j                  d      }d	t        |
j                  d
            z  }j                  d| d|        | t        |      t        |      z   d
z   d } t        j                  d|       }
|
s*j                  d| d|        j                  d       ns|
j                  d      |k7  r+j                  d|
j                  d       d| d| d       nj                  d| d       | dd } t        j                  d|       }
|
sj                  d       n|
j                  d      |k7  r+j                  d|
j                  d       d| d| d       nj                  d| d       | t        |
j                  d            dz   d } | j                  d      sj                  d       nW| t        d      d } t        j                  d|       }
|
sj                  d| d | d!       n|
j                  d      |k7  rR|
j                  d      r(j                  d"|
j                  d       d#| d       n.j                  d$| d%| d!       nj                  d"| d       | t        |
j                  d            dz   d } i | j                  d&      }|D ]E  }|s|j                  d'      }t        |      d
k7  rj                  d(| d)       ;|d   |d*   <   G  |	d+d,        |	d-d,        |	d.d/       d0v rd0   }d0= |d1k(  r1 |	d2d3d45       |sj                  d6       nj                  d7       n|d8k(  r&|sj                  d9       nxj                  d:       nf|d;k(  r|rj                  d<        |	d=d>d?5       nBj                  d@| d       n,|rj                  dA| d| dB       nj                  dC       j                         D ]  \  }}|j                  dD| d'| d        	 s|j                  dE       n|j                  dF| dG       t        |||H      S )JzDValidate the specified connection_string for the specified database.zP--------------------------------------------------------------------------------zEvaluating connection string: Nc                     | vrj                  | d|  d| d       y |    }| = ||k7  r!j                  | d|  d| d|  d| d
       y j                  |  d| d       y )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   
CheckParamz,ValidateConnectionString.<locals>.CheckParam>   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   ValidateConnectionStringrK   )   s    &'8&9:'
 
$(&J'	$(7& 	''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                    t        j                         }| j                  D ]!  }t        j                  j                  |       # | j                  D ]6  }t        j                  j                  |j                  dd       d|        8 | j                  D ]6  }t        j                  j                  |j                  dd       d|        8 | j                  D ]6  }t        j                  j                  |j                  dd       d|        8 | j                  D ]!  }t        j                  j                  |       # y)	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   )rL   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   )NNN)r   dataclassesr:   typingr   googlecloudsdk.corer   googlecloudsdk.core.consoler   	dataclassr   rK   r[   r   r   r   <module>ra      s_    A  	  # 4 d#  $" 	m`5F r   