Merge pull request #1467 from snyk/feat/aws_elasticache_cluster

Add aws_elasticache_cluster resource
main
William BEUIL 2022-04-11 16:06:55 +02:00 committed by GitHub
commit b217c0f8bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 178094 additions and 0 deletions

View File

@ -197,6 +197,7 @@ func TestTerraformStateReader_AWS_Resources(t *testing.T) {
{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},
{name: "ElastiCache Cluster", dirName: "aws_elasticache_cluster", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View File

@ -0,0 +1,33 @@
[
{
"Id": "cluster-example",
"Type": "aws_elasticache_cluster",
"Attrs": {
"arn": "arn:aws:elasticache:us-east-2:047081014315:cluster:cluster-example",
"availability_zone": "us-east-2b",
"az_mode": "single-az",
"cache_nodes": [
{
"address": "cluster-example.jkdpui.0001.use2.cache.amazonaws.com",
"availability_zone": "us-east-2b",
"id": "0001",
"port": 11211
}
],
"cluster_address": "cluster-example.jkdpui.cfg.use2.cache.amazonaws.com",
"cluster_id": "cluster-example",
"configuration_endpoint": "cluster-example.jkdpui.cfg.use2.cache.amazonaws.com:11211",
"engine": "memcached",
"engine_version": "1.6.6",
"id": "cluster-example",
"maintenance_window": "fri:02:30-fri:03:30",
"node_type": "cache.t2.micro",
"num_cache_nodes": 1,
"parameter_group_name": "default.memcached1.6",
"port": 11211,
"snapshot_retention_limit": 0,
"snapshot_window": "",
"subnet_group_name": "default"
}
}
]

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,62 @@
{
"version": 4,
"terraform_version": "1.1.6",
"serial": 3,
"lineage": "a304c9d1-d091-4667-702d-61729daf8a4a",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "aws_elasticache_cluster",
"name": "example",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"apply_immediately": null,
"arn": "arn:aws:elasticache:us-east-2:047081014315:cluster:cluster-example",
"availability_zone": "us-east-2b",
"az_mode": "single-az",
"cache_nodes": [
{
"address": "cluster-example.jkdpui.0001.use2.cache.amazonaws.com",
"availability_zone": "us-east-2b",
"id": "0001",
"port": 11211
}
],
"cluster_address": "cluster-example.jkdpui.cfg.use2.cache.amazonaws.com",
"cluster_id": "cluster-example",
"configuration_endpoint": "cluster-example.jkdpui.cfg.use2.cache.amazonaws.com:11211",
"engine": "memcached",
"engine_version": "1.6.6",
"engine_version_actual": "1.6.6",
"final_snapshot_identifier": null,
"id": "cluster-example",
"log_delivery_configuration": [],
"maintenance_window": "fri:02:30-fri:03:30",
"node_type": "cache.t2.micro",
"notification_topic_arn": null,
"num_cache_nodes": 1,
"parameter_group_name": "default.memcached1.6",
"port": 11211,
"preferred_availability_zones": null,
"replication_group_id": null,
"security_group_ids": [],
"security_group_names": [],
"snapshot_arns": null,
"snapshot_name": null,
"snapshot_retention_limit": 0,
"snapshot_window": "",
"subnet_group_name": "default",
"tags": null,
"tags_all": {}
},
"sensitive_attributes": [],
"private": "bnVsbA=="
}
]
}
]
}

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 ElastiCacheClusterEnumerator struct {
repository repository.ElastiCacheRepository
factory resource.ResourceFactory
}
func NewElastiCacheClusterEnumerator(repo repository.ElastiCacheRepository, factory resource.ResourceFactory) *ElastiCacheClusterEnumerator {
return &ElastiCacheClusterEnumerator{
repository: repo,
factory: factory,
}
}
func (e *ElastiCacheClusterEnumerator) SupportedType() resource.ResourceType {
return aws.AwsElastiCacheClusterResourceType
}
func (e *ElastiCacheClusterEnumerator) Enumerate() ([]*resource.Resource, error) {
clusters, err := e.repository.ListAllCacheClusters()
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
results := make([]*resource.Resource, 0, len(clusters))
for _, cluster := range clusters {
c := cluster
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*c.CacheClusterId,
map[string]interface{}{},
),
)
}
return results, err
}

