Skip to content

Commit

Permalink
Fixed the column rules value should populate all the properties cor…
Browse files Browse the repository at this point in the history
…rectly for the tables okta_signon_policy, okta_password_policy, okta_idp_discovery_policy and okta_authentication_policy Closes #144 (#145)
  • Loading branch information
ParthaI authored Sep 11, 2024
1 parent b00fe86 commit bdba51e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
28 changes: 21 additions & 7 deletions okta/table_okta_signon_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ func getOktaPolicyRules(ctx context.Context, d *plugin.QueryData, h *plugin.Hydr
return nil, nil
}

client, err := Connect(ctx, d)
client, err := ConnectV4(ctx, d)
if err != nil {
logger.Error("getOktaPolicyRules", "connect_error", err)
return nil, err
}

var rules []*okta.PolicyRule
var rules []oktaV4.ListPolicyRules200ResponseInner

policyRules, resp, err := client.Policy.ListPolicyRules(ctx, policyId)
policyRules, resp, err := client.PolicyAPI.ListPolicyRules(ctx, policyId).Execute()
if err != nil {
logger.Error("getOktaPolicyRules", "list_policies_error", err)
return nil, err
Expand All @@ -136,16 +136,30 @@ func getOktaPolicyRules(ctx context.Context, d *plugin.QueryData, h *plugin.Hydr

// paging
for resp.HasNextPage() {
var nextPolicyRules []*okta.PolicyRule
resp, err = resp.Next(ctx, &nextPolicyRules)
var nextPolicyRules []*oktaV4.ListPolicyRules200ResponseInner
resp, err = resp.Next(&nextPolicyRules)
if err != nil {
logger.Error("getOktaPolicyRules", "list_policies_paging_error", err)
return nil, err
}
rules = append(rules, nextPolicyRules...)
for _, r := range nextPolicyRules {
rules = append(rules, *r)
}
}

var allRules []interface{}
for _, rule := range rules {
r := rule.GetActualInstance()
// We need to extract the inner properties; otherwise, the values will be populated as null.
result, err := structToMap(r)
if err != nil {
logger.Error("getOktaPolicyRules", "error in parsing the rules for the policy:", policyId, err)
return nil, err
}
allRules = append(allRules, result)
}

return rules, nil
return allRules, nil
}

func getOktaPolicyAssociatedResources(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
Expand Down
39 changes: 39 additions & 0 deletions okta/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package okta

import (
"fmt"
"reflect"

"github.com/ettle/strcase"
"github.com/turbot/go-kit/helpers"
Expand Down Expand Up @@ -48,3 +49,41 @@ func buildQueryFilter(equalQuals plugin.KeyColumnEqualsQualMap, filterKeys []str

return filters
}

// StructToMap converts the fields of a struct from interface{} to map[string]interface{}
func structToMap(input interface{}) (map[string]interface{}, error) {
// Create the result map
if input == nil {
return nil, nil
}
result := make(map[string]interface{})

// Get the value and type of the input
value := reflect.ValueOf(input)
typ := reflect.TypeOf(input)

// Ensure the input is a struct or a pointer to a struct
if value.Kind() == reflect.Ptr {
value = value.Elem()
typ = typ.Elem()
}

if value.Kind() != reflect.Struct {
return nil, fmt.Errorf("input must be a struct or a pointer to a struct")
}

// Iterate over struct fields
for i := 0; i < value.NumField(); i++ {
// Get field name and value
field := value.Field(i)
fieldType := typ.Field(i)
fieldName := fieldType.Name

// Only exportable fields (starting with an uppercase letter) can be accessed
if field.CanInterface() {
result[fieldName] = field.Interface()
}
}

return result, nil
}

0 comments on commit bdba51e

Please sign in to comment.