Skip to content

Commit 801788c

Browse files
authored
feat: Add submodule for managing audit config (#82)
* Support for audit log config added * Fixed build errors * Chnages to the audit config * Change to the helper main * Added for_each instead of count
1 parent fc46920 commit 801788c

File tree

9 files changed

+154
-0
lines changed

9 files changed

+154
-0
lines changed

modules/audit_config/main.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
locals {
18+
audit_log_config = {
19+
for key, val in var.audit_log_config :
20+
key => val
21+
}
22+
}
23+
24+
resource "google_project_iam_audit_config" "project" {
25+
for_each = local.audit_log_config
26+
project = var.project
27+
service = each.value.service
28+
audit_log_config {
29+
log_type = each.value.log_type
30+
exempted_members = each.value.exempted_members
31+
}
32+
}

modules/audit_config/outputs.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "audit_log_config" {
18+
value = var.audit_log_config
19+
description = "Map of log type and exempted members to be added to service"
20+
}

modules/audit_config/variables.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "audit_log_config" {
18+
description = "List of objects to be added to audit log config"
19+
type = list(object({ service : string, log_type : string, exempted_members : list(string) }))
20+
}
21+
22+
variable "project" {
23+
description = "Project to add the IAM policies/bindings"
24+
type = string
25+
}

test/fixtures/authoritative/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,8 @@ output "roles" {
120120
value = tostring(var.roles)
121121
description = "Amount of roles assigned. Useful for testing how the module behaves on updates."
122122
}
123+
124+
output "audit_config" {
125+
value = module.generic.audit_config
126+
description = "Map of log type and exempted members to be addded to service"
127+
}

test/fixtures/helper/iam.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,9 @@ module "iam_binding_pubsub_topic" {
9999
project = var.project_id
100100
bindings = local.basic_bindings
101101
}
102+
103+
module "audit_config" {
104+
source = "../../../modules/audit_config"
105+
project = var.project_id
106+
audit_log_config = local.audit_log_config
107+
}

test/fixtures/helper/main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ locals {
2424
bucket_roles = ["roles/storage.legacyObjectReader", "roles/storage.legacyBucketReader"]
2525
members = [var.member1, var.member2]
2626

27+
audit_log_config = [{
28+
service = "storage.googleapis.com"
29+
log_type = "DATA_READ"
30+
exempted_members = ["serviceAccount:${var.member1}"]
31+
}, {
32+
service = "allServices"
33+
log_type = "DATA_READ"
34+
exempted_members = ["serviceAccount:${var.member2}"]
35+
36+
}]
37+
2738
member_group_0 = [
2839
"serviceAccount:${var.member1}",
2940
"serviceAccount:${var.member2}",

test/fixtures/helper/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,8 @@ output "project_id" {
114114
value = var.project_id
115115
description = "Project ID of the test fixture project. Used to avoid timing issues with recently created projects."
116116
}
117+
118+
output "audit_config" {
119+
description = "Map of log type and exempted members to be addded to service"
120+
value = module.audit_config.audit_log_config
121+
}

test/integration/authoritative/controls/authoritative.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
topics = attribute('topics')
3131
subscriptions = attribute('subscriptions')
3232
region = attribute('region')
33+
audit_config = attribute('audit_config')
3334

3435
# Role pairs (arrays of length = 2)
3536
basic_roles = attribute('basic_roles')
@@ -259,3 +260,49 @@
259260
end
260261
end
261262
end
263+
264+
# Audit config
265+
266+
control 'audit-log-config' do
267+
title 'Test if audit log config is correct'
268+
269+
describe command ("gcloud projects get-iam-policy #{project_id} --format='json(auditConfigs)'") do
270+
its(:exit_status) { should eq 0 }
271+
its(:stderr) { should eq '' }
272+
let!(:data) do
273+
if subject.exit_status == 0
274+
JSON.parse(subject.stdout)
275+
else
276+
{}
277+
end
278+
end
279+
describe "check auditConfigs count" do
280+
it "has two auditConfigs" do
281+
expect(data["auditConfigs"].length).to eq 2
282+
end
283+
end
284+
describe "check members email" do
285+
it "has correct exemptedMembers" do
286+
data["auditConfigs"].each do |config|
287+
expect([audit_config[0]["exempted_members"][0], audit_config[1]["exempted_members"][0]]).to include(
288+
config["auditLogConfigs"][0]["exemptedMembers"][0]
289+
)
290+
end
291+
end
292+
end
293+
describe "check log type " do
294+
it "has correct log type" do
295+
expect(data["auditConfigs"][0]["auditLogConfigs"][0]["logType"]).to eq audit_config[0]["log_type"]
296+
end
297+
end
298+
describe "check services " do
299+
it "has correct Services" do
300+
data["auditConfigs"].each do |config|
301+
expect([audit_config[0]["service"],audit_config[1]["service"]]).to include (
302+
config["service"]
303+
)
304+
end
305+
end
306+
end
307+
end
308+
end

test/integration/authoritative/inspec.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,6 @@ attributes:
7474
- name: roles
7575
required: true
7676
type: string
77+
- name: audit_config
78+
required: true
79+
type: array

0 commit comments

Comments
 (0)