Merge pull request #1465 from snyk/feat/add_aws_elb

Add aws_elb resource
main
William BEUIL 2022-04-11 14:33:45 +02:00 committed by GitHub
commit 8304f21607
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 174804 additions and 7 deletions

View File

@ -196,6 +196,7 @@ func TestTerraformStateReader_AWS_Resources(t *testing.T) {
{name: "Launch configuration", dirName: "aws_launch_configuration", wantErr: false},
{name: "EBS encryption by default", dirName: "aws_ebs_encryption_by_default", wantErr: false},
{name: "LoadBalancer", dirName: "aws_lb", wantErr: false},
{name: "Classic load balancer", dirName: "aws_elb", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View File

@ -0,0 +1,55 @@
[
{
"Id": "foobar-terraform-elb",
"Type": "aws_elb",
"Attrs": {
"arn": "arn:aws:elasticloadbalancing:us-east-1:533948124879:loadbalancer/foobar-terraform-elb",
"availability_zones": [
"us-east-1a",
"us-east-1b"
],
"connection_draining": true,
"connection_draining_timeout": 400,
"cross_zone_load_balancing": true,
"dns_name": "foobar-terraform-elb-862208417.us-east-1.elb.amazonaws.com",
"health_check": [
{
"healthy_threshold": 10,
"interval": 30,
"target": "TCP:8000",
"timeout": 5,
"unhealthy_threshold": 2
}
],
"id": "foobar-terraform-elb",
"idle_timeout": 400,
"instances": [
"i-0197bcecd8741f8f2"
],
"internal": false,
"listener": [
{
"instance_port": 8000,
"instance_protocol": "http",
"lb_port": 80,
"lb_protocol": "http",
"ssl_certificate_id": ""
}
],
"name": "foobar-terraform-elb",
"security_groups": [
"sg-06d494bd7e90d1978"
],
"source_security_group": "533948124879/default_elb_9cdec3e4-c42b-3ae8-a684-6161437e15d0",
"source_security_group_id": "sg-06d494bd7e90d1978",
"subnets": [
"subnet-86cffacb",
"subnet-a18debfe"
],
"tags": {
"Name": "foobar-terraform-elb"
},
"zone_id": "Z35SXDOTRQ7X7K"
}
}
]

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,80 @@
{
"version": 4,
"terraform_version": "1.0.0",
"serial": 539,
"lineage": "9566e18d-6080-4aa8-e9a6-4c38905cf68f",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "aws_elb",
"name": "bar",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"access_logs": [],
"arn": "arn:aws:elasticloadbalancing:us-east-1:533948124879:loadbalancer/foobar-terraform-elb",
"availability_zones": [
"us-east-1a",
"us-east-1b"
],
"connection_draining": true,
"connection_draining_timeout": 400,
"cross_zone_load_balancing": true,
"desync_mitigation_mode": "defensive",
"dns_name": "foobar-terraform-elb-862208417.us-east-1.elb.amazonaws.com",
"health_check": [
{
"healthy_threshold": 10,
"interval": 30,
"target": "TCP:8000",
"timeout": 5,
"unhealthy_threshold": 2
}
],
"id": "foobar-terraform-elb",
"idle_timeout": 400,
"instances": [
"i-0197bcecd8741f8f2"
],
"internal": false,
"listener": [
{
"instance_port": 8000,
"instance_protocol": "http",
"lb_port": 80,
"lb_protocol": "http",
"ssl_certificate_id": ""
}
],
"name": "foobar-terraform-elb",
"name_prefix": null,
"security_groups": [
"sg-06d494bd7e90d1978"
],
"source_security_group": "533948124879/default_elb_9cdec3e4-c42b-3ae8-a684-6161437e15d0",
"source_security_group_id": "sg-06d494bd7e90d1978",
"subnets": [
"subnet-86cffacb",
"subnet-a18debfe"
],
"tags": {
"Name": "foobar-terraform-elb"
},
"tags_all": {
"Name": "foobar-terraform-elb"
},
"zone_id": "Z35SXDOTRQ7X7K"
},
"sensitive_attributes": [],
"private": "bnVsbA==",
"dependencies": [
"aws_instance.web"
]
}
]
}
]
}

View File

