-
Notifications
You must be signed in to change notification settings - Fork 3
/
provider.go
334 lines (299 loc) · 12 KB
/
provider.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
package chronicle
import (
"context"
"fmt"
"time"
chronicle "github.com/form3tech-oss/terraform-provider-chronicle/client"
"github.com/form3tech-oss/terraform-provider-chronicle/version"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func Provider() *schema.Provider {
provider := &schema.Provider{
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validateRegion,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"CHRONICLE_REGION",
}, chronicle.RegionEurope),
Description: fmt.Sprintf(`Region to which send requests, available regions are: %v. It may be replaced by CHRONICLE_REGION environment variable.`, chronicle.Regions),
},
"bigqueryapi_credentials": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validateCredentials,
ConflictsWith: []string{"bigqueryapi_access_token"},
Description: `BigQuery API crendential. Local file path or content.
It may be replaced by CHRONICLE_BIGQUERY_CREDENTIALS environment variable, which expects base64 encoded credential.`,
},
"bigqueryapi_access_token": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"bigqueryapi_credentials"},
Description: `BigQuery API access token. Local file path or content.`,
},
"backstoryapi_credentials": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validateCredentials,
ConflictsWith: []string{"backstoryapi_access_token"},
Description: `Backstory API credential. Local file path or content.
It may be replaced by CHRONICLE_BACKSTORY_CREDENTIALS environment variable, which expects base64 encoded credential.`,
},
"backstoryapi_access_token": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"backstoryapi_credentials"},
Description: `Backstory API access token. Local file path or content.`,
},
"ingestionapi_credentials": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validateCredentials,
ConflictsWith: []string{"ingestionapi_access_token"},
Description: `Ingestion API crendential. Local file path or content.
It may be replaced by CHRONICLE_INGESTION_CREDENTIALS environment variable, which expects base64 encoded credential.`,
},
"ingestionapi_access_token": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"ingestionapi_credentials"},
Description: `Ingestion API access token. Local file path or content.`,
},
"forwarderapi_credentials": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validateCredentials,
ConflictsWith: []string{"forwarderapi_access_token"},
Description: `Forwarder API crendential. Local file path or content.
It may be replaced by CHRONICLE_FORWARDER_CREDENTIALS environment variable, which expects base64 encoded credential.`,
},
"forwarderapi_access_token": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"forwarderapi_credentials"},
Description: `Forwarder API Access token. Local file path or content.`,
},
"request_timeout": {
Type: schema.TypeInt,
Optional: true,
Description: `Request timeout in seconds. Defaults to 120 (s).`,
Default: 120,
},
"request_attempts": {
Type: schema.TypeInt,
Optional: true,
Description: `Number of attempts per request. Attempts follow exponential back-off strategy. Defaults to 5 attempts.`,
Default: 5,
},
"events_custom_endpoint": {
Type: schema.TypeString,
Optional: true,
Description: `Custom URL to events endpoint.`,
ValidateDiagFunc: validateCustomEndpoint,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"CHRONICLE_EVENTS_CUSTOM_ENDPOINT",
}, nil),
},
"alert_custom_endpoint": {
Type: schema.TypeString,
Optional: true,
Description: `Custom URL to alert endpoint.`,
ValidateDiagFunc: validateCustomEndpoint,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"CHRONICLE_ALERT_CUSTOM_ENDPOINT",
}, nil),
},
"artifact_custom_endpoint": {
Type: schema.TypeString,
Optional: true,
Description: `Custom URL to artifact endpoint.`,
ValidateDiagFunc: validateCustomEndpoint,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"CHRONICLE_ARTIFACT_CUSTOM_ENDPOINT",
}, nil),
},
"alias_custom_endpoint": {
Type: schema.TypeString,
Optional: true,
Description: `Custom URL to alias endpoint.`,
ValidateDiagFunc: validateCustomEndpoint,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"CHRONICLE_ALIAS_CUSTOM_ENDPOINT",
}, nil),
},
"asset_custom_endpoint": {
Type: schema.TypeString,
Optional: true,
Description: `Custom URL to asset endpoint.`,
ValidateDiagFunc: validateCustomEndpoint,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"CHRONICLE_ASSET_CUSTOM_ENDPOINT",
}, nil),
},
"ioc_custom_endpoint": {
Type: schema.TypeString,
Optional: true,
Description: `Custom URL to ioc endpoint.`,
ValidateDiagFunc: validateCustomEndpoint,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"CHRONICLE_IOC_CUSTOM_ENDPOINT",
}, nil),
},
"rule_custom_endpoint": {
Type: schema.TypeString,
Optional: true,
Description: `Custom URL to rule endpoint.`,
ValidateDiagFunc: validateCustomEndpoint,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"CHRONICLE_RULE_CUSTOM_ENDPOINT",
}, nil),
},
"feed_custom_endpoint": {
Type: schema.TypeString,
Optional: true,
Description: `Custom URL to feed endpoint.`,
ValidateDiagFunc: validateCustomEndpoint,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"CHRONICLE_FEED_CUSTOM_ENDPOINT",
}, nil),
},
"subjects_custom_endpoint": {
Type: schema.TypeString,
Optional: true,
Description: `Custom URL to subjects endpoint.`,
ValidateDiagFunc: validateCustomEndpoint,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"CHRONICLE_SUBJECTS_CUSTOM_ENDPOINT",
}, nil),
},
},
DataSourcesMap: map[string]*schema.Resource{},
ResourcesMap: map[string]*schema.Resource{
"chronicle_rbac_subject": resourceRBACSubject(),
"chronicle_rule": resourceRule(),
"chronicle_reference_list": resourceReferenceList(),
"chronicle_feed_amazon_s3": NewResourceFeedAmazonS3().TerraformResource,
"chronicle_feed_amazon_sqs": NewResourceFeedAmazonSQS().TerraformResource,
"chronicle_feed_qualys_vm": NewResourceFeedQualysVM().TerraformResource,
"chronicle_feed_microsoft_office_365_management_activity": NewResourceFeedMicrosoftOffice365ManagementActivity().TerraformResource,
"chronicle_feed_okta_system_log": NewResourceFeedOktaSystemLog().TerraformResource,
"chronicle_feed_okta_users": NewResourceFeedOktaUsers().TerraformResource,
"chronicle_feed_proofpoint_siem": NewResourceFeedProofpointSIEM().TerraformResource,
"chronicle_feed_google_cloud_storage_bucket": NewResourceFeedGoogleCloudStorageBucket().TerraformResource,
"chronicle_feed_azure_blobstore": NewResourceFeedAzureBlobStore().TerraformResource,
"chronicle_feed_thinkst_canary": NewResourceFeedThinkstCanary().TerraformResource,
},
}
provider.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
return providerConfigure(ctx, d, provider)
}
return provider
}
func providerConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Provider) (interface{}, diag.Diagnostics) {
var region string
if v, ok := d.GetOk("region"); ok {
region = v.(string)
} else {
region = envSearch(chronicle.ChronicleRegionEnvVar)
if region == "" {
region = chronicle.RegionEurope
} else if !isValidRegion(region) {
return nil, diag.FromErr(fmt.Errorf("region %s is not valid", region))
}
}
opts := getAPIAuthOpts(d)
if v, ok := d.GetOk("request_timeout"); ok {
opts = append(opts, chronicle.WithRequestTimeout(time.Duration(v.(int))*time.Second))
}
if v, ok := d.GetOk("request_attempts"); ok {
opts = append(opts, chronicle.WithRequestAttempts(uint(v.(int))))
}
//nolint:all
stopCtx, ok := schema.StopContext(ctx)
if !ok {
stopCtx = ctx
}
client, err := chronicle.NewClient(region, p.UserAgent("terraform-provider-chronicle", version.ProviderVersion), stopCtx, opts...)
if err != nil {
return nil, diag.FromErr(err)
}
if endpoint, isCustom := customEndpoint(d, "events_custom_endpoint"); isCustom {
client.WithEventsBasePath(endpoint)
}
if endpoint, isCustom := customEndpoint(d, "alert_custom_endpoint"); isCustom {
client.WithAlertBasePath(endpoint)
}
if endpoint, isCustom := customEndpoint(d, "artifact_custom_endpoint"); isCustom {
client.WithArtifactBasePath(endpoint)
}
if endpoint, isCustom := customEndpoint(d, "alias_custom_endpoint"); isCustom {
client.WithAliasBasePath(endpoint)
}
if endpoint, isCustom := customEndpoint(d, "asset_custom_endpoint"); isCustom {
client.WithAssetBasePath(endpoint)
}
if endpoint, isCustom := customEndpoint(d, "ioc_custom_endpoint"); isCustom {
client.WithIOCBasePath(endpoint)
}
if endpoint, isCustom := customEndpoint(d, "rule_custom_endpoint"); isCustom {
client.WithRuleBasePath(endpoint)
}
if endpoint, isCustom := customEndpoint(d, "subjects_custom_endpoint"); isCustom {
client.WithSubjectsBasePath(endpoint)
}
return client, nil
}
func getAPIAuthOpts(d *schema.ResourceData) []chronicle.Option {
opts := make([]chronicle.Option, 0)
if v, ok := d.GetOk("bigqueryapi_credentials"); ok {
opts = append(opts, chronicle.WithBigQueryAPICredentials(v.(string)))
} else if v, ok := d.GetOk("bigqueryapi_access_token"); ok {
opts = append(opts, chronicle.WithBigQueryAPIAccessToken(v.(string)))
} else {
env := envSearch(chronicle.BigQueryAPIEnvVar)
if env != "" {
opts = append(opts, chronicle.WithBigQueryAPIEnvVar())
}
}
if v, ok := d.GetOk("backstoryapi_credentials"); ok {
opts = append(opts, chronicle.WithBackstoryAPICredentials(v.(string)))
} else if v, ok := d.GetOk("backstoryapi_credentials"); ok {
opts = append(opts, chronicle.WithBackstoryAPIAccessToken(v.(string)))
} else {
env := envSearch(chronicle.BackstoryAPIEnvVar)
if env != "" {
opts = append(opts, chronicle.WithBackstoryAPIEnvVar())
}
}
if v, ok := d.GetOk("ingestionapi_credentials"); ok {
opts = append(opts, chronicle.WithIngestionAPICredentials(v.(string)))
} else if v, ok := d.GetOk("ingestionapi_credentials"); ok {
opts = append(opts, chronicle.WithIngestionAPIAccessToken(v.(string)))
} else {
env := envSearch(chronicle.IngestionAPIEnvVar)
if env != "" {
opts = append(opts, chronicle.WithIngestionAPIEnvVar())
}
}
if v, ok := d.GetOk("forwarderapi_credentials"); ok {
opts = append(opts, chronicle.WithForwarderAPICredentials(v.(string)))
} else if v, ok := d.GetOk("forwarderapi_access_token"); ok {
opts = append(opts, chronicle.WithForwarderAPIAccessToken(v.(string)))
} else {
env := envSearch(chronicle.ForwarderAPIEnvVar)
if env != "" {
opts = append(opts, chronicle.WithBigQueryAPIEnvVar())
}
}
return opts
}
func customEndpoint(d *schema.ResourceData, endpoint string) (string, bool) {
custom, ok := d.GetOk(endpoint)
if ok {
return custom.(string), true
}
return "", false
}