Skip to content

Instantly share code, notes, and snippets.

@stephenfin
Created September 1, 2023 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenfin/5834e4bd3213a2b9a9313ee3a7f56946 to your computer and use it in GitHub Desktop.
Save stephenfin/5834e4bd3213a2b9a9313ee3a7f56946 to your computer and use it in GitHub Desktop.
Using service tokens with openstacksdk

Using service tokens with openstacksdk

An example of using service tokens with SDK. This is now part of the SDK documentation:

https://review.opendev.org/c/openstack/openstacksdk/+/893505

Usage

Update the test.conf file with correct credentials for your cloud, then run:

python test.py --conf-file test.conf

Watch the debug logs and you will see authentication of the service user taking place.

[neutron]
region_name = RegionOne
auth_strategy = keystone
project_domain_name = Default
project_name = service
user_domain_name = Default
password = password
username = neutron
auth_url = http://10.0.110.85/identity
auth_type = password
service_metadata_proxy = True
default_floating_pool = public
[service_user]
auth_strategy = keystone
project_domain_name = Default
project_name = service
user_domain_name = Default
password = password
username = nova
auth_url = http://10.0.110.85/identity
auth_type = password
#!/usr/bin/env python3
from keystoneauth1 import loading as ks_loading
from keystoneauth1 import service_token
from oslo_config import cfg
import openstack
from openstack import connection
CONF = cfg.CONF
group = cfg.OptGroup('neutron')
ks_loading.register_session_conf_options(CONF, group)
ks_loading.register_auth_conf_options(CONF, group)
ks_loading.register_adapter_conf_options(CONF, group)
group = cfg.OptGroup('service_user')
ks_loading.register_session_conf_options(CONF, group)
ks_loading.register_auth_conf_options(CONF, group)
CONF()
user_auth = ks_loading.load_auth_from_conf_options(CONF, 'neutron')
service_auth = ks_loading.load_auth_from_conf_options(CONF, 'service_user')
auth = service_token.ServiceTokenAuthWrapper(user_auth, service_auth)
sess = ks_loading.load_session_from_conf_options(CONF, 'neutron', auth=auth)
conn = connection.Connection(
session=sess,
oslo_conf=CONF,
)
openstack.enable_logging(debug=True)
print('ports:')
for port in conn.network.ports():
print(f'- id: {port.id}')
print(f' network: {port.network_id}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment