def _username_password_login(self): resp = http.request( "GET", "{0}/auth".format(self.address), force_basic_auth=True, url_username=self.username, url_password=self.password, validate_certs=self.verify, ca_path=self.ca_path, ) if resp.status != 200: raise errors.SensuError( "Authentication call returned status {0}".format(resp.status), ) if resp.json is None: raise errors.SensuError( "Authentication call did not return a valid JSON", ) if "access_token" not in resp.json: raise errors.SensuError( "Authentication call did not return access token", ) return dict( Authorization="Bearer {0}".format(resp.json["access_token"]), )
def version(self): if self._version is None: resp = self.get("/version") if resp.status != 200: raise errors.SensuError( "Version API returned status {0}".format(resp.status), ) if resp.json is None: raise errors.SensuError( "Version API did not return a valid JSON", ) if "sensu_backend" not in resp.json: raise errors.SensuError( "Version API did not return backend version", ) self._version = version.StrictVersion( resp.json["sensu_backend"].split("#")[0] ) return self._version
def validate_auth_data(self, username, password): resp = http.request( "GET", "{0}/auth/test".format(self.address), force_basic_auth=True, url_username=username, url_password=password, validate_certs=self.verify, ca_path=self.ca_path, ) if resp.status not in (200, 401): raise errors.SensuError( "Authentication test returned status {0}".format(resp.status), ) return resp.status == 200
def _login(self): resp = http.request( "GET", "{0}/auth".format(self.address), force_basic_auth=True, url_username=self.username, url_password=self.password, ) if resp.status != 200: raise errors.SensuError( "Authentication call returned status {0}".format( resp.status), ) if resp.json is None: raise errors.SensuError( "Authentication call did not return a valid JSON", ) if "access_token" not in resp.json: raise errors.SensuError( "Authentication call did not return access token", ) return resp.json["access_token"]
def request(self, method, path, payload=None): url = self.address + path headers = self.auth_header response = http.request( method, url, payload=payload, headers=headers, validate_certs=self.verify, ca_path=self.ca_path, ) if response.status in (401, 403): raise errors.SensuError( "Authentication problem. Verify your credentials." ) return response
def _api_key_login(self): # We check the API key validity by using it to fetch its metadata from # the backend. This should also take care of validating if the backend # even supports the API key authentication. url = "{0}{1}".format(self.address, utils.build_core_v2_path( None, "apikeys", self.api_key, )) headers = dict(Authorization="Key {0}".format(self.api_key)) resp = http.request("GET", url, headers=headers) if resp.status != 200: raise errors.SensuError( "The API key {0}...{1} seems to be invalid or the backend " "does not support the API key authentication".format( self.api_key[:5], self.api_key[-5:], ) ) return headers