@ -0,0 +1,46 @@
package aws
import (
"github.com/snyk/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/snyk/driftctl/pkg/remote/error"
"github.com/snyk/driftctl/pkg/resource"
"github.com/snyk/driftctl/pkg/resource/aws"
)
type ClassicLoadBalancerEnumerator struct {
repository repository.ELBRepository
factory resource.ResourceFactory
}
func NewClassicLoadBalancerEnumerator(repo repository.ELBRepository, factory resource.ResourceFactory) *ClassicLoadBalancerEnumerator {
return &ClassicLoadBalancerEnumerator{
repository: repo,
factory: factory,
}
}
func (e *ClassicLoadBalancerEnumerator) SupportedType() resource.ResourceType {
return aws.AwsClassicLoadBalancerResourceType
}
func (e *ClassicLoadBalancerEnumerator) Enumerate() ([]*resource.Resource, error) {
loadBalancers, err := e.repository.ListAllLoadBalancers()
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
results := make([]*resource.Resource, 0, len(loadBalancers))
for _, lb := range loadBalancers {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*lb.LoadBalancerName,
map[string]interface{}{},
),
)
}
return results, nil
}

View File

@ -54,6 +54,7 @@ func Init(version string, alerter *alerter.Alerter,
appAutoScalingRepository := repository.NewAppAutoScalingRepository(provider.session, repositoryCache)
apigatewayv2Repository := repository.NewApiGatewayV2Repository(provider.session, repositoryCache)
autoscalingRepository := repository.NewAutoScalingRepository(provider.session, repositoryCache)
elbRepository := repository.NewELBRepository(provider.session, repositoryCache)
deserializer := resource.NewDeserializer(factory)
providerLibrary.AddProvider(terraform.AWS, provider)
@ -234,6 +235,8 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewLoadBalancerEnumerator(elbv2Repository, factory))
remoteLibrary.AddEnumerator(NewClassicLoadBalancerEnumerator(elbRepository, factory))
err = resourceSchemaRepository.Init(terraform.AWS, provider.Version(), provider.Schema())
if err != nil {
return err

View File

@ -0,0 +1,43 @@
package repository
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/aws/aws-sdk-go/service/elb/elbiface"
"github.com/snyk/driftctl/pkg/remote/cache"
)
type ELBRepository interface {
ListAllLoadBalancers() ([]*elb.LoadBalancerDescription, error)
}
type elbRepository struct {
client elbiface.ELBAPI
cache cache.Cache
}
func NewELBRepository(session *session.Session, c cache.Cache) *elbRepository {
return &elbRepository{
elb.New(session),
c,
}
}
func (r *elbRepository) ListAllLoadBalancers() ([]*elb.LoadBalancerDescription, error) {
if v := r.cache.Get("elbListAllLoadBalancers"); v != nil {
return v.([]*elb.LoadBalancerDescription), nil
}
results := make([]*elb.LoadBalancerDescription, 0)
input := elb.DescribeLoadBalancersInput{}
err := r.client.DescribeLoadBalancersPages(&input, func(res *elb.DescribeLoadBalancersOutput, lastPage bool) bool {
results = append(results, res.LoadBalancerDescriptions...)
return !lastPage
})
if err != nil {
return nil, err
}
r.cache.Put("elbListAllLoadBalancers", results)
return results, nil
}

View File

