Skip to content

Commit df22c77

Browse files
Add terraform support to Okta ML Policy
1 parent 32f66c1 commit df22c77

10 files changed

+682
-0
lines changed

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "chrome",
9+
"request": "launch",
10+
"name": "Launch Chrome against localhost",
11+
"url": "http://localhost:8080",
12+
"webRoot": "${workspaceFolder}"
13+
}
14+
]
15+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func dataSourceSysdigSecureOktaMLPolicy() *schema.Resource {
13+
timeout := 5 * time.Minute
14+
15+
return &schema.Resource{
16+
ReadContext: dataSourceSysdigSecureOktaMLPolicyRead,
17+
18+
Timeouts: &schema.ResourceTimeout{
19+
Read: schema.DefaultTimeout(timeout),
20+
},
21+
22+
Schema: createOktaMLPolicyDataSourceSchema(),
23+
}
24+
}
25+
26+
func dataSourceSysdigSecureOktaMLPolicyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
27+
return oktaMLPolicyDataSourceRead(ctx, d, meta, "custom Okta ML policy", isCustomCompositePolicy)
28+
}
29+
30+
func createOktaMLPolicyDataSourceSchema() map[string]*schema.Schema {
31+
return map[string]*schema.Schema{
32+
// IMPORTANT: Type is implicit: It's automatically added upon conversion to JSON
33+
"type": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
},
37+
"name": NameSchema(),
38+
"description": DescriptionComputedSchema(),
39+
"enabled": EnabledComputedSchema(),
40+
"severity": SeverityComputedSchema(),
41+
"scope": ScopeComputedSchema(),
42+
"version": VersionSchema(),
43+
"notification_channels": NotificationChannelsComputedSchema(),
44+
"runbook": RunbookComputedSchema(),
45+
"rule": {
46+
Type: schema.TypeList,
47+
Computed: true,
48+
Elem: &schema.Resource{
49+
Schema: map[string]*schema.Schema{
50+
"id": ReadOnlyIntSchema(),
51+
"name": ReadOnlyStringSchema(),
52+
"description": DescriptionComputedSchema(),
53+
"tags": TagsSchema(),
54+
"version": VersionSchema(),
55+
"anomalous_console_login": MLRuleThresholdAndSeverityComputedSchema(),
56+
},
57+
},
58+
},
59+
}
60+
}
61+
62+
func oktaMLPolicyDataSourceRead(ctx context.Context, d *schema.ResourceData, meta any, resourceName string, validationFunc func(v2.PolicyRulesComposite) bool) diag.Diagnostics {
63+
policy, err := compositePolicyDataSourceRead(ctx, d, meta, resourceName, policyTypeOktaML, validationFunc)
64+
if err != nil {
65+
return diag.FromErr(err)
66+
}
67+
68+
err = oktaMLPolicyToResourceData(policy, d)
69+
if err != nil {
70+
return diag.FromErr(err)
71+
}
72+
73+
return nil
74+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//go:build tf_acc_sysdig_secure || tf_acc_policies || tf_acc_policies_okta
2+
3+
package sysdig_test
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
14+
"github.com/draios/terraform-provider-sysdig/sysdig"
15+
)
16+
17+
func TestAccOktaMLPolicyDataSource(t *testing.T) {
18+
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
19+
20+
resource.ParallelTest(t, resource.TestCase{
21+
PreCheck: func() {
22+
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
23+
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
24+
}
25+
},
26+
ProviderFactories: map[string]func() (*schema.Provider, error){
27+
"sysdig": func() (*schema.Provider, error) {
28+
return sysdig.Provider(), nil
29+
},
30+
},
31+
Steps: []resource.TestStep{
32+
{
33+
Config: oktaOktaMLPolicyDataSource(rText),
34+
},
35+
},
36+
})
37+
}
38+
39+
func oktaOktaMLPolicyDataSource(name string) string {
40+
return fmt.Sprintf(`
41+
resource "sysdig_secure_okta_ml_policy" "policy_1" {
42+
name = "Test Okta ML Policy %s"
43+
description = "Test Okta ML Policy Description %s"
44+
enabled = true
45+
severity = 4
46+
47+
rule {
48+
description = "Test Okta ML Rule Description"
49+
50+
anomalous_console_login {
51+
enabled = true
52+
threshold = 2
53+
}
54+
}
55+
56+
}
57+
58+
data "sysdig_secure_okta_ml_policy" "policy_2" {
59+
name = sysdig_secure_okta_ml_policy.policy_1.name
60+
depends_on = [sysdig_secure_okta_ml_policy.policy_1]
61+
}
62+
`, name, name)
63+
}

sysdig/internal/client/v2/model.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ func (r *RuntimePolicyRule) UnmarshalJSON(b []byte) error {
337337
d = &MLRuleDetails{}
338338
case "AWS_MACHINE_LEARNING":
339339
d = &AWSMLRuleDetails{}
340+
case "OKTA_MACHINE_LEARNING":
341+
d = &OktaMLRuleDetails{}
340342
case "MALWARE":
341343
d = &MalwareRuleDetails{}
342344
default:
@@ -440,6 +442,16 @@ func (p AWSMLRuleDetails) GetRuleType() ElementType {
440442
return p.RuleType
441443
}
442444

445+
type OktaMLRuleDetails struct {
446+
RuleType ElementType `json:"ruleType" yaml:"ruleType"`
447+
AnomalousConsoleLogin *MLRuleThresholdAndSeverity `json:"anomalousConsoleLogin" yaml:"anomalousConsoleLogin"`
448+
Details `json:"-"`
449+
}
450+
451+
func (p OktaMLRuleDetails) GetRuleType() ElementType {
452+
return p.RuleType
453+
}
454+
443455
type PolicyRule struct {
444456
Name string `json:"ruleName"`
445457
Enabled bool `json:"enabled"`

sysdig/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ func (p *SysdigProvider) Provider() *schema.Provider {
151151
"sysdig_monitor_team": resourceSysdigMonitorTeam(),
152152

153153
"sysdig_secure_aws_ml_policy": resourceSysdigSecureAWSMLPolicy(),
154+
"sysdig_secure_okta_ml_policy": resourceSysdigSecureOktaMLPolicy(),
154155
"sysdig_secure_cloud_account": resourceSysdigSecureCloudAccount(),
155156
"sysdig_secure_cloud_auth_account": resourceSysdigSecureCloudauthAccount(),
156157
"sysdig_secure_cloud_auth_account_component": resourceSysdigSecureCloudauthAccountComponent(),
@@ -217,6 +218,7 @@ func (p *SysdigProvider) Provider() *schema.Provider {
217218

218219
"sysdig_secure_agentless_scanning_assets": dataSourceSysdigSecureAgentlessScanningAssets(),
219220
"sysdig_secure_aws_ml_policy": dataSourceSysdigSecureAWSMLPolicy(),
221+
"sysdig_secure_okta_ml_policy": dataSourceSysdigSecureOktaMLPolicy(),
220222
"sysdig_secure_cloud_ingestion_assets": dataSourceSysdigSecureCloudIngestionAssets(),
221223
"sysdig_secure_connection": dataSourceSysdigSecureConnection(),
222224
"sysdig_secure_custom_policy": dataSourceSysdigSecureCustomPolicy(),

0 commit comments

Comments
 (0)