def guacamole_get_connections_groups(base_url, validate_certs, datasource, auth_token): """ Returns a dict of dicts. Each dict provides the details for one of the connections groups defined in guacamole """ url_list_connections_groups = URL_LIST_CONNECTIONS_GROUPS.format( url=base_url, datasource=datasource, token=auth_token) try: connections_groups = json.load( open_url(url_list_connections_groups, method='GET', validate_certs=validate_certs)) except ValueError as e: raise GuacamoleError( 'API returned invalid JSON when trying to obtain connections groups from %s: %s' % (url_list_connections_groups, str(e))) except Exception as e: raise GuacamoleError( 'Could not obtain connections groups from %s: %s' % (url_list_connections_groups, str(e))) return connections_groups
def guacamole_get_user_permissions(base_url, validate_certs, datasource, username, auth_token): """ Return a dict with detailed current permissions for a user """ url_get_user_permissions = URL_GET_USER_PERMISSIONS.format( url=base_url, datasource=datasource, username=username, token=auth_token) try: user_permissions = json.load( open_url(url_get_user_permissions, method='GET', validate_certs=validate_certs)) except ValueError as e: raise GuacamoleError( 'API returned invalid JSON when trying to obtain user permissions from %s: %s' % (url_get_user_permissions, str(e))) except Exception as e: raise GuacamoleError('Could not obtain user permissions from %s: %s' % (url_get_user_permissions, str(e))) return user_permissions
def guacamole_get_connection_details(base_url, validate_certs, datasource, connection_id, auth_token): """ Return a dict with detailed connection parameters for a single connection. This function requires a connection id and provides more information than function guacamole_get_connections() """ url_connection_details = URL_CONNECTION_DETAILS.format( url=base_url, datasource=datasource, connection_id=connection_id, token=auth_token) try: connection_details = json.load( open_url(url_connection_details, method='GET', validate_certs=validate_certs)) except ValueError as e: raise GuacamoleError( 'API returned invalid JSON when trying to obtain connection details from %s: %s' % (url_connection_details, str(e))) except Exception as e: raise GuacamoleError( 'Could not obtain connection details from %s: %s' % (url_connection_details, str(e))) return connection_details
def guacamole_update_password_current_user(base_url, validate_certs, datasource, username, current_password, new_password, auth_token): """ Update just the password for the user we use to connect to the api We usually do this for the default admin user "guacadmin" http://mail-archives.apache.org/mod_mbox/guacamole-dev/202006.mbox/%3CCALKeL-PbLS8qodWEL3yHWWCir87Xqq0z9pVcbp3S-yjwEpYVTw%40mail.gmail.com%3E """ url_update_password_current_user = URL_UPDATE_PASSWORD_CURRENT_USER.format( url=base_url, datasource=datasource, username=username, token=auth_token) payload = {'oldPassword': current_password, 'newPassword': new_password} try: headers = {'Content-Type': 'application/json'} open_url(url_update_password_current_user, method='PUT', validate_certs=validate_certs, headers=headers, data=json.dumps(payload)) except Exception as e: raise GuacamoleError('Could not update user in %s: %s' % (url_update_password_current_user, str(e)))
def guacamole_update_user_permissions_for_group(base_url, validate_certs, datasource, username, group_id, operation, auth_token): """ Update permissions for existing user in a specific group of connections When granting access to a connection which is located in a group of connections we need to grant access to the parent group too """ url_update_user_permissions = URL_UPDATE_USER_PERMISSIONS.format( url=base_url, datasource=datasource, username=username, token=auth_token) payload = [{ 'op': operation, 'path': '/connectionGroupPermissions/' + str(group_id), 'value': 'READ' }] try: headers = {'Content-Type': 'application/json'} open_url(url_update_user_permissions, method='PATCH', validate_certs=validate_certs, headers=headers, data=json.dumps(payload)) except Exception as e: raise GuacamoleError('Could not update permissions in %s: %s' % (url_update_user_permissions, str(e)))
def guacamole_update_user_permissions_for_connection(base_url, validate_certs, datasource, username, connection_id, operation, auth_token): """ Update permissions for existing user in a specific connection """ url_update_user_permissions = URL_UPDATE_USER_PERMISSIONS.format( url=base_url, datasource=datasource, username=username, token=auth_token) payload = [{ 'op': operation, 'path': '/connectionPermissions/' + str(connection_id), 'value': 'READ' }] try: headers = {'Content-Type': 'application/json'} open_url(url_update_user_permissions, method='PATCH', validate_certs=validate_certs, headers=headers, data=json.dumps(payload)) except Exception as e: raise GuacamoleError('Could not update permissions in %s: %s' % (url_update_user_permissions, str(e)))
def guacamole_delete_connection(base_url, validate_certs, datasource, connection_id, auth_token): """ Delete an existing guacamole connection. API doesn't return any json """ url_delete_connection = URL_DELETE_CONNECTION.format( url=base_url, datasource=datasource, connection_id=connection_id, token=auth_token) try: open_url(url_delete_connection, method='DELETE', validate_certs=validate_certs) except Exception as e: raise GuacamoleError('Could not delete guacamole connection from %s: %s' % (url_delete_connection, str(e)))
def guacamole_get_users(base_url, validate_certs, datasource, auth_token): """ Returns a dict with all the users registered in the guacamole server """ url_list_users = URL_LIST_USERS.format(url=base_url, datasource=datasource, token=auth_token) try: guacamole_users = json.load( open_url(url_list_users, method='GET', validate_certs=validate_certs)) except ValueError as e: raise GuacamoleError( 'API returned invalid JSON when trying to obtain list of users from %s: %s' % (url_list_users, str(e))) except Exception as e: raise GuacamoleError( 'Could not obtain list of guacamole users from %s: %s' % (url_list_users, str(e))) return guacamole_users
def guacamole_update_connection(base_url, validate_certs, datasource, connection_id, auth_token, payload): """ Update an existing guacamole connection """ url_update_connection = URL_UPDATE_CONNECTION.format( url=base_url, datasource=datasource, connection_id=connection_id, token=auth_token) try: headers = {'Content-Type': 'application/json'} open_url(url_update_connection, method='PUT', validate_certs=validate_certs, headers=headers, data=json.dumps(payload)) except Exception as e: raise GuacamoleError('Could not add a new connection in %s: %s' % (url_update_connection, str(e)))
def guacamole_add_connection(base_url, validate_certs, datasource, auth_token, payload): """ Add a new connection to the guacamole server. ] Connection can be RDP, VNC, SSH or TELNET """ url_add_connection = URL_ADD_CONNECTION.format( url=base_url, datasource=datasource, token=auth_token) try: headers = {'Content-Type': 'application/json'} open_url(url_add_connection, method='POST', validate_certs=validate_certs, headers=headers, data=json.dumps(payload)) except Exception as e: raise GuacamoleError('Could not add a new connection in %s: %s' % (url_add_connection, str(e)))
def guacamole_delete_user(base_url, validate_certs, datasource, username, auth_token): """ Delete existing user in the guacamole server. """ url_delete_user = URL_DELETE_USER.format(url=base_url, datasource=datasource, username=username, token=auth_token) try: open_url(url_delete_user, method='DELETE', validate_certs=validate_certs) except Exception as e: raise GuacamoleError('Could not delete user in %s: %s' % (url_delete_user, str(e)))
def guacamole_add_user(base_url, validate_certs, datasource, auth_token, payload): """ Add a new user account to the guacamole server doing a POST of the payload to the API """ url_add_user = URL_ADD_USER.format(url=base_url, datasource=datasource, token=auth_token) try: headers = {'Content-Type': 'application/json'} open_url(url_add_user, method='POST', validate_certs=validate_certs, headers=headers, data=json.dumps(payload)) except Exception as e: raise GuacamoleError('Could not add a new user in %s: %s' % (url_add_user, str(e)))
def guacamole_add_connections_group(base_url, validate_certs, datasource, auth_token, payload): """ Add a new connections group to the guacamole server. """ url_add_connections_group = URL_ADD_CONNECTIONS_GROUP.format( url=base_url, datasource=datasource, token=auth_token) try: headers = {'Content-Type': 'application/json'} open_url(url_add_connections_group, method='POST', validate_certs=validate_certs, headers=headers, data=json.dumps(payload)) except Exception as e: raise GuacamoleError( 'Could not add a new connections group in %s: %s' % (url_add_connections_group, str(e)))
def guacamole_update_user(base_url, validate_certs, datasource, username, auth_token, payload): """ Update existing user in the guacamole server doing a PUT of the payload to the API """ url_update_user = URL_UPDATE_USER.format(url=base_url, datasource=datasource, username=username, token=auth_token) try: headers = {'Content-Type': 'application/json'} open_url(url_update_user, method='PUT', validate_certs=validate_certs, headers=headers, data=json.dumps(payload)) except Exception as e: raise GuacamoleError('Could not update user in %s: %s' % (url_update_user, str(e)))
def guacamole_delete_connections_group(base_url, validate_certs, datasource, auth_token, group_numeric_id): """ Delete a connections group """ url_delete_connections_group = URL_DELETE_CONNECTIONS_GROUP.format( url=base_url, datasource=datasource, group_numeric_id=group_numeric_id, token=auth_token) try: headers = {'Content-Type': 'application/json'} open_url(url_delete_connections_group, method='DELETE', validate_certs=validate_certs, headers=headers) except Exception as e: raise GuacamoleError('Could not delete a connections group in %s: %s' % (url_delete_connections_group, str(e)))
def guacamole_update_connections_group(base_url, validate_certs, datasource, auth_token, group_numeric_id, payload): """ Update an existing connections group """ url_update_connections_group = URL_UPDATE_CONNECTIONS_GROUP.format( url=base_url, datasource=datasource, group_numeric_id=group_numeric_id, token=auth_token) try: headers = {'Content-Type': 'application/json'} open_url(url_update_connections_group, method='PUT', validate_certs=validate_certs, headers=headers, data=json.dumps(payload)) except Exception as e: raise GuacamoleError('Could not update a connections group in %s: %s' % (url_update_connections_group, str(e)))