Ejemplo n.º 1
0
def test_time_manipulation():
    round_tripped_datetime = utils.datetime_to_iso(
        utils.from_unix_epoch_time_secs(
            utils.to_unix_epoch_time_secs(
                utils.iso_to_datetime("2014-12-10T19:09:34.000Z"))))
    assert "2014-12-10T19:09:34.000Z" == round_tripped_datetime

    round_tripped_datetime = utils.datetime_to_iso(
        utils.from_unix_epoch_time_secs(
            utils.to_unix_epoch_time_secs(
                utils.iso_to_datetime("1969-04-28T23:48:34.123Z"))))
    assert "1969-04-28T23:48:34.123Z" == round_tripped_datetime

    # check that rounding to milliseconds works
    round_tripped_datetime = utils.datetime_to_iso(
        utils.from_unix_epoch_time_secs(
            utils.to_unix_epoch_time_secs(
                utils.iso_to_datetime("1969-04-28T23:48:34.999499Z"))))
    assert "1969-04-28T23:48:34.999Z" == round_tripped_datetime

    # check that rounding to milliseconds works
    round_tripped_datetime = utils.datetime_to_iso(
        utils.from_unix_epoch_time_secs(
            utils.to_unix_epoch_time_secs(
                utils.iso_to_datetime("1969-04-27T23:59:59.999999Z"))))
    assert "1969-04-28T00:00:00.000Z" == round_tripped_datetime
Ejemplo n.º 2
0
    def get_token(self, syn, entity_id, permission,
                  min_remaining_life: datetime.timedelta):
        with self._lock:
            utcnow = datetime.datetime.utcnow()
            token_cache = self._tokens.get(permission)
            if token_cache is None:
                raise ValueError(f"Invalid STS permission {permission}")

            token = token_cache.get(entity_id)
            if not token or (iso_to_datetime(token['expiration']) -
                             utcnow) < min_remaining_life:
                # either there is no cached token or the remaining life on the token isn't enough so fetch new
                token = token_cache[entity_id] = self._fetch_token(
                    syn, entity_id, permission)

        return token
Ejemplo n.º 3
0
    def _prune(self):
        while len(self) > self.max_size:
            self.popitem(last=False)

        to_delete = []
        before_timestamp = datetime.datetime.utcnow().timestamp()
        for entity_id, token in self.items():
            expiration_iso_str = token['expiration']

            # our "iso_to_datetime" util naively assumes UTC ("Z") times which in practice STS tokens are
            if iso_to_datetime(
                    expiration_iso_str).timestamp() < before_timestamp:
                to_delete.append(entity_id)
            else:
                break

        for entity_id in to_delete:
            del self[entity_id]
Ejemplo n.º 4
0
def iso_time_to_epoch(iso_time):
    """
    Convert an ISO formatted time into seconds since unix epoch
    """
    return None if iso_time is None else utils.to_unix_epoch_time_secs(
        utils.iso_to_datetime(iso_time))