forked from hashicorp/terraform-provider-google-beta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresource_privateca_certificate_authority.go
2104 lines (1917 loc) · 83.6 KB
/
resource_privateca_certificate_authority.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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------
package privateca
import (
"context"
"fmt"
"log"
"net/http"
"reflect"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/verify"
)
func resourcePrivateCaCACustomDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
if diff.HasChange("desired_state") {
_, new := diff.GetChange("desired_state")
if tpgresource.IsNewResource(diff) {
if new.(string) != "STAGED" && new.(string) != "ENABLED" {
return fmt.Errorf("`desired_state` can only be set to `STAGED` or `ENABLED` when creating a new CA")
}
} else {
if new == "STAGED" && diff.Get("state") != new {
return fmt.Errorf("Field `desired_state` can only be set to `STAGED` when creating a new CA")
}
}
}
return nil
}
func ResourcePrivatecaCertificateAuthority() *schema.Resource {
return &schema.Resource{
Create: resourcePrivatecaCertificateAuthorityCreate,
Read: resourcePrivatecaCertificateAuthorityRead,
Update: resourcePrivatecaCertificateAuthorityUpdate,
Delete: resourcePrivatecaCertificateAuthorityDelete,
Importer: &schema.ResourceImporter{
State: resourcePrivatecaCertificateAuthorityImport,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Update: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},
CustomizeDiff: customdiff.All(
resourcePrivateCaCACustomDiff,
tpgresource.SetLabelsDiff,
tpgresource.DefaultProviderProject,
),
Schema: map[string]*schema.Schema{
"certificate_authority_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The user provided Resource ID for this Certificate Authority.`,
},
"config": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `The config used to create a self-signed X.509 certificate or CSR.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subject_config": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `Specifies some of the values in a certificate that are related to the subject.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subject": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `Contains distinguished name fields such as the location and organization.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"common_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The common name of the distinguished name.`,
},
"organization": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The organization of the subject.`,
},
"country_code": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The country code of the subject.`,
},
"locality": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The locality or city of the subject.`,
},
"organizational_unit": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The organizational unit of the subject.`,
},
"postal_code": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The postal code of the subject.`,
},
"province": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The province, territory, or regional state of the subject.`,
},
"street_address": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The street address of the subject.`,
},
},
},
},
"subject_alt_name": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `The subject alternative name fields.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"dns_names": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Contains only valid, fully-qualified host names.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
AtLeastOneOf: []string{"config.0.subject_config.0.subject_alt_name.0.dns_names", "config.0.subject_config.0.subject_alt_name.0.uris", "config.0.subject_config.0.subject_alt_name.0.email_addresses", "config.0.subject_config.0.subject_alt_name.0.ip_addresses"},
},
"email_addresses": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Contains only valid RFC 2822 E-mail addresses.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
AtLeastOneOf: []string{"config.0.subject_config.0.subject_alt_name.0.dns_names", "config.0.subject_config.0.subject_alt_name.0.uris", "config.0.subject_config.0.subject_alt_name.0.email_addresses", "config.0.subject_config.0.subject_alt_name.0.ip_addresses"},
},
"ip_addresses": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Contains only valid 32-bit IPv4 addresses or RFC 4291 IPv6 addresses.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
AtLeastOneOf: []string{"config.0.subject_config.0.subject_alt_name.0.dns_names", "config.0.subject_config.0.subject_alt_name.0.uris", "config.0.subject_config.0.subject_alt_name.0.email_addresses", "config.0.subject_config.0.subject_alt_name.0.ip_addresses"},
},
"uris": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Contains only valid RFC 3986 URIs.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
AtLeastOneOf: []string{"config.0.subject_config.0.subject_alt_name.0.dns_names", "config.0.subject_config.0.subject_alt_name.0.uris", "config.0.subject_config.0.subject_alt_name.0.email_addresses", "config.0.subject_config.0.subject_alt_name.0.ip_addresses"},
},
},
},
},
},
},
},
"x509_config": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `Describes how some of the technical X.509 fields in a certificate should be populated.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ca_options": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `Describes values that are relevant in a CA certificate.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"is_ca": {
Type: schema.TypeBool,
Required: true,
ForceNew: true,
Description: `When true, the "CA" in Basic Constraints extension will be set to true.`,
},
"max_issuer_path_length": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Description: `Refers to the "path length constraint" in Basic Constraints extension. For a CA certificate, this value describes the depth of
subordinate CA certificates that are allowed. If this value is less than 0, the request will fail. Setting the value to 0
requires setting 'zero_max_issuer_path_length = true'.`,
},
"non_ca": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `When true, the "CA" in Basic Constraints extension will be set to false.
If both 'is_ca' and 'non_ca' are unset, the extension will be omitted from the CA certificate.`,
},
"zero_max_issuer_path_length": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `When true, the "path length constraint" in Basic Constraints extension will be set to 0.
If both 'max_issuer_path_length' and 'zero_max_issuer_path_length' are unset,
the max path length will be omitted from the CA certificate.`,
},
},
},
},
"key_usage": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `Indicates the intended use for keys that correspond to a certificate.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"base_key_usage": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `Describes high-level ways in which a key may be used.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cert_sign": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `The key may be used to sign certificates.`,
},
"content_commitment": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `The key may be used for cryptographic commitments. Note that this may also be referred to as "non-repudiation".`,
},
"crl_sign": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `The key may be used sign certificate revocation lists.`,
},
"data_encipherment": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `The key may be used to encipher data.`,
},
"decipher_only": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `The key may be used to decipher only.`,
},
"digital_signature": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `The key may be used for digital signatures.`,
},
"encipher_only": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `The key may be used to encipher only.`,
},
"key_agreement": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `The key may be used in a key agreement protocol.`,
},
"key_encipherment": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `The key may be used to encipher other keys.`,
},
},
},
},
"extended_key_usage": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `Describes high-level ways in which a key may be used.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"client_auth": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Corresponds to OID 1.3.6.1.5.5.7.3.2. Officially described as "TLS WWW client authentication", though regularly used for non-WWW TLS.`,
},
"code_signing": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Corresponds to OID 1.3.6.1.5.5.7.3.3. Officially described as "Signing of downloadable executable code client authentication".`,
},
"email_protection": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Corresponds to OID 1.3.6.1.5.5.7.3.4. Officially described as "Email protection".`,
},
"ocsp_signing": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Corresponds to OID 1.3.6.1.5.5.7.3.9. Officially described as "Signing OCSP responses".`,
},
"server_auth": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Corresponds to OID 1.3.6.1.5.5.7.3.1. Officially described as "TLS WWW server authentication", though regularly used for non-WWW TLS.`,
},
"time_stamping": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Corresponds to OID 1.3.6.1.5.5.7.3.8. Officially described as "Binding the hash of an object to a time".`,
},
},
},
},
"unknown_extended_key_usages": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `An ObjectId specifies an object identifier (OID). These provide context and describe types in ASN.1 messages.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"object_id_path": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `An ObjectId specifies an object identifier (OID). These provide context and describe types in ASN.1 messages.`,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
},
},
},
},
},
},
"additional_extensions": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Specifies an X.509 extension, which may be used in different parts of X.509 objects like certificates, CSRs, and CRLs.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"critical": {
Type: schema.TypeBool,
Required: true,
ForceNew: true,
Description: `Indicates whether or not this extension is critical (i.e., if the client does not know how to
handle this extension, the client should consider this to be an error).`,
},
"object_id": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `Describes values that are relevant in a CA certificate.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"object_id_path": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `An ObjectId specifies an object identifier (OID). These provide context and describe types in ASN.1 messages.`,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
},
},
},
"value": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The value of this X.509 extension. A base64-encoded string.`,
},
},
},
},
"aia_ocsp_servers": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Describes Online Certificate Status Protocol (OCSP) endpoint addresses that appear in the
"Authority Information Access" extension in the certificate.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"name_constraints": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Describes the X.509 name constraints extension.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"critical": {
Type: schema.TypeBool,
Required: true,
ForceNew: true,
Description: `Indicates whether or not the name constraints are marked critical.`,
},
"excluded_dns_names": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Contains excluded DNS names. Any DNS name that can be
constructed by simply adding zero or more labels to
the left-hand side of the name satisfies the name constraint.
For example, 'example.com', 'www.example.com', 'www.sub.example.com'
would satisfy 'example.com' while 'example1.com' does not.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"excluded_email_addresses": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Contains the excluded email addresses. The value can be a particular
email address, a hostname to indicate all email addresses on that host or
a domain with a leading period (e.g. '.example.com') to indicate
all email addresses in that domain.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"excluded_ip_ranges": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Contains the excluded IP ranges. For IPv4 addresses, the ranges
are expressed using CIDR notation as specified in RFC 4632.
For IPv6 addresses, the ranges are expressed in similar encoding as IPv4
addresses.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"excluded_uris": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Contains the excluded URIs that apply to the host part of the name.
The value can be a hostname or a domain with a
leading period (like '.example.com')`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"permitted_dns_names": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Contains permitted DNS names. Any DNS name that can be
constructed by simply adding zero or more labels to
the left-hand side of the name satisfies the name constraint.
For example, 'example.com', 'www.example.com', 'www.sub.example.com'
would satisfy 'example.com' while 'example1.com' does not.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"permitted_email_addresses": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Contains the permitted email addresses. The value can be a particular
email address, a hostname to indicate all email addresses on that host or
a domain with a leading period (e.g. '.example.com') to indicate
all email addresses in that domain.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"permitted_ip_ranges": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Contains the permitted IP ranges. For IPv4 addresses, the ranges
are expressed using CIDR notation as specified in RFC 4632.
For IPv6 addresses, the ranges are expressed in similar encoding as IPv4
addresses.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"permitted_uris": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Contains the permitted URIs that apply to the host part of the name.
The value can be a hostname or a domain with a
leading period (like '.example.com')`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"policy_ids": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Describes the X.509 certificate policy object identifiers, per https://tools.ietf.org/html/rfc5280#section-4.2.1.4.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"object_id_path": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `An ObjectId specifies an object identifier (OID). These provide context and describe types in ASN.1 messages.`,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
},
},
},
},
},
},
"subject_key_id": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `When specified this provides a custom SKI to be used in the certificate. This should only be used to maintain a SKI of an existing CA originally created outside CA service, which was not generated using method (1) described in RFC 5280 section 4.2.1.2..`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The value of the KeyId in lowercase hexadecimal.`,
},
},
},
},
},
},
},
"key_spec": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: `Used when issuing certificates for this CertificateAuthority. If this CertificateAuthority
is a self-signed CertificateAuthority, this key is also used to sign the self-signed CA
certificate. Otherwise, it is used to sign a CSR.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"algorithm": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidateEnum([]string{"SIGN_HASH_ALGORITHM_UNSPECIFIED", "RSA_PSS_2048_SHA256", "RSA_PSS_3072_SHA256", "RSA_PSS_4096_SHA256", "RSA_PKCS1_2048_SHA256", "RSA_PKCS1_3072_SHA256", "RSA_PKCS1_4096_SHA256", "EC_P256_SHA256", "EC_P384_SHA384", ""}),
Description: `The algorithm to use for creating a managed Cloud KMS key for a for a simplified
experience. All managed keys will be have their ProtectionLevel as HSM. Possible values: ["SIGN_HASH_ALGORITHM_UNSPECIFIED", "RSA_PSS_2048_SHA256", "RSA_PSS_3072_SHA256", "RSA_PSS_4096_SHA256", "RSA_PKCS1_2048_SHA256", "RSA_PKCS1_3072_SHA256", "RSA_PKCS1_4096_SHA256", "EC_P256_SHA256", "EC_P384_SHA384"]`,
ExactlyOneOf: []string{"key_spec.0.cloud_kms_key_version", "key_spec.0.algorithm"},
},
"cloud_kms_key_version": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The resource name for an existing Cloud KMS CryptoKeyVersion in the format
'projects/*/locations/*/keyRings/*/cryptoKeys/*/cryptoKeyVersions/*'.`,
ExactlyOneOf: []string{"key_spec.0.cloud_kms_key_version", "key_spec.0.algorithm"},
},
},
},
},
"location": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Location of the CertificateAuthority. A full list of valid locations can be found by
running 'gcloud privateca locations list'.`,
},
"pool": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The name of the CaPool this Certificate Authority belongs to.`,
},
"gcs_bucket": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The name of a Cloud Storage bucket where this CertificateAuthority will publish content,
such as the CA certificate and CRLs. This must be a bucket name, without any prefixes
(such as 'gs://') or suffixes (such as '.googleapis.com'). For example, to use a bucket named
my-bucket, you would simply specify 'my-bucket'. If not specified, a managed bucket will be
created.`,
},
"ignore_active_certificates_on_deletion": {
Type: schema.TypeBool,
Optional: true,
Description: `This field allows the CA to be deleted even if the CA has active certs. Active certs include both unrevoked and unexpired certs.
Use with care. Defaults to 'false'.`,
Default: false,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Description: `Labels with user-defined metadata.
An object containing a list of "key": value pairs. Example: { "name": "wrench", "mass":
"1.3kg", "count": "3" }.
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
Please refer to the field 'effective_labels' for all of the labels present on the resource.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"lifetime": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The desired lifetime of the CA certificate. Used to create the "notBeforeTime" and
"notAfterTime" fields inside an X.509 certificate. A duration in seconds with up to nine
fractional digits, terminated by 's'. Example: "3.5s".`,
Default: "315360000s",
},
"pem_ca_certificate": {
Type: schema.TypeString,
Optional: true,
Description: `The signed CA certificate issued from the subordinated CA's CSR. This is needed when activating the subordiante CA with a third party issuer.`,
},
"skip_grace_period": {
Type: schema.TypeBool,
Optional: true,
Description: `If this flag is set, the Certificate Authority will be deleted as soon as
possible without a 30-day grace period where undeletion would have been
allowed. If you proceed, there will be no way to recover this CA.
Use with care. Defaults to 'false'.`,
Default: false,
},
"subordinate_config": {
Type: schema.TypeList,
Optional: true,
Description: `If this is a subordinate CertificateAuthority, this field will be set
with the subordinate configuration, which describes its issuers.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"certificate_authority": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: tpgresource.CompareResourceNames,
Description: `This can refer to a CertificateAuthority that was used to create a
subordinate CertificateAuthority. This field is used for information
and usability purposes only. The resource name is in the format
'projects/*/locations/*/caPools/*/certificateAuthorities/*'.`,
ExactlyOneOf: []string{"subordinate_config.0.certificate_authority", "subordinate_config.0.pem_issuer_chain"},
},
"pem_issuer_chain": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: `Contains the PEM certificate chain for the issuers of this CertificateAuthority,
but not pem certificate for this CA itself.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"pem_certificates": {
Type: schema.TypeList,
Optional: true,
Description: `Expected to be in leaf-to-root order according to RFC 5246.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
ExactlyOneOf: []string{"subordinate_config.0.certificate_authority", "subordinate_config.0.pem_issuer_chain"},
},
},
},
},
"type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidateEnum([]string{"SELF_SIGNED", "SUBORDINATE", ""}),
Description: `The Type of this CertificateAuthority.
~> **Note:** For 'SUBORDINATE' Certificate Authorities, they need to
be activated before they can issue certificates. Default value: "SELF_SIGNED" Possible values: ["SELF_SIGNED", "SUBORDINATE"]`,
Default: "SELF_SIGNED",
},
"access_urls": {
Type: schema.TypeList,
Computed: true,
Description: `URLs for accessing content published by this CA, such as the CA certificate and CRLs.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ca_certificate_access_url": {
Type: schema.TypeString,
Computed: true,
Description: `The URL where this CertificateAuthority's CA certificate is published. This will only be
set for CAs that have been activated.`,
},
"crl_access_urls": {
Type: schema.TypeList,
Computed: true,
Description: `The URL where this CertificateAuthority's CRLs are published. This will only be set for
CAs that have been activated.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: `The time at which this CertificateAuthority was created.
A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine
fractional digits. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".`,
},
"effective_labels": {
Type: schema.TypeMap,
Computed: true,
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The resource name for this CertificateAuthority in the format
projects/*/locations/*/certificateAuthorities/*.`,
},
"pem_ca_certificates": {
Type: schema.TypeList,
Computed: true,
Description: `This CertificateAuthority's certificate chain, including the current
CertificateAuthority's certificate. Ordered such that the root issuer is the final
element (consistent with RFC 5246). For a self-signed CA, this will only list the current
CertificateAuthority's certificate.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: `The State for this CertificateAuthority.`,
},
"terraform_labels": {
Type: schema.TypeMap,
Computed: true,
Description: `The combination of labels configured directly on the resource
and default labels configured on the provider.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"update_time": {
Type: schema.TypeString,
Computed: true,
Description: `The time at which this CertificateAuthority was updated.
A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine
fractional digits. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".`,
},
"deletion_protection": {
Type: schema.TypeBool,
Optional: true,
Description: `Whether Terraform will be prevented from destroying the CertificateAuthority.
When the field is set to true or unset in Terraform state, a 'terraform apply'
or 'terraform destroy' that would delete the CertificateAuthority will fail.
When the field is set to false, deleting the CertificateAuthority is allowed.`,
Default: true,
},
"desired_state": {
Type: schema.TypeString,
Optional: true,
Description: `Desired state of the CertificateAuthority. Set this field to 'STAGED' to create a 'STAGED' root CA.
Possible values: ENABLED, DISABLED, STAGED.`,
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
UseJSONNumber: true,
}
}
func resourcePrivatecaCertificateAuthorityCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
obj := make(map[string]interface{})
typeProp, err := expandPrivatecaCertificateAuthorityType(d.Get("type"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("type"); !tpgresource.IsEmptyValue(reflect.ValueOf(typeProp)) && (ok || !reflect.DeepEqual(v, typeProp)) {
obj["type"] = typeProp
}
configProp, err := expandPrivatecaCertificateAuthorityConfig(d.Get("config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("config"); !tpgresource.IsEmptyValue(reflect.ValueOf(configProp)) && (ok || !reflect.DeepEqual(v, configProp)) {
obj["config"] = configProp
}
lifetimeProp, err := expandPrivatecaCertificateAuthorityLifetime(d.Get("lifetime"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("lifetime"); !tpgresource.IsEmptyValue(reflect.ValueOf(lifetimeProp)) && (ok || !reflect.DeepEqual(v, lifetimeProp)) {
obj["lifetime"] = lifetimeProp
}
keySpecProp, err := expandPrivatecaCertificateAuthorityKeySpec(d.Get("key_spec"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("key_spec"); !tpgresource.IsEmptyValue(reflect.ValueOf(keySpecProp)) && (ok || !reflect.DeepEqual(v, keySpecProp)) {
obj["keySpec"] = keySpecProp
}
subordinateConfigProp, err := expandPrivatecaCertificateAuthoritySubordinateConfig(d.Get("subordinate_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("subordinate_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(subordinateConfigProp)) && (ok || !reflect.DeepEqual(v, subordinateConfigProp)) {
obj["subordinateConfig"] = subordinateConfigProp
}
gcsBucketProp, err := expandPrivatecaCertificateAuthorityGcsBucket(d.Get("gcs_bucket"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("gcs_bucket"); !tpgresource.IsEmptyValue(reflect.ValueOf(gcsBucketProp)) && (ok || !reflect.DeepEqual(v, gcsBucketProp)) {
obj["gcsBucket"] = gcsBucketProp
}
labelsProp, err := expandPrivatecaCertificateAuthorityEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("effective_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
url, err := tpgresource.ReplaceVars(d, config, "{{PrivatecaBasePath}}projects/{{project}}/locations/{{location}}/caPools/{{pool}}/certificateAuthorities?certificateAuthorityId={{certificate_authority_id}}")
if err != nil {
return err
}
log.Printf("[DEBUG] Creating new CertificateAuthority: %#v", obj)
billingProject := ""
project, err := tpgresource.GetProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for CertificateAuthority: %s", err)
}
billingProject = project
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
headers := make(http.Header)
// Drop `subordinateConfig` as it can not be set during CA creation.
// It can be used to activate CA during post_create or pre_update.
delete(obj, "subordinateConfig")
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutCreate),
Headers: headers,
})
if err != nil {
return fmt.Errorf("Error creating CertificateAuthority: %s", err)
}
// Store the ID now
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/caPools/{{pool}}/certificateAuthorities/{{certificate_authority_id}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
// Use the resource in the operation response to populate
// identity fields and d.Id() before read
var opRes map[string]interface{}
err = PrivatecaOperationWaitTimeWithResponse(
config, res, &opRes, project, "Creating CertificateAuthority", userAgent,
d.Timeout(schema.TimeoutCreate))
if err != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error waiting to create CertificateAuthority: %s", err)
}
opRes, err = resourcePrivatecaCertificateAuthorityDecoder(d, meta, opRes)
if err != nil {
return fmt.Errorf("Error decoding response from operation: %s", err)
}
if opRes == nil {
return fmt.Errorf("Error decoding response from operation, could not find object")
}
if err := d.Set("name", flattenPrivatecaCertificateAuthorityName(opRes["name"], d, config)); err != nil {
return err
}
// This may have caused the ID to update - update it if so.
id, err = tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/caPools/{{pool}}/certificateAuthorities/{{certificate_authority_id}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
staged := d.Get("type").(string) == "SELF_SIGNED"
if d.Get("type").(string) == "SUBORDINATE" {
if _, ok := d.GetOk("subordinate_config"); ok {
// First party issuer
log.Printf("[DEBUG] Activating CertificateAuthority with first party issuer")
if err := activateSubCAWithFirstPartyIssuer(config, d, project, billingProject, userAgent); err != nil {
return fmt.Errorf("Error activating subordinate CA with first party issuer: %v", err)
}
staged = true
log.Printf("[DEBUG] CertificateAuthority activated")
}
}