def setupWorkerBuildirProperty(self, workerforbuilder): path_module = workerforbuilder.worker.path_module # navigate our way back to the L{buildbot.worker.Worker} # object that came from the config, and get its properties if workerforbuilder.worker.worker_basedir: builddir = path_module.join( bytes2NativeString(workerforbuilder.worker.worker_basedir), bytes2NativeString(self.builder.config.workerbuilddir)) self.setProperty("builddir", builddir, "Worker")
def decode(x): if isinstance(x, bytes): return bytes2NativeString(x) elif isinstance(x, (list, tuple)): return [bytes2NativeString(y) for y in x] elif isinstance(x, dict): newArgs = {} for a, b in iteritems(x): newArgs[decode(a)] = decode(b) return newArgs return x
def test_bytes2NativeString(self): rv = util.bytes2NativeString(b'abcd') self.assertEqual((rv, type(rv)), ('abcd', str)) rv = util.bytes2NativeString('efgh') self.assertEqual((rv, type(rv)), ('efgh', str)) if PY3: self.assertNotEqual(type('abcd'), type(b'abcd')) self.assertNotEqual(str, bytes) else: self.assertEqual(type('abcd'), type(b'abcd')) self.assertEqual(str, bytes)
def test_getLogLines_bug3101(self): # regression test for #3101 content = self.bug3101Content yield self.insertTestData(self.backgroundData + self.bug3101Rows) # overall content is the same, with '\n' padding at the end expected = bytes2NativeString(self.bug3101Content + b'\n') self.assertEqual((yield self.db.logs.getLogLines(1470, 0, 99)), expected) # try to fetch just one line expected = bytes2NativeString(content.split(b'\n')[0] + b'\n') self.assertEqual((yield self.db.logs.getLogLines(1470, 0, 0)), expected)
def decode(x): if isinstance(x, bytes): return bytes2NativeString(x) elif isinstance(x, (list, tuple)): return [bytes2NativeString(y) for y in x] elif isinstance(x, dict): newArgs = {} for a, b in iteritems(x): newArgs[decode(a)] = decode(b) return newArgs else: return x
def _get_payload(self, request): content = request.content.read() content = bytes2NativeString(content) content_type = request.getHeader(b'Content-Type') content_type = bytes2NativeString(content_type) if content_type.startswith('application/json'): payload = json.loads(content) else: raise ValueError('Unknown content type: {}'.format(content_type)) log.msg("Payload: {}".format(payload)) return payload
def makeConfiguration(self, request): config = {} config.update(self.default) for k, v in self.ep.config.items(): if k == 'color_scheme': config[k].update(v) else: config[k] = v for k, v in request.args.items(): k = bytes2NativeString(k) config[k] = escape(bytes2NativeString(v[0])) return config
def _get_payload(self, request): content = request.content.read() content = bytes2NativeString(content) content_type = request.getHeader(b'Content-Type') content_type = bytes2NativeString(content_type) if content_type.startswith('application/json'): payload = json.loads(content) else: raise ValueError('Unknown content type: {}' .format(content_type)) log.msg("Payload: {}".format(payload)) return payload
def _convert_nonzero_to_failure(res, command, args, path): "utility to handle the result of getProcessOutputAndValue" (stdout, stderr, code) = res stdout = bytes2NativeString(stdout, self.encoding) stderr = bytes2NativeString(stderr, self.encoding) args = bytes2NativeString(args, self.encoding) if code != 0: if code == 128: raise GitError( 'command %s %s in %s on repourl %s failed with exit code %d: %s' % (command, args, path, self.repourl, code, stderr)) raise EnvironmentError( 'command %s %s in %s on repourl %s failed with exit code %d: %s' % (command, args, path, self.repourl, code, stderr)) return stdout.strip()
def getChanges(self, request): """ Reponds only to POST events and starts the build process :arguments: request the http request object """ expected_secret = isinstance(self.options, dict) and self.options.get('secret') if expected_secret: received_secret = request.getHeader(_HEADER_GITLAB_TOKEN) received_secret = bytes2NativeString(received_secret) if received_secret != expected_secret: raise ValueError("Invalid secret") try: payload = json.load(request.content) except Exception as e: raise ValueError("Error loading JSON: " + str(e)) event_type = request.getHeader(_HEADER_EVENT) event_type = bytes2NativeString(event_type) # newer version of gitlab have a object_kind parameter, # which allows not to use the http header event_type = payload.get('object_kind', event_type) project = request.args.get('project', [''])[0] codebase = request.args.get('codebase', [None])[0] if event_type in ("push", "tag_push", "Push Hook"): user = payload['user_name'] repo = payload['repository']['name'] repo_url = payload['repository']['url'] changes = self._process_change(payload, user, repo, repo_url, project, event_type, codebase=codebase) elif event_type == 'merge_request': changes = self._process_merge_request_change(payload, project, event_type, codebase=codebase) else: changes = [] if changes: log.msg("Received {} changes from {} gitlab event".format( len(changes), event_type)) return (changes, 'git')
def _convert_nonzero_to_failure(res, command, args, path): "utility to handle the result of getProcessOutputAndValue" (stdout, stderr, code) = res stdout = bytes2NativeString(stdout, self.encoding) stderr = bytes2NativeString(stderr, self.encoding) args = bytes2NativeString(args, self.encoding) if code != 0: if code == 128: raise GitError('command %s %s in %s on repourl %s failed with exit code %d: %s' % (command, args, path, self.repourl, code, stderr)) raise EnvironmentError('command %s %s in %s on repourl %s failed with exit code %d: %s' % (command, args, path, self.repourl, code, stderr)) return stdout.strip()
def render_control_resource(self, rsrc, path=b'/', params=None, requestJson=None, action="notfound", id=None, content_type=b'application/json'): # pass *either* a request or postpath if params is None: params = {} id = id or self.UUID request = self.make_request(path) request.method = b"POST" request.content = NativeStringIO(requestJson or json.dumps({ "jsonrpc": "2.0", "method": action, "params": params, "id": id })) request.input_headers = {b'content-type': content_type} rv = rsrc.render(request) if rv == server.NOT_DONE_YET: rv = yield request.deferred res = json.loads(bytes2NativeString(rv)) self.assertIn("jsonrpc", res) self.assertEqual(res["jsonrpc"], "2.0") if not requestJson: # requestJson is used for invalid requests, so don't expect ID self.assertIn("id", res) self.assertEqual(res["id"], id)
def getEndpoint(self, request, method, params): # note that trailing slashes are not allowed request_postpath = tuple(bytes2NativeString(p) for p in request.postpath) yield self.master.www.assertUserAllowed(request, request_postpath, method, params) ret = yield self.master.data.getEndpoint(request_postpath) defer.returnValue(ret)
def assertRestCollection(self, typeName, items, total=None, contentType=None, orderSignificant=False): self.assertFalse(isinstance(self.request.written, text_type)) got = {} got['content'] = json.loads(bytes2NativeString(self.request.written)) got['contentType'] = self.request.headers[b'content-type'] got['responseCode'] = self.request.responseCode meta = {} if total is not None: meta['total'] = total exp = {} exp['content'] = {typeName: items, 'meta': meta} exp['contentType'] = [contentType or b'text/plain; charset=utf-8'] exp['responseCode'] = 200 # if order is not significant, sort so the comparison works if not orderSignificant: if 'content' in got and typeName in got['content']: got['content'][typeName].sort(key=lambda x: sorted(x.items())) exp['content'][typeName].sort(key=lambda x: sorted(x.items())) if 'meta' in got['content'] and 'links' in got['content']['meta']: got['content']['meta']['links'].sort( key=lambda l: (l['rel'], l['href'])) self.assertEqual(got, exp)
def render_GET(self, request): def decode(x): if isinstance(x, bytes): return bytes2NativeString(x) elif isinstance(x, (list, tuple)): return [bytes2NativeString(y) for y in x] elif isinstance(x, dict): newArgs = {} for a, b in iteritems(x): newArgs[decode(a)] = decode(b) return newArgs else: return x args = decode(request.args) content_type = request.getHeader(b'content-type') if content_type == b"application/json": jsonBytes = request.content.read() jsonStr = bytes2NativeString(jsonBytes) args['json_received'] = json.loads(jsonStr) data = json.dumps(args) data = unicode2bytes(data) request.setHeader(b'content-type', b'application/json') request.setHeader(b'content-length', intToBytes(len(data))) if request.method == b'HEAD': return b'' return data
def onMessage(self, frame, isBinary): if self.debug: log.msg("FRAME %s" % frame) # parse the incoming request frame = json.loads(bytes2NativeString(frame)) _id = frame.get("_id") if _id is None: return self.sendJsonMessage(error="no '_id' in websocket frame", code=400, _id=None) cmd = frame.pop("cmd", None) if cmd is None: return self.sendJsonMessage(error="no 'cmd' in websocket frame", code=400, _id=None) cmdmeth = "cmd_" + cmd meth = getattr(self, cmdmeth, None) if meth is None: return self.sendJsonMessage(error="no such command '%s'" % (cmd, ), code=404, _id=_id) try: return meth(**frame) except TypeError as e: return self.sendJsonMessage(error="Invalid method argument '%s'" % (str(e), ), code=400, _id=_id) except Exception as e: log.err("while calling command %s" % (cmd, )) return self.sendJsonMessage(error="Internal Error '%s'" % (str(e), ), code=500, _id=_id)
def getChanges(self, request): """ Take the logic from the change hook, and then delegate it to the proper handler We use the buildbot plugin mechanisms to find out about dialects and call getChanges() the return value is a list of changes if DIALECT is unspecified, a sample implementation is provided """ uriRE = re.search(r'^/change_hook/?([a-zA-Z0-9_]*)', bytes2NativeString(request.uri)) if not uriRE: log.msg("URI doesn't match change_hook regex: %s" % request.uri) raise ValueError( "URI doesn't match change_hook regex: %s" % request.uri) changes = [] src = None # Was there a dialect provided? if uriRE.group(1): dialect = uriRE.group(1) else: dialect = 'base' handler = self.makeHandler(dialect) changes, src = yield handler.getChanges(request) defer.returnValue((changes, src))
def builderAdded(self, name, basedir, tags=None, description=None): """ @rtype: L{BuilderStatus} """ builder_status = builder.BuilderStatus(name, tags, self.master, description) builder_status.setTags(tags) builder_status.description = description builder_status.master = self.master builder_status.basedir = os.path.join(bytes2NativeString(self.basedir), bytes2NativeString(basedir)) builder_status.name = name # it might have been updated builder_status.status = self builder_status.setBigState("offline") return builder_status
def addHTMLLog(self, name, html): logid = yield self.master.data.updates.addLog(self.stepid, util.ascii2unicode(name), u'h') l = self._newLog(name, u'h', logid) html = bytes2NativeString(html) yield l.addContent(html) yield l.finish()
def testBasicWorker2_16(self): s = transfer.JSONPropertiesDownload("props.json") s.build = Mock() props = Properties() props.setProperty('key1', 'value1', 'test') s.build.getProperties.return_value = props s.build.getWorkerCommandVersion.return_value = '2.16' ss = Mock() ss.asDict.return_value = dict(revision="12345") s.build.getAllSourceStamps.return_value = [ss] s.worker = Mock() s.remote = Mock() s.start() for c in s.remote.method_calls: name, command, args = c commandName = command[3] kwargs = command[-1] if commandName == 'downloadFile': self.assertEqual(kwargs['slavedest'], 'props.json') reader = kwargs['reader'] data = reader.remote_read(100) data = bytes2NativeString(data) actualJson = json.loads(data) expectedJson = dict(sourcestamps=[ss.asDict()], properties={'key1': 'value1'}) self.assertEqual(actualJson, expectedJson) break else: raise ValueError("No downloadFile command found")
def assertRequest(self, content=None, contentJson=None, contentType=None, responseCode=None, contentDisposition=None, headers=None): if headers is None: headers = {} got, exp = {}, {} if content is not None: got['content'] = self.request.written exp['content'] = content if contentJson is not None: got['contentJson'] = json.loads( bytes2NativeString(self.request.written)) exp['contentJson'] = contentJson if contentType is not None: got['contentType'] = self.request.headers[b'content-type'] exp['contentType'] = [contentType] if responseCode is not None: got['responseCode'] = str(self.request.responseCode) exp['responseCode'] = str(responseCode) for header, value in iteritems(headers): got[header] = self.request.headers.get(header) exp[header] = value self.assertEqual(got, exp)
def run(self): l = yield self.addLog('xx') output_bytes = u'\N{CENT SIGN}'.encode('latin-1') output_str = bytes2NativeString(output_bytes, encoding='latin-1') yield l.addStdout(output_str) yield l.finish() defer.returnValue(results.SUCCESS)
def requestAvatar(self, username, mind, interface): assert interface == pb.IPerspective username = bytes2NativeString(username) if username not in self.users: d = defer.succeed(None) # no perspective else: _, afactory = self.users.get(username) d = defer.maybeDeferred(afactory, mind, username) # check that we got a perspective @d.addCallback def check(persp): if not persp: raise ValueError("no perspective for '%s'" % username) return persp # call the perspective's attached(mind) @d.addCallback def call_attached(persp): d = defer.maybeDeferred(persp.attached, mind) d.addCallback(lambda _: persp) # keep returning the perspective return d # return the tuple requestAvatar is expected to return @d.addCallback def done(persp): return (pb.IPerspective, persp, lambda: persp.detached(mind)) return d
def setupWorkerForBuilder(self, workerforbuilder): self.path_module = workerforbuilder.worker.path_module # navigate our way back to the L{buildbot.worker.Worker} # object that came from the config, and get its properties worker_properties = workerforbuilder.worker.properties self.getProperties().updateFromProperties(worker_properties) if workerforbuilder.worker.worker_basedir: builddir = self.path_module.join( bytes2NativeString(workerforbuilder.worker.worker_basedir), bytes2NativeString(self.builder.config.workerbuilddir)) self.setProperty("builddir", builddir, "Worker") self.workername = workerforbuilder.worker.workername self._registerOldWorkerAttr("workername") self.build_status.setWorkername(self.workername)
def getChanges(request, options=None): """ Reponds only to POST events and starts the build process :arguments: request the http request object """ expected_secret = isinstance(options, dict) and options.get('secret') if expected_secret: received_secret = request.getHeader(_HEADER_GITLAB_TOKEN) if received_secret != expected_secret: raise ValueError("Invalid secret") try: payload = json.load(request.content) except Exception as e: raise ValueError("Error loading JSON: " + str(e)) event_type = request.getHeader(_HEADER_EVENT) event_type = bytes2NativeString(event_type) user = payload['user_name'] repo = payload['repository']['name'] repo_url = payload['repository']['url'] project = request.args.get('project', [''])[0] codebase = request.args.get('codebase', None) if codebase: codebase = codebase[0] # This field is unused: # private = payload['repository']['private'] changes = _process_change( payload, user, repo, repo_url, project, event_type, codebase=codebase) log.msg("Received %s changes from gitlab" % len(changes)) return (changes, 'git')
def addHTMLLog(self, name, html): logid = yield self.master.data.updates.addLog(self.stepid, util.ascii2unicode(name), u'h') _log = self._newLog(name, u'h', logid) html = bytes2NativeString(html) yield _log.addContent(html) yield _log.finish()
def check_passwd(guess, passwd): """ Tests to see if the guess, after salting and hashing, matches the passwd from the database. @param guess: incoming password trying to be used for authentication @param passwd: already encrypted password from the database @returns: boolean """ m = sha1() salt = passwd[:salt_len * 2] # salt_len * 2 due to encode('hex_codec') m.update(unicode2bytes(guess) + unicode2bytes(salt)) crypted_guess = bytes2NativeString(salt) + m.hexdigest() return (crypted_guess == bytes2NativeString(passwd))
def getChanges(self, request): """ Take the logic from the change hook, and then delegate it to the proper handler We use the buildbot plugin mechanisms to find out about dialects and call getChanges() the return value is a list of changes if DIALECT is unspecified, a sample implementation is provided """ uriRE = re.search(r'^/change_hook/?([a-zA-Z0-9_]*)', bytes2NativeString(request.uri)) if not uriRE: log.msg("URI doesn't match change_hook regex: %s" % request.uri) raise ValueError("URI doesn't match change_hook regex: %s" % request.uri) changes = [] src = None # Was there a dialect provided? if uriRE.group(1): dialect = uriRE.group(1) else: dialect = 'base' handler = self.makeHandler(dialect) changes, src = yield handler.getChanges(request) defer.returnValue((changes, src))
def create_session_secret(): # Bootstrap: We need to create a key, that will be shared with other masters # and other runs of this master # we encode that in hex for db storage convenience return bytes2NativeString( hexlify(os.urandom(int(SESSION_SECRET_LENGTH / 8))))
def checkFields(fields, negOk=False): for field in fields: k = bytes2NativeString(field) if k[0] == '-' and negOk: k = k[1:] if k not in entityType.fieldNames: raise BadRequest("no such field '{}'".format(k))
def __init__(self, modulename, description): self.description = description self.version = pkg_resources.resource_string(modulename, "/VERSION").strip() self.version = bytes2NativeString(self.version) self.static_dir = pkg_resources.resource_filename( modulename, "/static") self.resource = static.File(self.static_dir)
def check(_json): res = json.loads(bytes2NativeString(_json)) self.assertIn("jsonrpc", res) self.assertEqual(res["jsonrpc"], "2.0") if not requestJson: # requestJson is used for invalid requests, so don't expect ID self.assertIn("id", res) self.assertEqual(res["id"], id)
def outReceived(self, data): vstr = b"gerrit version " if not data.startswith(vstr): log.msg(b"Error: Cannot interpret gerrit version info: " + data) return vers = data[len(vstr):].strip() log.msg(b"gerrit version: " + vers) self.gerrit_version = LooseVersion(bytes2NativeString(vers))
def verifyMessage(testcase, routingKey, message_): # the validator is a Selector wrapping a MessageValidator, so we need to # pass (arg, (routingKey, message)), where the routing key is the arg # the "type" of the message is identified by last path name # -1 being the event, and -2 the id. validator = message[bytes2NativeString(routingKey[-3])] _verify(testcase, validator, '', (routingKey, (routingKey, message_)))
def __init__(self, modulename, description): self.description = description self.version = pkg_resources.resource_string( modulename, "/VERSION").strip() self.version = bytes2NativeString(self.version) self.static_dir = pkg_resources.resource_filename( modulename, "/static") self.resource = static.File(self.static_dir)
def onMessage(self, event, data): request = self.request key = [bytes2NativeString(e) for e in event] msg = dict(key=key, message=data) request.write(b"event: " + b"event" + b"\n") request.write( b"data: " + unicode2bytes(json.dumps(msg, default=toJson)) + b"\n") request.write(b"\n")
def assertReceivesChangeNewMessage(self, request): self.master.mq.callConsumer( ("changes", "500", "new"), test_data_changes.Change.changeEvent) kw = self.readEvent(request) self.assertEqual(kw[b"event"], b"event") msg = json.loads(bytes2NativeString(kw[b"data"])) self.assertEqual(msg["key"], [u'changes', u'500', u'new']) self.assertEqual(msg["message"], json.loads( json.dumps(test_data_changes.Change.changeEvent, default=self._toJson)))
def writeError(msg, errcode=404, jsonrpccode=None): if self.debug: log.msg("REST error: %s" % (msg,)) request.setResponseCode(errcode) request.setHeader(b'content-type', b'text/plain; charset=utf-8') msg = bytes2NativeString(msg) data = json.dumps(dict(error=msg)) data = unicode2bytes(data) request.write(data)
def test_put_content_with_json(self): exp_content_json = dict(json_received=dict(a='b')) self.expect('post', '/', json=dict(a='b'), content_json=exp_content_json) res = yield self._http.post('/', json=dict(a='b')) content = yield res.content() content = bytes2NativeString(content) content = json.loads(content) self.assertEqual(content, exp_content_json)
def test_put_content_with_json_datetime(self): exp_content_json = dict(json_received=dict(a='b', ts=12)) dt = datetime.datetime.utcfromtimestamp(12) self.expect('post', '/', json=dict(a='b', ts=dt), content_json=exp_content_json) res = yield self._http.post('/', json=dict(a='b', ts=dt)) content = yield res.content() content = bytes2NativeString(content) content = json.loads(content) self.assertEqual(content, exp_content_json)
def assertRestError(self, responseCode, message): got = {} got['content'] = json.loads(bytes2NativeString(self.request.written)) got['responseCode'] = self.request.responseCode exp = {} exp['content'] = {'error': message} exp['responseCode'] = responseCode self.assertEqual(got, exp)
def _get_payload(self, request): content = request.content.read() content = bytes2NativeString(content) signature = request.getHeader(_HEADER_SIGNATURE) signature = bytes2NativeString(signature) if not signature and self._strict: raise ValueError('Request has no required signature') if self._secret and signature: try: hash_type, hexdigest = signature.split('=') except ValueError: raise ValueError( 'Wrong signature format: {}'.format(signature)) if hash_type != 'sha1': raise ValueError('Unknown hash type: {}'.format(hash_type)) mac = hmac.new(unicode2bytes(self._secret), msg=unicode2bytes(content), digestmod=sha1) # NOTE: hmac.compare_digest should be used, but it's only available # starting Python 2.7.7 if mac.hexdigest() != hexdigest: raise ValueError('Hash mismatch') content_type = request.getHeader(_HEADER_CT) content_type = bytes2NativeString(content_type) if content_type == 'application/json': payload = json.loads(content) elif content_type == 'application/x-www-form-urlencoded': payload = json.loads(request.args['payload'][0]) else: raise ValueError('Unknown content type: {}'.format(content_type)) log.msg("Payload: {}".format(payload), logLevel=logging.DEBUG) return payload