def _github_collector(self, version=None): session = requests.Session() cached_ctx = cache.get('workflow_context') if cached_ctx: return cached_ctx session.headers.update({'Accept': 'application/vnd.github.v3.raw'}) session.headers.update({'Authorization': 'token ' + str(self.token)}) response = session.get(self.url) if response.status_code >= 300: try: response_json = json.loads(str(response.text)) response_text = response_json['message'] except Exception: response_text = response.text msg = "Could not get remote file from Github:\nSTATUS CODE: %s\nRESPONSE:\n%s" % ( str(response.status_code), response_text) log.error(msg) ctx = "" else: ctx = response.text cache.set('workflow_context', ctx, 3600) return ctx
def _gerrit_get_versions(self): cache_key = 'workflow_versions_%s_%s' % (self.url, self.project_name) cached_versions = cache.get(cache_key) if cached_versions: return cached_versions tags_endpoint_url = '/projects/%s/tags/' % self.project_name master_endpoint_url = '/projects/%s/branches/master/' % self.project_name tags = self._gerrit_get(tags_endpoint_url) master = self._gerrit_get(master_endpoint_url) self.versions = {} for tag in tags: key = tag['ref'].replace('refs/tags/', '') self.versions[key] = tag['revision'] self.versions['master'] = master['revision'] cache.set(cache_key, self.versions, 3600) return self.versions
def _gerrit_collector(self, version=None): cache_key = 'workflow_context' endpoint_url = '/projects/%s/branches/master/files/%s/content' % ( self.project_name, self.file_name) if version: versions = self._gerrit_get_versions() if version in self.version_map.values(): version = [ v[0] for v in self.version_map.items() if v[1] == version ][0] revision = versions.get(version) cache_key = 'workflow_context_%s' % revision endpoint_url = '/projects/%s/commits/%s/files/%s/content' % ( self.project_name, revision, self.file_name) cached_ctx = cache.get(cache_key) if cached_ctx: return cached_ctx ctx = self._gerrit_get(endpoint_url) cache.set(cache_key, ctx, 3600) return ctx
def generate_ssh_keypair(seed=None): if not seed: private_key_str = "" public_key_str = "" else: private_key_cache = 'private_key_' + str(seed) public_key_cache = 'public_key_' + str(seed) cached_private_key = cache.get(private_key_cache) cached_public_key = cache.get(public_key_cache) if cached_private_key and cached_public_key: private_key_str = cached_private_key public_key_str = cached_public_key else: private_key_obj = rsa.generate_private_key( backend=default_backend(), public_exponent=65537, key_size=2048) public_key_obj = private_key_obj.public_key() public_key = public_key_obj.public_bytes( serialization.Encoding.OpenSSH, serialization.PublicFormat.OpenSSH) private_key = private_key_obj.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.TraditionalOpenSSL, encryption_algorithm=serialization.NoEncryption()) private_key_str = private_key.decode('utf-8') public_key_str = public_key.decode('utf-8') cache.set(private_key_cache, private_key_str, 3600) cache.set(public_key_cache, public_key_str, 3600) return (private_key_str, public_key_str)
def _http_collector(self, version=None): session = requests.Session() cached_ctx = cache.get('workflow_context') if cached_ctx: return cached_ctx if self.username and self.password: response = session.get(self.url, auth=(self.username, self.password)) else: response = session.get(self.url) if response.status_code >= 300: msg = "Could not get remote file from HTTP URL %s:\nSTATUS CODE: %s\nRESPONSE:\n%s" % ( self.url, str(response.status_code), response.text) log.error(msg) ctx = "" else: ctx = response.text cache.set('workflow_context', ctx, 3600) return ctx