How to use service accounts to connect to OpenStack
Objective
To automate calls to protected APIs, you need to provide credentials to the code that handles them. With OVHcloud service accounts, it is possible to have a single ID per script for the use of the different APIs offered by OVHcloud products (OVHcloud API, OpenStack API, etc.).
This guide details how to use service accounts to connect to OpenStack APIs.
This allows you to:
- Integrate alerts from your infrastructure.
- Dynamically manage the scalability of your infrastructure.
- Automate the resolution of the most common incidents.
Requirements
- An OVHcloud customer account.
- You know how to configure access policies via API.
- You know how to use the OpenStack API
- You have created a service account via API.
Instructions
For example purposes in this guide, we will use the service account urn:v1:eu:identity:credential:xx11111-ovh/oauth2-0f0f0f0f0f0f0f0f to access the Public Cloud project urn:v1:eu:resource:publicCloudProject:0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f.
You must change these values to match your own.
Assign OpenStack access rights to their service account
First of all, our service account must be able to access your OpenStack infrastructure. OVHcloud provides 11 levels of rights that can be used within OpenStack. They are described in the following table:
| OpenStack rights | Nova: compute_manage | Nova: compute_snapshot_manage | Nova: compute_read | Swift: objectstore_all | glance: image_manage | glance: image_read | glance: image_import | Cinder: volume_manage | Cinder: volume_snapshot_manage | Cinder: volume_read | Neutron: network_manage | Neutron: network_read | Neutron: network_secgroup_manage | Neutron: network_secgroup_read | AI Training: ai_training_all | AI Training: ai_training_read |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| publicCloudProject:openstack:administrator | x | x | x | x | x | x | x | x | x | x | x | x | x | x | ||
| publicCloudProject:openstack:backupOperator | x | x | x | x | x | x | ||||||||||
| publicCloudProject:openstack:computeOperator | x | x | x | x | x | x | x | |||||||||
| publicCloudProject:openstack:imageOperator | x | x | x | |||||||||||||
| publicCloudProject:openstack:infrastructureSupervisor | x | x | x | x | x | |||||||||||
| publicCloudProject:openstack:networkOperator | x | x | x | |||||||||||||
| publicCloudProject:openstack:networkSecurityOperator | x | x | ||||||||||||||
| publicCloudProject:openstack:objectstoreOperator | x | |||||||||||||||
| publicCloudProject:openstack:volumeOperator | x | x | x | |||||||||||||
| publicCloudProject:ai:aiTrainingOperator | x | x | ||||||||||||||
| publicCloudProject:ai:aiTrainingRead | x |
For our example, we would like to add the publicCloudProject:openstack:infrastructureSupervisor right, which can be used to retrieve information on our infrastructure. It can be useful for setting up monitoring scripts.
You can now add the following access policy:
Using a service account with the OpenStack command line (CLI)
If you are using your OpenStack infrastructure with the command line, you will need to use the following environment variables:
If you are using OVHcloud services from EMEA, add the following variables:
If you are using OVHcloud services from the “Rest of the World” regions, add the following variables:
Then add the following variables with the values corresponding to your configuration:
- OS_PROJECT_ID: your Public Cloud project ID.
- OS_CLIENT_ID: your service account ID.
- OS_CLIENT_SECRET: your service account secret.
- OS_REGION_NAME: the region affected by your script.
You can now use your command line to observe your virtual machines.
However, you will not have access to Swift Object Storage services with this service account:
Using the service account with the Python SDK
To connect using the Python SDK and OVHcloud access, you can use two techniques:
- Environment variables: As with the CLI, you can configure your access with environment variables. These are the same as those documented in the Use a service account with the Openstack command line (CLI) section above.
- A clouds.yaml configuration file: if you want to deploy your configuration with a clouds.yaml file as described in the official Openstack documentation, you must follow the following format:
The previous example works for the EMEA region. If you are using OVHcloud services from the "Rest of the World" regions, edit the following lines with these values:
- identity_provider:
ovhcloud-world - discovery_endpoint:
https://iam.ovh.ca/role-adapter/urn:v1:ca:resource:publicCloudProject:pci/.well-known/openid-configuration
As a reminder, you must replace the variables in these examples with the values that match your configuration.
Then add the following variables with the values corresponding to your configuration:
- project_id: your Public Cloud project ID.
- client_id: your service account ID.
- client_secret: your service account secret.
- region_name: the region concerned by your script.
You can now use your Python code to access the services authorized by the access policy associated with your service account. Using the previous example, you can access the list of servers as follows:
However, you will not have access to the Object Storage service containers:
Go further
Join our community of users on https://community.ovh.com/en/.