Setting up service account access permissions
This section describes how to assign roles for a service account as a resource. To grant the service account a role for another resource, follow the steps in Assigning a role to a service account.
Note
To assign a role for a service account, you need the iam.serviceAccounts.admin role.
Assigning a role for a service account
- In the management console
, click or in the top panel and select the folder the service account belongs to. - Navigate
to Identity and Access Management. - In the left-hand panel, select
Service accounts and then select the service account. - Navigate to the Access bindings tab.
- Click Assign roles.
- In the Who to grant access to field of the the Edit access bindings window, select a subject or use the search.
- Click
Add role and select a role. - Click Save.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder used by default is the one specified when creating the CLI profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also specify a different folder for any command using --folder-name or --folder-id.
If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.
-
View the description of the command for assigning a role for a service account as a resource:
yc iam service-account add-access-binding --help -
Get the role ID:
yc iam role listResult:
+-------------------------------------+-------------+ | ID | DESCRIPTION | +-------------------------------------+-------------+ | access-transparency.admin | | | access-transparency.billingProvider | | | access-transparency.editor | | | ... | | -
Assign a role for a service account to the subject:
yc iam service-account add-access-binding <service_account_name_or_ID> \ --role <role_ID> \ --subject <subject_type>:<subject_ID>Where:
-
--role: ID of the role to assign. -
--subject: Subject getting the role.Subject designations
To indicate a subject, use the
--subjectparameter in<subject_type>:<ID>format. For some subject types, the Yandex Cloud CLI provides separate parameters instead of--subject, where you only need to specify the subject name or ID without the type. Possible subject designations and matching CLI parameters:Subject type
Subject designation
Yandex Cloud CLI parameter
userAccountuserAccount:<user_ID>--user-account-idor--user-yandex-loginserviceAccountserviceAccount:<service_account_ID>--service-account-idor--service-account-namefederatedUserfederatedUser:<user_ID>--user-account-idgroupgroup:<group_ID>--group-memberssystemsystem:allAuthenticatedUsers(
All authenticated usersgroup)--all-authenticated-userssystem:allUsers(
All usersgroup)—
system:group:organization:<organization_ID>:users(
All users in organization Xgroup)--organization-userssystem:group:federation:<federation_ID>:users(
All users in federation Ngroup)--federation-userssystem:group:userpool:<pool_ID>:users(
All users in userpool Pgroup)—
-
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), authenticate using the appropriate method.
-
Describe the resource with the role for the organization in the configuration file.
Configuration file structure example:
resource "yandex_iam_service_account_iam_binding" "admin-account-iam" { service_account_id = "<service_account_ID>" role = "<role>" members = ["<subject_1_type>:<subject_1_ID>", "<subject_2_type>:<subject_2_ID>", ..., "<subject_n_type>:<subject_n_ID>"] }Where:
-
service_account_id: ID of the service account to configure access for. -
role: Role. -
members: Designations of subjects the role is assigned to.Subject designations
To indicate a subject, use a combination of its type and unique ID, i.e.,
<subject_type>:<ID>. Here is how you can designate a subject:Subject type
Subject designation
userAccountuserAccount:<user_ID>serviceAccountserviceAccount:<service_account_ID>federatedUserfederatedUser:<user_ID>groupgroup:<group_ID>systemsystem:allAuthenticatedUsers(
All authenticated usersgroup)system:allUsers(
All usersgroup)system:group:organization:<organization_ID>:users(
All users in organization Xgroup)system:group:federation:<federation_ID>:users(
All users in federation Ngroup)system:group:userpool:<pool_ID>:users(
All users in userpool Pgroup)
For more information about the resources you can create with Terraform, see this provider guide.
-
-
Make sure the settings are correct.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Assign the role.
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
This will create all the resources you need in the specified folder. You can check the new resource using the management console
or this CLI command:yc resource-manager service-account list-access-bindings <service_account_name_or_ID> -
Use the updateAccessBindings REST API method for the ServiceAccount resource or the ServiceAccountService/UpdateAccessBindings gRPC API call. You will need the ID of the service account and that of the subject getting the role for the service account.
-
Get the service account ID using the list REST API method:
curl \ --header "Authorization: Bearer <IAM_token>" \ https://iam.api.cloud.yandex.net/iam/v1/serviceAccounts?folderId=<folder_ID>Result:
{ "serviceAccounts": [ { "id": "aje6o61dvog2********", "folderId": "b1gvmob95yys********", "createdAt": "2018-10-19T13:26:29Z", "name": "my-robot" } ... ] } -
Assign a role for a service account to the subject; specify
ADDin theactionproperty:curl \ --request POST \ --header 'Content-Type: application/json' \ --header "Authorization: Bearer <IAM_token>" \ --data '{ "accessBindingDeltas": [{ "action": "ADD", "accessBinding": { "roleId": "<role>", "subject": { "id": "<subject_ID>", "type": "<subject_type>" }}}]}' \ https://iam.api.cloud.yandex.net/iam/v1/serviceAccounts/<service_account_ID>:updateAccessBindingsWhere:
-
roleId: Role. -
subject: Subject getting the role.Subject designations
To indicate a subject, use a combination of its type and unique ID in the
subject.typeandsubject.idfields of the request. Here are possible combinations:subject.type
subject.id
userAccount<user_ID>serviceAccount<service_account_ID>federatedUser<user_ID>group<group_ID>systemallAuthenticatedUsers(
All authenticated usersgroup)allUsers(
All usersgroup)group:organization:<organization_ID>:users(
All users in organization Xgroup)group:federation:<federation_ID>:users(
All users in federation Ngroup)group:userpool:<pool_ID>:users(
All users in userpool Pgroup) -
<service_account_ID>: ID of the service account to configure access for.
-
Examples
- Configuring user access permissions for a service account.
- Assigning multiple roles.
- Setting up access for a service account to another service account.
Configuring user access permissions for a service account
- In the management console
, click or in the top panel and select the folder the service account belongs to. - Navigate
to Identity and Access Management. - In the left-hand panel, select
Service accounts and then select the service account. - Navigate to the Access bindings tab.
- Click Assign roles.
- In the Edit access bindings field of the Who to grant access to window, select a user from the list or use the user search option.
- Click
Add role and select a role. - Click Save.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
Assign the editor role for the my-robot service account to test-user:
```bash
yc iam service-account add-access-binding my-robot \
--role editor \
--user-yandex-login test-user
```
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), authenticate using the appropriate method.
-
Add the resource parameters to the configuration file and specify the users' role to access the service account.
Configuration file structure example:
resource "yandex_iam_service_account_iam_binding" "admin-account-iam" { service_account_id = "aje6o61dvog2********" role = "editor" members = ["userAccount:gfei8n54hmfh********",] }Where:
service_account_id: ID of the service account to configure access for.role: Role.members: List of users getting the role. Use this format:userAccount:<user_ID>.
For more information about the resources you can create with Terraform, see this provider guide.
-
Make sure the settings are correct.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Assign the role.
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
This will create all the resources you need in the specified folder. You can check the new resource using the management console
or this CLI command:yc resource-manager service-account list-access-bindings <service_account_name_or_ID> -
Use the updateAccessBindings REST API method for the ServiceAccount resource or the ServiceAccountService/UpdateAccessBindings gRPC API call. You will need the ID of the service account and that of the user getting the role for the service account.
-
Find out the service account ID using the list REST API method:
curl \ --header "Authorization: Bearer <IAM_token>" \ https://iam.api.cloud.yandex.net/iam/v1/serviceAccounts?folderId=b1gvmob95yys********Result:
{ "serviceAccounts": [ { "id": "aje6o61dvog2********", "folderId": "b1gvmob95yys********", "createdAt": "2018-10-19T13:26:29Z", "name": "my-robot" } ... ] } -
Get the user ID by login using the getByLogin REST API method:
curl \ --header "Authorization: Bearer <IAM_token>" \ https://iam.api.cloud.yandex.net/iam/v1/yandexPassportUserAccounts:byLogin?login=test-userResult:
{ "id": "gfei8n54hmfh********", "yandexPassportUserAccount": { "login": "test-user", "defaultEmail": "test-user@yandex.ru" } } -
Assign the
editorrole for themy-robotservice account. EnterADDin theactionproperty and specify theuserAccounttype and user ID undersubject.curl \ --request POST \ --header 'Content-Type: application/json' \ --header "Authorization: Bearer <IAM_token>" \ --data '{ "accessBindingDeltas": [{ "action": "ADD", "accessBinding": { "roleId": "editor", "subject": { "id": "gfei8n54hmfh********", "type": "userAccount" }}}]}' \ https://iam.api.cloud.yandex.net/iam/v1/serviceAccounts/aje6o61dvog2********:updateAccessBindings
Assigning multiple roles
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The add-access-binding command allows you to add only one role. You can assign multiple roles using the set-access-binding command.
Alert
The set-access-binding command completely overwrites access permissions for the resource. All roles previously assigned for this resource will be deleted.
-
Make sure the resource has no roles assigned that you would not want to lose:
yc iam service-account list-access-bindings my-robot -
Assign a role to multiple users:
yc iam service-account set-access-bindings my-robot \ --access-binding role=editor,subject=userAccount:gfei8n54hmfh******** \ --access-binding role=viewer,subject=userAccount:helj89sfj80a********
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), authenticate using the appropriate method.
To assign multiple roles for a service account created with Terraform:
-
Add the resource parameters to the configuration file and specify the users' role to access the service account:
-
service_account_id: ID of the service account to configure access for. -
role: Role.Note
For each role, you can only use one
yandex_iam_service_account_iam_bindingresource. -
members: List of users or service accounts getting the role. Use one of these formats:userAccount:<user_ID>orserviceAccount:<service_account_ID>.
Example of assigning multiple roles for a service account using Terraform
... resource "yandex_iam_service_account_iam_binding" "admin-account-iam" { service_account_id = "aje82upckiqh********" role = "admin" members = [ "userAccount:aje82upckiqh********", ] } resource "yandex_iam_service_account_iam_binding" "admin-account-iam2" { service_account_id = "aje82upckiqh********" role = "viewer" members = [ "userAccount:aje82upckiqh********", ] } ...For more information about the resources you can create with Terraform, see this provider guide.
-
-
Make sure the settings are correct.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Assign the role.
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
You can check the folder update using the management console
or this CLI command:yc resource-manager service-account list-access-bindings <service_account_name_or_ID> -
Assign the editor role to one user and the viewer role to another user:
curl \
--request POST \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer <IAM_token>" \
--data '{
"accessBindingDeltas": [{
"action": "ADD",
"accessBinding": {
"roleId": "editor",
"subject": {
"id": "gfei8n54hmfh********",
"type": "userAccount"
}
}
},{
"action": "ADD",
"accessBinding": {
"roleId": "viewer",
"subject": {
"id": "helj89sfj80a********",
"type": "userAccount"
}}}]}' \
https://iam.api.cloud.yandex.net/iam/v1/serviceAccounts/aje6o61dvog2********:updateAccessBindings
You can also assign roles using the setAccessBindings REST API method for the ServiceAccount resource or the ServiceAccountService/SetAccessBindings gRPC API call.
Alert
The setAccessBindings method completely overwrites access permissions for the resource. All roles previously assigned for this resource will be deleted.
curl \
--request POST \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer <IAM_token>" \
--data '{
"accessBindings": [{
"roleId": "editor",
"subject": { "id": "ajei8n54hmfh********", "type": "userAccount" }
},{
"roleId": "viewer",
"subject": { "id": "helj89sfj80a********", "type": "userAccount" }
}]}' \
https://iam.api.cloud.yandex.net/iam/v1/serviceAccounts/aje6o61dvog2********:setAccessBindings
Setting up access for a service account to another service account
Allow the test-sa service account to manage the my-robot service account:
If you do not have the Yandex Cloud CLI yet, install and initialize it.
-
Get the ID of the
test-saservice account you want to assign a role to. To do this, get a list of available service accounts:yc iam service-account listResult:
+----------------------+----------+------------------+ | ID | NAME | DESCRIPTION | +----------------------+----------+------------------+ | ajebqtreob2d******** | test-sa | test-description | | aje6o61dvog2******** | my-robot | | +----------------------+----------+------------------+ -
Assign the
editorrole to thetest-saservice account by specifying its ID:yc iam service-account add-access-binding my-robot \ --role editor \ --service-account-id ajebqtreob2d********
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), authenticate using the appropriate method.
To allow the test-sa service account to manage the my-robot service account created with Terraform:
-
Add the resource parameters to the configuration file and specify the users' role to access the service account:
service_account_id: ID of the service account to configure access for.role: Role.members: List of users or service accounts getting the role. Use one of these formats:userAccount:<user_ID>orserviceAccount:<service_account_ID>.
Example of granting permissions to the
test-saservice account to manage themy-robotservice account using Terraform... resource "yandex_iam_service_account_iam_binding" "admin-account-iam" { service_account_id = "aje82upckiqh********" role = "admin" members = [ "serviceAccount:aje82upckiqh********", ] } ...For more information about the resources you can create with Terraform, see this provider guide.
-
Make sure the settings are correct.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Assign the role.
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
You can check the folder update using the management console
or this CLI command:yc resource-manager service-account list-access-bindings <service_account_name_or_ID> -
-
Get the ID of the
test-saservice account you want to assign a role to. To do this, get a list of available service accounts:curl \ --header "Authorization: Bearer <IAM_token>" \ https://iam.api.cloud.yandex.net/iam/v1/serviceAccounts?folderId=b1gvmob95yys********Result:
{ "serviceAccounts": [ { "id": "ajebqtreob2d********", "folderId": "b1gvmob95yys********", "createdAt": "2018-10-18T13:42:40Z", "name": "test-sa", "description": "test-description" }, { "id": "aje6o61dvog2********", "folderId": "b1gvmob95yys********", "createdAt": "2018-10-15T18:01:25Z", "name": "my-robot" } ] } -
Assign the
editorrole to thetest-saservice account for another service account namedmy-robot. In thesubjectproperty, specify theserviceAccounttype andtest-saID. In the request URL, specify themy-robotID as the resource:curl \ --request POST \ --header 'Content-Type: application/json' \ --header "Authorization: Bearer <IAM_token>" \ --data '{ "accessBindingDeltas": [{ "action": "ADD", "accessBinding": { "roleId": "editor", "subject": { "id": "ajebqtreob2d********", "type": "serviceAccount" }}}]}' \ https://iam.api.cloud.yandex.net/iam/v1/serviceAccounts/aje6o61dvog2********:updateAccessBindings