Ejemplo n.º 1
0
    def test_create_indexes_and_domains(self):
        indexes = ["index1", "index2"]
        for index_name in indexes:
            index_path = "{}/{}".format(self.es_url, index_name)
            requests.put(index_path, headers=COMMON_HEADERS)
            endpoint = "{}/_cat/indices/{}?format=json&pretty".format(
                self.es_url, index_name)
            req = requests.get(endpoint)
            self.assertEqual(200, req.status_code)
            req_result = json.loads(req.text)
            self.assertIn(req_result[0]["health"], ["green", "yellow"])
            self.assertIn(req_result[0]["index"], indexes)

        es_client = aws_stack.connect_to_service("es")
        test_domain_name_1 = "test1-%s" % short_uid()
        test_domain_name_2 = "test2-%s" % short_uid()
        self._create_domain(name=test_domain_name_1, version="6.8")
        self._create_domain(name=test_domain_name_2, version="6.8")
        status_test_domain_name_1 = es_client.describe_elasticsearch_domain(
            DomainName=test_domain_name_1)
        status_test_domain_name_2 = es_client.describe_elasticsearch_domain(
            DomainName=test_domain_name_2)
        self.assertFalse(
            status_test_domain_name_1["DomainStatus"]["Processing"])
        self.assertFalse(
            status_test_domain_name_2["DomainStatus"]["Processing"])
Ejemplo n.º 2
0
 def test_create_indices(self, opensearch_endpoint):
     indices = ["index1", "index2"]
     for index_name in indices:
         index_path = f"{opensearch_endpoint}/{index_name}"
         requests.put(index_path, headers=COMMON_HEADERS)
         endpoint = f"{opensearch_endpoint}/_cat/indices/{index_name}?format=json&pretty"
         req = requests.get(endpoint)
         assert req.status_code == 200
         req_result = json.loads(req.text)
         assert req_result[0]["health"] in ["green", "yellow"]
         assert req_result[0]["index"] in indices
Ejemplo n.º 3
0
    def test_multiple_api_keys_validate(self):
        response_templates = {'application/json': json.dumps({'TableName': 'MusicCollection',
                                                              'Item': {'id': '$.Id', 'data': '$.data'}})}

        api_id = self.create_api_gateway_and_deploy(response_templates, True)
        url = gateway_request_url(api_id=api_id, stage_name='staging', path='/')

        client = aws_stack.connect_to_service('apigateway')

        # Create multiple usage plans
        usage_plan_ids = []
        for i in range(2):
            payload = {
                'name': 'APIKEYTEST-PLAN-{}'.format(i),
                'description': 'Description',
                'quota': {'limit': 10, 'period': 'DAY', 'offset': 0},
                'throttle': {'rateLimit': 2, 'burstLimit': 1},
                'apiStages': [{'apiId': api_id, 'stage': 'staging'}],
                'tags': {'tag_key': 'tag_value'},
            }
            usage_plan_ids.append(client.create_usage_plan(**payload)['id'])

        api_keys = []
        key_type = 'API_KEY'
        # Create multiple API Keys in each usage plan
        for usage_plan_id in usage_plan_ids:
            for i in range(2):
                api_key = client.create_api_key(name='testMultipleApiKeys{}'.format(i))
                payload = {'usagePlanId': usage_plan_id, 'keyId': api_key['id'], 'keyType': key_type}
                client.create_usage_plan_key(**payload)
                api_keys.append(api_key['value'])

        response = requests.put(
            url,
            json.dumps({'id': 'id1', 'data': 'foobar123'}),
        )
        # when the api key is not passed as part of the header
        self.assertEqual(response.status_code, 403)

        # Check All API Keys work
        for key in api_keys:
            response = requests.put(
                url,
                json.dumps({'id': 'id1', 'data': 'foobar123'}),
                headers={'X-API-Key': key}
            )
            # when the api key is passed as part of the header
            self.assertEqual(response.status_code, 200)
Ejemplo n.º 4
0
    def test_put_integration_dynamodb_proxy_validation_with_response_template(
            self):
        response_templates = {
            'application/json':
            json.dumps({
                'TableName': 'MusicCollection',
                'Item': {
                    'id': '$.Id',
                    'data': '$.data'
                }
            })
        }

        api_id = self.create_api_gateway_and_deploy(response_templates)
        url = self.gateway_request_url(api_id=api_id,
                                       stage_name='staging',
                                       path='/')

        response = requests.put(
            url,
            json.dumps({
                'id': 'id1',
                'data': 'foobar123'
            }),
        )

        self.assertEqual(response.status_code, 200)
        dynamo_client = aws_stack.connect_to_resource('dynamodb')
        table = dynamo_client.Table('MusicCollection')
        result = table.get_item(Key={'id': 'id1'})
        self.assertEqual(result['Item']['data'], 'foobar123')
