-
Notifications
You must be signed in to change notification settings - Fork 3
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
ENG-14563: Policy Sets #579
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
093e104
Added policy wizards API. Note! not including the listing of wizards,…
gengdahlCyral b4af863
Fixed invalid policy set resource template
gengdahlCyral fb28fbb
Rename policywizardv1 to policysetv1 and improve URL handling
gengdahlCyral 9cd63b0
removed "v1" from policy set go package name.
gengdahlCyral 41a5302
Added MaxItem 1 for scope, and switched to set instead of list.
gengdahlCyral 4b5b8aa
Revert to list for scope.
gengdahlCyral 486d873
DefaultContextHandler => HTTPContextHandler
yoursnerdly 375715e
use gRPC to implement policy sets
yoursnerdly 6a3db1a
Removed un-used constant apiPathPolicySet, updated dependencies and r…
gengdahlCyral File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package policyset | ||
|
||
const ( | ||
resourceName = "cyral_policy_set" | ||
dataSourceName = resourceName | ||
apiPathPolicySet = "v1/policyWizards/policySets" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package policyset | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/cyralinc/terraform-provider-cyral/cyral/core" | ||
"github.com/cyralinc/terraform-provider-cyral/cyral/core/types/resourcetype" | ||
) | ||
|
||
var dsContextHandler = core.ContextHandler{ | ||
ResourceName: dataSourceName, | ||
ResourceType: resourcetype.DataSource, | ||
Read: readPolicySet, | ||
} | ||
|
||
func dataSourceSchema() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "This data source provides information about a policy set.", | ||
ReadContext: dsContextHandler.ReadContext, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Description: "Identifier for the policy set.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"wizard_id": { | ||
Description: "The ID of the policy wizard used to create this policy set.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Description: "Name of the policy set.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"description": { | ||
Description: "Description of the policy set.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"tags": { | ||
Description: "Tags associated with the policy set.", | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"scope": { | ||
Description: "Scope of the policy set.", | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"repo_ids": { | ||
Description: "List of repository IDs that are in scope. Empty list means all repositories are in scope.", | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"wizard_parameters": { | ||
Description: "Parameters passed to the wizard while creating the policy set.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"enabled": { | ||
Description: "Indicates if the policy set is enabled.", | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"policies": { | ||
Description: "List of policies that comprise the policy set.", | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"type": { | ||
Description: "Type of the policy.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"id": { | ||
Description: "ID of the policy.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"last_updated": { | ||
Description: "Information about when and by whom the policy set was last updated.", | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"created": { | ||
Description: "Information about when and by whom the policy set was created.", | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package policyset | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
methods "buf.build/gen/go/cyral/policy/grpc/go/policy/v1/policyv1grpc" | ||
msg "buf.build/gen/go/cyral/policy/protocolbuffers/go/policy/v1" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/cyralinc/terraform-provider-cyral/cyral/client" | ||
"github.com/cyralinc/terraform-provider-cyral/cyral/utils" | ||
) | ||
|
||
// ToMap converts PolicySetPolicy to a map | ||
func policySetPolicyToMap(p *msg.PolicySetPolicy) map[string]interface{} { | ||
return map[string]interface{}{ | ||
"type": p.GetType().String(), | ||
"id": p.GetId(), | ||
} | ||
} | ||
|
||
func policiesToMaps(policies []*msg.PolicySetPolicy) []map[string]interface{} { | ||
var result []map[string]interface{} | ||
for _, policy := range policies { | ||
result = append(result, policySetPolicyToMap(policy)) | ||
} | ||
return result | ||
} | ||
|
||
// changeInfoToMap converts ChangeInfo to a map | ||
func changeInfoToMap(c *msg.ChangeInfo) map[string]interface{} { | ||
return map[string]interface{}{ | ||
"actor": c.GetActor(), | ||
"actor_type": c.GetActorType().String(), | ||
"timestamp": c.GetTimestamp().AsTime().Format(time.RFC3339), | ||
} | ||
} | ||
|
||
// scopeToMap converts Scope to a list of maps | ||
func scopeToMap(s *msg.Scope) []map[string]interface{} { | ||
return []map[string]interface{}{ | ||
{ | ||
"repo_ids": s.GetRepoIds(), | ||
}, | ||
} | ||
} | ||
|
||
// updateSchema writes the policy set data to the schema | ||
func updateSchema(ps *msg.PolicySet, d *schema.ResourceData) error { | ||
if err := d.Set("id", ps.GetId()); err != nil { | ||
return fmt.Errorf("error setting 'id' field: %w", err) | ||
} | ||
if err := d.Set("wizard_id", ps.GetWizardId()); err != nil { | ||
return fmt.Errorf("error setting 'id' field: %w", err) | ||
} | ||
if err := d.Set("name", ps.GetName()); err != nil { | ||
return fmt.Errorf("error setting 'name' field: %w", err) | ||
} | ||
if err := d.Set("description", ps.GetDescription()); err != nil { | ||
return fmt.Errorf("error setting 'description' field: %w", err) | ||
} | ||
if err := d.Set("enabled", ps.GetEnabled()); err != nil { | ||
return fmt.Errorf("error setting 'enabled' field: %w", err) | ||
} | ||
if err := d.Set("tags", ps.GetTags()); err != nil { | ||
return fmt.Errorf("error setting 'tags' field: %w", err) | ||
} | ||
if err := d.Set("wizard_parameters", ps.GetWizardParameters()); err != nil { | ||
return fmt.Errorf("error setting 'document' field: %w", err) | ||
} | ||
|
||
if err := d.Set("policies", policiesToMaps(ps.GetPolicies())); err != nil { | ||
return fmt.Errorf("error setting 'policies' field: %w", err) | ||
} | ||
if ps.GetScope() != nil { | ||
if err := d.Set("scope", scopeToMap(ps.GetScope())); err != nil { | ||
return fmt.Errorf("error setting 'scope' field: %w", err) | ||
} | ||
} | ||
// Use the changeInfoToMap method to set the last_updated and created fields | ||
if err := d.Set("last_updated", changeInfoToMap(ps.GetLastUpdated())); err != nil { | ||
return fmt.Errorf("error setting 'last_updated' field: %w", err) | ||
} | ||
if err := d.Set("created", changeInfoToMap(ps.GetCreated())); err != nil { | ||
return fmt.Errorf("error setting 'created' field: %w", err) | ||
} | ||
d.SetId(ps.GetId()) | ||
return nil | ||
} | ||
|
||
func policySetFromSchema(d *schema.ResourceData) *msg.PolicySet { | ||
p := &msg.PolicySet{ | ||
Id: d.Get("id").(string), | ||
Name: d.Get("name").(string), | ||
Description: d.Get("description").(string), | ||
Enabled: d.Get("enabled").(bool), | ||
Tags: utils.ConvertFromInterfaceList[string](d.Get("tags").([]interface{})), | ||
WizardId: d.Get("wizard_id").(string), | ||
WizardParameters: d.Get("wizard_parameters").(string), | ||
} | ||
|
||
if v, ok := d.GetOk("scope"); ok { | ||
p.Scope = scopeFromInterface(v.([]interface{})) | ||
} | ||
return p | ||
} | ||
|
||
// scopeFromInterface converts the map to a Scope struct | ||
func scopeFromInterface(s []interface{}) *msg.Scope { | ||
if len(s) == 0 || s[0] == nil { | ||
return nil | ||
} | ||
m := s[0].(map[string]interface{}) | ||
return &msg.Scope{ | ||
RepoIds: utils.ConvertFromInterfaceList[string](m["repo_ids"].([]interface{})), | ||
} | ||
} | ||
|
||
func createPolicySet(ctx context.Context, cl *client.Client, rd *schema.ResourceData) error { | ||
ps := policySetFromSchema(rd) | ||
req := &msg.CreatePolicySetRequest{ | ||
PolicySet: ps, | ||
} | ||
grpcClient := methods.NewPolicyWizardServiceClient(cl.GRPCClient()) | ||
resp, err := grpcClient.CreatePolicySet(ctx, req) | ||
if err != nil { | ||
return err | ||
} | ||
rd.SetId(resp.GetPolicySet().GetId()) | ||
return nil | ||
} | ||
|
||
func readPolicySet(ctx context.Context, cl *client.Client, rd *schema.ResourceData) error { | ||
req := &msg.ReadPolicySetRequest{ | ||
Id: rd.Get("id").(string), | ||
} | ||
grpcClient := methods.NewPolicyWizardServiceClient(cl.GRPCClient()) | ||
resp, err := grpcClient.ReadPolicySet(ctx, req) | ||
if err != nil { | ||
return err | ||
} | ||
return updateSchema(resp.GetPolicySet(), rd) | ||
} | ||
|
||
func updatePolicySet(ctx context.Context, cl *client.Client, rd *schema.ResourceData) error { | ||
ps := policySetFromSchema(rd) | ||
req := &msg.UpdatePolicySetRequest{ | ||
Id: ps.GetId(), | ||
PolicySet: ps, | ||
} | ||
grpcClient := methods.NewPolicyWizardServiceClient(cl.GRPCClient()) | ||
resp, err := grpcClient.UpdatePolicySet(ctx, req) | ||
if err != nil { | ||
return err | ||
} | ||
return updateSchema(resp.GetPolicySet(), rd) | ||
} | ||
|
||
func deletePolicySet(ctx context.Context, cl *client.Client, rd *schema.ResourceData) error { | ||
req := &msg.DeletePolicySetRequest{ | ||
Id: rd.Get("id").(string), | ||
} | ||
grpcClient := methods.NewPolicyWizardServiceClient(cl.GRPCClient()) | ||
_, err := grpcClient.DeletePolicySet(ctx, req) | ||
return err | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unused and was inadvertently left over in the previous PR.