Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 10 additions & 0 deletions examples/data-sources/okta_captcha/datasource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "okta_captcha" "test" {
name = "testAcc_replace_with_uuid"
type = "HCAPTCHA"
site_key = "random_key"
secret_key = "random_secret_key"
}

data "okta_captcha" "test" {
id = okta_captcha.test.id
}
15 changes: 15 additions & 0 deletions examples/data-sources/okta_org_captcha/datasource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
resource "okta_captcha" "test" {
name = "testAcc_replace_with_uuid"
type = "HCAPTCHA"
site_key = "random_key"
secret_key = "random_secret_key"
}

resource "okta_org_captcha" "test" {
captcha_id = okta_captcha.test.id
enabled_pages = ["SIGN_IN"]
}

data "okta_org_captcha" "test" {
depends_on = [okta_org_captcha.test]
}
2 changes: 1 addition & 1 deletion examples/resources/okta_captcha/basic.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ resource "okta_captcha" "test" {
name = "testAcc_replace_with_uuid"
type = "HCAPTCHA"
site_key = "random_key"
secret_key = "random_key"
secret_key = "random_secret_key"
}
11 changes: 11 additions & 0 deletions examples/resources/okta_org_captcha/basic.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "okta_captcha" "test_captcha" {
name = "testAcc_replace_with_uuid"
type = "HCAPTCHA"
site_key = "random_key"
secret_key = "random_secret_key"
}

resource "okta_org_captcha" "test" {
captcha_id = okta_captcha.test_captcha.id
enabled_pages = ["SIGN_IN"]
}
2 changes: 2 additions & 0 deletions examples/resources/okta_org_captcha/empty.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resource "okta_org_captcha" "test" {
}
11 changes: 11 additions & 0 deletions examples/resources/okta_org_captcha/updated.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "okta_captcha" "test_captcha" {
name = "testAcc_replace_with_uuid"
type = "HCAPTCHA"
site_key = "random_key"
secret_key = "random_secret_key"
}

resource "okta_org_captcha" "test" {
captcha_id = okta_captcha.test_captcha.id
enabled_pages = ["SSO", "SSPR", "SIGN_IN"]
}
1 change: 1 addition & 0 deletions okta/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const (
OktaIDaaSBrand = "okta_brand"
OktaIDaaSBrands = "okta_brands"
OktaIDaaSCaptcha = "okta_captcha"
OktaIDaaSCaptchaOrgWide = "okta_org_captcha"
OktaIDaaSCaptchaOrgWideSettings = "okta_captcha_org_wide_settings"
OktaIDaaSDefaultPolicy = "okta_default_policy"
OktaIDaaSDomain = "okta_domain"
Expand Down
107 changes: 107 additions & 0 deletions okta/services/idaas/data_source_okta_captcha.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2025 - Present Okta, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package idaas

import (
"context"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/okta/terraform-provider-okta/okta/config"
)

var (
_ datasource.DataSource = &captchaDataSource{}
_ datasource.DataSourceWithConfigure = &captchaDataSource{}
)

// CaptchaDataSource defines the data source implementation.
type captchaDataSource struct {
Config *config.Config
}

// CaptchaDataSourceModel describes the data source data model.
type captchaDataSourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
SiteKey types.String `tfsdk:"site_key"`
Type types.String `tfsdk:"type"`
}

func newCaptchaDataSource() datasource.DataSource {
return &captchaDataSource{}
}

func (d *captchaDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_captcha"
}

func (d *captchaDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
d.Config = dataSourceConfiguration(req, resp)
}

func (d *captchaDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Retrieves the properties of a specified CAPTCHA instance",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "Unique identifier of the captcha.",
Required: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the CAPTCHA instance",
Computed: true,
},
"site_key": schema.StringAttribute{
MarkdownDescription: "The site key issued from the CAPTCHA provider to render a CAPTCHA on a page",
Computed: true,
},
"type": schema.StringAttribute{
MarkdownDescription: "The type of CAPTCHA provider",
Computed: true,
},
},
}
}

func (d *captchaDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state captchaDataSourceModel

resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

client := d.Config.OktaIDaaSClient.OktaSDKClientV6()
id := state.ID.ValueString()
result, httpResp, err := client.CAPTCHAAPI.GetCaptchaInstance(ctx, id).Execute()
if err != nil {
if httpResp != nil && httpResp.StatusCode == http.StatusNotFound {
resp.Diagnostics.AddError("Not Found", "captcha with the given ID was not found.")
return
}
resp.Diagnostics.AddError("Error reading captcha", err.Error())
return
}
state.ID = types.StringValue(string(result.GetId()))
state.Name = types.StringValue(string(result.GetName()))
state.SiteKey = types.StringValue(string(result.GetSiteKey()))
state.Type = types.StringValue(string(result.GetType()))

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
31 changes: 31 additions & 0 deletions okta/services/idaas/data_source_okta_captcha_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package idaas_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/okta/terraform-provider-okta/okta/acctest"
)