@ -0,0 +1,119 @@
package repository
import (
"errors"
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/snyk/driftctl/pkg/remote/cache"
awstest "github.com/snyk/driftctl/test/aws"
"github.com/stretchr/testify/mock"
"github.com/r3labs/diff/v2"
"github.com/stretchr/testify/assert"
)
func Test_ELBRepository_ListAllLoadBalancers(t *testing.T) {
dummyErr := errors.New("dummy error")
results := []*elb.LoadBalancerDescription{
{
LoadBalancerName: aws.String("test-lb-1"),
},
{
LoadBalancerName: aws.String("test-lb-2"),
},
}
tests := []struct {
name string
mocks func(*awstest.MockFakeELB, *cache.MockCache)
want []*elb.LoadBalancerDescription
wantErr error
}{
{
name: "List load balancers with multiple pages",
mocks: func(client *awstest.MockFakeELB, store *cache.MockCache) {
store.On("Get", "elbListAllLoadBalancers").Return(nil).Once()
client.On("DescribeLoadBalancersPages",
&elb.DescribeLoadBalancersInput{},
mock.MatchedBy(func(callback func(res *elb.DescribeLoadBalancersOutput, lastPage bool) bool) bool {
callback(&elb.DescribeLoadBalancersOutput{LoadBalancerDescriptions: []*elb.LoadBalancerDescription{
results[0],
}}, false)
callback(&elb.DescribeLoadBalancersOutput{LoadBalancerDescriptions: []*elb.LoadBalancerDescription{
results[1],
}}, true)
return true
})).Return(nil).Once()
store.On("Put", "elbListAllLoadBalancers", results).Return(false).Once()
},
want: []*elb.LoadBalancerDescription{
{
LoadBalancerName: aws.String("test-lb-1"),
},
{
LoadBalancerName: aws.String("test-lb-2"),
},
},
},
{
name: "List load balancers with multiple pages (cache hit)",
mocks: func(client *awstest.MockFakeELB, store *cache.MockCache) {
store.On("Get", "elbListAllLoadBalancers").Return(results).Once()
},
want: []*elb.LoadBalancerDescription{
{
LoadBalancerName: aws.String("test-lb-1"),
},
{
LoadBalancerName: aws.String("test-lb-2"),
},
},
},
{
name: "Error listing load balancers",
mocks: func(client *awstest.MockFakeELB, store *cache.MockCache) {
store.On("Get", "elbListAllLoadBalancers").Return(nil).Once()
client.On("DescribeLoadBalancersPages",
&elb.DescribeLoadBalancersInput{},
mock.MatchedBy(func(callback func(res *elb.DescribeLoadBalancersOutput, lastPage bool) bool) bool {
callback(&elb.DescribeLoadBalancersOutput{LoadBalancerDescriptions: []*elb.LoadBalancerDescription{}}, true)
return true
})).Return(dummyErr).Once()
},
wantErr: dummyErr,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := &cache.MockCache{}
client := &awstest.MockFakeELB{}
tt.mocks(client, store)
r := &elbRepository{
client: client,
cache: store,
}
got, err := r.ListAllLoadBalancers()
assert.Equal(t, tt.wantErr, err)
changelog, err := diff.Diff(got, tt.want)
assert.Nil(t, err)
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s: %v -> %v", strings.Join(change.Path, "."), change.From, change.To)
}
t.Fail()
}
client.AssertExpectations(t)
store.AssertExpectations(t)
})
}
}

View File

