Ejemplo n.º 1
0
    def openshift_login(self):
        os_oauth = OAuth2Session(client_id='openshift-challenging-client')
        authorization_url, state = os_oauth.authorization_url(
            self.openshift_auth_endpoint,
            state="1",
            code_challenge_method='S256')
        auth_headers = make_headers(basic_auth='{0}:{1}'.format(
            self.auth_username, self.auth_password))

        # Request authorization code using basic auth credentials
        ret = os_oauth.get(authorization_url,
                           headers={
                               'X-Csrf-Token': state,
                               'authorization':
                               auth_headers.get('authorization')
                           },
                           verify=self.con_verify_ca,
                           allow_redirects=False)

        if ret.status_code != 302:
            self.fail_request("Authorization failed.",
                              method='GET',
                              url=authorization_url,
                              reason=ret.reason,
                              status_code=ret.status_code)

        # In here we have `code` and `state`, I think `code` is the important one
        qwargs = {}
        for k, v in parse_qs(urlparse(ret.headers['Location']).query).items():
            qwargs[k] = v[0]
        qwargs['grant_type'] = 'authorization_code'

        # Using authorization code given to us in the Location header of the previous request, request a token
        ret = os_oauth.post(
            self.openshift_token_endpoint,
            headers={
                'Accept': 'application/json',
                'Content-Type': 'application/x-www-form-urlencoded',
                # This is just base64 encoded 'openshift-challenging-client:'
                'Authorization':
                'Basic b3BlbnNoaWZ0LWNoYWxsZW5naW5nLWNsaWVudDo='
            },
            data=urlencode(qwargs),
            verify=self.con_verify_ca)

        if ret.status_code != 200:
            self.fail_request("Failed to obtain an authorization token.",
                              method='POST',
                              url=self.openshift_token_endpoint,
                              reason=ret.reason,
                              status_code=ret.status_code)

        return ret.json()['access_token']
def get__prot_policy__all(self):
    try:
        uri = "https://" + self['server'] + \
            "/irisservices/api/v1/public/protectionPolicies"

        if 'policyId' in self:
            uri = uri + "?" + urllib_parse.urlencode(
                {"names": self['policyId']})
        headers = {
            "Accept": "application/json",
            "Authorization": "Bearer " + self['token']
        }
        objects = open_url(url=uri,
                           headers=headers,
                           validate_certs=self['validate_certs'],
                           timeout=120)
        objects = json.loads(objects.read())
        return objects
    except urllib_error as error:
        raise HTTPException(error.read())
def get__storage_domain_id__all(self):
    try:
        uri = "https://" + self['server'] + \
            "/irisservices/api/v1/public/viewBoxes"
        if 'viewBoxId' in self:
            if 'type' not in self:
                self['type'] = 'name'
                if isinstance(self['viewBoxId'], int):
                    self['type'] = 'id'
            uri = uri + "?" + urllib_parse.urlencode(
                {self['type']: self['viewBoxId']})

        headers = {
            "Accept": "application/json",
            "Authorization": "Bearer " + self['token']
        }
        objects = open_url(url=uri,
                           headers=headers,
                           validate_certs=self['validate_certs'],
                           timeout=120)
        objects = json.loads(objects.read())
        return objects
    except urllib_error.URLError as error:
        raise HTTPException(error.read())