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

ENG-10802: Support regopolicy instance API (OOTB policies) #345

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
110 changes: 110 additions & 0 deletions cyral/data_source_cyral_regopolicy_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package cyral

import (
"fmt"
"net/http"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/cyralinc/terraform-provider-cyral/client"
)

type GetRegopolicyInstancesResponse struct {
Instances []*PolicyInstance `json:"instances"`
}

func (resp *GetRegopolicyInstancesResponse) WriteToSchema(d *schema.ResourceData) error {
if err := writeRegopolicyInstancesToDataSourceSchema(resp.Instances, d); err != nil {
return err
}
// creating a new id for data source
d.SetId(uuid.New().String())
return nil
}

func writeRegopolicyInstancesToDataSourceSchema(instances []*PolicyInstance, d *schema.ResourceData) error {
var instancesList []interface{}
for _, instance := range instances {
instancesList = append(instancesList, map[string]interface{}{
"name": instance.Name,
"description": instance.Description,
"template_id": instance.TemplateId,
"tags": instance.TagsAsInterface(),
})
}
if err := d.Set("regopolicy_instance_list", instancesList); err != nil {
return err
}
return nil
}

func dataSourceRegopolicyInstanceReadConfig() ResourceOperationConfig {
return ResourceOperationConfig{
Name: "DatalabelDataSourceRead",
HttpMethod: http.MethodGet,
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
nameFilter := d.Get("name").(string)
typeFilter := d.Get("type").(string)
var pathParams string
if nameFilter != "" {
pathParams = fmt.Sprintf("/%s", nameFilter)
}
queryParams := urlQuery(map[string]string{
"type": typeFilter,
})

return fmt.Sprintf("https://%s/v1/datalabels%s%s", c.ControlPlane, pathParams, queryParams)
},
NewResponseData: func(d *schema.ResourceData) ResponseData {
nameFilter := d.Get("name").(string)
if nameFilter == "" {
return &GetDataLabelsResponse{}
} else {
return &GetDataLabelResponse{}
}
},
}
}

func dataSourceRegopolicyInstance() *schema.Resource {
return &schema.Resource{
Description: "Retrieve and filter policy instances. See also resource [`cyral_regopolicy`](../resources/regopolicy_instance.md).",
ReadContext: ReadResource(dataSourceDatalabelReadConfig()),
Schema: map[string]*schema.Schema{
"regopolicy_instance_list": {
Description: "List of existing regopolicy instances satisfying given filter criteria.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{

"name": {
Description: "Name of the policy instance.",
Type: schema.TypeString,
Required: true,
},
"description": {
Description: "Description for the policy instance.",
Type: schema.TypeString,
Required: true,
},
"template_id": {
Description: "Template Id on which the instance was based",
Type: schema.TypeString,
Required: true,
},
"tags": {
Description: "Tags used to categorize policy instance.",
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
},
},
},
},
}
}
116 changes: 116 additions & 0 deletions cyral/model_regopolicy_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cyral

import (
"google.golang.org/protobuf/types/known/timestamppb"
)

type Scope struct {
RepoIds []string `json:"repoIds,omitempty"`
}

const (
actorUser = "USER"
actorApiClient = "API_CLIENT"
)

func actorTypes() []string {
return []string{
actorUser,
actorApiClient,
}
}

const (
categoryTypeUnknown = "UNKNOWN"
categoryTypePredefined = "SECURITY"
categoryTypeCustom = "GRANT"
categoryUserDefined = "USER_DEFINED"
)

func categoryTypes() []string {
return []string{
categoryTypeUnknown,
categoryTypePredefined,
categoryTypeCustom,
categoryUserDefined,
}
}

type ChangeInfo struct {
Actor string `json:"actor,omitempty"`
ActorType string `json:"actorType,omitempty"`
Timestamp *timestamppb.Timestamp `json:"timestamp,omitempty"`
}

type Key struct {
Id string `json:"id,omitempty"`
Category string `json:"category,omitempty"`
}

func (pi *PolicyInstance) TagsAsInterface() []interface{} {
var tagIfaces []interface{}
for _, tag := range pi.Tags {
tagIfaces = append(tagIfaces, tag)
}
return tagIfaces
}

type PolicyInstance struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
TemplateId string `json:"templateId,omitempty"`
Parameters string `json:"parameters,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Scope *Scope `json:"scope,omitempty"`
Tags []string `json:"tags,omitempty"`
LastUpdated *ChangeInfo `json:"lastUpdated,omitempty"`
Created *ChangeInfo `json:"created,omitempty"`
}

// used for 'data' in requests
type PolicyInstanceDataRequest struct {
Instance *PolicyInstance `json:"instance,omitempty"`
Duration string `json:"duration,omitempty"`
}

type ListPolicyInstancePartial struct {
Key Key `json:"key,omitempty"`
Name string `json:"name,omitempty"`
TemplateId string `json:"templateId,omitempty"`
}

type InsertPolicyInstanceRequest PolicyInstanceDataRequest

type UpdatePolicyInstanceRequest PolicyInstanceDataRequest

type DeletePolicyInstanceRequest struct {
Id string `json:"id,omitempty"`
Category string `json:"category,omitempty"`
}

type ReadPolicyInstanceRequest struct {
Id string `json:"id,omitempty"`
Category string `json:"category,omitempty"`
}

type ReadPolicyInstancesRequest struct {
Category string `json:"category,omitempty"`
}

type InsertPolicyInstanceResponse struct {
Id string `json:"id,omitempty"`
Category string `json:"category,omitempty"`
}

type UpdatePolicyInstanceResponse struct {
}

type DeletePolicyInstanceResponse struct {
Instance PolicyInstance `json:"instance,omitempty"`
}

type ReadPolicyInstanceResponse PolicyInstance

type ReadPolicyInstancesResponse struct {
Instances []ListPolicyInstancePartial `json:"instances,omitempty"`
}
2 changes: 2 additions & 0 deletions cyral/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func Provider() *schema.Provider {
"cyral_sidecar_cft_template": dataSourceSidecarCftTemplate(),
"cyral_sidecar_id": dataSourceSidecarID(),
"cyral_sidecar_instance_ids": dataSourceSidecarInstanceIDs(),
"cyral_regopolicy_instance": dataSourceRegopolicyInstance(),
},

ResourcesMap: map[string]*schema.Resource{
Expand All @@ -106,6 +107,7 @@ func Provider() *schema.Provider {
"cyral_integration_sumo_logic": resourceIntegrationSumoLogic(),
"cyral_policy": resourcePolicy(),
"cyral_policy_rule": resourcePolicyRule(),
"cyral_regopolicy_instance": resourceRegopolicyInstance(),
"cyral_repository": resourceRepository(),
"cyral_repository_binding": resourceRepositoryBinding(),
"cyral_repository_conf_analysis": resourceRepositoryConfAnalysis(),
Expand Down
Loading