예제 #1
0
def _get_bakery_client(auth_info=None):
    """Return an httpbakery.Client."""
    interaction_methods = None
    if auth_info is not None:
        interaction_methods = [AgentInteractor(auth_info)]
    else:
        username, password = None, None
        credentials = os.environ.get('MAAS_CANDID_CREDENTIALS')
        if credentials:
            user_pass = credentials.split(':', 1)
            if len(user_pass) == 2:
                username, password = user_pass
        if username and password:

            def login_with_credentials(visit_url):
                """Login with Candid's builtin username/password form.

                This is only intended for use in automated testing.

                """
                resp = requests.get(visit_url)
                assert resp.status_code == 200
                requests.post(resp.url,
                              data={
                                  'username': username,
                                  'password': password
                              })

            interaction_methods = [
                httpbakery.WebBrowserInteractor(open=login_with_credentials)
            ]

    return httpbakery.Client(interaction_methods=interaction_methods)
예제 #2
0
def _get_bakery_client(auth_info=None):
    """Return an httpbakery.Client."""
    if auth_info is not None:
        interactor = AgentInteractor(auth_info)
    else:
        interactor = httpbakery.WebBrowserInteractor(
            open=_candid_login(os.environ.get("MAAS_CANDID_CREDENTIALS")))
    return httpbakery.Client(interaction_methods=[interactor])
예제 #3
0
    def _load_credentials(self):
        """Load credentials and set up internal auth request objects."""
        wbi = httpbakery.WebBrowserInteractor(open=visit_page_with_browser)
        self._cookiejar = MozillaCookieJar(self._cookiejar_filepath)
        self._client = httpbakery.Client(cookies=self._cookiejar, interaction_methods=[wbi])

        if os.path.exists(self._cookiejar_filepath):
            logger.debug("Loading credentials from file: %r", str(self._cookiejar_filepath))
            try:
                self._cookiejar.load()
            except Exception as err:
                # alert and continue processing (without having credentials, of course, the user
                # will be asked to authenticate)
                logger.warning("Failed to read credentials: %r", err)
        else:
            logger.debug("Credentials file not found: %r", str(self._cookiejar_filepath))

        # iterates the cookiejar (which is mutable, may change later) and get the cookies
        # for comparison after hitting the endpoint
        self._old_cookies = list(self._cookiejar)