Merge branch 'main' into fix/unbuffered-chan

main
William BEUIL 2021-11-10 16:00:38 +01:00 committed by GitHub
commit ab5ae0c4ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 574 additions and 0 deletions

View File

@ -357,6 +357,7 @@ func TestTerraformStateReader_Google_Resources(t *testing.T) {
{name: "bigtable table", dirName: "google_bigtable_table", wantErr: false}, {name: "bigtable table", dirName: "google_bigtable_table", wantErr: false},
{name: "sql database instance", dirName: "google_sql_database_instance", wantErr: false}, {name: "sql database instance", dirName: "google_sql_database_instance", wantErr: false},
{name: "health check", dirName: "google_compute_health_check", wantErr: false}, {name: "health check", dirName: "google_compute_health_check", wantErr: false},
{name: "cloudrun service", dirName: "google_cloudrun_service", wantErr: false},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View File

@ -0,0 +1,112 @@
[
{
"Id": "locations/us-central1/namespaces/cloudskiff-dev-elie/services/cloudrun-srv",
"Type": "google_cloud_run_service",
"Attrs": {
"autogenerate_revision_name": false,
"id": "locations/us-central1/namespaces/cloudskiff-dev-elie/services/cloudrun-srv",
"location": "us-central1",
"metadata": [
{
"annotations": {
"run.googleapis.com/ingress": "all",
"run.googleapis.com/ingress-status": "all",
"serving.knative.dev/creator": "elie.charra@cloudskiff.com",
"serving.knative.dev/lastModifier": "elie.charra@cloudskiff.com"
},
"generation": 1,
"labels": {
"cloud.googleapis.com/location": "us-central1"
},
"namespace": "cloudskiff-dev-elie",
"resource_version": "AAXPaYeA89Y",
"self_link": "/apis/serving.knative.dev/v1/namespaces/260375416303/services/cloudrun-srv",
"uid": "eff57385-823a-4664-b385-05465c574de2"
}
],
"name": "cloudrun-srv",
"project": "cloudskiff-dev-elie",
"status": [
{
"conditions": [
{
"message": "",
"reason": "",
"status": "True",
"type": "Ready"
},
{
"message": "",
"reason": "",
"status": "True",
"type": "ConfigurationsReady"
},
{
"message": "",
"reason": "",
"status": "True",
"type": "RoutesReady"
}
],
"latest_created_revision_name": "cloudrun-srv-9x6cp",
"latest_ready_revision_name": "cloudrun-srv-9x6cp",
"observed_generation": 1,
"url": "https://cloudrun-srv-kr2b64aapq-uc.a.run.app"
}
],
"template": [
{
"metadata": [
{
"annotations": {
"autoscaling.knative.dev/maxScale": "100"
},
"generation": 0,
"name": "",
"namespace": "",
"resource_version": "",
"self_link": "",
"uid": ""
}
],
"spec": [
{
"container_concurrency": 80,
"containers": [
{
"image": "us-docker.pkg.dev/cloudrun/container/hello",
"ports": [
{
"container_port": 8080,
"name": "http1",
"protocol": ""
}
],
"resources": [
{
"limits": {
"cpu": "1000m",
"memory": "512Mi"
}
}
],
"working_dir": ""
}
],
"service_account_name": "",
"serving_state": "",
"timeout_seconds": 300
}
]
}
],
"traffic": [
{
"latest_revision": true,
"percent": 100,
"revision_name": ""
}
]
}
}
]

View File

@ -0,0 +1,135 @@
{
"version": 4,
"terraform_version": "0.15.5",
"serial": 100,
"lineage": "0738cef4-9d69-9ccc-aebd-1177cafa0aa9",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "google_cloud_run_service",
"name": "default",
"provider": "provider[\"registry.terraform.io/hashicorp/google\"]",
"instances": [
{
"schema_version": 1,
"attributes": {
"autogenerate_revision_name": false,
"id": "locations/us-central1/namespaces/cloudskiff-dev-elie/services/cloudrun-srv",
"location": "us-central1",
"metadata": [
{
"annotations": {
"run.googleapis.com/ingress": "all",
"run.googleapis.com/ingress-status": "all",
"serving.knative.dev/creator": "elie.charra@cloudskiff.com",
"serving.knative.dev/lastModifier": "elie.charra@cloudskiff.com"
},
"generation": 1,
"labels": {
"cloud.googleapis.com/location": "us-central1"
},
"namespace": "cloudskiff-dev-elie",
"resource_version": "AAXPaYeA89Y",
"self_link": "/apis/serving.knative.dev/v1/namespaces/260375416303/services/cloudrun-srv",
"uid": "eff57385-823a-4664-b385-05465c574de2"
}
],
"name": "cloudrun-srv",
"project": "cloudskiff-dev-elie",
"status": [
{
"conditions": [
{
"message": "",
"reason": "",
"status": "True",
"type": "Ready"
},
{
"message": "",
"reason": "",
"status": "True",
"type": "ConfigurationsReady"
},
{
"message": "",
"reason": "",
"status": "True",
"type": "RoutesReady"
}
],
"latest_created_revision_name": "cloudrun-srv-9x6cp",
"latest_ready_revision_name": "cloudrun-srv-9x6cp",
"observed_generation": 1,
"url": "https://cloudrun-srv-kr2b64aapq-uc.a.run.app"
}
],
"template": [
{
"metadata": [
{
"annotations": {
"autoscaling.knative.dev/maxScale": "100"
},
"generation": 0,
"labels": {},
"name": "",
"namespace": "",
"resource_version": "",
"self_link": "",
"uid": ""
}
],
"spec": [
{
"container_concurrency": 80,
"containers": [
{
"args": null,
"command": null,
"env": [],
"env_from": [],
"image": "us-docker.pkg.dev/cloudrun/container/hello",
"ports": [
{
"container_port": 8080,
"name": "http1",
"protocol": ""
}
],
"resources": [
{
"limits": {
"cpu": "1000m",
"memory": "512Mi"
},
"requests": {}
}
],
"working_dir": ""
}
],
"service_account_name": "",
"serving_state": "",
"timeout_seconds": 300
}
]
}
],
"timeouts": null,
"traffic": [
{
"latest_revision": true,
"percent": 100,
"revision_name": ""
}
]
},
"sensitive_attributes": [],
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjozNjAwMDAwMDAwMDAsImRlbGV0ZSI6MjQwMDAwMDAwMDAwLCJ1cGRhdGUiOjkwMDAwMDAwMDAwMH0sInNjaGVtYV92ZXJzaW9uIjoiMSJ9"
}
]
}
]
}

