def update(context, name, bucket, zone, testing_account, credentials): """ Update a GCE account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = {} if credentials: with open(credentials) as credentials_file: creds = json.load(credentials_file) data['credentials'] = creds if bucket: data['bucket'] = bucket if zone: data['region'] = zone if testing_account: data['testing_account'] = testing_account if not data: echo_style('Nothing to update', config_data['no_color'], fg='red') sys.exit(1) result = handle_request_with_token( config_data, '/v1/accounts/gce/{name}'.format(name=name), data) echo_dict(result, config_data['no_color'])
def delete(context, job_id): """ Delete the job with the given ID from the MASH server pipeline. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = delete_job(config_data, job_id) echo_style(result['msg'], config_data['no_color'])
def refresh(context): """ Handle token refresh. Get a new access token using current refresh token. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): refresh_token(config_data) echo_style('Token refreshed.', config_data['no_color'])
def logout(context): """ Handle mash user logout. Deletes the current refresh token. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = logout_session(config_data) echo_style(result['msg'], config_data['no_color'])
def update( context, additional_regions, group, name, region, subnet, access_key_id, secret_access_key ): """ Update an EC2 account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = {} if all([access_key_id, secret_access_key]): data['credentials'] = { 'access_key_id': access_key_id, 'secret_access_key': secret_access_key } elif any([access_key_id, secret_access_key]): echo_style( 'Both secret_access_key and access_key_id are required ' 'when updating credentials.', config_data['no_color'], fg='red' ) sys.exit(1) if additional_regions: regions = additional_regions_repl() if regions: data['additional_regions'] = regions if group: data['group'] = group if region: data['region'] = region if subnet: data['subnet'] = subnet if not data: echo_style('Nothing to update', config_data['no_color'], fg='red') sys.exit(1) result = handle_request_with_token( config_data, '/v1/accounts/ec2/{name}'.format(name=name), data ) echo_dict(result, config_data['no_color'])
def delete(context, name): """ Delete an Aliyun account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token( config_data, '/v1/accounts/aliyun/{name}'.format(name=name), action='delete') echo_style(result['msg'], config_data['no_color'])
def delete_user(context): """ Handle mash user deletion requests. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): if click.confirm('Are you sure you want to delete user?'): result = handle_request_with_token(config_data, '/v1/user/', action='delete') echo_style(result['msg'], config_data['no_color']) else: echo_style('Aborted', config_data['no_color'], fg='red') sys.exit(1)
def update(context, name, bucket, region, security_group_id, vswitch_id, access_key, access_secret): """ Update a Aliyun account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = {} if all([access_key, access_secret]): data['credentials'] = { 'access_key': access_key, 'access_secret': access_secret } elif any([access_key, access_secret]): echo_style( 'Both access_secret and access_key are required ' 'when updating credentials.', config_data['no_color'], fg='red') sys.exit(1) if bucket: data['bucket'] = bucket if region: data['region'] = region if security_group_id: data['security_group_id'] = security_group_id if vswitch_id: data['vswitch_id'] = vswitch_id if not data: echo_style('Nothing to update', config_data['no_color'], fg='red') sys.exit(1) result = handle_request_with_token( config_data, '/v1/accounts/aliyun/{name}'.format(name=name), data) echo_dict(result, config_data['no_color'])
def login(context, email): """ Handle mash user login. """ config_data = get_config(context.obj) if not email: email = config_data.get('email') if not email: echo_style('No --email parameter and no email in config.', config_data['no_color'], fg='red') sys.exit(1) with handle_errors(config_data['log_level'], config_data['no_color']): password = click.prompt('Enter password', type=str, hide_input=True) result = login_with_pass(config_data, email, password) echo_style(result['msg'], config_data['no_color'])
def update(context, name, bucket, region, availability_domain, compartment_id, oci_user_id, tenancy, signing_key_file): """ Update a OCI account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = {} if signing_key_file: with open(signing_key_file) as key_file: signing_key = key_file.read() data['signing_key'] = signing_key if bucket: data['bucket'] = bucket if region: data['region'] = region if availability_domain: data['availability_domain'] = availability_domain if compartment_id: data['compartment_id'] = compartment_id if oci_user_id: data['oci_user_id'] = oci_user_id if tenancy: data['tenancy'] = tenancy if not data: echo_style('Nothing to update', config_data['no_color'], fg='red') sys.exit(1) result = handle_request_with_token( config_data, '/v1/accounts/oci/{name}'.format(name=name), data) echo_dict(result, config_data['no_color'])
def change_password(context, email): """ Change password for user. """ config_data = get_config(context.obj) if not email: email = config_data.get('email') if not email: echo_style('No --email parameter and no email in config.', config_data['no_color'], fg='red') sys.exit(1) with handle_errors(config_data['log_level'], config_data['no_color']): current_pass = click.prompt('Enter current password', type=str, hide_input=True) new_pass1 = click.prompt('Enter new password', type=str, hide_input=True) new_pass2 = click.prompt('Confirm new password', type=str, hide_input=True) if new_pass1 != new_pass2: raise MashClientException('New passwords do not match!') job_data = { 'email': email, 'current_password': current_pass, 'new_password': new_pass1 } result = handle_request(config_data, '/v1/user/password', job_data=job_data, action='put') echo_style(result['msg'], config_data['no_color'])
def reset_password(context, email): """ Initialize password reset for user. """ config_data = get_config(context.obj) if not email: email = config_data.get('email') if not email: echo_style('No --email parameter and no email in config.', config_data['no_color'], fg='red') sys.exit(1) with handle_errors(config_data['log_level'], config_data['no_color']): if click.confirm('Are you sure you want to reset password?'): result = handle_request(config_data, '/v1/user/password', job_data={'email': email}, action='post') echo_style(result['msg'], config_data['no_color']) else: echo_style('Aborted', config_data['no_color'], fg='red') sys.exit(1)
def add(context, dry_run, document, api_version): """ Send add azure job request to mash server based on provided json document. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): with open(document) as job_file: job_data = json.load(job_file) if dry_run: job_data['dry_run'] = True kwargs = {} if api_version: kwargs['api_version'] = api_version result = add_job(config_data, job_data, 'azure', **kwargs) if 'msg' in result: echo_style(result['msg'], config_data['no_color']) else: echo_dict(result, config_data['no_color'])
def delete(context, jti): """ If no jti is provided delete all tokens. Otherwise delete token matching the jti. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): if jti: result = handle_request_with_token( config_data, '/v1/auth/token/{jti}'.format(jti=jti), action='delete') elif click.confirm('Are you sure you want to delete all tokens?'): result = handle_request_with_token(config_data, '/v1/auth/token', action='delete') else: echo_style('No tokens deleted', config_data['no_color'], fg='red') sys.exit(1) echo_style(result['msg'], config_data['no_color'])
def update(context, name, region, source_container, source_resource_group, source_storage_account, credentials): """ Update an Azure account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = {} if credentials: with open(credentials) as credentials_file: creds = json.load(credentials_file) data['credentials'] = creds if region: data['region'] = region if source_container: data['source_container'] = source_container if source_resource_group: data['source_resource_group'] = source_resource_group if source_storage_account: data['source_storage_account'] = source_storage_account if not data: echo_style('Nothing to update', config_data['no_color'], fg='red') sys.exit(1) result = handle_request_with_token( config_data, '/v1/accounts/azure/{name}'.format(name=name), data) echo_dict(result, config_data['no_color'])
def oidc(context): """ Handle mash OpenID Connect authentication. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request(config_data, '/v1/auth/oauth2', action='get') redirect_port = get_free_port(result['redirect_ports']) if not redirect_port: raise MashClientException('No redirect port available') auth_url = '{}&redirect_uri=http%3A%2F%2Flocalhost%3A{}'.format( result['auth_url'], redirect_port) # display authentication message echo_style('{}: {}'.format(result['msg'], auth_url), config_data['no_color']) click.launch(auth_url) auth_code = get_oauth2_code(redirect_port) if not auth_code: echo_style('Failed login: No authentication code received.', config_data['no_color'], fg='red') sys.exit(1) job_data = { 'auth_code': auth_code, 'state': result['state'], 'redirect_port': redirect_port } tokens = handle_request(config_data, '/v1/auth/oauth2', job_data=job_data, action='post') tokens_file = get_tokens_file(config_data['config_dir'], config_data['profile']) save_tokens_to_file(tokens_file, tokens) echo_style('Login successful.', config_data['no_color'])