Skip to content

Commit 36d88f8

Browse files
committed
Implement auth0_encryption_key_manager resource using the framework
1 parent 2a46719 commit 36d88f8

33 files changed

+3430
-3250
lines changed

docs/data-sources/provider.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
page_title: "Data Source: auth0_provider"
3+
description: |-
4+
A data source for retrieving basic information about the provider.
5+
---
6+
7+
# Data Source: auth0_provider
8+
9+
A data source for retrieving basic information about the provider.
10+
11+
12+
13+
<!-- schema generated by tfplugindocs -->
14+
## Schema
15+
16+
### Read-Only
17+
18+
- `provider_version` (String) The version of the provider.
19+
20+

docs/resources/encryption_key_manager.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
22
page_title: "Resource: auth0_encryption_key_manager"
33
description: |-
4-
Resource to allow the rekeying of your tenant master key.
4+
A resource for managing the tenant root key.
55
---
66

77
# Resource: auth0_encryption_key_manager
88

9-
Resource to allow the rekeying of your tenant master key.
9+
A resource for managing the tenant root key.
1010

1111
## Example Usage
1212

@@ -50,13 +50,12 @@ resource "auth0_encryption_key_manager" "my_key_manager" {
5050

5151
### Optional
5252

53-
- `customer_provided_root_key` (Block List, Max: 1) This attribute is used for provisioning the customer provided root key. To initiate the provisioning process, create a new empty `customer_provided_root_key` block. After applying this, the `public_wrapping_key` can be retreived from the resource, and the new root key should be generated by the customer and wrapped with the wrapping key, then base64-encoded and added as the `wrapped_key` attribute. (see [below for nested schema](#nestedblock--customer_provided_root_key))
53+
- `customer_provided_root_key` (Block, Optional) This attribute is used for provisioning the customer provided root key. To initiate the provisioning process, create a new empty `customer_provided_root_key` block. After applying this, the `public_wrapping_key` can be retreived from the resource, and the new root key should be generated by the customer and wrapped with the wrapping key, then base64-encoded and added as the `wrapped_key` attribute. (see [below for nested schema](#nestedblock--customer_provided_root_key))
5454
- `key_rotation_id` (String) If this value is changed, the encryption keys will be rotated. A UUID is recommended for the `key_rotation_id`.
5555

5656
### Read-Only
5757

58-
- `encryption_keys` (List of Object) All encryption keys. (see [below for nested schema](#nestedatt--encryption_keys))
59-
- `id` (String) The ID of this resource.
58+
- `encryption_keys` (Attributes List) All encryption keys. (see [below for nested schema](#nestedatt--encryption_keys))
6059

6160
<a id="nestedblock--customer_provided_root_key"></a>
6261
### Nested Schema for `customer_provided_root_key`
@@ -82,11 +81,11 @@ Read-Only:
8281

8382
Read-Only:
8483

85-
- `created_at` (String)
86-
- `key_id` (String)
87-
- `parent_key_id` (String)
88-
- `state` (String)
89-
- `type` (String)
90-
- `updated_at` (String)
84+
- `created_at` (String) The ISO 8601 formatted date the customer provided root key was created.
85+
- `key_id` (String) The key ID of the customer provided root key.
86+
- `parent_key_id` (String) The key ID of the parent wrapping key.
87+
- `state` (String) The state of the encryption key. One of `pre-activation`, `active`, `deactivated`, or `destroyed`.
88+
- `type` (String) The type of the encryption key. One of `customer-provided-root-key`, `environment-root-key`, or `tenant-master-key`.
89+
- `updated_at` (String) The ISO 8601 formatted date the customer provided root key was updated.
9190

9291

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ require (
5252
github.com/hashicorp/logutils v1.0.0 // indirect
5353
github.com/hashicorp/terraform-exec v0.21.0 // indirect
5454
github.com/hashicorp/terraform-json v0.22.1 // indirect
55+
github.com/hashicorp/terraform-plugin-framework-timetypes v0.5.0 // indirect
5556
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
5657
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
5758
github.com/hashicorp/terraform-svchost v0.1.1 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ github.com/hashicorp/terraform-plugin-docs v0.19.4 h1:G3Bgo7J22OMtegIgn8Cd/CaSey
113113
github.com/hashicorp/terraform-plugin-docs v0.19.4/go.mod h1:4pLASsatTmRynVzsjEhbXZ6s7xBlUw/2Kt0zfrq8HxA=
114114
github.com/hashicorp/terraform-plugin-framework v1.12.0 h1:7HKaueHPaikX5/7cbC1r9d1m12iYHY+FlNZEGxQ42CQ=
115115
github.com/hashicorp/terraform-plugin-framework v1.12.0/go.mod h1:N/IOQ2uYjW60Jp39Cp3mw7I/OpC/GfZ0385R0YibmkE=
116+
github.com/hashicorp/terraform-plugin-framework-timetypes v0.5.0 h1:v3DapR8gsp3EM8fKMh6up9cJUFQ2iRaFsYLP8UJnCco=
117+
github.com/hashicorp/terraform-plugin-framework-timetypes v0.5.0/go.mod h1:c3PnGE9pHBDfdEVG9t1S1C9ia5LW+gkFR0CygXlM8ak=
116118
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0 h1:bxZfGo9DIUoLLtHMElsu+zwqI4IsMZQBRRy4iLzZJ8E=
117119
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0/go.mod h1:wGeI02gEhj9nPANU62F2jCaHjXulejm/X+af4PdZaNo=
118120
github.com/hashicorp/terraform-plugin-go v0.24.0 h1:2WpHhginCdVhFIrWHxDEg6RBn3YaWzR2o6qUeIEat2U=

internal/acctest/acctest.go

+94-16
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,31 @@ import (
77
"testing"
88

99
"github.com/auth0/go-auth0/management"
10+
"github.com/hashicorp/terraform-plugin-framework/provider"
11+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1012
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1214
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1315
"gopkg.in/dnaeon/go-vcr.v3/recorder"
1416

1517
"github.com/auth0/terraform-provider-auth0/internal/config"
16-
"github.com/auth0/terraform-provider-auth0/internal/provider"
18+
frameworkError "github.com/auth0/terraform-provider-auth0/internal/framework/error"
19+
frameworkProvider "github.com/auth0/terraform-provider-auth0/internal/framework/provider"
20+
internalProvider "github.com/auth0/terraform-provider-auth0/internal/provider"
1721
)
1822

1923
// Test checks to see if http recordings are enabled and runs the tests
2024
// in parallel if they are, otherwise it simply wraps resource.Test.
2125
func Test(t *testing.T, testCase resource.TestCase) {
2226
if httpRecordingsAreEnabled() {
2327
httpRecorder := newHTTPRecorder(t)
24-
testCase.ProviderFactories = testFactoriesWithHTTPRecordings(httpRecorder)
28+
testCase.ProtoV6ProviderFactories = testProviderFactoriesWithHTTPRecordings(httpRecorder)
2529
resource.ParallelTest(t, testCase)
2630

2731
return
2832
}
2933

30-
testCase.ProviderFactories = TestFactories()
34+
testCase.ProtoV6ProviderFactories = TestProviderFactories()
3135
resource.Test(t, testCase)
3236
}
3337

@@ -36,23 +40,29 @@ func httpRecordingsAreEnabled() bool {
3640
return httpRecordings == "true" || httpRecordings == "1" || httpRecordings == "on"
3741
}
3842

39-
// TestFactories returns the configured auth0 provider used in testing.
40-
func TestFactories() map[string]func() (*schema.Provider, error) {
41-
return map[string]func() (*schema.Provider, error){
42-
"auth0": func() (*schema.Provider, error) {
43-
return provider.New(), nil
43+
// TestProviderFactories returns the configured auth0 provider used in testing for the framework.
44+
func TestProviderFactories() map[string]func() (tfprotov6.ProviderServer, error) {
45+
// Set descriptions to support Markdown syntax for SDK resources,
46+
// this will be used in document generation.
47+
schema.DescriptionKind = schema.StringMarkdown
48+
return map[string]func() (tfprotov6.ProviderServer, error){
49+
"auth0": func() (tfprotov6.ProviderServer, error) {
50+
return frameworkProvider.MuxServer(internalProvider.New(), frameworkProvider.New())
4451
},
4552
}
4653
}
4754

48-
func testFactoriesWithHTTPRecordings(httpRecorder *recorder.Recorder) map[string]func() (*schema.Provider, error) {
49-
return map[string]func() (*schema.Provider, error){
50-
"auth0": func() (*schema.Provider, error) {
51-
auth0Provider := provider.New()
52-
53-
auth0Provider.ConfigureContextFunc = configureTestProviderWithHTTPRecordings(httpRecorder)
54-
55-
return auth0Provider, nil
55+
func testProviderFactoriesWithHTTPRecordings(httpRecorder *recorder.Recorder) map[string]func() (tfprotov6.ProviderServer, error) {
56+
// Set descriptions to support Markdown syntax for SDK resources,
57+
// this will be used in document generation.
58+
schema.DescriptionKind = schema.StringMarkdown
59+
return map[string]func() (tfprotov6.ProviderServer, error){
60+
"auth0": func() (tfprotov6.ProviderServer, error) {
61+
sdkProvider := internalProvider.New()
62+
sdkProvider.ConfigureContextFunc = configureTestProviderWithHTTPRecordings(httpRecorder)
63+
fwkProvider := frameworkProvider.New()
64+
fwkProvider.SetConfigureFunc(configureTestFrameworkProviderWithHTTPRecordings(httpRecorder))
65+
return frameworkProvider.MuxServer(sdkProvider, fwkProvider)
5666
},
5767
}
5868
}
@@ -96,3 +106,71 @@ func configureTestProviderWithHTTPRecordings(httpRecorder *recorder.Recorder) sc
96106
return config.New(apiClient), nil
97107
}
98108
}
109+
110+
func configureTestFrameworkProviderWithHTTPRecordings(httpRecorder *recorder.Recorder) func(context.Context, provider.ConfigureRequest, *provider.ConfigureResponse) {
111+
return func(ctx context.Context, request provider.ConfigureRequest, response *provider.ConfigureResponse) {
112+
domain := os.Getenv("AUTH0_DOMAIN")
113+
debugStr := os.Getenv("AUTH0_DEBUG")
114+
debug := (debugStr == "1" || debugStr == "true" || debugStr == "TRUE" || debugStr == "on" || debugStr == "ON")
115+
116+
var data config.FrameworkProviderModel
117+
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
118+
119+
if data.Domain.ValueString() != "" {
120+
domain = data.Domain.ValueString()
121+
}
122+
if !data.Debug.IsNull() && !data.Debug.IsUnknown() {
123+
debug = data.Debug.ValueBool()
124+
}
125+
126+
clientOptions := []management.Option{
127+
management.WithStaticToken("insecure"),
128+
management.WithClient(httpRecorder.GetDefaultClient()),
129+
management.WithDebug(debug),
130+
management.WithRetries(3, []int{http.StatusTooManyRequests, http.StatusInternalServerError}),
131+
}
132+
133+
if domain != RecordingsDomain {
134+
clientID := os.Getenv("AUTH0_CLIENT_ID")
135+
clientSecret := os.Getenv("AUTH0_CLIENT_SECRET")
136+
apiToken := os.Getenv("AUTH0_API_TOKEN")
137+
audience := os.Getenv("AUTH0_AUDIENCE")
138+
139+
if data.ClientID.ValueString() != "" {
140+
clientID = data.ClientID.ValueString()
141+
}
142+
if data.ClientSecret.ValueString() != "" {
143+
clientSecret = data.ClientSecret.ValueString()
144+
}
145+
if data.APIToken.ValueString() != "" {
146+
apiToken = data.APIToken.ValueString()
147+
}
148+
if data.Audience.ValueString() != "" {
149+
audience = data.Audience.ValueString()
150+
}
151+
152+
authenticationOption := management.WithStaticToken(apiToken)
153+
if apiToken == "" {
154+
ctx := context.Background()
155+
156+
authenticationOption = management.WithClientCredentials(ctx, clientID, clientSecret)
157+
if audience != "" {
158+
authenticationOption = management.WithClientCredentialsAndAudience(ctx, clientID, clientSecret, audience)
159+
}
160+
}
161+
162+
clientOptions = append(clientOptions, authenticationOption)
163+
}
164+
165+
apiClient, err := management.New(domain, clientOptions...)
166+
if err != nil {
167+
response.Diagnostics.Append(frameworkError.Diagnostics(err)...)
168+
}
169+
170+
if !response.Diagnostics.HasError() {
171+
config := config.New(apiClient)
172+
response.ResourceData = config
173+
response.DataSourceData = config
174+
}
175+
}
176+
}

internal/auth0/client/resource_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ resource "auth0_client" "my_client" {
2121

2222
func TestAccClientInitiateLoginUriValidation(t *testing.T) {
2323
resource.UnitTest(t, resource.TestCase{
24-
ProviderFactories: acctest.TestFactories(),
24+
ProtoV6ProviderFactories: acctest.TestProviderFactories(),
2525
Steps: []resource.TestStep{
2626
{
2727
Config: acctest.ParseTestName(testAccClientValidationOnInitiateLoginURIWithHTTP, t.Name()),
@@ -44,7 +44,7 @@ resource "auth0_client" "my_client" {
4444

4545
func TestAccClientMobileValidationError(t *testing.T) {
4646
resource.UnitTest(t, resource.TestCase{
47-
ProviderFactories: acctest.TestFactories(),
47+
ProtoV6ProviderFactories: acctest.TestProviderFactories(),
4848
Steps: []resource.TestStep{
4949
{
5050
Config: acctest.ParseTestName(testAccClientValidationOnMobile, t.Name()),

internal/auth0/connection/data_source_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ data "auth0_connection" "test" {
5555

5656
func TestAccDataSourceConnectionRequiredArguments(t *testing.T) {
5757
resource.UnitTest(t, resource.TestCase{
58-
ProviderFactories: acctest.TestFactories(),
58+
ProtoV6ProviderFactories: acctest.TestProviderFactories(),
5959
Steps: []resource.TestStep{
6060
{
6161
Config: `data "auth0_connection" "test" { }`,

internal/auth0/encryptionkeymanager/flatten.go

-65
This file was deleted.

0 commit comments

Comments
 (0)