Esempio n. 1
0
    def test_io(self):
        config = {
            'default_env':
            'a',
            'env_configs': [
                {
                    'env': 'b',
                    'url': 'https://b',
                    'workspace': 'b-b-b',
                    'token': 'bbb'
                },
                {
                    'env': 'a',
                    'url': 'https://a',
                    'workspace': 'a-a-a',
                    'token': 'aaa'
                },
            ]
        }
        with tempfile.NamedTemporaryFile('w+') as f:
            json.dump(config, f, indent=4)
            f.seek(0)

            config_manager = ConfigManager(f.name)
            a_config = config_manager.get_config()
            a_config.set_token('ccc')

            config_manager.to_file(f.name)
            written_config = json.load(f)
            self.assertDictEqual(config_manager.raw_config, written_config)
Esempio n. 2
0
        def wrapper(*args, **kwargs):
            # Attempt to execute the initial request.
            response_code, response_body = func(*args, **kwargs)
            # Responses that don't result in 401 (unauthorized) should pass through.
            if response_code != 401:
                return response_code, response_body

            cm = ConfigManager()
            default_env = cm.get_default_env()
            config = cm.get_config(environment=default_env)

            # A 401 response not caused by an expired token, and the access token is not None (i.e.
            # at some point, the caller received an access token and saved it in their config)
            # should just return the response. This means there's an issue with the user's account
            # and they should reach out to B612 team for help.
            if not AccessTokenRefresher.is_expired_access_token(
                    response_body) and config.get('access_token') is not None:
                return response_code, response_body

            # Otherwise, received a 401 due to an expired token or if we have an empty access token,
            # so request a token refresh.
            refresh_token_url = f"{config.get('url')}/users/{config.get('user_id', '-')}/idToken"
            request_body = {'refreshToken': config.get('refresh_token')}
            response = requests.post(refresh_token_url, json=request_body)
            response.raise_for_status()
            refresh_response_body = response.json()

            # Update the access and refresh token in the ADAM config, then write out the file.
            config['access_token'] = yaml.safe_load(
                refresh_response_body.get('idToken'))
            config['refresh_token'] = yaml.safe_load(
                refresh_response_body.get('refreshToken'))
            cm.set_config(default_env, config)
            cm.to_file()

            # The original function should reload the ADAM configuration to pick up the new
            # access_token.
            kwargs['force_reload_config'] = True

            # Then re-execute the method again.
            return func(*args, **kwargs)
Esempio n. 3
0
    def test_io(self):
        config = {
            'default_env': 'a',
            'envs': {
                'b': {
                    'url': 'https://b',
                    'workspace': 'b-b-b',
                    'token': 'bbb'
                },
                'a': {
                    'url': 'https://a',
                    'workspace': 'a-a-a',
                    'token': 'aaa'
                },
            }
        }
        with tempfile.TemporaryDirectory() as tmp:
            fn = os.path.join(tmp, "config1")
            with open(fn, 'w') as fp:
                yaml.dump(config, fp)

            # load, make a change, and write back
            config_manager = ConfigManager(fn)
            a_config = config_manager.get_config()
            a_config['token'] = 'ccc'
            config_manager.to_file()
            print(config_manager)

            # re-read and verify the change has been recorded
            written_config = yaml.safe_load(open(fn))
            print(type(written_config))
            import pprint
            pp = pprint.PrettyPrinter(indent=2)
            pp.pprint(config_manager._config)
            pp.pprint(written_config)
            self.assertDictEqual(config_manager._config, written_config)