View File

@ -0,0 +1,62 @@
package google
import (
"strings"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/remote/google/repository"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/google"
"github.com/sirupsen/logrus"
)
type GoogleCloudRunServiceEnumerator struct {
repository repository.AssetRepository
factory resource.ResourceFactory
}
func NewGoogleCloudRunServiceEnumerator(repo repository.AssetRepository, factory resource.ResourceFactory) *GoogleCloudRunServiceEnumerator {
return &GoogleCloudRunServiceEnumerator{
repository: repo,
factory: factory,
}
}
func (e *GoogleCloudRunServiceEnumerator) SupportedType() resource.ResourceType {
return google.GoogleCloudRunServiceResourceType
}
func (e *GoogleCloudRunServiceEnumerator) Enumerate() ([]*resource.Resource, error) {
subnets, err := e.repository.SearchAllCloudRunServices()
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
results := make([]*resource.Resource, 0, len(subnets))
for _, res := range subnets {
splittedName := strings.Split(res.GetName(), "/")
if len(splittedName) != 9 {
logrus.WithField("name", res.GetName()).Error("Unable to decode project from resource name")
continue
}
project := splittedName[4]
id := strings.Join([]string{
"locations", res.GetLocation(),
"namespaces", project,
"services", res.GetDisplayName(),
}, "/")
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
id,
map[string]interface{}{
"name": res.GetDisplayName(),
},
),
)
}
return results, err
}

View File

