def setUp(self): RSA = encrypter.get_RSA() master_key_priv = os.path.join( os.path.abspath(os.path.dirname(__file__)), 'test-files', 'test-executors', 'internet.private.key') with open(master_key_priv, 'rb') as f: buf = unicode_to_bytes(f.read()) self.mfkey_priv = RSA.PrivateKey.load_pkcs1(buf) master_key_pub = os.path.join( os.path.abspath(os.path.dirname(__file__)), 'test-files', 'test-executors', 'internet.public.key') with open(master_key_pub, 'rb') as f: buf = unicode_to_bytes(f.read()) self.mfkey_pub = RSA.PublicKey.load_pkcs1(buf)
def _get_value_from_expression(): logger.debug('\n\n\n\n\n\n\n\n\n\nEVAL EXPR %s\n\n' % this_expr) expr_64 = base64.b64encode(unicode_to_bytes(this_expr)) try: r = post_opsbro_json('/agent/evaluator/eval', {'expr': expr_64}) except Exception as exp: logger.debug('\n\n\n\n\n\n\n\n\n\nExcep Result %s => %s\n\n' % (this_expr, exp)) r = None logger.debug('\n\n\n\n\n\n\n\nResult %s => %s\n\n' % (this_expr, r)) return r
def _save_key(key_string, zone_name, key_path): with open(key_path, 'wb') as f: f.write(unicode_to_bytes(key_string)) cprint('%s OK the key is saved as file %s' % (CHARACTERS.check, key_path)) # Try to send the information to the agent, so it can reload the key try: get_opsbro_json('/agent/zones-keys/reload/%s' % zone_name) except get_request_errors(): cprint( ' | The agent seems to not be started. Skipping hot key reload.', color='grey') return
def test_encryption(self): orig_test = 'Hi I am Alice' RSA = encrypter.get_RSA() encrypted = RSA.encrypt(unicode_to_bytes(orig_test), self.mfkey_pub) # encrypted thanks to public decrypted = bytes_to_unicode(RSA.decrypt( encrypted, self.mfkey_priv)) # decrypte with private print('Original:%s(%s)\nDecrypted:%s(%s)' % (orig_test, type(orig_test), decrypted, type(decrypted))) self.assert_(decrypted == orig_test) self.assert_(encrypted != orig_test) print('OK')
def do_exec(group='*', cmd='uname -a'): if cmd == '': logger.error('Missing command') return # The information is available only if the agent is started wait_for_agent_started(visual_wait=True) cmd_64 = bytes_to_unicode(base64.b64encode(unicode_to_bytes(cmd))) try: r = get_opsbro_json('/exec/%s?cmd=%s' % (group, cmd_64)) except get_request_errors() as exp: cprint('ERROR: cannot launch the command: %s' % exp, color='red') sys.exit(2) cid = r print("Command group launch as cid", cid) time.sleep(5) # TODO: manage a real way to get the result.. try: r = get_opsbro_json('/exec-get/%s' % cid) except get_request_errors() as exp: cprint('ERROR: cannot get execution results: %s' % exp, color='red') sys.exit(2) res = r['res'] print('Launched at: %s' % res) for (uuid, e) in res.items(): node = e['node'] nname = node['name'] color = {'alive': 'green', 'dead': 'red', 'suspect': 'yellow', 'leave': 'cyan'}.get(node['state'], 'cyan') cprint(nname, color=color) cprint('Return code for [%s]:' % e['cmd'], end='') color = {0: 'green', 1: 'yellow', 2: 'red'}.get(e['rc'], 'cyan') cprint(e['rc'], color=color) cprint('Output:', end='') cprint(e['output'].strip(), color=color) if e['err']: cprint('Error:', end='') cprint(e['err'].strip(), color='red') cprint('')
def _get_size_hex(self, nb): nb = min(nb, 256 * 256) d, r = divmod(nb, 256) s = chr(d) + chr(r) return unicode_to_bytes(s)