python requests - Can't create cloudsql role for Service Account via api -
i have been trying use api create service accounts in gcp.
to create service account send following post request:
base_url = f"https://iam.googleapis.com/v1/projects/{project}/serviceaccounts" auth = f"?access_token={access_token}" data = {"accountid": name} # create service account r = requests.post(base_url + auth, json=data)
this returns 200 , creates service account:
then, code use create specific roles:
sa = f"{name}@dotmudus-service.iam.gserviceaccount.com" sa_url = base_url + f'/{sa}:setiampolicy' + auth data = {"policy": {"bindings": [ { "role": roles, "members": [ f"serviceaccount:{sa}" ] } ]} }
if roles set 1 of roles/viewer
, roles/editor
or roles/owner
approach work. however, if want use, roles/cloudsql.viewer
api tells me option not supported.
here roles. https://cloud.google.com/iam/docs/understanding-roles
i don't want give service account full viewer rights project, it's against principle of least privilege.
how can set specific roles api?
edit:
here response using resource manager api: roles/cloudsql.admin
role
post https://cloudresourcemanager.googleapis.com/v1/projects/{project}:setiampolicy?key={your_api_key} { "policy": { "bindings": [ { "members": [ "serviceaccount:sa@{project}.iam.gserviceaccount.com" ], "role": "roles/cloudsql.viewer" } ] } } { "error": { "code": 400, "message": "request contains invalid argument.", "status": "invalid_argument", "details": [ { "@type": "type.googleapis.com/google.cloudresourcemanager.projects.v1beta1.projectiampolicyerror", "type": "solo_require_tos_acceptor", "role": "roles/owner" } ] } }
with code provided appears appending first base_url
not correct context modify project roles.
this try place appended path to: https://iam.googleapis.com/v1/projects/{project}/serviceaccount
the post path adding roles needs be: https://cloudresourcemanager.googleapis.com/v1/projects/{project]:setiampolicy
if remove /serviceaccounts
base_url
, should work.
edited response add more information due edit
ok, see issue here, sorry had set new project test this.
cloudresourcemanager.projects.setiampolicy
needs replace entire policy. appears can add constraints change have submit complete policy in json project.
note gcloud
has --log-http
option dig through of these issues. if run
gcloud projects add-iam-policy-binding $project --member serviceaccount:$name --role roles/cloudsql.viewer --log-http
it show how pulls existing existing policy, appends new role , adds it.
i recommend using example code provided here make these changes if don't want use gcloud or console add role user impact entire project.
hopefully improve api need.
Comments
Post a Comment