Exemple #1
0
    def test_kinesis_client_with_local(self):
        os.environ["CIS_ENVIRONMENT"] = "local"
        name = "local-stream"
        os.environ["CIS_DYNALITE_PORT"] = self.dynalite_port
        os.environ["CIS_KINESALITE_PORT"] = self.kinesalite_port
        os.environ["CIS_KINESALITE_HOST"] = self.kinesalite_host
        os.environ["CIS_ASSUME_ROLE_ARN"] = "arn:aws:iam::123456789000:role/demo-assume-role"

        conn = Stubber(boto3.session.Session(region_name="us-west-2")).client.client(
            "kinesis", endpoint_url="http://{}:{}".format(self.kinesalite_host, self.kinesalite_port)
        )

        response = conn.create_stream(StreamName=name, ShardCount=1)

        waiter = conn.get_waiter("stream_exists")

        waiter.wait(StreamName=name, Limit=100, WaiterConfig={"Delay": 1, "MaxAttempts": 5})

        assert response is not None

        from cis_aws import connect

        c = connect.AWS()
        c.session(region_name="us-west-2")
        c.assume_role()

        result = c.input_stream_client()
        assert result is not None
        assert result.get("arn").endswith(name)
Exemple #2
0
    def setup(self):
        from cis_change_service.common import get_config

        os.environ["CIS_ENVIRONMENT"] = "local"
        name = "local-identity-vault"
        os.environ["CIS_CONFIG_INI"] = "tests/mozilla-cis.ini"
        config = get_config()
        self.kinesalite_port = config("kinesalite_port", namespace="cis")
        self.kinesalite_host = config("kinesalite_host", namespace="cis")
        self.dynalite_port = config("dynalite_port", namespace="cis")
        self.dynaliteprocess = subprocess.Popen(
            ["dynalite", "--port", self.dynalite_port], preexec_fn=os.setsid)
        self.kinesaliteprocess = subprocess.Popen(
            ["kinesalite", "--port", self.kinesalite_port],
            preexec_fn=os.setsid)
        conn = boto3.client(
            "dynamodb",
            region_name="us-west-2",
            aws_access_key_id="ak",
            aws_secret_access_key="sk",
            endpoint_url="http://localhost:{}".format(self.dynalite_port),
        )

        # XXX TBD this will eventually be replaced by logic from the vault module
        # The vault module will have the authoritative definitions for Attributes and GSI
        try:
            conn.create_table(
                TableName=name,
                KeySchema=[{
                    "AttributeName": "id",
                    "KeyType": "HASH"
                }],
                AttributeDefinitions=[
                    {
                        "AttributeName": "id",
                        "AttributeType": "S"
                    },
                    {
                        "AttributeName": "uuid",
                        "AttributeType": "S"
                    },
                    {
                        "AttributeName": "sequence_number",
                        "AttributeType": "S"
                    },
                    {
                        "AttributeName": "primary_email",
                        "AttributeType": "S"
                    },
                    {
                        "AttributeName": "primary_username",
                        "AttributeType": "S"
                    },
                ],
                ProvisionedThroughput={
                    "ReadCapacityUnits": 5,
                    "WriteCapacityUnits": 5
                },
                GlobalSecondaryIndexes=[
                    {
                        "IndexName":
                        "{}-sequence_number".format(name),
                        "KeySchema": [{
                            "AttributeName": "sequence_number",
                            "KeyType": "HASH"
                        }],
                        "Projection": {
                            "ProjectionType": "ALL"
                        },
                        "ProvisionedThroughput": {
                            "ReadCapacityUnits": 5,
                            "WriteCapacityUnits": 5
                        },
                    },
                    {
                        "IndexName":
                        "{}-primary_email".format(name),
                        "KeySchema": [{
                            "AttributeName": "primary_email",
                            "KeyType": "HASH"
                        }],
                        "Projection": {
                            "ProjectionType": "ALL"
                        },
                        "ProvisionedThroughput": {
                            "ReadCapacityUnits": 5,
                            "WriteCapacityUnits": 5
                        },
                    },
                    {
                        "IndexName":
                        "{}-primary_username".format(name),
                        "KeySchema": [{
                            "AttributeName": "primary_username",
                            "KeyType": "HASH"
                        }],
                        "Projection": {
                            "ProjectionType": "ALL"
                        },
                        "ProvisionedThroughput": {
                            "ReadCapacityUnits": 5,
                            "WriteCapacityUnits": 5
                        },
                    },
                    {
                        "IndexName": "{}-uuid".format(name),
                        "KeySchema": [{
                            "AttributeName": "uuid",
                            "KeyType": "HASH"
                        }],
                        "Projection": {
                            "ProjectionType": "ALL"
                        },
                        "ProvisionedThroughput": {
                            "ReadCapacityUnits": 5,
                            "WriteCapacityUnits": 5
                        },
                    },
                ],
            )
        except Exception as e:
            logger.error("Table error: {}".format(e))

        conn = Stubber(
            boto3.session.Session(region_name="us-west-2")).client.client(
                "kinesis",
                endpoint_url="http://{}:{}".format(self.kinesalite_host,
                                                   self.kinesalite_port))

        try:
            name = "local-stream"
            conn.create_stream(StreamName=name, ShardCount=1)
        except Exception as e:
            logger.error("Stream creation error: {}".format(e))
            # This just means we tried too many tests too fast.
            pass

        waiter = conn.get_waiter("stream_exists")

        waiter.wait(StreamName=name,
                    Limit=100,
                    WaiterConfig={
                        "Delay": 1,
                        "MaxAttempts": 5
                    })

        tags_1 = {"Key": "cis_environment", "Value": "local"}
        tags_2 = {"Key": "application", "Value": "change-stream"}
        conn.add_tags_to_stream(StreamName=name, Tags=tags_1)
        conn.add_tags_to_stream(StreamName=name, Tags=tags_2)
        self.user_profile = FakeUser().as_json()