aws-samples / sample-python-helper-aws-appconfig

A Python helper library for AWS AppConfig
Apache License 2.0
31 stars 9 forks source link

Start a new session if token expired #5

Closed ljmc-github closed 2 years ago

ljmc-github commented 2 years ago

The helper as it is right now is not able to recover from expired token (>24h old).

I don't think there is a way to refresh token other than starting a new session and using this initial token.

I would like opinions about possible recovery/prevention methods:

First thought was a recovery method by adding a try/except block around update_config's get_latest_configuration() call to catch invalid tokens and start a fresh session.

        try: 
            response = self._client.get_latest_configuration(
                ConfigurationToken=self._next_config_token
            )
        except BadRequestException:
            self.start_session()  # get a fresh session, might need to close old one not sure
            response = self._client.get_latest_configuration(
                ConfigurationToken=self._next_config_token
            )

Second option would be preventive by adding a time check similar to the if time since update < polling time, but for time since update over 24h.

        if (
            self._next_config_token is None
            or time.time() - self._last_update_time >= 86400
        ):
            self.start_session()

I know this is a rare case, but it happened to me in testing where the service was deployed and testers took over 24h to get to testing, which caused the service to be DOA according to them.

jamesoff commented 2 years ago

My preference would be the former, it feels more Pythonic (Ask Forgiveness Not Permission) and would also cover any case where the token was invalid for any other reason.

You're right that there's no way to refresh an expired token, so the only option is to create a new session.

ljmc-github commented 2 years ago

I agree on both AFNP and broarder recovery, I'll send a PR your way ASAP.