@ -97,6 +97,7 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewGoogleBigtableTableEnumerator(assetRepository, factory)) remoteLibrary.AddEnumerator(NewGoogleBigtableTableEnumerator(assetRepository, factory))
remoteLibrary.AddEnumerator(NewGoogleSQLDatabaseInstanceEnumerator(assetRepository, factory)) remoteLibrary.AddEnumerator(NewGoogleSQLDatabaseInstanceEnumerator(assetRepository, factory))
remoteLibrary.AddEnumerator(NewGoogleComputeHealthCheckEnumerator(assetRepository, factory)) remoteLibrary.AddEnumerator(NewGoogleComputeHealthCheckEnumerator(assetRepository, factory))
remoteLibrary.AddEnumerator(NewGoogleCloudRunServiceEnumerator(assetRepository, factory))
err = resourceSchemaRepository.Init(terraform.GOOGLE, provider.Version(), provider.Schema()) err = resourceSchemaRepository.Init(terraform.GOOGLE, provider.Version(), provider.Schema())
if err != nil { if err != nil {

View File

@ -32,6 +32,7 @@ const (
bigtableTableAssetType = "bigtableadmin.googleapis.com/Table" bigtableTableAssetType = "bigtableadmin.googleapis.com/Table"
sqlDatabaseInstanceAssetType = "sqladmin.googleapis.com/Instance" sqlDatabaseInstanceAssetType = "sqladmin.googleapis.com/Instance"
healthCheckAssetType = "compute.googleapis.com/HealthCheck" healthCheckAssetType = "compute.googleapis.com/HealthCheck"
cloudRunServiceAssetType = "run.googleapis.com/Service"
) )
type AssetRepository interface { type AssetRepository interface {
@ -54,6 +55,7 @@ type AssetRepository interface {
SearchAllBigtableTables() ([]*assetpb.Asset, error) SearchAllBigtableTables() ([]*assetpb.Asset, error)
SearchAllSQLDatabaseInstances() ([]*assetpb.Asset, error) SearchAllSQLDatabaseInstances() ([]*assetpb.Asset, error)
SearchAllHealthChecks() ([]*assetpb.ResourceSearchResult, error) SearchAllHealthChecks() ([]*assetpb.ResourceSearchResult, error)
SearchAllCloudRunServices() ([]*assetpb.ResourceSearchResult, error)
} }
type assetRepository struct { type assetRepository struct {
@ -134,6 +136,7 @@ func (s assetRepository) searchAllResources(ty string) ([]*assetpb.ResourceSearc
computeDiskAssetType, computeDiskAssetType,
computeImageAssetType, computeImageAssetType,
healthCheckAssetType, healthCheckAssetType,
cloudRunServiceAssetType,
}, },
} }
var results []*assetpb.ResourceSearchResult var results []*assetpb.ResourceSearchResult
@ -245,3 +248,7 @@ func (s assetRepository) SearchAllSQLDatabaseInstances() ([]*assetpb.Asset, erro
func (s assetRepository) SearchAllHealthChecks() ([]*assetpb.ResourceSearchResult, error) { func (s assetRepository) SearchAllHealthChecks() ([]*assetpb.ResourceSearchResult, error) {
return s.searchAllResources(healthCheckAssetType) return s.searchAllResources(healthCheckAssetType)
} }
func (s assetRepository) SearchAllCloudRunServices() ([]*assetpb.ResourceSearchResult, error) {
return s.searchAllResources(cloudRunServiceAssetType)
}

View File

@ -104,6 +104,29 @@ func (_m *MockAssetRepository) SearchAllBuckets() ([]*asset.ResourceSearchResult
return r0, r1 return r0, r1
} }
// SearchAllCloudRunServices provides a mock function with given fields:
func (_m *MockAssetRepository) SearchAllCloudRunServices() ([]*asset.ResourceSearchResult, error) {
ret := _m.Called()
var r0 []*asset.ResourceSearchResult
if rf, ok := ret.Get(0).(func() []*asset.ResourceSearchResult); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*asset.ResourceSearchResult)
}
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// SearchAllDNSManagedZones provides a mock function with given fields: // SearchAllDNSManagedZones provides a mock function with given fields:
func (_m *MockAssetRepository) SearchAllDNSManagedZones() ([]*asset.ResourceSearchResult, error) { func (_m *MockAssetRepository) SearchAllDNSManagedZones() ([]*asset.ResourceSearchResult, error) {
ret := _m.Called() ret := _m.Called()

View File

@ -0,0 +1,148 @@
package remote
import (
"testing"
"github.com/cloudskiff/driftctl/mocks"
"github.com/cloudskiff/driftctl/pkg/filter"
"github.com/cloudskiff/driftctl/pkg/remote/alerts"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
"github.com/cloudskiff/driftctl/pkg/remote/common"
remoteerr "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/remote/google"
"github.com/cloudskiff/driftctl/pkg/remote/google/repository"
"github.com/cloudskiff/driftctl/pkg/resource"
googleresource "github.com/cloudskiff/driftctl/pkg/resource/google"
"github.com/cloudskiff/driftctl/pkg/terraform"
testgoogle "github.com/cloudskiff/driftctl/test/google"
testresource "github.com/cloudskiff/driftctl/test/resource"
terraform2 "github.com/cloudskiff/driftctl/test/terraform"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
assetpb "google.golang.org/genproto/googleapis/cloud/asset/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestGoogleCloudRunService(t *testing.T) {
cases := []struct {
test string
response []*assetpb.ResourceSearchResult
responseErr error
setupAlerterMock func(alerter *mocks.AlerterInterface)
wantErr error
assertExpected func(t *testing.T, got []*resource.Resource)
}{
{
test: "no resource",
response: []*assetpb.ResourceSearchResult{},
wantErr: nil,
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "multiples resources",
response: []*assetpb.ResourceSearchResult{
{
AssetType: "run.googleapis.com/Service",
Name: "invalid ID", // Should be ignored
},
{
AssetType: "run.googleapis.com/Service",
DisplayName: "cloudrun-srv-1",
Name: "//run.googleapis.com/projects/cloudskiff-dev-elie/locations/us-central1/services/cloudrun-srv-1",
Location: "us-central1",
},
{
AssetType: "run.googleapis.com/Service",
DisplayName: "cloudrun-srv-2",
Name: "//run.googleapis.com/projects/cloudskiff-dev-elie/locations/us-central1/services/cloudrun-srv-2",
Location: "us-central1",
},
},
wantErr: nil,
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 2)
assert.Equal(t, got[0].ResourceId(), "locations/us-central1/namespaces/cloudskiff-dev-elie/services/cloudrun-srv-1")
assert.Equal(t, got[0].ResourceType(), googleresource.GoogleCloudRunServiceResourceType)
assert.Equal(t, got[1].ResourceId(), "locations/us-central1/namespaces/cloudskiff-dev-elie/services/cloudrun-srv-2")
assert.Equal(t, got[1].ResourceType(), googleresource.GoogleCloudRunServiceResourceType)
},
},
{
test: "should return access denied error",
wantErr: nil,
responseErr: status.Error(codes.PermissionDenied, "The caller does not have permission"),
setupAlerterMock: func(alerter *mocks.AlerterInterface) {
alerter.On(
"SendAlert",
googleresource.GoogleCloudRunServiceResourceType,
alerts.NewRemoteAccessDeniedAlert(
common.RemoteGoogleTerraform,
remoteerr.NewResourceListingError(
status.Error(codes.PermissionDenied, "The caller does not have permission"),
googleresource.GoogleCloudRunServiceResourceType,
),
alerts.EnumerationPhase,
),
).Once()
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
}
providerVersion := "3.78.0"
schemaRepository := testresource.InitFakeSchemaRepository("google", providerVersion)
googleresource.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
for _, c := range cases {
t.Run(c.test, func(tt *testing.T) {
scanOptions := ScannerOptions{}
providerLibrary := terraform.NewProviderLibrary()
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
alerter := &mocks.AlerterInterface{}
if c.setupAlerterMock != nil {
c.setupAlerterMock(alerter)
}
assetClient, err := testgoogle.NewFakeAssetServer(c.response, c.responseErr)
if err != nil {
tt.Fatal(err)
}
realProvider, err := terraform2.InitTestGoogleProvider(providerLibrary, providerVersion)
if err != nil {
tt.Fatal(err)
}
repo := repository.NewAssetRepository(assetClient, realProvider.GetConfig(), cache.New(0))
remoteLibrary.AddEnumerator(google.NewGoogleCloudRunServiceEnumerator(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
}
alerter.AssertExpectations(tt)
testFilter.AssertExpectations(tt)
if c.assertExpected != nil {
c.assertExpected(t, got)
}
})
}
}

View File

@ -0,0 +1,3 @@
package google
const GoogleCloudRunServiceResourceType = "google_cloud_run_service"

View File

@ -0,0 +1,30 @@
package google_test
import (
"testing"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/acceptance"
)
func TestAcc_Google_CloudRunService(t *testing.T) {
acceptance.Run(t, acceptance.AccTestCase{
TerraformVersion: "0.15.5",
Paths: []string{"./testdata/acc/google_cloudrun_service"},
Args: []string{
"scan",
"--to", "gcp+tf",
},
Checks: []acceptance.AccCheck{
{
Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)
}
result.AssertInfrastructureIsInSync()
result.AssertManagedCount(1)
},
},
},
})
}

