Skip to content

Commit

Permalink
Merged in hotfix/OPT-353 (pull request #85)
Browse files Browse the repository at this point in the history
[OPT-353] to main

Approved-by: David Fernandez Vigo
Approved-by: David Antolín Álvarez
  • Loading branch information
jmgarcia-iriusrisk authored and dfernandezvigo committed Aug 17, 2022
2 parents 6365179 + b6e981e commit 7a624b9
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 129 deletions.
10 changes: 8 additions & 2 deletions startleft/iac/mapping/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,19 @@ def get_tags(source_model, source_object, mapping):
if mapping is not None:
if isinstance(mapping, list):
for tag in mapping:
c_tags.append(source_model.search(tag, source=source_object))
__search_and_add_tag(c_tags, tag, source_model, source_object)
else:
c_tags.append(source_model.search(mapping, source=source_object))
__search_and_add_tag(c_tags, mapping, source_model, source_object)

return c_tags


def __search_and_add_tag(c_tags: [], query, source_model, source_object):
tag = source_model.search(query, source=source_object)
if isinstance(tag, str):
c_tags.append(tag)


def set_optional_parameters_to_resource(resource, mapping_tags, resource_tags, singleton_multiple_name=None,
singleton_multiple_tags=None):
if mapping_tags is not None and resource_tags is not None and len(
Expand Down
70 changes: 0 additions & 70 deletions tests/resources/terraform/aws_altsource_components.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,108 +32,38 @@ variable "subnet_cidrs" {
default = []
}

resource "aws_subnet" "subnets" {
count = length(local.selected_subnet_cidrs)
vpc_id = data.aws_vpc.selected.id
availability_zone = data.aws_availability_zones.available.names[count.index]
cidr_block = local.selected_subnet_cidrs[count.index]
tags {
Name = "VPC Endpoint"
}
}
resource "aws_security_group" "vpc_endpoint" {
name = "vpc_endpoint"
description = "Allow VPC traffic to communicate with AWS Services"
vpc_id = data.aws_vpc.selected.id

ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = [var.vpc_id]
}
}
resource "aws_vpc_endpoint" "ec2" {
vpc_id = data.aws_vpc.selected.id
service_name = "com.amazonaws.${var.region}.ec2"
vpc_endpoint_type = "Interface"
subnet_ids = [aws_subnet.subnets.*.id]

security_group_ids = [
aws_security_group.vpc_endpoint.id
]

private_dns_enabled = true
}
resource "aws_vpc_endpoint" "ec2_messages" {
vpc_id = data.aws_vpc.selected.id
service_name = "com.amazonaws.${var.region}.ec2messages"
vpc_endpoint_type = "Interface"
subnet_ids = [aws_subnet.subnets.*.id]

security_group_ids = [
aws_security_group.vpc_endpoint.id
]

private_dns_enabled = true
}
resource "aws_vpc_endpoint" "ssm" {
vpc_id = data.aws_vpc.selected.id
service_name = "com.amazonaws.${var.region}.ssm"
vpc_endpoint_type = "Interface"
subnet_ids = [aws_subnet.subnets.*.id]

security_group_ids = [
aws_security_group.vpc_endpoint.id
]

private_dns_enabled = true
}
resource "aws_vpc_endpoint" "ssm_messages" {
vpc_id = data.aws_vpc.selected.id
service_name = "com.amazonaws.${var.region}.ssmmessages"
vpc_endpoint_type = "Interface"
subnet_ids = [aws_subnet.subnets.*.id]

security_group_ids = [
aws_security_group.vpc_endpoint.id
]

private_dns_enabled = true
}
resource "aws_vpc_endpoint" "ecr" {
vpc_id = data.aws_vpc.selected.id
service_name = "com.amazonaws.${var.region}.ecr.dkr"
vpc_endpoint_type = "Interface"
subnet_ids = [aws_subnet.subnets.*.id]

security_group_ids = [
aws_security_group.vpc_endpoint.id
]

private_dns_enabled = true
}
resource "aws_vpc_endpoint" "dynamodb" {
vpc_id = data.aws_vpc.selected.id
service_name = "com.amazonaws.${var.region}.dynamodb"
vpc_endpoint_type = "Interface"
subnet_ids = [aws_subnet.subnets.*.id]

security_group_ids = [
aws_security_group.vpc_endpoint.id
]

private_dns_enabled = true
}
resource "aws_vpc_endpoint" "s3" {
vpc_id = data.aws_vpc.selected.id
service_name = "com.amazonaws.${var.region}.s3"
vpc_endpoint_type = "Interface"
subnet_ids = [aws_subnet.subnets.*.id]

security_group_ids = [
aws_security_group.vpc_endpoint.id
]

private_dns_enabled = true
}
24 changes: 24 additions & 0 deletions tests/unit/iac/mapping/test_mapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from unittest.mock import patch, MagicMock

from startleft.iac.mapping.mapper import get_tags


class TestMapper:

@patch("startleft.iac.mapping.sourcemodel.SourceModel")
def test_get_tags_with_mapping_str(self, mock_source_model):
mock_source_model.search.return_value = 'value'
c_tags = get_tags(mock_source_model, MagicMock(), MagicMock())
assert len(c_tags) is 1

@patch("startleft.iac.mapping.sourcemodel.SourceModel")
def test_get_tags_with_mapping_list(self, mock_source_model):
mock_source_model.search.return_value = 'value'
c_tags = get_tags(mock_source_model, MagicMock(), [MagicMock(), MagicMock()])
assert len(c_tags) is 2

@patch("startleft.iac.mapping.sourcemodel.SourceModel")
def test_get_tags_with_attribute_not_found(self, mock_source_model):
mock_source_model.search.return_value = []
c_tags = get_tags(mock_source_model, MagicMock(), MagicMock())
assert len(c_tags) is 0
Loading

0 comments on commit 7a624b9

Please sign in to comment.