How to use Service Accounts via the IaaS-API
About Service Accounts
Section titled “About Service Accounts”Service Accounts allow you to access the whole STACKIT API programmatically. The IaaS-API allows attaching service accounts to servers which makes them available inside the VM. Accessing serviceaccounts inside a VM is possible through the metadata API on the well-known url http://169.254.169.254/stackit/v1/service-accounts. A service account is referenced by its service account mail which is globally unique.
Creating Service Accounts
Section titled “Creating Service Accounts”Service accounts can be created in the portal, the service account API or the STACKIT CLI.
Creating a Service Account with the STACKIT CLI
stackit service-account create --name <NAME>Be sure to give the service account the correct permissions or roles for the intended tasks.
Connecting Service Accounts to Servers
Section titled “Connecting Service Accounts to Servers”The IaaS-API allows to attach or detach Service Accounts on already running servers or adding service accounts on creation of a server.
stackit curl https://iaas.api.eu01.stackit.cloud/v1/projects/{projectId}/servers/{serverId}/service-accountsstackit curl -X PUT https://iaas.api.eu01.stackit.cloud/v1/projects/{projectId}/servers/{serverId}/service-accounts/{serviceAccountMail}stackit curl -X DELETE https://iaas.api.eu01.stackit.cloud/v1/projects/{projectId}/servers/{serverId}/service-accounts/{serviceAccountMail}stackit server create --service-account-emails my-svc-acc@sa.stackit.cloud --name server1 --machine-type g1.1 [...]Using Service Accounts inside a Server
Section titled “Using Service Accounts inside a Server”After attaching the service account to a server it can immediately be used. The token of the service account is available through the server-local metadata API at http://169.254.169.254/stackit/v1/service-accounts.
Creating a short-lived token
Section titled “Creating a short-lived token”Listing attached Service Accounts
curl http://169.254.169.254/stackit/v1/service-accountsA token can be created and retrieved by calling the /token path.
Creating a Service Account Token
curl http://169.254.169.254/stackit/v1/service-accounts/<SERVICEACCOUNTMAIL>/tokenTokens have short lifetime and must be renewed regularly by retrieving a new token from this URL.
Usage with STACKIT CLI
Section titled “Usage with STACKIT CLI”To follow this example install the STACKIT CLI.
- First we need to authenticate with the token
Authenticate with the Token
stackit auth activate-service-account --service-account-token <TOKEN>- Then we can use the client as usual as the service account permissions allow.