aaf0cc8fae
pyupgrade is a tool (and pre-commit hook) to automatically upgrade syntax for newer versions of the language. It helps getting rid of syntax required for older python versions not supported anymore and prepare us for easier support of newer python versions. The tool is already used in some other OpenStack projects, so it is time to start using it also for Keystone. The change is generated by uncommenting the pre-commit hook and executing `pre-commit run -a` to convert the data. The same could be also achieved by simply trying to commit and adding converted files in few iterations. Change-Id: Ia1f64709e57ebb4e44db128bfea4c5957b2071df
65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
import os
|
|
import requests
|
|
|
|
KEYCLOAK_USERNAME = os.environ.get('KEYCLOAK_USERNAME')
|
|
KEYCLOAK_PASSWORD = os.environ.get('KEYCLOAK_PASSWORD')
|
|
KEYCLOAK_URL = os.environ.get('KEYCLOAK_URL')
|
|
HOST_IP = os.environ.get('HOST_IP', 'localhost')
|
|
|
|
|
|
class KeycloakClient:
|
|
def __init__(self):
|
|
self.session = requests.session()
|
|
|
|
@staticmethod
|
|
def construct_url(realm, path):
|
|
return f'{KEYCLOAK_URL}/admin/realms/{realm}/{path}'
|
|
|
|
@staticmethod
|
|
def token_endpoint(realm):
|
|
return f'{KEYCLOAK_URL}/realms/{realm}/protocol/openid-connect/token'
|
|
|
|
def _admin_auth(self, realm):
|
|
params = {
|
|
'grant_type': 'password',
|
|
'client_id': 'admin-cli',
|
|
'username': KEYCLOAK_USERNAME,
|
|
'password': KEYCLOAK_PASSWORD,
|
|
'scope': 'openid',
|
|
}
|
|
r = requests.post(self.token_endpoint(realm), data=params).json()
|
|
headers = {
|
|
'Authorization': ("Bearer %s" % r['access_token']),
|
|
'Content-Type': 'application/json',
|
|
}
|
|
self.session.headers.update(headers)
|
|
return r
|
|
|
|
def create_client(self, realm, client_id, client_secret, redirect_uris):
|
|
self._admin_auth(realm)
|
|
data = {
|
|
'clientId': client_id,
|
|
'secret': client_secret,
|
|
'redirectUris': redirect_uris,
|
|
'implicitFlowEnabled': True,
|
|
'directAccessGrantsEnabled': True,
|
|
}
|
|
return self.session.post(
|
|
self.construct_url(realm, 'clients'), json=data
|
|
)
|
|
|
|
|
|
def main():
|
|
c = KeycloakClient()
|
|
|
|
redirect_uris = [
|
|
f'http://{HOST_IP}/identity/v3/auth/OS-FEDERATION/identity_providers/sso/protocols/openid/websso',
|
|
f'http://{HOST_IP}/identity/v3/auth/OS-FEDERATION/websso/openid',
|
|
]
|
|
|
|
c.create_client('master', 'devstack', 'nomoresecret', redirect_uris)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|