From 199ff05e4ef54f3988e6c6d6f1d20dee15959825 Mon Sep 17 00:00:00 2001 From: John Corrales Date: Sat, 14 Dec 2019 19:21:41 -0800 Subject: [PATCH 1/2] parent 0f67a74d254127d4a64570145966679ab68d9f3d author John Corrales 1576380101 -0800 committer John Corrales 1576633072 -0800 added send_ssh_public_key --- IMPLEMENTATION_COVERAGE.md | 2 +- moto/__init__.py | 1 + moto/backends.py | 2 ++ moto/ec2_instance_connect/__init__.py | 4 ++++ moto/ec2_instance_connect/models.py | 11 +++++++++ moto/ec2_instance_connect/responses.py | 9 ++++++++ moto/ec2_instance_connect/urls.py | 6 +++++ .../test_ec2_instance_connect_boto3.py | 23 +++++++++++++++++++ 8 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 moto/ec2_instance_connect/__init__.py create mode 100644 moto/ec2_instance_connect/models.py create mode 100644 moto/ec2_instance_connect/responses.py create mode 100644 moto/ec2_instance_connect/urls.py create mode 100644 tests/test_ec2_instance_connect/test_ec2_instance_connect_boto3.py diff --git a/IMPLEMENTATION_COVERAGE.md b/IMPLEMENTATION_COVERAGE.md index 5d9f18ebf..e243c16aa 100644 --- a/IMPLEMENTATION_COVERAGE.md +++ b/IMPLEMENTATION_COVERAGE.md @@ -2425,7 +2425,7 @@ ## ec2-instance-connect 0% implemented -- [ ] send_ssh_public_key +- [x] send_ssh_public_key ## ecr 27% implemented diff --git a/moto/__init__.py b/moto/__init__.py index 767c0ee27..7006ee588 100644 --- a/moto/__init__.py +++ b/moto/__init__.py @@ -20,6 +20,7 @@ from .dynamodb import mock_dynamodb, mock_dynamodb_deprecated # noqa from .dynamodb2 import mock_dynamodb2, mock_dynamodb2_deprecated # noqa from .dynamodbstreams import mock_dynamodbstreams # noqa from .ec2 import mock_ec2, mock_ec2_deprecated # noqa +from .ec2_instance_connect import mock_ec2_instance_connect # noqa from .ecr import mock_ecr, mock_ecr_deprecated # noqa from .ecs import mock_ecs, mock_ecs_deprecated # noqa from .elb import mock_elb, mock_elb_deprecated # noqa diff --git a/moto/backends.py b/moto/backends.py index 53a5cafc3..9a741d6f3 100644 --- a/moto/backends.py +++ b/moto/backends.py @@ -18,6 +18,7 @@ from moto.dynamodb import dynamodb_backends from moto.dynamodb2 import dynamodb_backends2 from moto.dynamodbstreams import dynamodbstreams_backends from moto.ec2 import ec2_backends +from moto.ec2_instance_connect import ec2_instance_connect_backends from moto.ecr import ecr_backends from moto.ecs import ecs_backends from moto.elb import elb_backends @@ -69,6 +70,7 @@ BACKENDS = { "dynamodb2": dynamodb_backends2, "dynamodbstreams": dynamodbstreams_backends, "ec2": ec2_backends, + "ec2_instance_connect": ec2_instance_connect_backends, "ecr": ecr_backends, "ecs": ecs_backends, "elb": elb_backends, diff --git a/moto/ec2_instance_connect/__init__.py b/moto/ec2_instance_connect/__init__.py new file mode 100644 index 000000000..c20d59cfa --- /dev/null +++ b/moto/ec2_instance_connect/__init__.py @@ -0,0 +1,4 @@ +from ..core.models import base_decorator +from .models import ec2_instance_connect_backends + +mock_ec2_instance_connect = base_decorator(ec2_instance_connect_backends) diff --git a/moto/ec2_instance_connect/models.py b/moto/ec2_instance_connect/models.py new file mode 100644 index 000000000..cc8cc3f33 --- /dev/null +++ b/moto/ec2_instance_connect/models.py @@ -0,0 +1,11 @@ +import boto +from moto.core import BaseBackend + + +class Ec2InstanceConnectBackend(BaseBackend): + pass + + +ec2_instance_connect_backends = {} +for region in boto.ec2.regions(): + ec2_instance_connect_backends[region.name] = Ec2InstanceConnectBackend() diff --git a/moto/ec2_instance_connect/responses.py b/moto/ec2_instance_connect/responses.py new file mode 100644 index 000000000..462f1fddc --- /dev/null +++ b/moto/ec2_instance_connect/responses.py @@ -0,0 +1,9 @@ +import json +from moto.core.responses import BaseResponse + + +class Ec2InstanceConnectResponse(BaseResponse): + def send_ssh_public_key(self): + return json.dumps( + {"RequestId": "example-2a47-4c91-9700-e37e85162cb6", "Success": True} + ) diff --git a/moto/ec2_instance_connect/urls.py b/moto/ec2_instance_connect/urls.py new file mode 100644 index 000000000..829a2145b --- /dev/null +++ b/moto/ec2_instance_connect/urls.py @@ -0,0 +1,6 @@ +from __future__ import unicode_literals +from .responses import Ec2InstanceConnectResponse + +url_bases = ["https?://ec2-instance-connect.(.+).amazonaws.com"] + +url_paths = {"{0}/$": Ec2InstanceConnectResponse.dispatch} diff --git a/tests/test_ec2_instance_connect/test_ec2_instance_connect_boto3.py b/tests/test_ec2_instance_connect/test_ec2_instance_connect_boto3.py new file mode 100644 index 000000000..eb685d80a --- /dev/null +++ b/tests/test_ec2_instance_connect/test_ec2_instance_connect_boto3.py @@ -0,0 +1,23 @@ +import boto3 + +from moto import mock_ec2_instance_connect + +pubkey = """ssh-rsa +AAAAB3NzaC1yc2EAAAADAQABAAABAQDV5+voluw2zmzqpqCAqtsyoP01TQ8Ydx1eS1yD6wUsHcPqMIqpo57YxiC8XPwrdeKQ6GG6MC3bHsgXoPypGP0LyixbiuLTU31DnnqorcHt4bWs6rQa7dK2pCCflz2fhYRt5ZjqSNsAKivIbqkH66JozN0SySIka3kEV79GdB0BicioKeEJlCwM9vvxafyzjWf/z8E0lh4ni3vkLpIVJ0t5l+Qd9QMJrT6Is0SCQPVagTYZoi8+fWDoGsBa8vyRwDjEzBl28ZplKh9tSyDkRIYszWTpmK8qHiqjLYZBfAxXjGJbEYL1iig4ZxvbYzKEiKSBi1ZMW9iWjHfZDZuxXAmB +example +""" + + +@mock_ec2_instance_connect +def test_send_ssh_public_key(): + client = boto3.client("ec2-instance-connect", region_name="us-east-1") + fake_request_id = "example-2a47-4c91-9700-e37e85162cb6" + + response = client.send_ssh_public_key( + InstanceId="i-abcdefg12345", + InstanceOSUser="ec2-user", + SSHPublicKey=pubkey, + AvailabilityZone="us-east-1a", + ) + + assert response["RequestId"] == fake_request_id From d508bd72ce06f5d179569860ae90facfe958a9d4 Mon Sep 17 00:00:00 2001 From: John Corrales Date: Thu, 19 Dec 2019 17:47:17 -0800 Subject: [PATCH 2/2] escape the dots --- moto/ec2/urls.py | 2 +- moto/ec2_instance_connect/urls.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/moto/ec2/urls.py b/moto/ec2/urls.py index b83a9e950..4d85b2f56 100644 --- a/moto/ec2/urls.py +++ b/moto/ec2/urls.py @@ -2,6 +2,6 @@ from __future__ import unicode_literals from .responses import EC2Response -url_bases = ["https?://ec2.(.+).amazonaws.com(|.cn)"] +url_bases = ["https?://ec2\.(.+)\.amazonaws\.com(|\.cn)"] url_paths = {"{0}/": EC2Response.dispatch} diff --git a/moto/ec2_instance_connect/urls.py b/moto/ec2_instance_connect/urls.py index 829a2145b..e7078264f 100644 --- a/moto/ec2_instance_connect/urls.py +++ b/moto/ec2_instance_connect/urls.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals from .responses import Ec2InstanceConnectResponse -url_bases = ["https?://ec2-instance-connect.(.+).amazonaws.com"] +url_bases = ["https?://ec2-instance-connect\.(.+)\.amazonaws\.com"] url_paths = {"{0}/$": Ec2InstanceConnectResponse.dispatch}