Ejemplo n.º 5
0
    def test_api_key_required_for_methods(self):
        response_templates = {
            'application/json':
            json.dumps({
                'TableName': 'MusicCollection',
                'Item': {
                    'id': '$.Id',
                    'data': '$.data'
                }
            })
        }

        api_id = self.create_api_gateway_and_deploy(response_templates, True)
        url = self.gateway_request_url(api_id=api_id,
                                       stage_name='staging',
                                       path='/')

        response = requests.put(
            url,
            json.dumps({
                'id': 'id1',
                'data': 'foobar123'
            }),
        )

        self.assertEqual(response.status_code, 403)
Ejemplo n.º 6
0
def add_document(id, document):
    article_path = '{}/{}/employee/{}?pretty'.format(es_url, test_index, id)
    resp = requests.put(article_path,
                        data=json.dumps(document),
                        headers=common_headers)
    # Pause to allow the document to be indexed
    time.sleep(1)
    return resp
Ejemplo n.º 7
0
def add_document(id, document):
    article_path = '{}/{}/employee/{}?pretty'.format(ES_URL, TEST_INDEX, id)
    resp = requests.put(article_path,
                        data=json.dumps(document),
                        headers=COMMON_HEADERS)
    # Pause to allow the document to be indexed
    time.sleep(1)
    return resp
Ejemplo n.º 8
0
def add_document(id, document):
    article_path = '{}/{}/employee/{}?pretty'.format(ES_URL, TEST_INDEX, id)
    resp = requests.put(
        article_path,
        data=json.dumps(document),
        headers=COMMON_HEADERS)
    # Pause to allow the document to be indexed
    time.sleep(1)
    return resp
Ejemplo n.º 9
0
    def test_put_integration_dynamodb_proxy_validation_without_response_template(self):
        api_id = self.create_api_gateway_and_deploy({})
        url = self.gateway_request_url(api_id=api_id, stage_name='staging', path='/')
        response = requests.put(
            url,
            json.dumps({'id': 'id1', 'data': 'foobar123'}),
        )

        self.assertEqual(response.status_code, 404)
Ejemplo n.º 10
0
    def test_api_key_required_for_methods(self):
        response_templates = {'application/json': json.dumps({'TableName': 'MusicCollection',
                                                              'Item': {'id': '$.Id', 'data': '$.data'}})}

        api_id = self.create_api_gateway_and_deploy(response_templates, True)
        url = self.gateway_request_url(api_id=api_id, stage_name='staging', path='/')

        payload = {
            'name': 'TEST-PLAN-2',
            'description': 'Description',
            'quota': {'limit': 10, 'period': 'DAY', 'offset': 0},
            'throttle': {'rateLimit': 2, 'burstLimit': 1},
            'apiStages': [{'apiId': api_id, 'stage': 'staging'}],
            'tags': {'tag_key': 'tag_value'},
        }

        client = aws_stack.connect_to_service('apigateway')
        usage_plan_id = client.create_usage_plan(**payload)['id']

        key_name = 'testApiKey'
        key_type = 'API_KEY'
        api_key = client.create_api_key(name=key_name)

        payload = {'usagePlanId': usage_plan_id, 'keyId': api_key['id'], 'keyType': key_type}
        client.create_usage_plan_key(**payload)

        response = requests.put(
            url,
            json.dumps({'id': 'id1', 'data': 'foobar123'}),
        )
        # when the api key is not passed as part of the header
        self.assertEqual(response.status_code, 403)

        response = requests.put(
            url,
            json.dumps({'id': 'id1', 'data': 'foobar123'}),
            headers={'X-API-Key': api_key['value']}
        )
        # when the api key is passed as part of the header
        self.assertEqual(response.status_code, 200)
Ejemplo n.º 11
0
    def test_create_indexes_and_domains(self):
        indexes = ['index1', 'index2']
        for index_name in indexes:
            index_path = '{}/{}'.format(self.es_url, index_name)
            requests.put(index_path, headers=COMMON_HEADERS)
            endpoint = 'http://localhost:{}/_cat/indices/{}?format=json&pretty'.format(
                config.PORT_ELASTICSEARCH, index_name)
            req = requests.get(endpoint)
            self.assertEqual(req.status_code, 200)
            req_result = json.loads(req.text)
            self.assertTrue(req_result[0]['health'] in ['green', 'yellow'])
            self.assertTrue(req_result[0]['index'] in indexes)

        es_client = aws_stack.connect_to_service('es')
        test_domain_name_1 = 'test1-%s' % short_uid()
        test_domain_name_2 = 'test2-%s' % short_uid()
        self._create_domain(name=test_domain_name_1, version='6.8')
        self._create_domain(name=test_domain_name_2, version='6.8')
        status_test_domain_name_1 = es_client.describe_elasticsearch_domain(DomainName=test_domain_name_1)
        status_test_domain_name_2 = es_client.describe_elasticsearch_domain(DomainName=test_domain_name_2)
        self.assertTrue(status_test_domain_name_1['DomainStatus']['Created'])
        self.assertTrue(status_test_domain_name_2['DomainStatus']['Created'])
