|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package logbucket_project |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud" |
| 22 | + "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft" |
| 23 | + "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + defaultRetentionDays int64 = 30 |
| 29 | +) |
| 30 | + |
| 31 | +func TestLogBucketProjectModule(t *testing.T) { |
| 32 | + |
| 33 | + bpt := tft.NewTFBlueprintTest(t, |
| 34 | + tft.WithTFDir("../../../examples/logbucket/project"), |
| 35 | + ) |
| 36 | + bpt.DefineVerify(func(assert *assert.Assertions) { |
| 37 | + bpt.DefaultVerify(assert) |
| 38 | + |
| 39 | + for _, tc := range []struct { |
| 40 | + projId string |
| 41 | + bktName string |
| 42 | + sinkDest string |
| 43 | + sinkProjId string |
| 44 | + sinkName string |
| 45 | + writerIdentity string |
| 46 | + }{ |
| 47 | + { |
| 48 | + projId: bpt.GetStringOutput("log_bucket_project"), |
| 49 | + bktName: bpt.GetStringOutput("log_bucket_name"), |
| 50 | + sinkDest: bpt.GetStringOutput("log_sink_destination_uri"), |
| 51 | + sinkProjId: bpt.GetStringOutput("log_sink_project_id"), |
| 52 | + sinkName: bpt.GetStringOutput("log_sink_resource_name"), |
| 53 | + writerIdentity: bpt.GetStringOutput("log_sink_writer_identity"), |
| 54 | + }, |
| 55 | + { |
| 56 | + projId: bpt.GetStringOutput("log_bkt_same_proj"), |
| 57 | + bktName: bpt.GetStringOutput("log_bkt_name_same_proj"), |
| 58 | + sinkDest: bpt.GetStringOutput("log_sink_dest_uri_same_proj"), |
| 59 | + sinkProjId: bpt.GetStringOutput("log_sink_id_same_proj"), |
| 60 | + sinkName: bpt.GetStringOutput("log_sink_resource_name_same_proj"), |
| 61 | + // writerIdentity: As sink and bucket are in same project no service account is needed and writerIdentity is empty |
| 62 | + }, |
| 63 | + } { |
| 64 | + //************************ |
| 65 | + // Assert bucket details * |
| 66 | + //************************ |
| 67 | + bktFullName := fmt.Sprintf("projects/%s/locations/%s/buckets/%s", tc.projId, "global", tc.bktName) |
| 68 | + logBucketDetails := gcloud.Runf(t, fmt.Sprintf("logging buckets describe %s --location=%s --project=%s", tc.bktName, "global", tc.projId)) |
| 69 | + |
| 70 | + // assert log bucket name, retention days & location |
| 71 | + assert.Equal(bktFullName, logBucketDetails.Get("name").String(), "log bucket name should match") |
| 72 | + assert.Equal(defaultRetentionDays, logBucketDetails.Get("retentionDays").Int(), "retention days should match") |
| 73 | + |
| 74 | + logSinkDetails := gcloud.Runf(t, fmt.Sprintf("logging sinks describe %s --project=%s", tc.sinkName, tc.sinkProjId)) |
| 75 | + |
| 76 | + // assert log sink name, destination & filter |
| 77 | + assert.Equal(tc.sinkDest, logSinkDetails.Get("destination").String(), "log sink destination should match") |
| 78 | + assert.Equal("resource.type = gce_instance", logSinkDetails.Get("filter").String(), "log sink filter should match") |
| 79 | + assert.Equal(tc.writerIdentity, logSinkDetails.Get("writerIdentity").String(), "log sink writerIdentity should match") |
| 80 | + } |
| 81 | + |
| 82 | + //***************************** |
| 83 | + // Assert SAs and Permissions * |
| 84 | + //***************************** |
| 85 | + bktDestProjId := bpt.GetStringOutput("log_bkt_same_proj") |
| 86 | + sinkWriterIdentity := bpt.GetStringOutput("log_sink_writer_identity") |
| 87 | + |
| 88 | + projPermissionsDetails := gcloud.Runf(t, fmt.Sprintf("projects get-iam-policy %s", bktDestProjId)) |
| 89 | + listMembers := utils.GetResultStrSlice(projPermissionsDetails.Get("bindings.#(role==\"roles/logging.bucketWriter\").members").Array()) |
| 90 | + |
| 91 | + // assert sink writer identity service account permission |
| 92 | + assert.Contains(listMembers, sinkWriterIdentity, "log sink writer identity permission should match") |
| 93 | + assert.Len(listMembers, 1, "only one writer identity should have logbucket write permission") |
| 94 | + }) |
| 95 | + bpt.Test() |
| 96 | +} |
0 commit comments