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

fix priority mismatch when adding new mfa policy, leading to update error #2130

Merged
merged 2 commits into from
Nov 7, 2024
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
21 changes: 21 additions & 0 deletions examples/resources/okta_policy_mfa_default/priority.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
resource "okta_factor" "okta_email" {
provider_id = "okta_email"
}

resource "okta_factor" "okta_password" {
provider_id = "okta_password"
}

resource "okta_policy_mfa_default" "test" {
is_oie = true

okta_password = {
enroll = "REQUIRED"
}

okta_email = {
enroll = "REQUIRED"
}

depends_on = [okta_factor.okta_email, okta_factor.okta_password]
}
42 changes: 42 additions & 0 deletions examples/resources/okta_policy_mfa_default/priority_updated.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
data "okta_group" "all" {
name = "Everyone"
}

resource "okta_policy_mfa" "test" {
name = "testAcc_replace_with_uuid_new"
status = "INACTIVE"
description = "Terraform Acceptance Test MFA Policy Updated"
groups_included = [data.okta_group.all.id]

okta_email = {
enroll = "REQUIRED"
}

okta_password = {
enroll = "OPTIONAL"
}

depends_on = [okta_factor.okta_email, okta_factor.okta_password]
}

resource "okta_factor" "okta_email" {
provider_id = "okta_email"
}

resource "okta_factor" "okta_password" {
provider_id = "okta_password"
}

resource "okta_policy_mfa_default" "test" {
is_oie = true

okta_password = {
enroll = "REQUIRED"
}

okta_email = {
enroll = "OPTIONAL"
}

depends_on = [okta_factor.okta_email, okta_factor.okta_password]
}
17 changes: 9 additions & 8 deletions okta/resource_okta_policy_mfa_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ This resource allows you to configure default MFA Policy.
}

func resourcePolicyMfaDefaultCreateOrUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
id := d.Id()
if id == "" {
policy, err := setDefaultPolicy(ctx, d, m, sdk.MfaPolicyType)
if err != nil {
return diag.FromErr(err)
}
id = policy.Id
var id string
// Issue #2107, where adding a new MFA_ENROLL policy change the priority of the default policy, leading to the default policy unable to update
// It is now required that the default policy is set again for every create and update, and the only thing that can be change is factor/authenticator
policy, err := setDefaultPolicy(ctx, d, m, sdk.MfaPolicyType)
if err != nil {
return diag.FromErr(err)
}
_, _, err := getAPISupplementFromMetadata(m).UpdatePolicy(ctx, id, buildDefaultMFAPolicy(d))
id = policy.Id

_, _, err = getAPISupplementFromMetadata(m).UpdatePolicy(ctx, id, buildDefaultMFAPolicy(d))
if err != nil {
return diag.Errorf("failed to update default MFA policy: %v", err)
}
Expand Down
36 changes: 36 additions & 0 deletions okta/resource_okta_policy_mfa_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,39 @@ resource "okta_policy_mfa_default" "test" {
},
})
}

func TestAccResourceOktaDefaultMFAPolicyIssue2107(t *testing.T) {
mgr := newFixtureManager("resources", policyMfaDefault, t.Name())
config := mgr.GetFixtures("priority.tf", t)
updatedConfig := mgr.GetFixtures("priority_updated.tf", t)
resourceName := fmt.Sprintf("%s.test", policyMfaDefault)

oktaResourceTest(t, resource.TestCase{
PreCheck: testAccPreCheck(t),
ErrorCheck: testAccErrorChecks(t),
ProviderFactories: testAccProvidersFactories,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
ensurePolicyExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "status", statusActive),
resource.TestCheckResourceAttr(resourceName, "priority", "1"),
resource.TestCheckResourceAttr(resourceName, "okta_password.enroll", "REQUIRED"),
resource.TestCheckResourceAttr(resourceName, "okta_email.enroll", "REQUIRED"),
),
},
{
Config: updatedConfig,
Check: resource.ComposeTestCheckFunc(
ensurePolicyExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "status", statusActive),
resource.TestCheckResourceAttr(resourceName, "priority", "2"),
resource.TestCheckResourceAttr(resourceName, "okta_password.enroll", "REQUIRED"),
resource.TestCheckResourceAttr(resourceName, "okta_email.enroll", "OPTIONAL"),
),
},
},
})
}
Loading