Ejemplo n.º 12
0
def opensearch_document_path(opensearch_client, opensearch_endpoint):
    document = {
        "first_name": "Boba",
        "last_name": "Fett",
        "age": 41,
        "about": "I'm just a simple man, trying to make my way in the universe.",
        "interests": ["mandalorian armor", "tusken culture"],
    }
    document_path = f"{opensearch_endpoint}/bounty/hunters/1"
    response = requests.put(
        document_path,
        data=json.dumps(document),
        headers={"content-type": "application/json", "Accept-encoding": "identity"},
    )
    assert response.status_code == 201, f"could not create document at: {document_path}"
    return document_path
Ejemplo n.º 13
0
    def test_multiplexing_cluster(self, monkeypatch):
        monkeypatch.setattr(config, "ES_ENDPOINT_STRATEGY", "domain")
        monkeypatch.setattr(config, "ES_MULTI_CLUSTER", False)

        manager = MultiplexingClusterManager()

        # create two elasticsearch domains
        domain0_name = f"domain-{short_uid()}"
        domain1_name = f"domain-{short_uid()}"
        domain0_arn = get_domain_arn(domain0_name, "us-east-1",
                                     TEST_AWS_ACCOUNT_ID)
        domain1_arn = get_domain_arn(domain1_name, "us-east-1",
                                     TEST_AWS_ACCOUNT_ID)
        cluster0 = manager.create(domain0_arn, dict(DomainName=domain0_name))
        cluster1 = manager.create(domain1_arn, dict(DomainName=domain1_name))

        try:
            # spawn the two clusters
            assert cluster0.wait_is_up(240)
            assert cluster1.wait_is_up(240)

            retry(lambda: try_cluster_health(cluster0.url),
                  retries=12,
                  sleep=10)
            retry(lambda: try_cluster_health(cluster1.url),
                  retries=12,
                  sleep=10)

            # create an index in cluster0, wait for it to appear, make sure it's not in cluster1
            index0_url = cluster0.url + "/my-index?pretty"
            index1_url = cluster1.url + "/my-index?pretty"

            response = requests.put(index0_url)
            assert response.ok, "failed to put index into cluster %s: %s" % (
                cluster0.url,
                response.text,
            )
            assert poll_condition(lambda: requests.head(index0_url).ok,
                                  timeout=10), "gave up waiting for index"

            assert requests.head(
                index1_url).ok, "expected index to appear by multiplexing"

        finally:
            call_safe(cluster0.shutdown)
            call_safe(cluster1.shutdown)
Ejemplo n.º 14
0
    def test_multiplexing_cluster(self, monkeypatch):
        monkeypatch.setattr(config, "OPENSEARCH_ENDPOINT_STRATEGY", "domain")
        monkeypatch.setattr(config, "OPENSEARCH_MULTI_CLUSTER", False)

        manager = MultiplexingClusterManager()

        # create two opensearch domains
        domain_key_0 = DomainKey(domain_name=f"domain-{short_uid()}",
                                 region="us-east-1",
                                 account=TEST_AWS_ACCOUNT_ID)
        domain_key_1 = DomainKey(domain_name=f"domain-{short_uid()}",
                                 region="us-east-1",
                                 account=TEST_AWS_ACCOUNT_ID)
        cluster_0 = manager.create(domain_key_0.arn,
                                   OPENSEARCH_DEFAULT_VERSION)
        cluster_1 = manager.create(domain_key_1.arn,
                                   OPENSEARCH_DEFAULT_VERSION)

        try:
            # spawn the two clusters
            assert cluster_0.wait_is_up(240)
            assert cluster_1.wait_is_up(240)

            retry(lambda: try_cluster_health(cluster_0.url),
                  retries=12,
                  sleep=10)
            retry(lambda: try_cluster_health(cluster_1.url),
                  retries=12,
                  sleep=10)

            # create an index in cluster_0, wait for it to appear, make sure it's in cluster_1, too
            index_url_0 = cluster_0.url + "/my-index?pretty"
            index_url_1 = cluster_1.url + "/my-index?pretty"

            response = requests.put(index_url_0)
            assert response.ok, f"failed to put index into cluster {cluster_0.url}: {response.text}"
            assert poll_condition(lambda: requests.head(index_url_0).ok,
                                  timeout=10), "gave up waiting for index"

            assert requests.head(
                index_url_1).ok, "index should appear in second cluster"

        finally:
            call_safe(cluster_0.shutdown)
            call_safe(cluster_1.shutdown)