From 3b3540037c7130ac1943231ae75cb8ae40472316 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea Date: Thu, 16 Jun 2022 11:54:36 +0200 Subject: [PATCH 1/3] Update editorconfig --- .editorconfig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.editorconfig b/.editorconfig index 4b5ecd3a2..7c1491f2f 100644 --- a/.editorconfig +++ b/.editorconfig @@ -17,8 +17,8 @@ indent_style = tab [*.md] trim_trailing_whitespace = false -[*.{yml, yaml, raml, yml.dist, feature, toml}] +[*.{yml, yaml, raml, yml.dist, feature, toml, tf}] indent_size = 2 -[GNUmakefile] +[Makefile] indent_style = tab From d17220fd394a4b558ef9c58a81843d223758b384 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea Date: Thu, 16 Jun 2022 11:54:59 +0200 Subject: [PATCH 2/3] Refactor hook tests --- auth0/resource_auth0_hook_test.go | 76 +++--- auth0/testdata/recordings/TestAccHook.yaml | 62 ++++- .../recordings/TestAccHookSecrets.yaml | 252 ++++++++++++++---- 3 files changed, 284 insertions(+), 106 deletions(-) diff --git a/auth0/resource_auth0_hook_test.go b/auth0/resource_auth0_hook_test.go index a252294f1..edd008e52 100644 --- a/auth0/resource_auth0_hook_test.go +++ b/auth0/resource_auth0_hook_test.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/go-cty/cty" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/stretchr/testify/assert" ) func TestAccHook(t *testing.T) { @@ -15,7 +16,7 @@ func TestAccHook(t *testing.T) { ProviderFactories: testProviders(httpRecorder), Steps: []resource.TestStep{ { - Config: testAccHookCreate, + Config: fmt.Sprintf(testAccHookCreate, ""), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("auth0_hook.my_hook", "name", "pre-user-reg-hook"), resource.TestCheckResourceAttr("auth0_hook.my_hook", "script", "function (user, context, callback) { callback(null, { user }); }"), @@ -33,6 +34,7 @@ resource "auth0_hook" "my_hook" { script = "function (user, context, callback) { callback(null, { user }); }" trigger_id = "pre-user-registration" enabled = true + %s } ` @@ -43,7 +45,7 @@ func TestAccHookSecrets(t *testing.T) { ProviderFactories: testProviders(httpRecorder), Steps: []resource.TestStep{ { - Config: testAccHookSecrets("alpha"), + Config: fmt.Sprintf(testAccHookCreate, testAccHookSecrets), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("auth0_hook.my_hook", "name", "pre-user-reg-hook"), resource.TestCheckResourceAttr("auth0_hook.my_hook", "dependencies.auth0", "2.30.0"), @@ -55,7 +57,7 @@ func TestAccHookSecrets(t *testing.T) { ), }, { - Config: testAccHookSecrets2("gamma", "kappa"), + Config: fmt.Sprintf(testAccHookCreate, testAccHookSecretsUpdate), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("auth0_hook.my_hook", "name", "pre-user-reg-hook"), resource.TestCheckResourceAttr("auth0_hook.my_hook", "dependencies.auth0", "2.30.0"), @@ -67,7 +69,7 @@ func TestAccHookSecrets(t *testing.T) { ), }, { - Config: testAccHookSecrets("delta"), + Config: fmt.Sprintf(testAccHookCreate, testAccHookSecretsUpdateAndRemoval), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("auth0_hook.my_hook", "name", "pre-user-reg-hook"), resource.TestCheckResourceAttr("auth0_hook.my_hook", "script", "function (user, context, callback) { callback(null, { user }); }"), @@ -81,58 +83,44 @@ func TestAccHookSecrets(t *testing.T) { }) } -func testAccHookSecrets(fooValue string) string { - return fmt.Sprintf(` -resource "auth0_hook" "my_hook" { - name = "pre-user-reg-hook" - script = "function (user, context, callback) { callback(null, { user }); }" - trigger_id = "pre-user-registration" - enabled = true +const testAccHookSecrets = ` dependencies = { auth0 = "2.30.0" } - secrets = { - foo = "%s" + secrets = { + foo = "alpha" } -} -`, fooValue) -} +` -func testAccHookSecrets2(fooValue string, barValue string) string { - return fmt.Sprintf(` -resource "auth0_hook" "my_hook" { - name = "pre-user-reg-hook" - script = "function (user, context, callback) { callback(null, { user }); }" - trigger_id = "pre-user-registration" +const testAccHookSecretsUpdate = ` dependencies = { auth0 = "2.30.0" } - enabled = true secrets = { - foo = "%s" - bar = "%s" + foo = "gamma" + bar = "kappa" } -} -`, fooValue, barValue) -} +` + +const testAccHookSecretsUpdateAndRemoval = ` + dependencies = { + auth0 = "2.30.0" + } + secrets = { + foo = "delta" + } +` func TestHookNameRegexp(t *testing.T) { - for name, valid := range map[string]bool{ - "my-hook-1": true, - "hook 2 name with spaces": true, - " hook with a space prefix": false, - "hook with a space suffix ": false, - " ": false, - " ": false, + for givenHookName, expectedError := range map[string]bool{ + "my-hook-1": false, + "hook 2 name with spaces": false, + " hook with a space prefix": true, + "hook with a space suffix ": true, + " ": true, + " ": true, } { - fn := validateHookName() - - validationResult := fn(name, cty.Path{cty.GetAttrStep{Name: "name"}}) - if validationResult.HasError() && valid { - t.Fatalf("Expected %q to be valid, but got validation errors %v", name, validationResult) - } - if !validationResult.HasError() && !valid { - t.Fatalf("Expected %q to be invalid, but got no validation errors.", name) - } + validationResult := validateHookName()(givenHookName, cty.Path{cty.GetAttrStep{Name: "name"}}) + assert.Equal(t, expectedError, validationResult.HasError()) } } diff --git a/auth0/testdata/recordings/TestAccHook.yaml b/auth0/testdata/recordings/TestAccHook.yaml index d8168b2b7..e66ffeb73 100644 --- a/auth0/testdata/recordings/TestAccHook.yaml +++ b/auth0/testdata/recordings/TestAccHook.yaml @@ -9,11 +9,11 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 + - Go-Auth0-SDK/0.6.4 url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks method: POST response: - body: '{"id":"01G5EKEJB0YXT0DXEJ2NTHAV50","name":"pre-user-reg-hook","script":"function + body: '{"id":"01G5NYT4ED3CJDCTHCGY55AVWJ","name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{},"triggerId":"pre-user-registration","enabled":true}' headers: Content-Type: @@ -29,8 +29,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEJB0YXT0DXEJ2NTHAV50/secrets + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYT4ED3CJDCTHCGY55AVWJ/secrets method: GET response: body: '{}' @@ -48,11 +48,11 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEJB0YXT0DXEJ2NTHAV50 + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYT4ED3CJDCTHCGY55AVWJ method: GET response: - body: '{"id":"01G5EKEJB0YXT0DXEJ2NTHAV50","name":"pre-user-reg-hook","script":"function + body: '{"id":"01G5NYT4ED3CJDCTHCGY55AVWJ","name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{},"enabled":true,"triggerId":"pre-user-registration"}' headers: Content-Type: @@ -68,11 +68,30 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEJB0YXT0DXEJ2NTHAV50 + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYT4ED3CJDCTHCGY55AVWJ/secrets method: GET response: - body: '{"id":"01G5EKEJB0YXT0DXEJ2NTHAV50","name":"pre-user-reg-hook","script":"function + body: '{}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms +- request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYT4ED3CJDCTHCGY55AVWJ + method: GET + response: + body: '{"id":"01G5NYT4ED3CJDCTHCGY55AVWJ","name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{},"enabled":true,"triggerId":"pre-user-registration"}' headers: Content-Type: @@ -80,6 +99,25 @@ interactions: status: 200 OK code: 200 duration: 1ms +- request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYT4ED3CJDCTHCGY55AVWJ/secrets + method: GET + response: + body: '{}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms - request: body: "" form: {} @@ -87,8 +125,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEJB0YXT0DXEJ2NTHAV50 + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYT4ED3CJDCTHCGY55AVWJ method: DELETE response: body: "" diff --git a/auth0/testdata/recordings/TestAccHookSecrets.yaml b/auth0/testdata/recordings/TestAccHookSecrets.yaml index 458890c96..5a71fad4b 100644 --- a/auth0/testdata/recordings/TestAccHookSecrets.yaml +++ b/auth0/testdata/recordings/TestAccHookSecrets.yaml @@ -9,11 +9,11 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 + - Go-Auth0-SDK/0.6.4 url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks method: POST response: - body: '{"id":"01G5EKEPTWPFMWHYMAAEEDF66V","name":"pre-user-reg-hook","script":"function + body: '{"id":"01G5NYTCBQZC0SZP2GKZ84CWNM","name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{"auth0":"2.30.0"},"triggerId":"pre-user-registration","enabled":true}' headers: Content-Type: @@ -29,8 +29,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V/secrets + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets method: GET response: body: '{}' @@ -48,8 +48,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V/secrets + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets method: POST response: body: '{}' @@ -67,11 +67,11 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM method: GET response: - body: '{"id":"01G5EKEPTWPFMWHYMAAEEDF66V","name":"pre-user-reg-hook","script":"function + body: '{"id":"01G5NYTCBQZC0SZP2GKZ84CWNM","name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{"auth0":"2.30.0"},"enabled":true,"triggerId":"pre-user-registration"}' headers: Content-Type: @@ -87,11 +87,30 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets method: GET response: - body: '{"id":"01G5EKEPTWPFMWHYMAAEEDF66V","name":"pre-user-reg-hook","script":"function + body: '{"foo":"_VALUE_NOT_SHOWN_"}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms +- request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM + method: GET + response: + body: '{"id":"01G5NYTCBQZC0SZP2GKZ84CWNM","name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{"auth0":"2.30.0"},"enabled":true,"triggerId":"pre-user-registration"}' headers: Content-Type: @@ -107,11 +126,30 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets + method: GET + response: + body: '{"foo":"_VALUE_NOT_SHOWN_"}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms +- request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM method: GET response: - body: '{"id":"01G5EKEPTWPFMWHYMAAEEDF66V","name":"pre-user-reg-hook","script":"function + body: '{"id":"01G5NYTCBQZC0SZP2GKZ84CWNM","name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{"auth0":"2.30.0"},"enabled":true,"triggerId":"pre-user-registration"}' headers: Content-Type: @@ -119,6 +157,25 @@ interactions: status: 200 OK code: 200 duration: 1ms +- request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets + method: GET + response: + body: '{"foo":"_VALUE_NOT_SHOWN_"}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms - request: body: | {"name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{"auth0":"2.30.0"},"enabled":true} @@ -127,11 +184,11 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM method: PATCH response: - body: '{"id":"01G5EKEPTWPFMWHYMAAEEDF66V","name":"pre-user-reg-hook","script":"function + body: '{"id":"01G5NYTCBQZC0SZP2GKZ84CWNM","name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{"auth0":"2.30.0"},"triggerId":"pre-user-registration","enabled":true}' headers: Content-Type: @@ -147,8 +204,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V/secrets + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets method: GET response: body: '{"foo":"_VALUE_NOT_SHOWN_"}' @@ -166,8 +223,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V/secrets + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets method: POST response: body: '{}' @@ -185,8 +242,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V/secrets + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets method: PATCH response: body: '{}' @@ -204,11 +261,11 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM method: GET response: - body: '{"id":"01G5EKEPTWPFMWHYMAAEEDF66V","name":"pre-user-reg-hook","script":"function + body: '{"id":"01G5NYTCBQZC0SZP2GKZ84CWNM","name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{"auth0":"2.30.0"},"enabled":true,"triggerId":"pre-user-registration"}' headers: Content-Type: @@ -224,11 +281,30 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets + method: GET + response: + body: '{"foo":"_VALUE_NOT_SHOWN_","bar":"_VALUE_NOT_SHOWN_"}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms +- request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM method: GET response: - body: '{"id":"01G5EKEPTWPFMWHYMAAEEDF66V","name":"pre-user-reg-hook","script":"function + body: '{"id":"01G5NYTCBQZC0SZP2GKZ84CWNM","name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{"auth0":"2.30.0"},"enabled":true,"triggerId":"pre-user-registration"}' headers: Content-Type: @@ -244,11 +320,30 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets method: GET response: - body: '{"id":"01G5EKEPTWPFMWHYMAAEEDF66V","name":"pre-user-reg-hook","script":"function + body: '{"foo":"_VALUE_NOT_SHOWN_","bar":"_VALUE_NOT_SHOWN_"}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms +- request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM + method: GET + response: + body: '{"id":"01G5NYTCBQZC0SZP2GKZ84CWNM","name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{"auth0":"2.30.0"},"enabled":true,"triggerId":"pre-user-registration"}' headers: Content-Type: @@ -256,6 +351,25 @@ interactions: status: 200 OK code: 200 duration: 1ms +- request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets + method: GET + response: + body: '{"foo":"_VALUE_NOT_SHOWN_","bar":"_VALUE_NOT_SHOWN_"}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms - request: body: | {"name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{"auth0":"2.30.0"},"enabled":true} @@ -264,11 +378,11 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM method: PATCH response: - body: '{"id":"01G5EKEPTWPFMWHYMAAEEDF66V","name":"pre-user-reg-hook","script":"function + body: '{"id":"01G5NYTCBQZC0SZP2GKZ84CWNM","name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{"auth0":"2.30.0"},"triggerId":"pre-user-registration","enabled":true}' headers: Content-Type: @@ -284,8 +398,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V/secrets + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets method: GET response: body: '{"foo":"_VALUE_NOT_SHOWN_","bar":"_VALUE_NOT_SHOWN_"}' @@ -303,8 +417,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V/secrets + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets method: PATCH response: body: '{}' @@ -322,8 +436,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V/secrets + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets method: DELETE response: body: "" @@ -341,11 +455,11 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM method: GET response: - body: '{"id":"01G5EKEPTWPFMWHYMAAEEDF66V","name":"pre-user-reg-hook","script":"function + body: '{"id":"01G5NYTCBQZC0SZP2GKZ84CWNM","name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{"auth0":"2.30.0"},"enabled":true,"triggerId":"pre-user-registration"}' headers: Content-Type: @@ -361,11 +475,30 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets method: GET response: - body: '{"id":"01G5EKEPTWPFMWHYMAAEEDF66V","name":"pre-user-reg-hook","script":"function + body: '{"foo":"_VALUE_NOT_SHOWN_"}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms +- request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM + method: GET + response: + body: '{"id":"01G5NYTCBQZC0SZP2GKZ84CWNM","name":"pre-user-reg-hook","script":"function (user, context, callback) { callback(null, { user }); }","dependencies":{"auth0":"2.30.0"},"enabled":true,"triggerId":"pre-user-registration"}' headers: Content-Type: @@ -373,6 +506,25 @@ interactions: status: 200 OK code: 200 duration: 1ms +- request: + body: | + null + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM/secrets + method: GET + response: + body: '{"foo":"_VALUE_NOT_SHOWN_"}' + headers: + Content-Type: + - application/json; charset=utf-8 + status: 200 OK + code: 200 + duration: 1ms - request: body: "" form: {} @@ -380,8 +532,8 @@ interactions: Content-Type: - application/json User-Agent: - - Go-Auth0-SDK/0.6.3 - url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5EKEPTWPFMWHYMAAEEDF66V + - Go-Auth0-SDK/0.6.4 + url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/hooks/01G5NYTCBQZC0SZP2GKZ84CWNM method: DELETE response: body: "" From b609fb564387a60b00da1cc10ca742cf8d5920cf Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea Date: Thu, 16 Jun 2022 11:55:59 +0200 Subject: [PATCH 3/3] Refactor hook resource and add warnings for untracked secrets --- auth0/resource_auth0_hook.go | 99 ++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 32 deletions(-) diff --git a/auth0/resource_auth0_hook.go b/auth0/resource_auth0_hook.go index 4e5fcfb86..f6d291093 100644 --- a/auth0/resource_auth0_hook.go +++ b/auth0/resource_auth0_hook.go @@ -2,11 +2,12 @@ package auth0 import ( "context" + "fmt" "net/http" "regexp" - "github.com/auth0/go-auth0" "github.com/auth0/go-auth0/management" + "github.com/hashicorp/go-cty/cty" "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -58,9 +59,10 @@ func newHook() *schema.Resource { }, "secrets": { Type: schema.TypeMap, + Elem: schema.TypeString, + Sensitive: true, Optional: true, Description: "The secrets associated with the hook", - Elem: schema.TypeString, }, "enabled": { Type: schema.TypeBool, @@ -73,15 +75,15 @@ func newHook() *schema.Resource { } func createHook(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - hook := buildHook(d) + hook := expandHook(d) api := m.(*management.Management) if err := api.Hook.Create(hook); err != nil { return diag.FromErr(err) } - d.SetId(auth0.StringValue(hook.ID)) + d.SetId(hook.GetID()) - if err := upsertHookSecrets(d, m); err != nil { + if err := upsertHookSecrets(ctx, d, m); err != nil { return diag.FromErr(err) } @@ -101,6 +103,8 @@ func readHook(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D return diag.FromErr(err) } + diagnostics := checkForUntrackedHookSecrets(ctx, d, m) + result := multierror.Append( d.Set("name", hook.Name), d.Set("dependencies", hook.Dependencies), @@ -109,44 +113,27 @@ func readHook(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D d.Set("enabled", hook.Enabled), ) - return diag.FromErr(result.ErrorOrNil()) + if err = result.ErrorOrNil(); err != nil { + diagnostics = append(diagnostics, diag.FromErr(err)...) + } + + return diagnostics } func updateHook(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - hook := buildHook(d) + hook := expandHook(d) api := m.(*management.Management) if err := api.Hook.Update(d.Id(), hook); err != nil { return diag.FromErr(err) } - if err := upsertHookSecrets(d, m); err != nil { + if err := upsertHookSecrets(ctx, d, m); err != nil { return diag.FromErr(err) } return readHook(ctx, d, m) } -func upsertHookSecrets(d *schema.ResourceData, m interface{}) error { - if d.IsNewResource() || d.HasChange("secrets") { - secrets := Map(d, "secrets") - api := m.(*management.Management) - hookSecrets := toHookSecrets(secrets) - return api.Hook.ReplaceSecrets(d.Id(), hookSecrets) - } - - return nil -} - -func toHookSecrets(val map[string]interface{}) management.HookSecrets { - hookSecrets := management.HookSecrets{} - for key, value := range val { - if strVal, ok := value.(string); ok { - hookSecrets[key] = strVal - } - } - return hookSecrets -} - func deleteHook(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { api := m.(*management.Management) if err := api.Hook.Delete(d.Id()); err != nil { @@ -162,7 +149,43 @@ func deleteHook(ctx context.Context, d *schema.ResourceData, m interface{}) diag return nil } -func buildHook(d *schema.ResourceData) *management.Hook { +func checkForUntrackedHookSecrets(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + secretsFromConfig := Map(d, "secrets") + + api := m.(*management.Management) + secretsFromAPI, err := api.Hook.Secrets(d.Id()) + if err != nil { + return diag.FromErr(err) + } + + var warnings diag.Diagnostics + for key := range secretsFromAPI { + if _, ok := secretsFromConfig[key]; !ok { + warnings = append(warnings, diag.Diagnostic{ + Severity: diag.Warning, + Summary: "Unexpected Hook Secrets", + Detail: fmt.Sprintf("Found unexpected hook secrets with key: %s. ", key) + + "To prevent issues, manage them through terraform. If you've just imported this resource " + + "(and your secrets match), to make this warning disappear, run a terraform apply.", + AttributePath: cty.Path{cty.GetAttrStep{Name: "secrets"}}, + }) + } + } + + return warnings +} + +func upsertHookSecrets(ctx context.Context, d *schema.ResourceData, m interface{}) error { + if d.IsNewResource() || d.HasChange("secrets") { + hookSecrets := expandHookSecrets(d) + api := m.(*management.Management) + return api.Hook.ReplaceSecrets(d.Id(), hookSecrets) + } + + return nil +} + +func expandHook(d *schema.ResourceData) *management.Hook { hook := &management.Hook{ Name: String(d, "name"), Script: String(d, "script"), @@ -170,14 +193,26 @@ func buildHook(d *schema.ResourceData) *management.Hook { Enabled: Bool(d, "enabled"), } - deps := Map(d, "dependencies") - if deps != nil { + if deps := Map(d, "dependencies"); deps != nil { hook.Dependencies = &deps } return hook } +func expandHookSecrets(d *schema.ResourceData) management.HookSecrets { + hookSecrets := management.HookSecrets{} + secrets := Map(d, "secrets") + + for key, value := range secrets { + if strVal, ok := value.(string); ok { + hookSecrets[key] = strVal + } + } + + return hookSecrets +} + func validateHookName() schema.SchemaValidateDiagFunc { hookNameValidation := validation.StringMatch( regexp.MustCompile(`^[^\s-][\w -]+[^\s-]$`),