def fetch_profile_attribute(config, profile, query): profile_attributes = dict(config.items(profile)) is_active = utils.is_profile_active(config, profile) profile_attributes['status'] = 'active' if is_active else 'expired' attribute_value = profile_attributes.get(query) if attribute_value is None: click.secho("Invalid value {!r} for 'query' parameter. Valid choices:".format(query), fg='red') click.secho(", ".join(profile_attributes.keys()), fg='red') sys.exit(1) else: click.secho(attribute_value)
def print_profile(config, profile): click.secho('[{}]'.format(profile), fg='green') for k, v in config.items(profile): click.secho('{}='.format(k), fg='blue', nl=False) if k == 'aws_credentials_expiry': v = '{} ({})'.format(v, str(utils.from_epoch(v))) click.secho(v, fg='green') click.secho('status=', fg='blue', nl=False) if utils.is_profile_active(config, profile): click.secho('active', fg='green') else: click.secho('expired', fg='red')
def fetch_profiles_from_config(config): profiles = config.sections() expiry = [] statuses = [] accounts = [] for profile in profiles: account = config.get(profile, 'account', fallback='None') accounts.append(account) profile_expiry = config.get(profile, 'aws_credentials_expiry', fallback=None) profile_expiry_string = str(utils.from_epoch(profile_expiry)) if profile_expiry else 'No Expiry Set' expiry.append(profile_expiry_string) is_active = utils.is_profile_active(config, profile) statuses.append('Active' if is_active else 'Expired') return [accounts, profiles, expiry, statuses]
def credentials_expired(self): return not utils.is_profile_active(self.config, self.profile)
def test_profile_has_is_not_expired(self): future = utils.to_epoch(datetime.utcnow() + timedelta(1)) self.config.set(self.account, 'aws_credentials_expiry', future) is_active = utils.is_profile_active(self.config, self.account) self.assertTrue(is_active)
def test_profile_has_is_expired(self): past = utils.to_epoch(datetime.utcnow() + timedelta(-1)) self.config.set(self.account, 'aws_credentials_expiry', past) is_active = utils.is_profile_active(self.config, self.account) self.assertFalse(is_active)
def test_profile_has_no_expiry(self): self.config.remove_option(self.account, 'aws_credentials_expiry') is_active = utils.is_profile_active(self.config, self.account) self.assertTrue(is_active)
def test_no_profile_in_config(self): is_active = utils.is_profile_active(self.config, 'does_not_exist') self.assertFalse(is_active)