-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform.go
409 lines (356 loc) · 12.6 KB
/
terraform.go
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
// 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.
package apply
import (
"encoding/json"
"errors"
"fmt"
"log"
"os/exec"
"github.com/GoogleCloudPlatform/healthcare/deploy/config"
"github.com/GoogleCloudPlatform/healthcare/deploy/config/tfconfig"
"github.com/GoogleCloudPlatform/healthcare/deploy/runner"
"github.com/GoogleCloudPlatform/healthcare/deploy/terraform"
)
// Terraform applies the project configs for each applicable project in the config to GCP.
// Empty list of projects is equivalent to all projects.
// Base projects (remote audit and forseti) are deployed in a round-table fashion with each phase being applied to each project.
// This makes sure dependencies between base projects are handled correctly
// (e.g. forseti project wants to store its audit logs in remote audit project while remote audit project wants to be monitored by the forseti project).
// Then, all data hosting projects are deployed from beginning till end so one data project doesn't leave other data projects in a half deployed state.
func Terraform(conf *config.Config, projectIDs []string, opts *Options, rn runner.Runner) error {
idSet := make(map[string]bool)
for _, p := range projectIDs {
idSet[p] = true
}
wantProject := func(p *config.Project) bool {
return p != nil && (len(projectIDs) == 0 || idSet[p.ID])
}
var baseProjects, dataProjects []*config.Project
if conf.Devops != nil && wantProject(conf.Devops.Project) {
baseProjects = append(baseProjects, conf.Devops.Project)
}
if wantProject(conf.AuditLogsProject) {
baseProjects = append(baseProjects, conf.AuditLogsProject)
}
if conf.Forseti != nil && wantProject(conf.Forseti.Project) {
baseProjects = append(baseProjects, conf.Forseti.Project)
}
for _, p := range conf.Projects {
if wantProject(p) {
dataProjects = append(dataProjects, p)
}
}
if len(baseProjects) > 0 {
// Apply all base projects in a round-robin fashion.
log.Println("Applying base projects")
if err := projects(conf, baseProjects, opts, rn); err != nil {
return fmt.Errorf("failed to apply base projects: %v", err)
}
}
// Apply each data hosting project from beginning to end.
for _, p := range dataProjects {
log.Printf("Applying project %q", p.ID)
if err := projects(conf, []*config.Project{p}, opts, rn); err != nil {
return fmt.Errorf("failed to apply %q: %v", p.ID, err)
}
}
return nil
}
// projects applies phases (one or more steps) to the given projects in a round-robin fashion.
// The purpose behind the current process is to ensure dependencies between base projects are setup properly.
func projects(conf *config.Config, projs []*config.Project, opts *Options, rn runner.Runner) error {
for _, p := range projs {
log.Printf("Creating project and state bucket for %q", p.ID)
workDir, err := terraform.WorkDir(opts.TerraformConfigsPath, p.ID)
if err != nil {
return err
}
if err := createProjectTerraform(conf, p, opts, workDir, rn); err != nil {
return err
}
}
for _, p := range projs {
log.Printf("Appling project %q", p.ID)
workDir, err := terraform.WorkDir(opts.TerraformConfigsPath, p.ID)
if err != nil {
return err
}
if err := services(p, opts, workDir, rn); err != nil {
return fmt.Errorf("failed to apply services: %v", err)
}
if err := createStackdriverAccount(p, rn); err != nil {
return err
}
if err := resources(p, opts, workDir, rn); err != nil {
return fmt.Errorf("failed to apply resources: %v", err)
}
// TODO: the user being used for terraform is actually set by gcloud auth application-default login.
// This user can differ than the one set by gcloud auth login which is what removeOwnerUser checks for.
// Fix this to remove the application-default owner.
if err := removeOwnerUser(p, rn); err != nil {
return fmt.Errorf("failed to remove authenticated user: %v", err)
}
if err := collectGCEInfo(p, rn); err != nil {
return fmt.Errorf("failed to collect GCE instances info: %v", err)
}
}
for _, p := range projs {
log.Printf("Applying audit resources for %q", p.ID)
workDir, err := terraform.WorkDir(opts.TerraformConfigsPath, p.ID)
if err != nil {
return err
}
if err := auditResources(p, opts, workDir, rn); err != nil {
return fmt.Errorf("failed to apply audit resources for %q: %v", p.ID, err)
}
}
if conf.Forseti == nil {
return nil
}
for _, p := range projs {
// Deploy the forseti instance if forseti project is being requested.
if conf.Forseti.Project.ID == p.ID {
log.Printf("Applying Forseti instance in %q", conf.Forseti.Project.ID)
workDir, err := terraform.WorkDir(opts.TerraformConfigsPath, p.ID)
if err != nil {
return err
}
if err := forsetiConfig(conf, opts, workDir, rn); err != nil {
return fmt.Errorf("failed to apply forseti instance in %q: %v", conf.Forseti.Project.ID, err)
}
break
}
}
fsa := conf.AllGeneratedFields.Forseti.ServiceAccount
if fsa == "" {
return errors.New("forseti service account not set in generated fields")
}
for _, p := range projs {
log.Printf("Granting Forseti instance access to project %q", p.ID)
workDir, err := terraform.WorkDir(opts.TerraformConfigsPath, p.ID)
if err != nil {
return err
}
if err := GrantForsetiPermissions(p.ID, fsa, p.DevopsConfig.StateBucket.Name, opts, workDir, rn); err != nil {
return fmt.Errorf("failed to grant forseti access to project %q: %v", p.ID, err)
}
}
return nil
}
func createProjectTerraform(config *config.Config, project *config.Project, opts *Options, workDir string, rn runner.Runner) error {
fid := project.FolderID
if fid == "" {
fid = config.Overall.FolderID
}
// Only one of folder ID or org ID can be set in the project resource.
var oid string
if fid == "" {
oid = config.Overall.OrganizationID
}
ba := project.BillingAccount
if ba == "" {
ba = config.Overall.BillingAccount
}
pr := &tfconfig.ProjectResource{
OrgID: oid,
FolderID: fid,
BillingAccount: ba,
}
if err := pr.Init(project.ID); err != nil {
return err
}
tfConf := terraform.NewConfig()
if err := addResources(tfConf, pr, project.DevopsConfig.StateBucket); err != nil {
return err
}
tfConf.Outputs = []*terraform.Output{{
Name: "project_number",
Value: "${google_project.project.number}",
}}
tfOpts := &terraform.Options{ApplyFlags: opts.TerraformApplyFlags}
// Since the project creation deployment is not linked to a remote state, always import the project and state bucket.
if err := addImports(tfOpts, rn, pr, project.DevopsConfig.StateBucket); err != nil {
return err
}
dir, err := terraform.WorkDir(workDir, "project")
if err != nil {
return err
}
if err := terraformApply(tfConf, dir, tfOpts, rn); err != nil {
return err
}
if project.GeneratedFields.ProjectNumber != "" {
return nil
}
// Output in JSON format so we don't need to handle extra spacing around the output.
pnCmd := exec.Command("terraform", "output", "-json", "project_number")
pnCmd.Dir = dir
out, err := rn.CmdOutput(pnCmd)
if err != nil {
return fmt.Errorf("failed to get terraform project number: %v", err)
}
var pn string
if err := json.Unmarshal(out, &pn); err != nil {
return fmt.Errorf("failed to parse project number from terraform output: %v", err)
}
project.GeneratedFields.ProjectNumber = pn
return nil
}
func services(project *config.Project, opts *Options, workDir string, rn runner.Runner) error {
tfConf := terraform.NewConfig()
tfConf.Terraform.Backend = &terraform.Backend{
Bucket: project.DevopsConfig.StateBucket.Name,
Prefix: "services",
}
// Services don't need to be imported since they can be re-created even if they already exist.
if err := addResources(tfConf, project.Services); err != nil {
return err
}
dir, err := terraform.WorkDir(workDir, "services")
if err != nil {
return err
}
return terraformApply(tfConf, dir, &terraform.Options{ApplyFlags: opts.TerraformApplyFlags}, rn)
}
func auditResources(project *config.Project, opts *Options, workDir string, rn runner.Runner) error {
tfConf := terraform.NewConfig()
tfConf.Terraform.Backend = &terraform.Backend{
Bucket: project.DevopsConfig.StateBucket.Name,
Prefix: "audit",
}
d := project.Audit.LogsBigqueryDataset
d.Accesses = append(d.Accesses, &tfconfig.Access{
Role: "WRITER",
// Trim "serviceAccount:" prefix as user_by_email does not allow it.
UserByEmail: fmt.Sprintf(
`${replace(google_logging_project_sink.%s.writer_identity, "serviceAccount:", "")}`,
project.BQLogSinkTF.ID(),
),
})
rs := []tfconfig.Resource{project.BQLogSinkTF, d}
if b := project.Audit.LogsStorageBucket; b != nil {
rs = append(rs, b)
}
if err := addResources(tfConf, rs...); err != nil {
return err
}
tfOpts := &terraform.Options{ApplyFlags: opts.TerraformApplyFlags}
if opts.ImportExisting {
if err := addImports(tfOpts, rn, rs...); err != nil {
return err
}
}
dir, err := terraform.WorkDir(workDir, "audit")
if err != nil {
return err
}
if err := terraformApply(tfConf, dir, tfOpts, rn); err != nil {
return err
}
project.GeneratedFields.LogSinkServiceAccount, err = getLogSinkServiceAccount(project, project.BQLogSinkTF.Name, rn)
if err != nil {
return fmt.Errorf("failed to get log sink service account: %v", err)
}
return nil
}
func resources(project *config.Project, opts *Options, workDir string, rn runner.Runner) error {
rs := project.TerraformResources()
tfConf := terraform.NewConfig()
tfConf.Providers = append(tfConf.Providers,
&terraform.Provider{
Name: "google",
// Needed to work around issues like https://github.com/terraform-providers/terraform-provider-google/issues/4460.
// Also used if a resource does not explicitly set the project field.
Properties: map[string]interface{}{"project": project.ID},
},
// Beta provider needed for some resources such as healthcare resources.
&terraform.Provider{
Name: "google-beta",
Properties: map[string]interface{}{"project": project.ID},
},
)
tfConf.Terraform.Backend = &terraform.Backend{
Bucket: project.DevopsConfig.StateBucket.Name,
Prefix: "resources",
}
// Allow user resources to access project runtime info such as project number.
tfConf.Data = []*terraform.Resource{{
Name: project.ID,
Type: "google_project",
Properties: map[string]interface{}{
"project_id": project.ID,
},
}}
if err := addResources(tfConf, rs...); err != nil {
return err
}
tfOpts := &terraform.Options{ApplyFlags: opts.TerraformApplyFlags, CustomConfig: project.TerraformDeployments.Resources.Config}
if opts.ImportExisting {
if err := addImports(tfOpts, rn, rs...); err != nil {
return err
}
}
dir, err := terraform.WorkDir(workDir, "resources")
if err != nil {
return err
}
return terraformApply(tfConf, dir, tfOpts, rn)
}
type dependerTF interface {
DependentResources() []tfconfig.Resource
}
// addResources adds the given resources to the given terraform config.
func addResources(config *terraform.Config, resources ...tfconfig.Resource) error {
for _, r := range resources {
config.Resources = append(config.Resources, &terraform.Resource{
Name: r.ID(),
Type: r.ResourceType(),
Properties: r,
})
if d, ok := r.(dependerTF); ok {
if err := addResources(config, d.DependentResources()...); err != nil {
return err
}
}
}
return nil
}
// addImports updates the terraform options with the given resources' import IDs, so the resources are imported to the terraform state if they already exist.
// The import ID for a resource can be found in terraform's documentation for the resource.
func addImports(opts *terraform.Options, rn runner.Runner, resources ...tfconfig.Resource) error {
type importer interface {
ImportID(rn runner.Runner) (string, error)
}
for _, r := range resources {
if i, ok := r.(importer); ok {
id, err := i.ImportID(rn)
if err != nil {
return fmt.Errorf("failed to get import ID for %q %q: %v", r.ResourceType(), r.ID(), err)
}
if id != "" {
opts.Imports = append(opts.Imports, terraform.Import{
Address: fmt.Sprintf("%s.%s", r.ResourceType(), r.ID()),
ID: id,
})
}
}
if d, ok := r.(dependerTF); ok {
if err := addImports(opts, rn, d.DependentResources()...); err != nil {
return err
}
}
}
return nil
}