View File

@ -55,6 +55,7 @@ func Init(version string, alerter *alerter.Alerter,
apigatewayv2Repository := repository.NewApiGatewayV2Repository(provider.session, repositoryCache)
autoscalingRepository := repository.NewAutoScalingRepository(provider.session, repositoryCache)
elbRepository := repository.NewELBRepository(provider.session, repositoryCache)
elasticacheRepository := repository.NewElastiCacheRepository(provider.session, repositoryCache)
deserializer := resource.NewDeserializer(factory)
providerLibrary.AddProvider(terraform.AWS, provider)
@ -237,6 +238,8 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewClassicLoadBalancerEnumerator(elbRepository, factory))
remoteLibrary.AddEnumerator(NewElastiCacheClusterEnumerator(elasticacheRepository, factory))
err = resourceSchemaRepository.Init(terraform.AWS, provider.Version(), provider.Schema())
if err != nil {
return err

View File

@ -0,0 +1,45 @@
package repository
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/aws/aws-sdk-go/service/elasticache/elasticacheiface"
"github.com/snyk/driftctl/pkg/remote/cache"
)
type ElastiCacheRepository interface {
ListAllCacheClusters() ([]*elasticache.CacheCluster, error)
}
type elasticacheRepository struct {
client elasticacheiface.ElastiCacheAPI
cache cache.Cache
}
func NewElastiCacheRepository(session *session.Session, c cache.Cache) *elasticacheRepository {
return &elasticacheRepository{
elasticache.New(session),
c,
}
}
func (r *elasticacheRepository) ListAllCacheClusters() ([]*elasticache.CacheCluster, error) {
if v := r.cache.Get("elasticacheListAllCacheClusters"); v != nil {
return v.([]*elasticache.CacheCluster), nil
}
var clusters []*elasticache.CacheCluster
input := elasticache.DescribeCacheClustersInput{}
err := r.client.DescribeCacheClustersPages(&input,
func(resp *elasticache.DescribeCacheClustersOutput, lastPage bool) bool {
clusters = append(clusters, resp.CacheClusters...)
return !lastPage
},
)
if err != nil {
return nil, err
}
r.cache.Put("elasticacheListAllCacheClusters", clusters)
return clusters, nil
}

View File

