def _zone_list(zone, nameserver): request = { "method": "list", "parameters": { "zonename": zone, } } try: sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(Env.paths.dnsuxsock) sock.send(bencode(json.dumps(request) + "\n")) response = "" while True: buff = sock.recv(4096) if not buff: break response += bdecode(buff) if response[-1] == "\n": break finally: sock.close() if not response: return try: return json.loads(response)["result"] except ValueError: raise ex.Error("invalid response format")
def add_file(self, key, path, append=None): if key is None: key = os.path.basename(path) if append: data = bencode(self.decode_key(key)) else: data = b"" with open(path, "rb") as ofile: data += ofile.read() self.add_key(key, data)
def _add_key(self, key, data): if not key: raise ex.Error("configuration key name can not be empty") if data is None: raise ex.Error("configuration value can not be empty") if not is_string(data): data = "base64:" + bdecode(base64.urlsafe_b64encode(data)) elif "\n" in data: data = "base64:" + bdecode(base64.urlsafe_b64encode(bencode(data))) else: data = "literal:" + data self.set_multi(["data.%s=%s" % (key, data)]) self.log.info("configuration key '%s' added (%s)", key, print_size(len(data), compact=True, unit="b")) # refresh if in use self.postinstall(key)
def _pkcs12(self, password): required = set(["private_key", "certificate_chain"]) if required & set(self.data_keys()) != required: self.gen_cert() from subprocess import Popen, PIPE import tempfile _tmpcert = tempfile.NamedTemporaryFile() _tmpkey = tempfile.NamedTemporaryFile() tmpcert = _tmpcert.name tmpkey = _tmpkey.name _tmpcert.close() _tmpkey.close() if password is None: from getpass import getpass pwd = getpass("Password: "******"\n" elif password in ["/dev/stdin", "-"]: pwd = sys.stdin.readline() else: pwd = password + "\n" if six.PY3: pwd = bencode(pwd) try: with open(tmpkey, "w") as _tmpkey: os.chmod(tmpkey, 0o600) _tmpkey.write(bdecode(self.decode_key("private_key"))) with open(tmpcert, "w") as _tmpcert: os.chmod(tmpcert, 0o600) _tmpcert.write(bdecode(self.decode_key("certificate_chain"))) cmd = [ "openssl", "pkcs12", "-export", "-in", tmpcert, "-inkey", tmpkey, "-passout", "stdin" ] proc = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE) out, err = proc.communicate(input=pwd) if err: print(err, file=sys.stderr) return out finally: if os.path.exists(tmpcert): os.unlink(tmpcert) if os.path.exists(tmpkey): os.unlink(tmpkey)
def justcall(argv=None, stdin=None, input=None): """ Call subprocess' Popen(argv, stdout=PIPE, stderr=PIPE, stdin=stdin) The 'close_fds' value is autodectected (true on unix, false on windows). Returns (stdout, stderr, returncode) """ if argv is None: argv = [Env.syspaths.false] if input: stdin = PIPE input = bencode(input) try: proc = Popen(argv, stdin=stdin, stdout=PIPE, stderr=PIPE, close_fds=close_fds) out, err = proc.communicate(input=input) return bdecode(out), bdecode(err), proc.returncode except OSError as exc: if exc.errno in (ENOENT, EACCES): return "", "", 1 raise
def cni_cmd(self, _env, data): cmd = [self.cni_bin(data)] if not which(cmd[0]): raise ex.Error("%s not found" % cmd[0]) self.log_cmd(_env, data, cmd) env = {} env.update(Env.initial_env) env.update(_env) proc = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE, env=env) out, err = proc.communicate(input=bencode(json.dumps(data))) out = bdecode(out) try: data = json.loads(out) except ValueError: if proc.returncode == 0: # for example a del portmap outs nothing return raise ex.Error(err) if "code" in data: raise ex.Error(data.get("msg", "")) for line in format_str_flat_json(data).splitlines(): self.log.info(line) return data
def test_bencode(): """ bencode() """ assert bencode("foo")