
                               d 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mZ ddlmZ dd	lmZ d
ZdZdZddeddgZdZdZddZd Z	 ddZ	 ddZd Zy)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                     t        j                         }d|  d| d}|j                  j                  |j                  j                  |            j                  S )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                 V    t        j                  d|       }|r|j                  d      S y)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                 b   |t        t        | |            }t        j                  |       }|t        j                  |      k(  r	 t        j                  t        j                  |             }d| }|j                  D cg c]  }||j                  v r|j                    }}t        |vrHt"        |vr?t%        j&                         r*t%        j(                  ddd| dt         d	|  d
| d	       yyyyy# t        j                  $ r t        j                  d| |t               Y yw xY wc c}w )aK  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>   =   si    "7%j&9  00<.gnnNnKK,,

#
#J
/j  ''<&=>N "***GW__, 	*   	_,/  " 56K5L M""/ 1  **8)9 :-	-	 # 0 	-3 L
 11 	kk4 

	 s   (C7 !D,7/D)(D)c                    t        j                  |      }|r|nt        j                  |      }	 t	        j
                  t        j                  |            }t        ||      r<t        j                  | d| d|       t        j                  j                  d       yy# t        j                  $ r t        j                  d||t                Y yw xY w)a  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*AB((
34
5	 
jj34 C 
	/	/ KK	A 	s   A/B" "/CCc                    d}d| }| j                   D ]C  }||j                  vr|j                  t        v r y|j                  j	                  d      rBd}E d| dt
         d}|rd| dt         d	t
         d
}t        j                         xr t        j                  dd|dz         }|st        j                  |       |S )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W__,||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   