func TestAccDataSourceOktaCaptcha_read(t *testing.T) {
mgr := newFixtureManager("data-sources", "okta_captcha", t.Name())
config := mgr.GetFixtures("datasource.tf", t)
resourceName := "data.okta_captcha.test"

acctest.OktaResourceTest(t, resource.TestCase{
PreCheck: acctest.AccPreCheck(t),
ErrorCheck: testAccErrorChecks(t),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactoriesForTestAcc(t),
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttrSet(resourceName, "name"),
resource.TestCheckResourceAttrSet(resourceName, "site_key"),
resource.TestCheckResourceAttrSet(resourceName, "type"),
),
},
},
})
}
102 changes: 102 additions & 0 deletions okta/services/idaas/data_source_okta_org_captcha.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2025 - Present Okta, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package idaas

import (
"context"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/okta/terraform-provider-okta/okta/config"
)

var (
_ datasource.DataSource = &orgCaptchaDataSource{}
_ datasource.DataSourceWithConfigure = &orgCaptchaDataSource{}
)

// OrgCaptchaDataSource defines the data source implementation.
type orgCaptchaDataSource struct {
Config *config.Config
}

// OrgCaptchaDataSourceModel describes the data source data model.
type orgCaptchaDataSourceModel struct {
ID types.String `tfsdk:"id"`
CaptchaId types.String `tfsdk:"captcha_id"`
EnabledPages types.List `tfsdk:"enabled_pages"`
}

func newOrgCaptchaDataSource() datasource.DataSource {
return &orgCaptchaDataSource{}
}

func (d *orgCaptchaDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_org_captcha"
}

func (d *orgCaptchaDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
d.Config = dataSourceConfiguration(req, resp)
}

func (d *orgCaptchaDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Retrieves the CAPTCHA settings object for your organization > **Note**: If the current organization hasn't configured CAPTCHA Settings, the request returns an empty object.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "Unique identifier of the org_captcha.",
Optional: true,
Computed: true,
},
"captcha_id": schema.StringAttribute{
MarkdownDescription: "The unique key of the associated CAPTCHA instance",
Computed: true,
},
"enabled_pages": schema.ListAttribute{
MarkdownDescription: "An array of pages that have CAPTCHA enabled",
ElementType: types.StringType,
Computed: true,
},
},
}
}

func (d *orgCaptchaDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state orgCaptchaDataSourceModel

resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

client := d.Config.OktaIDaaSClient.OktaSDKClientV6()
result, httpResp, err := client.CAPTCHAAPI.GetOrgCaptchaSettings(ctx).Execute()
if err != nil {
if httpResp != nil && httpResp.StatusCode == http.StatusNotFound {
resp.Diagnostics.AddError("Not Found", "org_captcha with the given ID was not found.")
return
}
resp.Diagnostics.AddError("Error reading org_captcha", err.Error())
return
}
state.ID = types.StringValue("org_captcha")
_ = result // IDExpr may not reference result; prevent "declared and not used"
state.CaptchaId = types.StringValue(string(result.GetCaptchaId()))

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
29 changes: 29 additions & 0 deletions okta/services/idaas/data_source_okta_org_captcha_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package idaas_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/okta/terraform-provider-okta/okta/acctest"
)

func TestAccDataSourceOktaOrgCaptcha_read(t *testing.T) {
mgr := newFixtureManager("data-sources", "okta_org_captcha", t.Name())
config := mgr.GetFixtures("datasource.tf", t)
resourceName := "data.okta_org_captcha.test"

acctest.OktaResourceTest(t, resource.TestCase{
PreCheck: acctest.AccPreCheck(t),
ErrorCheck: testAccErrorChecks(t),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactoriesForTestAcc(t),
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttrSet(resourceName, "captcha_id"),
),
},
},
})
}
6 changes: 4 additions & 2 deletions okta/services/idaas/idaas.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func FWProviderResources() []func() resource.Resource {
newIdentitySourceGroupMembershipResource,
newIdentitySourceImportResource,
newIdentitySourceUserResource,
newCaptchaResource,
newOrgCaptchaResource,
}
// Wrap all resources with SafeResource for panic recovery
return resources.WrapResources(rawResources)
Expand Down Expand Up @@ -184,6 +186,8 @@ func FWProviderDataSources() []func() datasource.DataSource {
newAuthorizationServersPoliciesRuleDataSource,
newIamAssigneesUserDataSource,
newIamResourceSetDataSource,
newCaptchaDataSource,
newOrgCaptchaDataSource,
}
}

Expand Down Expand Up @@ -221,8 +225,6 @@ func ProviderResources() map[string]*schema.Resource {
resources.OktaIDaaSAuthServerPolicyRule: resourceAuthServerPolicyRule(),
resources.OktaIDaaSAuthServerScope: resourceAuthServerScope(),
resources.OktaIDaaSBehavior: resourceBehavior(),
resources.OktaIDaaSCaptcha: resourceCaptcha(),
resources.OktaIDaaSCaptchaOrgWideSettings: resourceCaptchaOrgWideSettings(),
resources.OktaIDaaSDomain: resourceDomain(),
resources.OktaIDaaSDomainCertificate: resourceDomainCertificate(),
resources.OktaIDaaSDomainVerification: resourceDomainVerification(),
Expand Down
Loading