def restart_instance(self, app, part): _iss = unquote_plus(part[0]) _tag = unquote_plus(part[1]) url = app.run_test_instance(quote_plus(_iss), quote_plus(_tag)) if isinstance(url, Response): return url(self.environ, self.start_response) resp = Response(mako_template='message.mako', template_lookup=self.lookup, headers=[]) if url: args = { 'title': "Action performed", 'base': self.baseurl, 'note': 'Your test instance "{iss}:{tag}" has been ' 'restarted as <a href="{url}">{url}</a>'.format(iss=_iss, tag=_tag, url=url) } else: args = { 'title': "Action Failed", 'base': self.baseurl, 'note': 'Could not restart your test instance' } return resp(self.environ, self.start_response, **args)
def run_test_instance(self, iss, tag): _port = self.get_port(iss, tag) args = [ "optest.py", "-i", unquote_plus(iss), "-t", unquote_plus(tag), "-p", str(_port), "-M", self.mako_dir ] for _fl in self.flows: args.extend(["-f", _fl]) if self.path2port: args.extend(["-m", self.path2port]) ppmap = read_path2port_map(self.path2port) try: _path = ppmap[str(_port)] except KeyError: _errtxt = 'Port not in path2port map file {}'.format( self.path2port) logger.error(_errtxt) return ServiceError(_errtxt) url = '{}{}'.format(self.test_tool_base, _path) else: url = '{}:{}'.format(self.test_tool_base[:-1], _port) _econf = self.rest.read_conf(iss, tag) if _econf['tool']['insecure']: args.append('-k') args.append(self.test_tool_conf) _key = '{}:{}'.format(iss, tag) # If already running - kill try: pid = self.running_processes[_key] except KeyError: pass else: logger.info('kill {}'.format(pid)) subprocess.call(['kill', pid]) logger.info(args) if False: # Only on Windows DETACHED_PROCESS = 0x00000008 process = subprocess.Popen(args, creationflags=DETACHED_PROCESS).pid else: process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # continues immediately if process.pid: logger.info("process id: {}".format(process.pid)) self.running_processes['{}:{}'.format(iss, tag)] = process.pid time.sleep(5) return url
def get_iss_and_tag(path): p = path.split('/') try: iss = p[-2] except IndexError: iss = '' try: tag = p[-1] except IndexError: tag = '' return unquote_plus(iss), unquote_plus(tag)
def delete_instance(self, parts, pid=0, app=None): lp = [unquote_plus(p) for p in parts] qp = [quote_plus(p) for p in lp] _key = app.assigned_ports.make_key(*lp) if pid: kill_process(pid) del app.running_processes[_key] os.unlink(os.path.join(self.entpath, *qp)) # Remove issuer if out of tags if not os.listdir(os.path.join(self.entpath, qp[0])): os.rmdir(os.path.join(self.entpath, qp[0])) del app.assigned_ports[_key] resp = Response(mako_template='message.mako', template_lookup=self.lookup, headers=[]) args = { 'title': "Action performed", 'base': self.baseurl, 'note': 'Your test tool instance <em>{} {}</em> has been ' 'removed'.format(*lp) } return resp(self.environ, self.start_response, **args)
def get_iss_and_tag(path): p = path.split('/') if len(p) == 1: return unquote_plus(p[0]), '' try: iss = p[-2] except IndexError: iss = '' try: tag = p[-1] except IndexError: tag = '' return unquote_plus(iss), unquote_plus(tag)
def delete_instance(self, parts, pid=0, app=None): lp = [unquote_plus(p) for p in parts] qp = [quote_plus(p) for p in lp] _key = app.assigned_ports.make_key(*lp) if pid: kill_process(pid) del app.running_processes[_key] os.unlink(os.path.join(self.entpath, *qp)) # Remove issuer if out of tags if not os.listdir(os.path.join(self.entpath, qp[0])): os.rmdir(os.path.join(self.entpath, qp[0])) del app.assigned_ports[_key] resp = Response(mako_template='message.mako', template_lookup=self.lookup, headers=[]) args = {'title': "Action performed", 'base': self.baseurl, 'note': 'Your test tool instance <em>{} {}</em> has been ' 'removed'.format(*lp)} return resp(self.environ, self.start_response, **args)
def get_pid(self, parts): lp = [unquote_plus(p) for p in parts] qp = [quote_plus(p) for p in lp] try: return self.running_processes[self.key(*qp)] except KeyError: return 0
def get_pid(self, parts): lp = [unquote_plus(p) for p in parts] qp = [quote_plus(p) for p in lp] try: return self.running_processes[self.key(*qp)] except KeyError: return 0
def authenticated_as(self, cookie=None, authorization="", **kwargs): """ :param cookie: A HTTP Cookie :param authorization: The HTTP Authorization header :param args: extra args :param kwargs: extra key word arguments :return: """ if authorization.startswith("Basic"): authorization = authorization[6:] _decoded = as_unicode(base64.b64decode(authorization)) (user, pwd) = _decoded.split(":") user = unquote_plus(user) pwd = unquote_plus(pwd) self.verify_password(user, pwd) return {"uid": user}, time.time()
def authenticated_as(self, cookie=None, authorization="", **kwargs): """ :param cookie: A HTTP Cookie :param authorization: The HTTP Authorization header :param args: extra args :param kwargs: extra key word arguments :return: """ if authorization.startswith("Basic"): authorization = authorization[6:] _decoded = as_unicode(base64.b64decode(authorization)) (user, pwd) = _decoded.split(":") user = unquote_plus(user) pwd = unquote_plus(pwd) self.verify_password(user, pwd) return {"uid": user}, time.time()
def get_sms_from_dir(self): try: fetched_sms, hist = self.get_files_from_dir( self.signed_metadata_statements_dir, self.sms_mtime) except Exception as err: logger.error(err) else: for qiss, sms in fetched_sms.items(): self.signed_metadata_statements[unquote_plus(qiss)] = sms self.sms_mtime = hist
def update_instance(self, *parts): resp = Response(mako_template="instance.mako", template_lookup=self.lookup, headers=[]) lp = [unquote_plus(p) for p in parts] qp = [quote_plus(p) for p in lp] format, _conf = self.rest.read_conf(qp[0], qp[1]) # provider_info and registration_response dicts = {'tool': _conf['tool']} for item in tool_conf: if item not in dicts['tool']: dicts['tool'][item] = '' for typ in ['provider_info', 'registration_response']: try: dicts[typ] = _conf['client'][typ] except KeyError: try: dicts[typ] = update(typ, _conf[typ]) except KeyError: pass state = {'immutable': {}, 'required': {}} if 'registration_response' in dicts: state['registration_response'] = { 'immutable': ['redirect_uris'], 'required': ['client_id', 'client_secret'] } if 'provider_info': state['provider_info'] = { 'immutable': ['issuer'], 'required': [ 'authorization_endpoint', 'jwks_uri', 'response_types_supported', 'subject_types_supported', 'id_token_signing_alg_values_supported' ] } if return_type(_conf['tool']['profile']) not in ['I', 'IT']: state['provider_info']['required'].append('token_endpoint') arg = { 'base': '', 'iss': qp[0], 'tag': qp[1], 'dicts': dicts, 'state': state } return resp(self.environ, self.start_response, **arg)
def get_fo_keyjar_from_dir(self): try: fetched_jwks, _mtime = self.get_files_from_dir(self.fo_jwks_dir, self.jwks_mtime) except Exception as err: logger.error(err) else: _kj = KeyJar() for iss, jwks in fetched_jwks.items(): _kj.import_jwks(json.loads(jwks), unquote_plus(iss)) self.fo_keyjar = _kj self.jwks_mtime = _mtime
def list_tag(self, iss): resp = Response(mako_template="list_tag.mako", template_lookup=self.lookup, headers=[]) _iss = unquote_plus(iss) qiss = quote_plus(_iss) fils = os.listdir(os.path.join(self.entpath, qiss)) active = dict([(fil, isrunning(_iss, fil)) for fil in fils]) args = {'base': self.baseurl, 'items': fils, "qiss": qiss, "iss": _iss, 'active': active} return resp(self.environ, self.start_response, **args)
def restart_instance(self, app, part): _iss = unquote_plus(part[0]) _tag = unquote_plus(part[1]) url = app.run_test_instance(quote_plus(_iss), quote_plus(_tag)) if isinstance(url, Response): return url(self.environ, self.start_response) resp = Response(mako_template='message.mako', template_lookup=self.lookup, headers=[]) if url: args = { 'title': "Action performed", 'base': self.baseurl, 'note': 'Your test instance "{iss}:{tag}" has been ' 'restarted as <a href="{url}">{url}</a>'.format( iss=_iss, tag=_tag, url=url)} else: args = { 'title': "Action Failed", 'base': self.baseurl, 'note': 'Could not restart your test instance'} return resp(self.environ, self.start_response, **args)
def run_test_instance(self, iss, tag): _port = self.assigned_ports.register_port(iss, tag) args = [ self.test_script, "-i", unquote_plus(iss), "-t", unquote_plus(tag), "-p", str(_port), "-M", self.mako_dir, "-f", self.flowdir ] if self.path2port: args.extend(["-m", self.path2port]) ppmap = read_path2port_map(self.path2port) try: _path = ppmap[str(_port)] except KeyError: _errtxt = 'Port not in path2port map file {}'.format( self.path2port) logger.error(_errtxt) return ServiceError(_errtxt) url = '{}{}'.format(self.test_tool_base, _path) else: url = '{}:{}'.format(self.test_tool_base[:-1], _port) typ, _econf = self.rest.read_conf(iss, tag) if _econf['tool']['insecure']: args.append('-k') args.append(self.test_tool_conf) # If already running - kill try: pid = isrunning(unquote_plus(iss), unquote_plus(tag)) except KeyError: pass else: if pid: logger.info('kill {}'.format(pid)) subprocess.call(['kill', str(pid)]) # Now get it running args.append('&') logger.info("Test tool command: {}".format(" ".join(args))) # spawn independent process os.system(" ".join(args)) pid = 0 for i in range(0, 10): time.sleep(1) pid = isrunning(unquote_plus(iss), unquote_plus(tag)) if pid: break if pid: logger.info("process id: {}".format(pid)) self.running_processes[self.key(iss, tag)] = pid return url else: return None
def update_instance(self, *parts): resp = Response(mako_template="instance.mako", template_lookup=self.lookup, headers=[]) lp = [unquote_plus(p) for p in parts] qp = [quote_plus(p) for p in lp] format, _conf = self.rest.read_conf(qp[0], qp[1]) # provider_info and registration_response dicts = {'tool': _conf['tool']} for item in tool_conf: if item not in dicts['tool']: dicts['tool'][item] = '' for typ in ['provider_info', 'registration_response']: try: dicts[typ] = _conf['client'][typ] except KeyError: try: dicts[typ] = update(typ, _conf[typ]) except KeyError: pass state = {'immutable': {}, 'required': {}} if 'registration_response' in dicts: state['registration_response'] = { 'immutable': ['redirect_uris'], 'required': ['client_id', 'client_secret']} if 'provider_info': state['provider_info'] = { 'immutable': ['issuer'], 'required': ['authorization_endpoint', 'jwks_uri', 'response_types_supported', 'subject_types_supported', 'id_token_signing_alg_values_supported'] } if return_type(_conf['tool']['profile']) not in ['I', 'IT']: state['provider_info']['required'].append('token_endpoint') arg = {'base': '', 'iss': qp[0], 'tag': qp[1], 'dicts': dicts, 'state': state} return resp(self.environ, self.start_response, **arg)
def show_tag(self, part): resp = Response(mako_template="action.mako", template_lookup=self.lookup, headers=[]) lp = [unquote_plus(p) for p in part[1:]] if find_test_instance(*lp): active = True else: active = False qp = [quote_plus(p) for p in lp] info = open(os.path.join(self.entpath, *qp), 'r').read() args = {'base': self.baseurl, 'info': json.loads(info), "qargs": qp, "largs": lp, 'active': active} return resp(self.environ, self.start_response, **args)
def list_tag(self, iss): resp = Response(mako_template="list_tag.mako", template_lookup=self.lookup, headers=[]) _iss = unquote_plus(iss) qiss = quote_plus(_iss) fils = os.listdir(os.path.join(self.entpath, qiss)) active = dict([(fil, isrunning(_iss, fil)) for fil in fils]) args = { 'base': self.baseurl, 'items': fils, "qiss": qiss, "iss": _iss, 'active': active } return resp(self.environ, self.start_response, **args)
def show_tag(self, part): resp = Response(mako_template="action.mako", template_lookup=self.lookup, headers=[]) lp = [unquote_plus(p) for p in part[1:]] if find_test_instance(*lp): active = True else: active = False qp = [quote_plus(p) for p in lp] info = open(os.path.join(self.entpath, *qp), 'r').read() args = { 'base': self.baseurl, 'info': json.loads(info), "qargs": qp, "largs": lp, 'active': active } return resp(self.environ, self.start_response, **args)
def list_dir(self, dirname, qiss): if not os.path.isdir(dirname): if qiss.endswith('%2F'): # try to remove qiss = qiss[:-3] else: # else add qiss += '%2F' dirname = self.entity_file_name(qiss, '') if not os.path.isdir(dirname): raise NoSuchFile(dirname) iss = unquote_plus(qiss) res = ['<p>'] for file in os.listdir(dirname): _url = '{}{}/{}'.format(self.base_url, qiss, quote_plus(file)) res.append('<a href="{}">{}</a><br>'.format(_url, file)) res.append('</p') _html = [ '<html><head>List of tags for "{}"</head>'.format(iss), '<body>' ] _html.extend(res) _html.append('</body></html>') return 'html', '\n'.join(_html)
def list_dir(self, dirname, qiss): if not os.path.isdir(dirname): if qiss.endswith('%2F'): # try to remove qiss = qiss[:-3] else: # else add qiss += '%2F' dirname = self.entity_file_name(qiss, '') if not os.path.isdir(dirname): raise NoSuchFile(dirname) iss = unquote_plus(qiss) res = ['<p>'] for file in os.listdir(dirname): _url = '{}{}/{}'.format(self.base_url, qiss, quote_plus(file)) res.append('<a href="{}">{}</a><br>'.format(_url, file)) res.append('</p') _html = [ '<html><head>List of tags for "{}"</head>'.format(iss), '<body>' ] _html.extend(res) _html.append('</body></html>') return 'html', '\n'.join(_html)
def urlunquote_plus(quoted_url): """ A legacy compatibility wrapper to Python's urllib.parse.unquote_plus() function. (was used for unicode handling on Python 2) """ return unquote_plus(quoted_url)
def parse_kodi_url(self, url): params = dict(parse_qsl(url)) params.pop('addon_version') for item in params.keys(): setattr(self, item, unquote_plus(params[item]))
def run_test_instance(self, iss, tag): _port = self.assigned_ports.register_port(iss, tag) args = [self.test_script] args.extend(["-i", shlex.quote(unquote_plus(iss))]) args.extend(["-t", shlex.quote(unquote_plus(tag))]) args.extend(["-p", str(_port)]) args.extend(["-M", self.mako_dir]) args.extend(["-f", self.flowdir]) if self.path2port: args.extend(["-m", self.path2port]) ppmap = read_path2port_map(self.path2port) try: _path = ppmap[str(_port)] except KeyError: _errtxt = 'Port not in path2port map file {}'.format( self.path2port) logger.error(_errtxt) return ServiceError(_errtxt) url = '{}{}'.format(self.test_tool_base, _path) else: url = '{}:{}'.format(self.test_tool_base[:-1], _port) typ, _econf = self.rest.read_conf(iss, tag) if _econf['tool']['insecure']: args.append('-k') args.append(self.test_tool_conf) # If already running - kill try: pid = isrunning(unquote_plus(iss), unquote_plus(tag)) except KeyError: pass else: if pid: logger.info('kill {}'.format(pid)) subprocess.call(['kill', str(pid)]) # Now get it running args.append('&') cmd = " ".join(args) logger.info("Test tool command: {}".format(cmd)) # spawn independent process os.system(cmd) pid = 0 for i in range(0,10): time.sleep(1) pid = isrunning(unquote_plus(iss), unquote_plus(tag)) if pid: break if pid: logger.info("process id: {}".format(pid)) self.running_processes[self.key(iss, tag)] = pid return url else: return None