def broadcast_operation(operation, transaction_id, file_server_session_key, encrypted_file_server_session_key, lock_service_session_key, encrytped_lock_service_session_key, replication_service_session_key=None, encrypted_replication_service_session_key=None): # broadcast first to file servers, when they have completed, then send to lock service for ip in file_server_addrs: msg = encrypt_msg( { 'operation': operation, 'transaction_id': transaction_id, 'replication_service_session_key': replication_service_session_key, 'encrypted_replication_service_session_key': encrypted_replication_service_session_key }, file_server_session_key) msg['encrypted_session_key'] = encrypted_file_server_session_key r = requests.put(ip, data=msg) msg = encrypt_msg( { 'operation': operation, 'transaction_id': transaction_id }, lock_service_session_key) msg['encrypted_session_key'] = encrytped_lock_service_session_key r = requests.put(lock_service_ip, data=msg)
def broadcast_updated_file(file_id, bytes, user_id, session_key, encrypted_session_key, replication_service_session_key, encrypted_replication_service_session_key): servers_with_file_copies = get_file_servers( file_id, replication_service_session_key, encrypted_replication_service_session_key) for server in filter( lambda server: server != 'http://' + host + ':' + str(port_num), servers_with_file_copies): msg = encrypt_msg( { 'operation': 'store', 'file_id': file_id, 'bytes': bytes, 'transaction_id': None, 'replication_service_session_key': replication_service_session_key, 'encrypted_replication_service_session_key': encrypted_replication_service_session_key, 'is_broadcast': True, 'user_id': user_id }, session_key) msg['encrypted_session_key'] = encrypted_session_key r = requests.post(server, data=msg)
def api(): session_key, params = get_session_key_decrypt_msg( request.args, DIRECTORY_SERVICE_SECRET_KEY) file_name = params.get('file_name') replication_service_key = params.get('replication_service_key') encrytped_replication_service_key = params.get( 'encrytped_replication_service_key') if not file_name: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must specify a file_name' }, session_key)) if replication_service_key in [ None, 'None' ] or encrytped_replication_service_key in [None, 'None']: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must specify a replication service key' }, session_key)) file_id = file_name.replace('/', '_') server = get_file_server(file_id, replication_service_key, encrytped_replication_service_key) if server: return jsonify( encrypt_msg( { 'status': 'success', 'server': server, 'file_id': file_id }, session_key)) else: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'Could not find a server' }, session_key))
def api(): session_key, params = get_session_key_decrypt_msg(request.args, REPLICATION_SERVICE_SECRET_KEY) file_id = params.get('file_id') operation = params.get('operation') fs_session_key = params.get('fs_session_key') if operation == 'get all servers with copies': file_servers = r_lib.get_all_file_servers_with_copy(file_id) return jsonify(encrypt_msg({ 'status': 'success', 'servers': file_servers }, session_key)) elif operation == 'get server': file_server = r_lib.get_file_server(file_id, session_key=fs_session_key) return jsonify(encrypt_msg({ 'status': 'success', 'server': file_server }, session_key)) else: return jsonify(encrypt_msg({ 'status': 'error', 'error_message': 'Illegal operation: {0}'.format(operation) }, session_key))
def get_file_servers(file_id, replication_service_key, encrytped_replication_service_key): msg = encrypt_msg( { 'file_id': file_id, 'operation': 'get all servers with copies' }, replication_service_key) msg['encrypted_session_key'] = encrytped_replication_service_key r = requests.get(replication_service_addr, params=msg) res = decrypt_msg(r.json(), replication_service_key) if res.get('status') == 'success': return eval(res.get('servers')) else: return None
def api(): if request.method == 'GET': params = request.args else: params = request.form if request.method == 'GET': user_id = params.get('user_id') user_password = get_user_password(user_id) if user_password is None: return jsonify({ 'status': 'error', 'error_message': 'We do not have a password for that user' }) encrypted_server_name = params.get('server_name') server_name = decrypt_str(encrypted_server_name, password_to_key(user_password)) if server_name in token_maps: new_session_key = gen_key() timeout = datetime.now() + timedelta(hours=8) return jsonify( encrypt_msg( { 'status': 'success', 'fs_session_key': encrypt_str(new_session_key, token_maps[server_name]), 'session_key': new_session_key, 'timeout': timeout }, user_password)) else: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'Busted you failed authentication - better luck next time' }, user_password)) elif request.method == 'POST': operation = params.get('operation') if operation == 'create_user': if params.get('admin_password') != ADMIN_PASSWORD: return jsonify({ 'status': 'error', 'error_message': 'You have provided administrative authentication' }) password = params.get('password') access_level = params.get('access_level') cur = g.db.execute( 'insert into clients (password, access_level) values (?, ?)', (password, access_level)) g.db.commit() return jsonify({'status': 'success', 'user_id': cur.lastrowid})
def api(): if request.method == 'GET': params = request.args else: params = request.form session_key, params = get_session_key_decrypt_msg(params, FS_SERVER_SECRET_KEY) operation = params.get('operation') file_id = params.get('file_id') if request.method == 'POST': if operation == 'store': bytes = params.get('bytes') transaction_id = params.get('transaction_id') if bytes: replication_service_session_key = params.get( 'replication_service_session_key') encrypted_replication_service_session_key = params.get( 'encrypted_replication_service_session_key') if not replication_service_session_key or not encrypted_replication_service_session_key: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must specify a replication service session key' }, session_key)) try: is_broadcast = params.get('is_broadcast') user_id = params.get('user_id') write_(file_id, bytes, user_id, transaction_id, session_key, params.get('encrypted_session_key'), replication_service_session_key, encrypted_replication_service_session_key, broadcast=(is_broadcast is None or not is_broadcast)) return jsonify( encrypt_msg({'status': 'success'}, session_key)) except Exception as e: return jsonify( encrypt_msg({ 'status': 'error', 'error_message': e }, session_key)) else: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must provide bytes to write to the file' }, session_key)) else: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'The only operation allowed with the POST method is store, you specified: {0}' .format(operation) }, session_key)) elif request.method == 'GET': if operation == 'fetch': if not does_file_exist(file_id): return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'File ID: {0} does not exist'.format(file_id) }, session_key)) else: try: res = read_(file_id) return jsonify( encrypt_msg({ 'status': 'success', 'file_contents': res }, session_key)) except Exception as e: return jsonify( encrypt_msg({ 'status': 'error', 'error_message': e }, session_key)) elif operation == 'poll': user_id = params.get('user_id') return jsonify( encrypt_msg( { 'status': 'success', 'is_stale_copy': is_stale_copy(file_id, user_id) }, session_key)) else: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'The only operations allowed with the GET method are (fetch/poll), you specified: {0}' .format(operation) }, session_key)) elif request.method == 'PUT': transaction_id = params.get('transaction_id') if not transaction_id: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must specify a transaction id' }, session_key)) if operation == 'commit_transaction': replication_service_session_key = params.get( 'replication_service_session_key') encrypted_replication_service_session_key = params.get( 'encrypted_replication_service_session_key') if (replication_service_session_key in [ None, 'None' ]) or (encrypted_replication_service_session_key in [None, 'None' ]): return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must specify a replication service session key' }, session_key)) try: user_id = params.get('user_id') commit_transaction(transaction_id, user_id, session_key, params.get('encrypted_session_key'), replication_service_session_key, encrypted_replication_service_session_key) return jsonify(encrypt_msg({'status': 'success'}, session_key)) except Exception as e: return jsonify( encrypt_msg({ 'status': 'error', 'error_message': e }, session_key)) elif operation == 'cancel_transaction': try: cancel_transaction(transaction_id) return jsonify(encrypt_msg({'status': 'success'}, session_key)) except Exception as e: return jsonify( encrypt_msg({ 'status': 'error', 'error_message': e }, session_key)) else: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'The only operations allowed with the PUT method are (commit_transaction/cancel_transaction), you specified: {0}' .format(operation) }, session_key))
def api(): session_key, params = get_session_key_decrypt_msg(request.form, LOCK_SERVICE_SECRET_KEY) operation = params.get('operation') if not operation: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must specify an operation from (lock/unlock)' }, session_key)) if request.method == 'POST': file_id = params.get('file_id') if not file_id: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must specify a file_id' }, session_key)) if operation == 'lock': res = lock(file_id) if res: return jsonify(encrypt_msg({'status': 'success'}, session_key)) else: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'Timeout: failed to lock file: {0}'.format(file_id) }, session_key)) elif operation == 'unlock': unlock(file_id) return jsonify(encrypt_msg({'status': 'success'}, session_key)) elif request.method == 'PUT': transaction_id = params.get('transaction_id') if not transaction_id: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must specify a transaction id' }, session_key)) if operation == 'commit_transaction': try: commit_transaction(transaction_id) return jsonify(encrypt_msg({'status': 'success'}, session_key)) except Exception as e: return jsonify({'status': 'error', 'error_message': e}) elif operation == 'cancel_transaction': try: cancel_transaction(transaction_id) return jsonify(encrypt_msg({'status': 'success'}, session_key)) except Exception as e: return jsonify( encrypt_msg({ 'status': 'error', 'error_message': e }, session_key)) else: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'The only operations allowed with the PUT method are (commit_transaction/cancel_transaction), you specified: {0}' .format(operation) }, session_key))
def api(): session_key, params = get_session_key_decrypt_msg( request.form, TRANSACTION_SERVICE_SECRET_KEY) operation = params.get('operation') file_server_session_key = params.get('file_server_session_key') encrypted_file_server_session_key = params.get( 'encrypted_file_server_session_key') lock_service_session_key = params.get('lock_service_session_key') encrypted_lock_service_session_key = params.get( 'encrypted_lock_service_session_key') if not operation: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must specify an operation from (start/commit/cancel)' }, session_key)) if not file_server_session_key or not encrypted_file_server_session_key: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must specify a file server session key' }, session_key)) if not lock_service_session_key or not encrypted_lock_service_session_key: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must specify a lock service session key' }, session_key)) if operation == 'start_transaction': t_id = get_new_transaction_id() return jsonify( encrypt_msg({ 'status': 'success', 'transaction_id': t_id }, session_key)) else: transaction_id = params.get('transaction_id') if not transaction_id: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must specify a transaction id' }, session_key)) if operation == 'commit_transaction': replication_service_session_key = params.get( 'replication_service_session_key') encrypted_replication_service_session_key = params.get( 'encrypted_replication_service_session_key') if replication_service_session_key in [ None, 'None' ] or encrypted_replication_service_session_key in [None, 'None']: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must specify a replication service session key if you wish to commit a transaction' }, session_key)) broadcast_commit(transaction_id, file_server_session_key, encrypted_file_server_session_key, lock_service_session_key, encrypted_lock_service_session_key, replication_service_session_key, encrypted_replication_service_session_key) return jsonify(encrypt_msg({'status': 'success'}, session_key)) elif operation == 'cancel_transaction': broadcast_cancel(transaction_id, file_server_session_key, encrypted_file_server_session_key, lock_service_session_key, encrypted_lock_service_session_key) return jsonify(encrypt_msg({'status': 'success'}, session_key)) else: return jsonify( encrypt_msg( { 'status': 'error', 'error_message': 'You must specify an operation from (start/commit/cancel), you specified: {0}' .format(operation) }, session_key))