def token_url(self): """ Find token url if not defined """ if self.__token_url is None: url = self.BASE_API_URL.format(registry=self) + "/v2/" self.logger.debug("REQUEST_BASE_API_URL_FOR_TOKEN_ENDPOINT:URL:{}".format(url)) response = requests.get(url, verify=self.verify) header = None if "www-authenticate" in response.headers: header = response.headers["www-authenticate"] if "WWW-Authenticate" in response.headers: header = response.headers["WWW-Authenticate"] if header == None: self.logger.error("REQUEST_TOKEN:HTTPCODEERROR:{}".format(response.status_code)) raise RegistryAccessError("Error access to : {} \nCode Error : {}".format(url, response.status_code)) matcher = re.match(self.TOKEN_REGEX, header) if matcher is not None: self.__token_url = "{}?client_id=paclair&service={}&scope=repository:{}:pull".format( matcher.group("realm"), matcher.group("service"), "{image.name}") self.logger.debug("TOKEN_URL:{}".format(self.__token_url)) else: matcher = re.match(self.BASIC_REGEX, header) if matcher is not None: self.__token_url = self.USE_BASIC_AUTH self.logger.debug("TOKEN_URL:{}".format(self.__token_url)) else: raise RegistryAccessError("Can't find token url") return self.__token_url
def get_manifest(self, docker_image): """ Get manifest :param docker_image: paclair.docker.DockerImage :return: manifest """ # Define url url = self.get_manifest_url(docker_image) self.logger.debug("REQUESTMANIFESTS:{url}".format(url=url)) # Get token token = self.get_token(docker_image) if token is not "": header={"Authorization": "Bearer {}".format(token)} else: header={} resp = requests.get(url, verify=self.verify, headers=header, auth=self.auth) if not resp.ok: self.logger.error("MANIFESTS:HTTPCODEERROR:{}".format(resp.status_code)) raise RegistryAccessError("Error access to : {url} \nCode Error : {status_code}".format( url=url, status_code=resp.status_code)) else: return resp.json()
def get_authorization(self, docker_image): """ Get token :param docker_image: paclair.docker.DockerImage :returns str: """ if self.__token is None: # Define url url = self.token_url.format(registry=self, image=docker_image) self.logger.debug("REQUEST_TOKEN:URL:{url}".format(url=url)) # Get token resp = requests.get(url, verify=self.verify, auth=self.auth) if not resp.ok: self.logger.error("REQUEST_TOKEN:HTTPCODEERROR:{}".format( resp.status_code)) raise RegistryAccessError( "Error access to : {url} \nCode Error : {status_code}". format(url=url, status_code=resp.status_code)) token = resp.json()['token'] self.logger.debug("TOKEN:{token}".format(token=token)) else: token = self.__token return "{token_type} {token}".format(token_type=self.__token_type, token=token)
def get_token(self, docker_image): """ Get token :param docker_image: paclair.docker.DockerImage :returns str: """ # Define url url = self.token_url.format(registry=self, image=docker_image) if url == self.USE_BASIC_AUTH: self.logger.debug("GET_TOKEN:URL:{url}".format(url=self.__token_url)) return "" # Get token resp = requests.get(url, verify=self.verify, auth=self.auth) if not resp.ok: self.logger.error("REQUEST_TOKEN:HTTPCODEERROR:{}".format(resp.status_code)) raise RegistryAccessError("Error access to : {url} \nCode Error : {status_code}".format( url=url, status_code=resp.status_code)) token = resp.json()['token'] self.logger.debug("TOKEN:{token}".format(token=token)) return token