Add iam.get_open_id_connect_provider

This commit is contained in:
gruebel 2019-10-18 20:37:35 +02:00
parent bd627b65f7
commit c492c5c228
4 changed files with 99 additions and 3 deletions

View File

@ -3227,7 +3227,7 @@
- [X] get_group_policy - [X] get_group_policy
- [X] get_instance_profile - [X] get_instance_profile
- [X] get_login_profile - [X] get_login_profile
- [ ] get_open_id_connect_provider - [X] get_open_id_connect_provider
- [ ] get_organizations_access_report - [ ] get_organizations_access_report
- [X] get_policy - [X] get_policy
- [X] get_policy_version - [X] get_policy_version

View File

@ -104,11 +104,16 @@ class OpenIDConnectProvider(BaseModel):
self.url = parsed_url.netloc + parsed_url.path self.url = parsed_url.netloc + parsed_url.path
self.thumbprint_list = thumbprint_list self.thumbprint_list = thumbprint_list
self.client_id_list = client_id_list self.client_id_list = client_id_list
self.create_date = datetime.utcnow()
@property @property
def arn(self): def arn(self):
return 'arn:aws:iam::{0}:oidc-provider/{1}'.format(ACCOUNT_ID, self.url) return 'arn:aws:iam::{0}:oidc-provider/{1}'.format(ACCOUNT_ID, self.url)
@property
def created_iso_8601(self):
return iso_8601_datetime_without_milliseconds(self.create_date)
def _validate(self, url, thumbprint_list, client_id_list): def _validate(self, url, thumbprint_list, client_id_list):
if any(len(client_id) > 255 for client_id in client_id_list): if any(len(client_id) > 255 for client_id in client_id_list):
self._errors.append(self._format_error( self._errors.append(self._format_error(
@ -1347,5 +1352,13 @@ class IAMBackend(BaseBackend):
self.open_id_providers[open_id_provider.arn] = open_id_provider self.open_id_providers[open_id_provider.arn] = open_id_provider
return open_id_provider return open_id_provider
def get_open_id_connect_provider(self, arn):
open_id_provider = self.open_id_providers.get(arn)
if not open_id_provider:
raise IAMNotFoundException('OpenIDConnect Provider not found for arn {}'.format(arn))
return open_id_provider
iam_backend = IAMBackend() iam_backend = IAMBackend()

View File

@ -759,11 +759,20 @@ class IamResponse(BaseResponse):
open_id_provider_url = self._get_param('Url') open_id_provider_url = self._get_param('Url')
thumbprint_list = self._get_multi_param('ThumbprintList.member') thumbprint_list = self._get_multi_param('ThumbprintList.member')
client_id_list = self._get_multi_param('ClientIDList.member') client_id_list = self._get_multi_param('ClientIDList.member')
open_id_provider = iam_backend.create_open_id_connect_provider(open_id_provider_url, thumbprint_list, client_id_list) open_id_provider = iam_backend.create_open_id_connect_provider(open_id_provider_url, thumbprint_list, client_id_list)
template = self.response_template(CREATE_OPEN_ID_CONNECT_PROVIDER_TEMPLATE) template = self.response_template(CREATE_OPEN_ID_CONNECT_PROVIDER_TEMPLATE)
return template.render(open_id_provider=open_id_provider) return template.render(open_id_provider=open_id_provider)
def get_open_id_connect_provider(self):
open_id_provider_arn = self._get_param('OpenIDConnectProviderArn')
open_id_provider = iam_backend.get_open_id_connect_provider(open_id_provider_arn)
template = self.response_template(GET_OPEN_ID_CONNECT_PROVIDER_TEMPLATE)
return template.render(open_id_provider=open_id_provider)
LIST_ENTITIES_FOR_POLICY_TEMPLATE = """<ListEntitiesForPolicyResponse> LIST_ENTITIES_FOR_POLICY_TEMPLATE = """<ListEntitiesForPolicyResponse>
<ListEntitiesForPolicyResult> <ListEntitiesForPolicyResult>
@ -1993,3 +2002,24 @@ CREATE_OPEN_ID_CONNECT_PROVIDER_TEMPLATE = """<CreateOpenIDConnectProviderRespon
<RequestId>f248366a-4f64-11e4-aefa-bfd6aEXAMPLE</RequestId> <RequestId>f248366a-4f64-11e4-aefa-bfd6aEXAMPLE</RequestId>
</ResponseMetadata> </ResponseMetadata>
</CreateOpenIDConnectProviderResponse>""" </CreateOpenIDConnectProviderResponse>"""
GET_OPEN_ID_CONNECT_PROVIDER_TEMPLATE = """<GetOpenIDConnectProviderResponse xmlns="https://iam.amazonaws.com/doc/2010-05-08/">
<GetOpenIDConnectProviderResult>
<ThumbprintList>
{% for thumbprint in open_id_provider.thumbprint_list %}
<member>{{ thumbprint }}</member>
{% endfor %}
</ThumbprintList>
<CreateDate>{{ open_id_provider.created_iso_8601 }}</CreateDate>
<ClientIDList>
{% for client_id in open_id_provider.client_id_list %}
<member>{{ client_id }}</member>
{% endfor %}
</ClientIDList>
<Url>{{ open_id_provider.url }}</Url>
</GetOpenIDConnectProviderResult>
<ResponseMetadata>
<RequestId>2c91531b-4f65-11e4-aefa-bfd6aEXAMPLE</RequestId>
</ResponseMetadata>
</GetOpenIDConnectProviderResponse>"""

View File

@ -9,6 +9,9 @@ import sure # noqa
import sys import sys
from boto.exception import BotoServerError from boto.exception import BotoServerError
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
from dateutil.tz import tzutc
from freezegun import freeze_time
from moto import mock_iam, mock_iam_deprecated from moto import mock_iam, mock_iam_deprecated
from moto.iam.models import aws_managed_policies from moto.iam.models import aws_managed_policies
from nose.tools import assert_raises, assert_equals from nose.tools import assert_raises, assert_equals
@ -1615,11 +1618,10 @@ def test_create_open_id_connect_provider():
@mock_iam @mock_iam
def test_create_open_id_connect_provider_errors(): def test_create_open_id_connect_provider_errors():
client = boto3.client('iam', region_name='us-east-1') client = boto3.client('iam', region_name='us-east-1')
response = client.create_open_id_connect_provider( client.create_open_id_connect_provider(
Url='https://example.com', Url='https://example.com',
ThumbprintList=[] ThumbprintList=[]
) )
open_id_arn = response['OpenIDConnectProviderArn']
client.create_open_id_connect_provider.when.called_with( client.create_open_id_connect_provider.when.called_with(
Url='https://example.com', Url='https://example.com',
@ -1695,3 +1697,54 @@ def test_create_open_id_connect_provider_errors():
'Value "{2}" at "url" failed to satisfy constraint: ' 'Value "{2}" at "url" failed to satisfy constraint: '
'Member must have length less than or equal to 255'.format([too_long_client_id], [too_long_thumbprint], too_long_url) 'Member must have length less than or equal to 255'.format([too_long_client_id], [too_long_thumbprint], too_long_url)
) )
@freeze_time('2019-01-01 00:00:00')
@mock_iam
def test_get_open_id_connect_provider():
client = boto3.client('iam', region_name='us-east-1')
response = client.create_open_id_connect_provider(
Url='https://example.com',
ThumbprintList=[
'b' * 40
],
ClientIDList=[
'b'
]
)
open_id_arn = response['OpenIDConnectProviderArn']
response = client.get_open_id_connect_provider(
OpenIDConnectProviderArn=open_id_arn
)
response['Url'].should.equal('example.com')
response['ThumbprintList'].should.equal([
'b' * 40
])
response['ClientIDList'].should.equal([
'b'
])
response['CreateDate'].should.equal(datetime.now(tzutc()))
@mock_iam
def test_get_open_id_connect_provider_errors():
client = boto3.client('iam', region_name = 'us-east-1')
response = client.create_open_id_connect_provider(
Url='https://example.com',
ThumbprintList=[
'b' * 40
],
ClientIDList=[
'b'
]
)
open_id_arn = response['OpenIDConnectProviderArn']
client.get_open_id_connect_provider.when.called_with(
OpenIDConnectProviderArn = open_id_arn + '-not-existing'
).should.throw(
ClientError,
'OpenIDConnect Provider not found for arn {}'.format(open_id_arn + '-not-existing')
)