Exemplo n.º 1
0
def test_get_account_id(monkeypatch):
    boto3 = MagicMock()
    boto3.get_user.return_value = {
        'User': {
            'Arn': 'arn:aws:iam::0123456789:user/admin'
        }
    }
    monkeypatch.setattr('boto3.client', MagicMock(return_value=boto3))

    assert '0123456789' == get_account_id()

    boto3 = MagicMock()
    boto3.get_user.side_effect = Exception()
    boto3.list_roles.return_value = {
        'Roles': [{
            'Arn': 'arn:aws:iam::0123456789:role/role-test'
        }]
    }
    monkeypatch.setattr('boto3.client', MagicMock(return_value=boto3))

    assert '0123456789' == get_account_id()

    boto3 = MagicMock()
    boto3.get_user.side_effect = Exception()
    boto3.list_roles.return_value = {'Roles': []}
    boto3.list_users.return_value = {
        'Users': [{
            'Arn': 'arn:aws:iam::0123456789:user/user-test'
        }]
    }
    monkeypatch.setattr('boto3.client', MagicMock(return_value=boto3))

    assert '0123456789' == get_account_id()

    boto3 = MagicMock()
    boto3.get_user.side_effect = Exception()
    boto3.list_roles.return_value = {'Roles': []}
    boto3.list_users.return_value = {'Users': []}
    boto3.list_saml_providers.return_value = {
        'SAMLProviderList': [{
            'Arn':
            'arn:aws:iam::0123456789:saml-provider/saml-test'
        }]
    }
    monkeypatch.setattr('boto3.client', MagicMock(return_value=boto3))

    assert '0123456789' == get_account_id()

    boto3 = MagicMock()
    boto3.get_user.side_effect = Exception()
    boto3.list_roles.return_value = {'Roles': []}
    boto3.list_users.return_value = {'Users': []}
    boto3.list_saml_providers.return_value = {'SAMLProviderList': []}
    monkeypatch.setattr('boto3.client', MagicMock(return_value=boto3))

    assert get_account_id() is None
Exemplo n.º 2
0
 def AccountID(self) -> str:
     """
     Returns the (non-human friendly) account id
     """
     if self.__AccountID is None:
         self.__AccountID = get_account_id()
     return self.__AccountID
Exemplo n.º 3
0
def test_get_account_id(monkeypatch):
    boto3 = MagicMock()
    boto3.get_user.return_value = {
        'User': {'Arn': 'arn:aws:iam::0123456789:user/admin'}}
    monkeypatch.setattr('boto3.client', MagicMock(return_value=boto3))

    assert '0123456789' == get_account_id()

    boto3 = MagicMock()
    boto3.get_user.side_effect = Exception()
    boto3.list_roles.return_value = {
        'Roles': [{'Arn': 'arn:aws:iam::0123456789:role/role-test'}]}
    monkeypatch.setattr('boto3.client', MagicMock(return_value=boto3))

    assert '0123456789' == get_account_id()

    boto3 = MagicMock()
    boto3.get_user.side_effect = Exception()
    boto3.list_roles.return_value = {'Roles': []}
    boto3.list_users.return_value = {
        'Users': [{'Arn': 'arn:aws:iam::0123456789:user/user-test'}]}
    monkeypatch.setattr('boto3.client', MagicMock(return_value=boto3))

    assert '0123456789' == get_account_id()

    boto3 = MagicMock()
    boto3.get_user.side_effect = Exception()
    boto3.list_roles.return_value = {'Roles': []}
    boto3.list_users.return_value = {'Users': []}
    boto3.list_saml_providers.return_value = {'SAMLProviderList': [
        {'Arn': 'arn:aws:iam::0123456789:saml-provider/saml-test'}]}
    monkeypatch.setattr('boto3.client', MagicMock(return_value=boto3))

    assert '0123456789' == get_account_id()

    boto3 = MagicMock()
    boto3.get_user.side_effect = Exception()
    boto3.list_roles.return_value = {'Roles': []}
    boto3.list_users.return_value = {'Users': []}
    boto3.list_saml_providers.return_value = {'SAMLProviderList': []}
    monkeypatch.setattr('boto3.client', MagicMock(return_value=boto3))

    assert get_account_id() is None
Exemplo n.º 4
0
def test_get_account_id(monkeypatch):
    boto3 = MagicMock()
    boto3.get_user.return_value = {"User": {"Arn": "arn:aws:iam::0123456789:user/admin"}}
    monkeypatch.setattr("boto3.client", MagicMock(return_value=boto3))

    assert "0123456789" == get_account_id()

    boto3 = MagicMock()
    boto3.get_user.side_effect = Exception()
    boto3.list_roles.return_value = {"Roles": [{"Arn": "arn:aws:iam::0123456789:role/role-test"}]}
    monkeypatch.setattr("boto3.client", MagicMock(return_value=boto3))

    assert "0123456789" == get_account_id()

    boto3 = MagicMock()
    boto3.get_user.side_effect = Exception()
    boto3.list_roles.return_value = {"Roles": []}
    boto3.list_users.return_value = {"Users": [{"Arn": "arn:aws:iam::0123456789:user/user-test"}]}
    monkeypatch.setattr("boto3.client", MagicMock(return_value=boto3))

    assert "0123456789" == get_account_id()

    boto3 = MagicMock()
    boto3.get_user.side_effect = Exception()
    boto3.list_roles.return_value = {"Roles": []}
    boto3.list_users.return_value = {"Users": []}
    boto3.list_saml_providers.return_value = {
        "SAMLProviderList": [{"Arn": "arn:aws:iam::0123456789:saml-provider/saml-test"}]
    }
    monkeypatch.setattr("boto3.client", MagicMock(return_value=boto3))

    assert "0123456789" == get_account_id()

    boto3 = MagicMock()
    boto3.get_user.side_effect = Exception()
    boto3.list_roles.return_value = {"Roles": []}
    boto3.list_users.return_value = {"Users": []}
    boto3.list_saml_providers.return_value = {"SAMLProviderList": []}
    monkeypatch.setattr("boto3.client", MagicMock(return_value=boto3))

    assert get_account_id() is None
Exemplo n.º 5
0
def get_mint_bucket_name(region: str):
    account_id = get_account_id()
    account_alias = get_account_alias()
    s3 = boto3.resource('s3')
    parts = account_alias.split('-')
    prefix = parts[0]
    bucket_name = '{}-stups-mint-{}-{}'.format(prefix, account_id, region)
    bucket = s3.Bucket(bucket_name)
    try:
        bucket.load()
        return bucket.name
    except:
        bucket = None
    for bucket in s3.buckets.all():
        if bucket.name.startswith('{}-stups-mint-{}-'.format(prefix, account_id)):
            return bucket.name
    return bucket_name