@ -0,0 +1,96 @@
package repository
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/pkg/errors"
"github.com/r3labs/diff/v2"
"github.com/snyk/driftctl/pkg/remote/cache"
awstest "github.com/snyk/driftctl/test/aws"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func Test_elasticacheRepository_ListAllCacheClusters(t *testing.T) {
clusters := []*elasticache.CacheCluster{
{CacheClusterId: aws.String("cluster1")},
{CacheClusterId: aws.String("cluster2")},
{CacheClusterId: aws.String("cluster3")},
{CacheClusterId: aws.String("cluster4")},
{CacheClusterId: aws.String("cluster5")},
{CacheClusterId: aws.String("cluster6")},
}
remoteError := errors.New("remote error")
tests := []struct {
name string
mocks func(client *awstest.MockFakeElastiCache, store *cache.MockCache)
want []*elasticache.CacheCluster
wantErr error
}{
{
name: "List cache clusters",
mocks: func(client *awstest.MockFakeElastiCache, store *cache.MockCache) {
client.On("DescribeCacheClustersPages",
&elasticache.DescribeCacheClustersInput{},
mock.MatchedBy(func(callback func(res *elasticache.DescribeCacheClustersOutput, lastPage bool) bool) bool {
callback(&elasticache.DescribeCacheClustersOutput{
CacheClusters: clusters[:3],
}, false)
callback(&elasticache.DescribeCacheClustersOutput{
CacheClusters: clusters[3:],
}, true)
return true
})).Return(nil).Once()
store.On("Get", "elasticacheListAllCacheClusters").Return(nil).Times(1)
store.On("Put", "elasticacheListAllCacheClusters", clusters).Return(false).Times(1)
},
want: clusters,
},
{
name: "should hit cache",
mocks: func(client *awstest.MockFakeElastiCache, store *cache.MockCache) {
store.On("Get", "elasticacheListAllCacheClusters").Return(clusters).Times(1)
},
want: clusters,
},
{
name: "should return remote error",
mocks: func(client *awstest.MockFakeElastiCache, store *cache.MockCache) {
client.On("DescribeCacheClustersPages",
&elasticache.DescribeCacheClustersInput{},
mock.AnythingOfType("func(*elasticache.DescribeCacheClustersOutput, bool) bool")).Return(remoteError).Once()
store.On("Get", "elasticacheListAllCacheClusters").Return(nil).Times(1)
},
wantErr: remoteError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := &cache.MockCache{}
client := &awstest.MockFakeElastiCache{}
tt.mocks(client, store)
r := &elasticacheRepository{
client: client,
cache: store,
}
got, err := r.ListAllCacheClusters()
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: %s -> %s", strings.Join(change.Path, "."), change.From, change.To)
}
t.Fail()
}
store.AssertExpectations(t)
client.AssertExpectations(t)
})
}
}

View File

@ -0,0 +1,36 @@
// Code generated by mockery v2.10.4. DO NOT EDIT.
package repository
import (
elasticache "github.com/aws/aws-sdk-go/service/elasticache"
mock "github.com/stretchr/testify/mock"
)
// MockElastiCacheRepository is an autogenerated mock type for the ElastiCacheRepository type
type MockElastiCacheRepository struct {
mock.Mock
}
// ListAllCacheClusters provides a mock function with given fields:
func (_m *MockElastiCacheRepository) ListAllCacheClusters() ([]*elasticache.CacheCluster, error) {
ret := _m.Called()
var r0 []*elasticache.CacheCluster
if rf, ok := ret.Get(0).(func() []*elasticache.CacheCluster); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*elasticache.CacheCluster)
}
}
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,109 @@
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/elasticache"
"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 TestElastiCacheCluster(t *testing.T) {
dummyError := errors.New("dummy error")
tests := []struct {
test string
mocks func(*repository.MockElastiCacheRepository, *mocks.AlerterInterface)
assertExpected func(t *testing.T, got []*resource.Resource)
wantErr error
}{
{
test: "no elasticache clusters",
mocks: func(repository *repository.MockElastiCacheRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllCacheClusters").Return([]*elasticache.CacheCluster{}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "should list elasticache clusters",
mocks: func(repository *repository.MockElastiCacheRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllCacheClusters").Return([]*elasticache.CacheCluster{
{CacheClusterId: awssdk.String("cluster-foo")},
}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 1)
assert.Equal(t, got[0].ResourceId(), "cluster-foo")
assert.Equal(t, got[0].ResourceType(), resourceaws.AwsElastiCacheClusterResourceType)
},
},
{
test: "cannot list elasticache clusters (403)",
mocks: func(repository *repository.MockElastiCacheRepository, alerter *mocks.AlerterInterface) {
awsError := awserr.NewRequestFailure(awserr.New("AccessDeniedException", "", errors.New("")), 403, "")
repository.On("ListAllCacheClusters").Return(nil, awsError)
alerter.On("SendAlert", resourceaws.AwsElastiCacheClusterResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(awsError, resourceaws.AwsElastiCacheClusterResourceType, resourceaws.AwsElastiCacheClusterResourceType), alerts.EnumerationPhase)).Return()
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "cannot list elasticache clusters (dummy error)",
mocks: func(repository *repository.MockElastiCacheRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllCacheClusters").Return(nil, dummyError)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
wantErr: remoteerr.NewResourceScanningError(dummyError, resourceaws.AwsElastiCacheClusterResourceType, ""),
},
}
providerVersion := "3.19.0"
schemaRepository := testresource.InitFakeSchemaRepository("aws", providerVersion)
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.MockElastiCacheRepository{}
c.mocks(fakeRepo, alerter)
var repo repository.ElastiCacheRepository = fakeRepo
remoteLibrary.AddEnumerator(aws.NewElastiCacheClusterEnumerator(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)
c.assertExpected(tt, got)
alerter.AssertExpectations(tt)
fakeRepo.AssertExpectations(tt)
})
}
}