View File

@ -29,6 +29,7 @@ func TestGoogle_Metadata_Flags(t *testing.T) {
GoogleComputeDiskResourceType: {}, GoogleComputeDiskResourceType: {},
GoogleBigTableInstanceResourceType: {}, GoogleBigTableInstanceResourceType: {},
GoogleComputeGlobalAddressResourceType: {}, GoogleComputeGlobalAddressResourceType: {},
GoogleCloudRunServiceResourceType: {},
} }
schemaRepository := testresource.InitFakeSchemaRepository(tf.GOOGLE, "3.78.0") schemaRepository := testresource.InitFakeSchemaRepository(tf.GOOGLE, "3.78.0")

View File

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

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/google" {
version = "3.78.0"
constraints = "3.78.0"
hashes = [
"h1:iCyTW8BWdr6Bvd5B89wkxlrB8xLxqHvT1CPmGuKembU=",
"zh:027971c4689b6130619827fe57ce260aaca060db3446817d3a92869dba7cc07f",
"zh:0876dbecc0d441bf2479edd17fe9141d77274b5071ea5f68ac26a2994bff66f3",
"zh:2a5363ed6b1b880f5284e604567cfdabecca809584c30bbe7f19ff568d1ea4cd",
"zh:2f5af69b70654bda91199f6393253e3e479107deebfeddc3fe5850b3a1e83dfb",
"zh:52e6816ef11f5f799a6626dfff384e2153b37450d8320f1ef1eee8f71a2a87b2",
"zh:59ae534607db13db35c0015c06d1ae6d4886f01f7e8fd4e07bc120236a01c494",
"zh:65ab2ed1746ea02d0b1bbd8a22ff3a95d09dc8bdb3841fbc17e45e9feccfb327",
"zh:877a71d24ff65ede3f0c5973168acfeaea0f2fea3757cab5600efcddfd3171d5",
"zh:8b10c9643a4a53148f6758bfd60804b33c2b838482f2c39ed210b729e6b1e2e8",
"zh:ba682648d9f6c11a6d04a250ac79eec39271f615f3ff60c5ae73ebfcc2cdb450",
"zh:e946561921e0279450e9b9f705de9354ce35562ed4cc0d4cd3512aa9eb1f6486",
]
}

View File

@ -0,0 +1,27 @@
provider "google" {}
terraform {
required_version = "~> 0.15.0"
required_providers {
google = {
version = "3.78.0"
}
}
}
resource "google_cloud_run_service" "default" {
name = "cloudrun-srv"
location = "us-central1"
template {
spec {
containers {
image = "us-docker.pkg.dev/cloudrun/container/hello"
}
}
}
traffic {
percent = 100
}
}

View File

@ -176,6 +176,7 @@ var supportedTypes = map[string]ResourceTypeMeta{
"google_compute_image": {}, "google_compute_image": {},
"google_compute_health_check": {}, "google_compute_health_check": {},
"google_compute_global_address": {}, "google_compute_global_address": {},
"google_cloud_run_service": {},
"azurerm_storage_account": {}, "azurerm_storage_account": {},
"azurerm_storage_container": {}, "azurerm_storage_container": {},