@ -24,7 +24,7 @@ func NewELBV2Repository(session *session.Session, c cache.Cache) *elbv2Repositor
}
func (r *elbv2Repository) ListAllLoadBalancers() ([]*elbv2.LoadBalancer, error) {
if v := r.cache.Get("elbListAllLoadBalancers"); v != nil {
if v := r.cache.Get("elbv2ListAllLoadBalancers"); v != nil {
return v.([]*elbv2.LoadBalancer), nil
}
@ -37,6 +37,6 @@ func (r *elbv2Repository) ListAllLoadBalancers() ([]*elbv2.LoadBalancer, error)
if err != nil {
return nil, err
}
r.cache.Put("elbListAllLoadBalancers", results)
r.cache.Put("elbv2ListAllLoadBalancers", results)
return results, err
}

View File

@ -40,7 +40,7 @@ func Test_ELBV2Repository_ListAllLoadBalancers(t *testing.T) {
},
}
store.On("Get", "elbListAllLoadBalancers").Return(nil).Once()
store.On("Get", "elbv2ListAllLoadBalancers").Return(nil).Once()
client.On("DescribeLoadBalancersPages",
&elbv2.DescribeLoadBalancersInput{},
@ -54,7 +54,7 @@ func Test_ELBV2Repository_ListAllLoadBalancers(t *testing.T) {
return true
})).Return(nil).Once()
store.On("Put", "elbListAllLoadBalancers", results.LoadBalancers).Return(false).Once()
store.On("Put", "elbv2ListAllLoadBalancers", results.LoadBalancers).Return(false).Once()
},
want: []*elbv2.LoadBalancer{
{
@ -79,7 +79,7 @@ func Test_ELBV2Repository_ListAllLoadBalancers(t *testing.T) {
},
}
store.On("Get", "elbListAllLoadBalancers").Return(output.LoadBalancers).Once()
store.On("Get", "elbv2ListAllLoadBalancers").Return(output.LoadBalancers).Once()
},
want: []*elbv2.LoadBalancer{
{
@ -91,7 +91,7 @@ func Test_ELBV2Repository_ListAllLoadBalancers(t *testing.T) {
{
name: "error listing load balancers",
mocks: func(client *awstest.MockFakeELBV2, store *cache.MockCache) {
store.On("Get", "elbListAllLoadBalancers").Return(nil).Once()
store.On("Get", "elbv2ListAllLoadBalancers").Return(nil).Once()
client.On("DescribeLoadBalancersPages",
&elbv2.DescribeLoadBalancersInput{},

View File

@ -0,0 +1,36 @@
// Code generated by mockery v2.10.0. DO NOT EDIT.
package repository
import (
elb "github.com/aws/aws-sdk-go/service/elb"
mock "github.com/stretchr/testify/mock"
)
// MockELBRepository is an autogenerated mock type for the ELBRepository type
type MockELBRepository struct {
mock.Mock
}
// ListAllLoadBalancers provides a mock function with given fields:
func (_m *MockELBRepository) ListAllLoadBalancers() ([]*elb.LoadBalancerDescription, error) {
ret := _m.Called()
var r0 []*elb.LoadBalancerDescription
if rf, ok := ret.Get(0).(func() []*elb.LoadBalancerDescription); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*elb.LoadBalancerDescription)
}
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}

View File

@ -0,0 +1,116 @@
package remote
import (
"errors"
"testing"
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/snyk/driftctl/mocks"
"github.com/snyk/driftctl/pkg/filter"
"github.com/snyk/driftctl/pkg/remote/alerts"
"github.com/snyk/driftctl/pkg/remote/aws"
"github.com/snyk/driftctl/pkg/remote/aws/repository"
"github.com/snyk/driftctl/pkg/remote/common"
remoteerr "github.com/snyk/driftctl/pkg/remote/error"
"github.com/snyk/driftctl/pkg/resource"
resourceaws "github.com/snyk/driftctl/pkg/resource/aws"
"github.com/snyk/driftctl/pkg/terraform"
testresource "github.com/snyk/driftctl/test/resource"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestELB_LoadBalancer(t *testing.T) {
dummyError := errors.New("dummy error")
tests := []struct {
test string
mocks func(*repository.MockELBRepository, *mocks.AlerterInterface)
assertExpected func(t *testing.T, got []*resource.Resource)
wantErr error
}{
{
test: "no load balancer",
mocks: func(repository *repository.MockELBRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllLoadBalancers").Return([]*elb.LoadBalancerDescription{}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "should list load balancers",
mocks: func(repository *repository.MockELBRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllLoadBalancers").Return([]*elb.LoadBalancerDescription{
{
LoadBalancerName: awssdk.String("acc-test-lb-tf"),
},
}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 1)
assert.Equal(t, "acc-test-lb-tf", got[0].ResourceId())
assert.Equal(t, resourceaws.AwsClassicLoadBalancerResourceType, got[0].ResourceType())
},
},
{
test: "cannot list load balancers",
mocks: func(repository *repository.MockELBRepository, alerter *mocks.AlerterInterface) {
awsError := awserr.NewRequestFailure(awserr.New("AccessDeniedException", "", errors.New("")), 403, "")
repository.On("ListAllLoadBalancers").Return(nil, awsError)
alerter.On("SendAlert", resourceaws.AwsClassicLoadBalancerResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(awsError, resourceaws.AwsClassicLoadBalancerResourceType, resourceaws.AwsClassicLoadBalancerResourceType), alerts.EnumerationPhase)).Return()
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "cannot list load balancers (dummy error)",
mocks: func(repository *repository.MockELBRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllLoadBalancers").Return(nil, dummyError)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
wantErr: remoteerr.NewResourceScanningError(dummyError, resourceaws.AwsClassicLoadBalancerResourceType, ""),
},
}
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
for _, c := range tests {
t.Run(c.test, func(tt *testing.T) {
scanOptions := ScannerOptions{}
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
alerter := &mocks.AlerterInterface{}
fakeRepo := &repository.MockELBRepository{}
c.mocks(fakeRepo, alerter)
var repo repository.ELBRepository = fakeRepo
remoteLibrary.AddEnumerator(aws.NewClassicLoadBalancerEnumerator(repo, factory))
testFilter := &filter.MockFilter{}
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
got, err := s.Resources()
assert.Equal(tt, c.wantErr, err)
if err != nil {
return
}
c.assertExpected(tt, got)
alerter.AssertExpectations(tt)
fakeRepo.AssertExpectations(tt)
})
}
}

View File

@ -22,7 +22,7 @@ import (
"github.com/stretchr/testify/mock"
)
func TestLoadBalancer(t *testing.T) {
func TestELBV2_LoadBalancer(t *testing.T) {
dummyError := errors.New("dummy error")
tests := []struct {

View File

@ -0,0 +1,3 @@
package aws
const AwsClassicLoadBalancerResourceType = "aws_elb"

View File

@ -0,0 +1,30 @@
package aws_test
import (
"testing"
"github.com/snyk/driftctl/test"
"github.com/snyk/driftctl/test/acceptance"
)
func TestAcc_Aws_ClassicLoadBalancer(t *testing.T) {
acceptance.Run(t, acceptance.AccTestCase{
TerraformVersion: "0.15.5",
Paths: []string{"./testdata/acc/aws_elb"},
Args: []string{"scan"},
Checks: []acceptance.AccCheck{
{
Env: map[string]string{
"AWS_REGION": "us-east-1",
},
Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)
}
result.AssertInfrastructureIsInSync()
result.AssertManagedCount(1)
},
},
},
})
}

View File

@ -110,6 +110,7 @@ func TestAWS_Metadata_Flags(t *testing.T) {
AwsLaunchConfigurationResourceType: {},
AwsLoadBalancerResourceType: {},
AwsApplicationLoadBalancerResourceType: {},
AwsClassicLoadBalancerResourceType: {},
}
schemaRepository := testresource.InitFakeSchemaRepository(tf.AWS, "3.19.0")

View File

@ -0,0 +1,2 @@
*
!aws_elb

View File

@ -0,0 +1,21 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/aws" {
version = "3.44.0"
constraints = "3.44.0"
hashes = [
"h1:hxQ8n9SHHfAIXd/FtfAqxokFYWBedzZf7xqQZWJajUs=",
"zh:0680315b29a140e9b7e4f5aeed3f2445abdfab31fc9237f34dcad06de4f410df",
"zh:13811322a205fb4a0ee617f0ae51ec94176befdf569235d0c7064db911f0acc7",
"zh:25e427a1cfcb1d411bc12040cf0684158d094416ecf18889a41196bacc761729",
"zh:40cd6acd24b060823f8d116355d8f844461a11925796b1757eb2ee18abc0bc7c",
"zh:94e2463eef555c388cd27f6e85ad803692d6d80ffa621bdc382ab119001d4de4",
"zh:aadc3bc216b14839e85b463f07b8507920ace5f202a608e4a835df23711c8a0d",
"zh:ab50dc1242af5a8fcdb18cf89beeaf2b2146b51ecfcecdbea033913a5f4c1c14",
"zh:ad48bbf4af66b5d48ca07c5c558d2f5724311db4dd943c1c98a7f3f107e03311",
"zh:ad76796c2145a7aaec1970a5244f5c0a9d200556121e2c5b382f296597b1a03c",
"zh:cf0a2181356598f8a2abfeaf0cdf385bdeea7f2e52821c850a2a08b60c26b9f6",
"zh:f76801af6bc34fe4a5bf1c63fa0204e24b81691049efecd6baa1526593f03935",
]
}

View File

@ -0,0 +1,55 @@
provider "aws" {
region = "us-east-1"
}
terraform {
required_providers {
aws = {
version = "3.44.0"
}
}
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
tags = {
Name = "HelloWorld"
}
}
# Create a new load balancer
resource "aws_elb" "bar" {
name = "acc-test-terraform-elb"
availability_zones = ["us-east-1a", "us-east-1b"]
listener {
instance_port = 8000
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
instances = [aws_instance.web.id]
cross_zone_load_balancing = true
idle_timeout = 400
connection_draining = true
connection_draining_timeout = 400
}

View File

@ -163,6 +163,7 @@ var supportedTypes = map[string]ResourceTypeMeta{
"aws_apigatewayv2_integration_response": {},
"aws_launch_template": {},
"aws_launch_configuration": {},
"aws_elb": {},
"github_branch_protection": {},
"github_membership": {},

9
test/aws/elb.go Normal file
View File

@ -0,0 +1,9 @@
package aws
import (
"github.com/aws/aws-sdk-go/service/elb/elbiface"
)
type FakeELB interface {
elbiface.ELBAPI
}

2419
test/aws/mock_FakeELB.go Normal file

File diff suppressed because it is too large Load Diff