View File

@ -0,0 +1,3 @@
package aws
const AwsElastiCacheClusterResourceType = "aws_elasticache_cluster"

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_ElastiCacheCluster(t *testing.T) {
acceptance.Run(t, acceptance.AccTestCase{
TerraformVersion: "0.15.5",
Paths: []string{"./testdata/acc/aws_elasticache_cluster"},
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

@ -62,6 +62,7 @@ func TestAWS_Metadata_Flags(t *testing.T) {
AwsEcrRepositoryResourceType: {resource.FlagDeepMode},
AwsEipResourceType: {resource.FlagDeepMode},
AwsEipAssociationResourceType: {resource.FlagDeepMode},
AwsElastiCacheClusterResourceType: {},
AwsIamAccessKeyResourceType: {resource.FlagDeepMode},
AwsIamPolicyResourceType: {resource.FlagDeepMode},
AwsIamPolicyAttachmentResourceType: {resource.FlagDeepMode},

View File

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

View File

@ -0,0 +1,20 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/aws" {
version = "3.19.0"
constraints = "3.19.0"
hashes = [
"h1:xur9tF49NgsovNnmwmBR8RdpN8Fcg1TD4CKQPJD6n1A=",
"zh:185a5259153eb9ee4699d4be43b3d509386b473683392034319beee97d470c3b",
"zh:2d9a0a01f93e8d16539d835c02b8b6e1927b7685f4076e96cb07f7dd6944bc6c",
"zh:703f6da36b1b5f3497baa38fccaa7765fb8a2b6440344e4c97172516b49437dd",
"zh:770855565462abadbbddd98cb357d2f1a8f30f68a358cb37cbd5c072cb15b377",
"zh:8008db43149fe4345301f81e15e6d9ddb47aa5e7a31648f9b290af96ad86e92a",
"zh:8cdd27d375da6dcb7687f1fed126b7c04efce1671066802ee876dbbc9c66ec79",
"zh:be22ae185005690d1a017c1b909e0d80ab567e239b4f06ecacdba85080667c1c",
"zh:d2d02e72dbd80f607636cd6237a6c862897caabc635c7b50c0cb243d11246723",
"zh:d8f125b66a1eda2555c0f9bbdf12036a5f8d073499a22ca9e4812b68067fea31",
"zh:f5a98024c64d5d2973ff15b093725a074c0cb4afde07ef32c542e69f17ac90bc",
]
}

View File

@ -0,0 +1,18 @@
provider "aws" {
region = "us-east-1"
}
terraform {
required_providers {
aws = "3.19.0"
}
}
resource "aws_elasticache_cluster" "foo" {
cluster_id = "cluster-foo"
engine = "memcached"
node_type = "cache.t2.micro"
num_cache_nodes = 1
parameter_group_name = "default.memcached1.6"
}

View File

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

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

@ -0,0 +1,9 @@
package aws
import (
"github.com/aws/aws-sdk-go/service/elasticache/elasticacheiface"
)
type FakeElastiCache interface {
elasticacheiface.ElastiCacheAPI
}

File diff suppressed because it is too large Load Diff