You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datalab.apache.org by lf...@apache.org on 2022/11/02 08:47:06 UTC
[incubator-datalab] 05/09: [DATALAB-3073]: added client creation removal for azure
This is an automated email from the ASF dual-hosted git repository.
lfrolov pushed a commit to branch epm-v2.5.2.1
in repository https://gitbox.apache.org/repos/asf/incubator-datalab.git
commit df538ccbe7e76d9a44734e4ab2f956d6abf1708b
Author: leonidfrolov <fr...@gmail.com>
AuthorDate: Thu Oct 13 11:13:40 2022 +0300
[DATALAB-3073]: added client creation removal for azure
---
.../scripts/azure/common_terminate_notebook.py | 39 ++++++++++++++++++++
.../src/general/scripts/azure/jupyter_configure.py | 41 ++++++++++++++++++++++
2 files changed, 80 insertions(+)
diff --git a/infrastructure-provisioning/src/general/scripts/azure/common_terminate_notebook.py b/infrastructure-provisioning/src/general/scripts/azure/common_terminate_notebook.py
index 77ef93f41..d6396b192 100644
--- a/infrastructure-provisioning/src/general/scripts/azure/common_terminate_notebook.py
+++ b/infrastructure-provisioning/src/general/scripts/azure/common_terminate_notebook.py
@@ -25,6 +25,7 @@ import datalab.actions_lib
import datalab.fab
import datalab.meta_lib
import json
+import requests
from datalab.logger import logging
import os
import sys
@@ -54,6 +55,44 @@ def terminate_nb(resource_group_name, notebook_name):
datalab.fab.append_result("Failed to terminate instance", str(err))
sys.exit(1)
+ if os.environ['notebook_create_keycloak_client'] == 'True':
+ logging.info("Terminating notebook keycloak client")
+ try:
+ keycloak_auth_server_url = '{}/realms/master/protocol/openid-connect/token'.format(
+ os.environ['keycloak_auth_server_url'])
+ keycloak_client_url = '{0}/admin/realms/{1}/clients'.format(os.environ['keycloak_auth_server_url'],
+ os.environ['keycloak_realm_name'])
+
+ keycloak_auth_data = {
+ "username": os.environ['keycloak_user'],
+ "password": os.environ['keycloak_user_password'],
+ "grant_type": "password",
+ "client_id": "admin-cli",
+ }
+
+ client_params = {
+ "clientId": "{}-{}-{}-{}".format(notebook_config['service_base_name'], notebook_config['project_name'],
+ notebook_config['endpoint_name'], notebook_config['exploratory_name'])
+ }
+
+ keycloak_token = requests.post(keycloak_auth_server_url, data=keycloak_auth_data).json()
+
+ keycloak_get_id_client = requests.get(keycloak_client_url, data=keycloak_auth_data, params=client_params,
+ headers={"Authorization": "Bearer " + keycloak_token.get("access_token"),
+ "Content-Type": "application/json"})
+ json_keycloak_client_id = json.loads(keycloak_get_id_client.text)
+ keycloak_id_client = json_keycloak_client_id[0]['id']
+
+ keycloak_client_delete_url = '{0}/admin/realms/{1}/clients/{2}'.format(os.environ['keycloak_auth_server_url'],
+ os.environ['keycloak_realm_name'],
+ keycloak_id_client)
+
+ requests.delete(keycloak_client_delete_url,
+ headers={"Authorization": "Bearer " + keycloak_token.get("access_token"),
+ "Content-Type": "application/json"})
+ except Exception as err:
+ logging.error("Failed to remove project client from Keycloak", str(err))
+
if __name__ == "__main__":
# generating variables dictionary
diff --git a/infrastructure-provisioning/src/general/scripts/azure/jupyter_configure.py b/infrastructure-provisioning/src/general/scripts/azure/jupyter_configure.py
index 6f428037a..4ebbb60b3 100644
--- a/infrastructure-provisioning/src/general/scripts/azure/jupyter_configure.py
+++ b/infrastructure-provisioning/src/general/scripts/azure/jupyter_configure.py
@@ -31,6 +31,7 @@ import sys
import traceback
import subprocess
from fabric import *
+import uuid
if __name__ == "__main__":
try:
@@ -288,6 +289,46 @@ if __name__ == "__main__":
AzureActions.remove_instance(notebook_config['resource_group_name'], notebook_config['instance_name'])
sys.exit(1)
+ if os.environ['notebook_create_keycloak_client'] == 'True':
+ try:
+ logging.info('[SETUP KEYCLOAK CLIENT]')
+ notebook_config['keycloak_client_name'] = '{}-{}-{}-{}'\
+ .format(notebook_config['service_base_name'], notebook_config['project_name'],
+ notebook_config['endpoint_name'], notebook_config['exploratory_name'])
+ notebook_config['keycloak_client_secret'] = str(uuid.uuid4())
+ keycloak_params = "--service_base_name {} --keycloak_auth_server_url {} --keycloak_realm_name {} " \
+ "--keycloak_user {} --keycloak_user_password {} --keycloak_client_secret {} " \
+ "--project_name {} --endpoint_name {} --exploratory_name {}"\
+ .format(notebook_config['service_base_name'], os.environ['keycloak_auth_server_url'],
+ os.environ['keycloak_realm_name'], os.environ['keycloak_user'],
+ os.environ['keycloak_user_password'], notebook_config['keycloak_client_secret'],
+ notebook_config['project_name'], notebook_config['endpoint_name'],
+ notebook_config['exploratory_name'])
+ try:
+ subprocess.run("~/scripts/{}.py {}".format('configure_keycloak', keycloak_params), shell=True, check=True)
+ except:
+ datalab.fab.append_result("Failed setup keycloak client")
+ raise Exception
+
+ try:
+ conn = datalab.fab.init_datalab_connection(instance_hostname, notebook_config['datalab_ssh_user'],
+ notebook_config['ssh_key_path'], '', False)
+ content = json.loads(conn.sudo("cat /home/{}/.local/share/jupyter/kernels/py3spark_local/kernel.json"
+ .format(notebook_config['datalab_ssh_user'])).stdout)
+ content['env']['KEYCLOAK_CLIENT'] = notebook_config['keycloak_client_name']
+ content['env']['KEYCLOAK_SECRET'] = notebook_config['keycloak_client_secret']
+ conn.sudo("echo '{}' > /home/{}/.local/share/jupyter/kernels/py3spark_local/kernel.json"
+ .format(json.dumps(content), notebook_config['datalab_ssh_user']))
+ conn.sudo('systemctl restart jupyter-notebook')
+ except:
+ datalab.fab.append_result("Failed to write variables to .bashrc")
+ raise Exception
+
+ except Exception as err:
+ datalab.fab.append_result("Failed setup keycloak client ", str(err))
+ GCPActions.remove_instance(notebook_config['instance_name'], notebook_config['zone'])
+ sys.exit(1)
+
try:
logging.info('[SETUP EDGE REVERSE PROXY TEMPLATE]')
additional_info = {
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datalab.apache.org
For additional commands, e-mail: commits-help@datalab.apache.org