
                               S 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Jr  SSKJr  SS	KJr  S
rSrSrSS\SS/rSrSrSS jrS r SS jr SS jrS rg)zService account utils.    N)
exceptions)cloudbuild_util)projects_api)run_util)util)log)
console_iozroles/cloudbuild.builds.builderzroles/editorzroles/run.invokerzroles/run.adminzroles/run.developerzroles/run.servicesInvokerzroles/run.sourceDeveloperzrun.routes.invokez6{project_number}-compute@developer.gserviceaccount.comc                     [         R                  " 5       nSU  SU S3nUR                  R                  UR                  R                  US95      R                  $ )z5Gets the default build service account for a project.z	projects/z/locations/z/defaultServiceAccount)name)r   GetClientInstanceprojects_locationsGetDefaultServiceAccountMESSAGES_MODULE:CloudbuildProjectsLocationsGetDefaultServiceAccountRequestserviceAccountEmail)
project_idregionclientr   s       @lib/googlecloudsdk/command_lib/functions/service_account_util.pyGetDefaultBuildServiceAccountr   )   sc    ,,.&ZLF83I	J$		"	"	;	;WW X 
 	    c                 b    [         R                  " SU 5      nU(       a  UR                  S5      $ g)zEExtracts the service account email from the service account resource.z/serviceAccounts/([^/]+)$   N)researchgroup)service_accountmatchs     r   _ExtractServiceAccountEmailr   4   s'    
))0/
B%
;;q>r   c                    Uc  [        [        X5      5      n[        R                  " U 5      nU[        R                  US9:X  a   [        R                  " [        R                  " U 5      5      nSU 3nUR                   Vs/ s H   nXVR                  ;   d  M  UR                   PM"     nn[        U;  aN  ["        U;  aC  [$        R&                  " 5       (       a(  [$        R(                  " SSSU S[         S	U  S
U S3	S9  ggggg! [        R                   a     [        R                  " SU U[        5         gf = fs  snf )a=  Util to validate the default build service account permission.

Prompt a warning to users if cloudbuild.builds.builder is missing.

Args:
  project_id: id of project
  region: region to deploy the function
  build_service_account: user provided build service account. It will be None,
    if user doesn't provide it.
Nproject_numberzYour account does not have permission to check or bind IAM policies to project [%s]. If the deployment fails, ensure [%s] has the role [%s] before retrying.serviceAccount:FTz$
The default build service account [] is missing the [z] role. This may cause issues when deploying a function. You could fix it by running the command: 
gcloud projects add-iam-policy-binding z \
 --member=serviceAccount:z-compute@developer.gserviceaccount.com \
 --role=roles/cloudbuild.builds.builder 
For more information, please refer to: https://cloud.google.com/functions/docs/troubleshooting#build-service-account.
 Would you like to continue?defaultcancel_on_noprompt_string)r   r   project_utilGetProjectNumber_GCE_SAformatr   GetIamPolicyParseProjectapitools_exceptionsHttpForbiddenErrorr   warning_BUILDER_ROLEbindingsmembersrole_EDITOR_ROLEr	   	CanPromptPromptContinue)r   r   build_service_accountr"   
iam_policyaccount_stringbindingcontained_roless           r   2ValidateDefaultBuildServiceAccountAndPromptWarningr>   =   sj    "7%j9  00<.gnnNnKK,,

#
#J
/j  ''<&=>N "***G__, 	*   	_,/  "" 56K5L M""/ 1  **8)9 :-	-	 # 0 	-3 L
 11 	kk4 

	 s   *D D=D=1D:9D:c                    [         R                  " U5      nU(       a  UO[        R                  US9n [        R
                  " [         R                  " U5      5      n[        XR5      (       a:  [        R                  " U SU 3SUS9  [        R                  R                  S5        gg! [        R                   a     [        R                  " SUU[         5         gf = f)az  Validates trigger service account has route.invoker permission on project.

If missing, prompt user to add the run invoker role on the function's Cloud
Run service.

Args:
  function: the function to add the binding to
  project_id: the project id to validate
  trigger_service_account: the trigger service account to validate
  is_gen2: whether the function is a gen2 function
r!   r#   T)add_bindingis_gen2zRole successfully bound.
zYour account does not have permission to check or bind IAM policies to project [%s]. If your function encounters authentication errors, ensure [%s] has the role [%s].N)r)   r*   r+   r,   r   r-   r.   _ShouldBindInvokerRoler   AddOrRemoveInvokerBindingr   statusPrintr/   r0   r1   _RUN_INVOKER_ROLE)functionr   trigger_service_accountrA   r"   r:   s         r   $ValidateAndBindTriggerServiceAccountrI      s    "  00<. 
! >>>8 
**!!*-J jBB((
34
5	 
jj34 C 
	/	/ KK	A 	s   A3B) )1CCc                    SnSU 3nU R                    HN  nX4R                  ;  a  M  UR                  [        ;   a    gUR                  R	                  S5      (       a  ML  SnMP     SU S[
         S3nU(       a  SU S[         S	[
         S
3n[        R                  " 5       =(       a    [        R                  " SSUS-   S9nU(       d  [        R                  " U5        U$ )z1Prompts user to bind the invoker role if missing.Fr#   zroles/TzYour trigger service account [r$   zI] role. This will cause authentication errors when running the function.
z] likely lacks the [z] permission, which will cause authentication errors. Since this service account uses a custom role, please verify that the custom role includes this permission. If not, you'll need to either add this permission to the custom role, or grant the [z(] role to the service account directly.
zU Do you want to add the invoker binding to the IAM policy of your Cloud Run function?r%   )r3   r4   r5   ._PREDEFINE_ROLES_WITH_ROUTE_INVOKER_PERMISSION
startswithrF   _ROUTE_INVOKER_PERMISSIONr	   r7   r8   r   r1   )r:   r   custom_role_detectedr;   r<   r(   should_binds          r   rB   rB      s   $_$56.$$g__,||EE\\$$X.. " % 	'&7 8 ! "-- 
 
((9 :01 2+ ,=*= >'	'  $$& :+D+D!	"",+ 
KK	r   )global)N)F)__doc__r   apitools.base.pyr   r/   !googlecloudsdk.api_lib.cloudbuildr   +googlecloudsdk.api_lib.cloudresourcemanagerr   $googlecloudsdk.command_lib.functionsr   #googlecloudsdk.command_lib.projectsr   r)   googlecloudsdk.corer   googlecloudsdk.core.consoler	   r2   r6   rF   rK   rM   r+   r   r   r>   rI   rB    r   r   <module>rZ      s|     	 > = D 9 D # 21' 2 . 0 
B /3DV 	+\)r   