-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
480 lines (370 loc) · 15.2 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# Required Providers
# https://www.terraform.io/docs/language/providers/requirements.html#requiring-providers
terraform {
required_providers {
github = {
source = "integrations/github"
}
# Random Provider
# https://registry.terraform.io/providers/hashicorp/random/latest/docs
random = {
source = "hashicorp/random"
}
# Time Provider
# https://registry.terraform.io/providers/hashicorp/time/latest/docs
time = {
source = "hashicorp/time"
}
}
}
# Github Provider
# https://registry.terraform.io/providers/integrations/github/latest/docs
# Some API operations may not be available when using a GitHub App installation configuration. For more information, refer to the list of
# supported operations: https://docs.github.com/en/rest/authentication/endpoints-available-for-github-app-installation-access-tokens
# provider "github" {
# app_auth {
# id = "1081373"
# installation_id = "58130651"
# pem_file = base64decode(var.app_pem_file_base64)
# }
# owner = "osinfra-io"
# }
provider "github" {
token = var.token
owner = "osinfra-io"
}
# GitHub Application Data Source
# https://registry.terraform.io/providers/integrations/github/latest/docs/data-sources/app
# Currently private GitHub Apps can only use this endpoint on themselves and if we use a private GitHub App for authentication, we can't
# use this data source to get the node_id of another GitHub App.
data "github_app" "pr_approve_and_merge_osinfra_io" {
slug = "pr-approve-and-merge-osinfra-io"
}
# Template File Data Source
# https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file
data "template_file" "security_policy" {
for_each = var.repositories
template = file("${path.module}/markdown/SECURITY.md.tpl")
vars = {
repository = each.key
}
}
# GitHub Actions Organization Permissions Resource
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/actions_organization_permissions
resource "github_actions_organization_permissions" "this" {
allowed_actions = "selected"
allowed_actions_config {
github_owned_allowed = true
patterns_allowed = [
"datadog/*",
"dependabot/*",
"docker/*",
"github/*",
"githubsecuritylab/*",
"google-github-actions/*",
"hashicorp/*",
"infracost/*",
"open-policy-agent/*"
]
verified_allowed = false
}
enabled_repositories = "all"
}
# Github Actions Secret Resource
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/actions_organization_secret
resource "github_actions_organization_secret" "this" {
# Ensure GitHub Actions secrets are encrypted
# checkov:skip=CKV_GIT_4: We are passing the secret from the random_password resource which is sensitive by default
# and not checking in any plain text values. State is always secured.
for_each = var.organization_secrets
plaintext_value = random_password.this[each.key].result
secret_name = each.key
visibility = each.value.visibility
}
# Github Branch Protection Resource
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch_protection
resource "github_branch_protection" "this" {
# GitHub pull requests should require at least 2 approvals
# checkov:skip=CKV_GIT_5: 1 approval is reasonable for a small team
for_each = local.branch_protections
enforce_admins = false
pattern = "main"
repository_id = github_repository.this[each.key].name
require_conversation_resolution = true
required_linear_history = true
require_signed_commits = true
required_pull_request_reviews {
dismiss_stale_reviews = true
require_code_owner_reviews = true
required_approving_review_count = 1
}
required_status_checks {
contexts = each.value.required_status_checks_contexts
strict = true
}
restrict_pushes {
push_allowances = concat(
each.value.push_allowances,
[
data.github_app.pr_approve_and_merge_osinfra_io.node_id
]
)
}
}
# GitHub Issue Labels Resource
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/issue_label
resource "github_issue_label" "this" {
for_each = local.repository_labels
name = each.value.name
color = each.value.color
description = each.value.description
repository = github_repository.this[each.value.repository].name
}
# GitHub Membership Resource
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/membership
resource "github_membership" "this" {
for_each = local.users
role = each.value
username = each.key
}
# Github Organization Security Manager Resource
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/organization_security_manager
resource "github_organization_security_manager" "this" {
team_slug = github_team.parents["enabling-security"].slug
}
# Github Organization Settings Resource
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/organization_settings
resource "github_organization_settings" "this" {
# If you need to import the organization settings, you can do so with the following command:
# terraform import github_organization_settings.this <org_id>
# To get the organization id, you can run the following curl command with a token that has the read:org scope against your existing organization.
# curl -H "Authorization: token $GITHUB_READ_ORG_TOKEN" https://api.github.com/orgs/osinfra-io
billing_email = "brett@osinfra.io"
blog = "https://osinfra.io"
company = "Open Source Infrastructure (as Code)"
default_repository_permission = "read"
dependabot_alerts_enabled_for_new_repositories = true
dependency_graph_enabled_for_new_repositories = true
dependabot_security_updates_enabled_for_new_repositories = true
description = "Open Source Infrastructure (as Code)"
email = "help@osinfra.io"
has_organization_projects = true
has_repository_projects = true
location = "United States of America"
members_can_create_internal_repositories = false
members_can_create_pages = false
members_can_create_private_pages = false
members_can_create_private_repositories = true
members_can_create_public_pages = false
members_can_create_public_repositories = true
members_can_create_repositories = true
members_can_fork_private_repositories = false
name = "osinfra.io (Alpha)"
secret_scanning_enabled_for_new_repositories = true
secret_scanning_push_protection_enabled_for_new_repositories = true
web_commit_signoff_required = false
}
# Github Repository Resource
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository
resource "github_repository" "this" {
# Ensure GitHub repository is Private
# checkov:skip=CKV_GIT_1: Public is ok for us since we are an open source project
for_each = var.repositories
allow_auto_merge = true
allow_merge_commit = false
allow_rebase_merge = false
allow_squash_merge = true
allow_update_branch = true
archive_on_destroy = true
delete_branch_on_merge = true
description = each.value.description
has_downloads = false
has_discussions = each.value.has_discussions
has_issues = true
has_projects = true
has_wiki = false
homepage_url = "https://www.osinfra.io"
is_template = each.value.is_template
license_template = "gpl-2.0"
name = each.key
squash_merge_commit_message = "BLANK"
squash_merge_commit_title = "PR_TITLE"
topics = each.value.topics
visibility = each.value.visibility
vulnerability_alerts = true
dynamic "template" {
for_each = each.value.template != null ? [each.value.template] : []
content {
owner = "osinfra-io"
repository = template.value
}
}
}
# GitHub Repository File Resource
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_file
resource "github_repository_file" "release" {
for_each = var.repositories
branch = "main"
content = file("${path.module}/markdown/release.yml")
file = ".github/release.yml"
repository = each.key
commit_message = "Update .github/release.yml"
commit_author = "Open Source Infrastructure as Code Service Account"
commit_email = "github-sa@osinfra.io"
overwrite_on_create = true
depends_on = [
github_branch_protection.this
]
}
resource "github_repository_file" "security_policy" {
for_each = var.repositories
branch = "main"
content = data.template_file.security_policy[each.key].rendered
file = "SECURITY.md"
repository = each.key
commit_message = "Update SECURITY.md"
commit_author = "Open Source Infrastructure as Code Service Account"
commit_email = "github-sa@osinfra.io"
overwrite_on_create = true
depends_on = [
github_branch_protection.this
]
}
# Github Repository Webhook Resource
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_webhook
resource "github_repository_webhook" "discord" {
for_each = local.discord_webhooks
active = true
configuration {
content_type = "json"
insecure_ssl = false
url = "https://discord.com/api/webhooks/1175823442415722517/${var.discord_webhook_api_key}/github"
}
events = ["*"]
repository = each.key
depends_on = [
github_repository.this
]
}
resource "github_repository_webhook" "datadog" {
for_each = local.datadog_webhooks
active = true
configuration {
content_type = "json"
insecure_ssl = false
url = "https://app.datadoghq.com/intake/webhook/github?api_key=${var.datadog_webhook_api_key}"
}
events = [
"commit_comment", # This event occurs when there is activity relating to commit comments.
"create", # This event occurs when a Git branch or tag is created.
"issue_comment", # This event occurs when there is activity relating to a comment on an issue or pull request.
"issues", # This event occurs when there is activity relating to an issue.
"pull_request", # This event occurs when there is activity on a pull request.
"pull_request_review_comment", # This event occurs when there is activity relating to a pull request review comment.
"push", # This event occurs when there is a push to a repository branch.
"repository", # This event occurs when there is activity relating to repositories.
"security_and_analysis", # This event occurs when code security and analysis features are enabled or disabled for a repository.
"team_add" # This event occurs when a team is added to a repository.
]
repository = each.key
depends_on = [
github_repository.this
]
}
# Github Team Resource
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team
# If you need to import a team, you can do so with the following command:
# terraform import github_team.this\[\"google-cloud-platform\"\] <team_id>
# To get the team ids, you can run the following curl command with a token that has the read:org scope against your own organization.
# curl -H "Authorization: token $GITHUB_READ_ORG_TOKEN" https://api.github.com/orgs/osinfra-io/teams
resource "github_team" "parents" {
for_each = var.team_parents
name = each.key
description = each.value.description
privacy = each.value.privacy
}
resource "github_team" "children" {
for_each = var.team_children
description = each.value.description
name = each.key
parent_team_id = github_team.parents[each.value.parent_team_key].id
privacy = github_team.parents[each.value.parent_team_key].privacy
}
# GitHub Team Membership Resource
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team_members
resource "github_team_members" "parents" {
for_each = var.team_parents
team_id = github_team.parents[each.key].id
dynamic "members" {
for_each = each.value.members
content {
username = members.value
role = "member"
}
}
dynamic "members" {
for_each = each.value.maintainers
content {
username = members.value
role = "maintainer"
}
}
}
resource "github_team_members" "children" {
for_each = var.team_children
team_id = github_team.children[each.key].id
dynamic "members" {
for_each = each.value.members
content {
username = members.value
role = "member"
}
}
dynamic "members" {
for_each = each.value.maintainers
content {
username = members.value
role = "maintainer"
}
}
}
# Github Team Repository Resource
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team_repository
resource "github_team_repository" "children" {
for_each = local.child_team_repositories
team_id = github_team.children[each.value.team_child].id
repository = github_repository.this[each.value.repository].name
permission = each.value.permission
}
resource "github_team_repository" "parents" {
for_each = local.parent_team_repositories
team_id = github_team.parents[each.value.team_parent].id
repository = github_repository.this[each.value.repository].name
permission = each.value.permission
}
# GitHub Team Settings Resource
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team_settings
resource "github_team_settings" "this" {
for_each = local.review_request_delegations
review_request_delegation {
algorithm = "LOAD_BALANCE"
member_count = 2
notify = false
}
team_id = github_team.parents[each.key].id
}
# Random Password Resource
# https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password
resource "random_password" "this" {
for_each = var.organization_secrets
length = 32
special = false
keepers = {
rotation_time = time_rotating.this.rotation_rfc3339
}
}
# Time Rotating Resource
# https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/rotating
resource "time_rotating" "this" {
rotation_days = 5
}