Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4/X] [DXCDT-175] Add recovery code to guardian resource and refactor #216

Merged
merged 7 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 32 additions & 59 deletions auth0/resource_auth0_guardian.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func newGuardian() *schema.Resource {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
MinItems: 0,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gets ignored completely if optional is true.

Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"user_verification": {
Expand Down Expand Up @@ -71,7 +70,6 @@ func newGuardian() *schema.Resource {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
MinItems: 0,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"override_relying_party": {
Expand All @@ -92,7 +90,6 @@ func newGuardian() *schema.Resource {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
MinItems: 0,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"provider": {
Expand All @@ -119,7 +116,6 @@ func newGuardian() *schema.Resource {
Optional: true,
Computed: true,
MaxItems: 1,
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enrollment_message": {
Expand Down Expand Up @@ -163,11 +159,15 @@ func newGuardian() *schema.Resource {
Optional: true,
Default: false,
},
"recovery_code": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"duo": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
MinItems: 0,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"integration_key": {
Expand All @@ -190,14 +190,12 @@ func newGuardian() *schema.Resource {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
MinItems: 0,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"amazon_sns": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"aws_access_key_id": {
Expand Down Expand Up @@ -262,15 +260,12 @@ func createGuardian(ctx context.Context, d *schema.ResourceData, m interface{})
func readGuardian(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

multiFactorPolicies, err := api.Guardian.MultiFactor.Policy()
flattenedPolicy, err := flattenMultiFactorPolicy(api)
if err != nil {
return diag.FromErr(err)
}

result := multierror.Append(d.Set("policy", "never"))
if len(*multiFactorPolicies) > 0 {
result = multierror.Append(result, d.Set("policy", (*multiFactorPolicies)[0]))
}
result := multierror.Append(d.Set("policy", flattenedPolicy))
Comment on lines -265 to +268
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracting this logic somewhere else so it's easier to read through the readGuardian steps.


multiFactorList, err := api.Guardian.MultiFactor.List()
if err != nil {
Expand All @@ -283,6 +278,8 @@ func readGuardian(ctx context.Context, d *schema.ResourceData, m interface{}) di
result = multierror.Append(result, d.Set("email", factor.GetEnabled()))
case "otp":
result = multierror.Append(result, d.Set("otp", factor.GetEnabled()))
case "recovery-code":
result = multierror.Append(result, d.Set("recovery_code", factor.GetEnabled()))
case "sms":
result = multierror.Append(result, d.Set("phone", nil))

Expand Down Expand Up @@ -347,34 +344,18 @@ func readGuardian(ctx context.Context, d *schema.ResourceData, m interface{}) di
func updateGuardian(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

if err := updatePolicy(d, api); err != nil {
return diag.FromErr(err)
}

if err := updateEmailFactor(d, api); err != nil {
return diag.FromErr(err)
}
if err := updateOTPFactor(d, api); err != nil {
return diag.FromErr(err)
}

if err := updatePhoneFactor(d, api); err != nil {
return diag.FromErr(err)
}

if err := updateWebAuthnRoaming(d, api); err != nil {
return diag.FromErr(err)
}

if err := updateWebAuthnPlatform(d, api); err != nil {
return diag.FromErr(err)
}

if err := updateDUO(d, api); err != nil {
return diag.FromErr(err)
}

if err := updatePush(d, api); err != nil {
result := multierror.Append(
updatePolicy(d, api),
updateEmailFactor(d, api),
updateOTPFactor(d, api),
updateRecoveryCodeFactor(d, api),
updatePhoneFactor(d, api),
updateWebAuthnRoaming(d, api),
updateWebAuthnPlatform(d, api),
updateDUO(d, api),
updatePush(d, api),
)
Comment on lines +347 to +357
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better if we try al of these updates and report on any accumulated errors instead of failing at the first and not doing any of the others.

if err := result.ErrorOrNil(); err != nil {
return diag.FromErr(err)
}

Expand All @@ -384,25 +365,17 @@ func updateGuardian(ctx context.Context, d *schema.ResourceData, m interface{})
func deleteGuardian(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

if err := api.Guardian.MultiFactor.Phone.Enable(false); err != nil {
return diag.FromErr(err)
}
if err := api.Guardian.MultiFactor.Email.Enable(false); err != nil {
return diag.FromErr(err)
}
if err := api.Guardian.MultiFactor.OTP.Enable(false); err != nil {
return diag.FromErr(err)
}
if err := api.Guardian.MultiFactor.WebAuthnRoaming.Enable(false); err != nil {
return diag.FromErr(err)
}
if err := api.Guardian.MultiFactor.WebAuthnPlatform.Enable(false); err != nil {
return diag.FromErr(err)
}
if err := api.Guardian.MultiFactor.DUO.Enable(false); err != nil {
return diag.FromErr(err)
}
if err := api.Guardian.MultiFactor.Push.Enable(false); err != nil {
result := multierror.Append(
api.Guardian.MultiFactor.Phone.Enable(false),
api.Guardian.MultiFactor.Email.Enable(false),
api.Guardian.MultiFactor.OTP.Enable(false),
api.Guardian.MultiFactor.RecoveryCode.Enable(false),
api.Guardian.MultiFactor.WebAuthnRoaming.Enable(false),
api.Guardian.MultiFactor.WebAuthnPlatform.Enable(false),
api.Guardian.MultiFactor.DUO.Enable(false),
api.Guardian.MultiFactor.Push.Enable(false),
Comment on lines +368 to +376
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better if we try al of these updates and report on any accumulated errors instead of failing at the first and not doing any of the others.

)
if err := result.ErrorOrNil(); err != nil {
return diag.FromErr(err)
}

Expand Down
Loading