Skip to content

Commit

Permalink
feat: Add submodule for managing audit config (terraform-google-modul…
Browse files Browse the repository at this point in the history
…es#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
  • Loading branch information
neelesh9795 authored Apr 11, 2020
1 parent fc46920 commit 801788c
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 0 deletions.
32 changes: 32 additions & 0 deletions modules/audit_config/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

locals {
audit_log_config = {
for key, val in var.audit_log_config :
key => val
}
}

resource "google_project_iam_audit_config" "project" {
for_each = local.audit_log_config
project = var.project
service = each.value.service
audit_log_config {
log_type = each.value.log_type
exempted_members = each.value.exempted_members
}
}
20 changes: 20 additions & 0 deletions modules/audit_config/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

output "audit_log_config" {
value = var.audit_log_config
description = "Map of log type and exempted members to be added to service"
}
25 changes: 25 additions & 0 deletions modules/audit_config/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

variable "audit_log_config" {
description = "List of objects to be added to audit log config"
type = list(object({ service : string, log_type : string, exempted_members : list(string) }))
}

variable "project" {
description = "Project to add the IAM policies/bindings"
type = string
}
5 changes: 5 additions & 0 deletions test/fixtures/authoritative/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,8 @@ output "roles" {
value = tostring(var.roles)
description = "Amount of roles assigned. Useful for testing how the module behaves on updates."
}

output "audit_config" {
value = module.generic.audit_config
description = "Map of log type and exempted members to be addded to service"
}
6 changes: 6 additions & 0 deletions test/fixtures/helper/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,9 @@ module "iam_binding_pubsub_topic" {
project = var.project_id
bindings = local.basic_bindings
}

module "audit_config" {
source = "../../../modules/audit_config"
project = var.project_id
audit_log_config = local.audit_log_config
}
11 changes: 11 additions & 0 deletions test/fixtures/helper/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ locals {
bucket_roles = ["roles/storage.legacyObjectReader", "roles/storage.legacyBucketReader"]
members = [var.member1, var.member2]

audit_log_config = [{
service = "storage.googleapis.com"
log_type = "DATA_READ"
exempted_members = ["serviceAccount:${var.member1}"]
}, {
service = "allServices"
log_type = "DATA_READ"
exempted_members = ["serviceAccount:${var.member2}"]

}]

member_group_0 = [
"serviceAccount:${var.member1}",
"serviceAccount:${var.member2}",
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/helper/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,8 @@ output "project_id" {
value = var.project_id
description = "Project ID of the test fixture project. Used to avoid timing issues with recently created projects."
}

output "audit_config" {
description = "Map of log type and exempted members to be addded to service"
value = module.audit_config.audit_log_config
}
47 changes: 47 additions & 0 deletions test/integration/authoritative/controls/authoritative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
topics = attribute('topics')
subscriptions = attribute('subscriptions')
region = attribute('region')
audit_config = attribute('audit_config')

# Role pairs (arrays of length = 2)
basic_roles = attribute('basic_roles')
Expand Down Expand Up @@ -259,3 +260,49 @@
end
end
end

# Audit config

control 'audit-log-config' do
title 'Test if audit log config is correct'

describe command ("gcloud projects get-iam-policy #{project_id} --format='json(auditConfigs)'") do
its(:exit_status) { should eq 0 }
its(:stderr) { should eq '' }
let!(:data) do
if subject.exit_status == 0
JSON.parse(subject.stdout)
else
{}
end
end
describe "check auditConfigs count" do
it "has two auditConfigs" do
expect(data["auditConfigs"].length).to eq 2
end
end
describe "check members email" do
it "has correct exemptedMembers" do
data["auditConfigs"].each do |config|
expect([audit_config[0]["exempted_members"][0], audit_config[1]["exempted_members"][0]]).to include(
config["auditLogConfigs"][0]["exemptedMembers"][0]
)
end
end
end
describe "check log type " do
it "has correct log type" do
expect(data["auditConfigs"][0]["auditLogConfigs"][0]["logType"]).to eq audit_config[0]["log_type"]
end
end
describe "check services " do
it "has correct Services" do
data["auditConfigs"].each do |config|
expect([audit_config[0]["service"],audit_config[1]["service"]]).to include (
config["service"]
)
end
end
end
end
end
3 changes: 3 additions & 0 deletions test/integration/authoritative/inspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ attributes:
- name: roles
required: true
type: string
- name: audit_config
required: true
type: array

0 comments on commit 801788c

Please sign in to comment.