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

feat: Display supported policy exception constraints #1068

Merged
merged 2 commits into from
Dec 15, 2022
Merged
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
39 changes: 23 additions & 16 deletions api/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,29 @@ func ParseUpdatePolicy(s string) (UpdatePolicy, error) {
}

type Policy struct {
PolicyID string `json:"policyId" yaml:"policyId"`
PolicyType string `json:"policyType" yaml:"-"`
QueryID string `json:"queryId" yaml:"queryId"`
Title string `json:"title" yaml:"title"`
Enabled bool `json:"enabled" yaml:"enabled"`
Description string `json:"description" yaml:"description"`
Remediation string `json:"remediation" yaml:"remediation"`
Severity string `json:"severity" yaml:"severity"`
Limit int `json:"limit" yaml:"limit"`
EvalFrequency string `json:"evalFrequency" yaml:"evalFrequency"`
AlertEnabled bool `json:"alertEnabled" yaml:"alertEnabled"`
AlertProfile string `json:"alertProfile" yaml:"alertProfile"`
Tags []string `json:"tags" yaml:"tags"`
Owner string `json:"owner" yaml:"-"`
LastUpdateTime string `json:"lastUpdateTime" yaml:"-"`
LastUpdateUser string `json:"lastUpdateUser" yaml:"-"`
PolicyID string `json:"policyId" yaml:"policyId"`
PolicyType string `json:"policyType" yaml:"-"`
QueryID string `json:"queryId" yaml:"queryId"`
Title string `json:"title" yaml:"title"`
Enabled bool `json:"enabled" yaml:"enabled"`
Description string `json:"description" yaml:"description"`
Remediation string `json:"remediation" yaml:"remediation"`
Severity string `json:"severity" yaml:"severity"`
Limit int `json:"limit" yaml:"limit"`
EvalFrequency string `json:"evalFrequency" yaml:"evalFrequency"`
AlertEnabled bool `json:"alertEnabled" yaml:"alertEnabled"`
AlertProfile string `json:"alertProfile" yaml:"alertProfile"`
Tags []string `json:"tags" yaml:"tags"`
Owner string `json:"owner" yaml:"-"`
LastUpdateTime string `json:"lastUpdateTime" yaml:"-"`
LastUpdateUser string `json:"lastUpdateUser" yaml:"-"`
ExceptionConfiguration map[string][]PolicyExceptionConfigurationConstraints `json:"exceptionConfiguration" yaml:"-"`
Copy link
Collaborator Author

@rmoles rmoles Dec 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this, but I couldn't find a better way to parse the Policy Exception constraints, as the format is -
an Array of Maps in a Map.
It's not clear to me why the outer map was required 🤷

    "exceptionConfiguration": {
      "constraintFields": [
        {
          "dataType": "String",
          "fieldKey": "accountIds",
          "multiValue": true
        },
        {
          "dataType": "String",
          "fieldKey": "resourceNames",
          "multiValue": false
        },
        {
          "dataType": "KVTagPair",
          "fieldKey": "resourceTags",
          "multiValue": true
        }
      ]
    },

Copy link

@iok0 iok0 Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, doesn't look necessary right now. Maybe they've plans to add another field to exceptionConfiguration.
Looks like Go needs the structure/type to be defined up front.

Copy link
Collaborator Author

@rmoles rmoles Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps. Still annoying for parsing!
For parsing the values into structs we need to declare the structure up front. I was hoping I could tell GoLang to chain the map keys when parsing ie. json:"exceptionConfiguration.constraintFields" yaml:"-" that way we could remove the outer map[string] but that doesn't seem to be possible.

}

type PolicyExceptionConfigurationConstraints struct {
DataType string `json:"dataType" yaml:"dataType"`
FieldKey string `json:"fieldKey" yaml:"fieldKey"`
MultiValue bool `json:"multiValue" yaml:"multiValue"`
}

func (p *Policy) HasTag(t string) bool {
Expand Down
20 changes: 20 additions & 0 deletions cli/cmd/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"strings"

"github.com/AlecAivazis/survey/v2"

"github.com/lacework/go-sdk/api"
"github.com/lacework/go-sdk/internal/array"
"github.com/lacework/go-sdk/lwseverity"
Expand Down Expand Up @@ -590,6 +591,15 @@ func buildPolicyDetailsTable(policy api.Policy) string {
{"UPDATED BY", policy.LastUpdateUser},
{"EVALUATION FREQUENCY", policy.EvalFrequency},
}
// Append VALID EXCEPTION CONSTRAINTS to the table
// Add "None" when ExceptionConfiguration is empty
exceptionConstraints := strings.Join(
getPolicyExceptionConstraintsSlice(policy.ExceptionConfiguration), ", ")
if exceptionConstraints == "" {
exceptionConstraints = "None"
}
entry := []string{"VALID EXCEPTION CONSTRAINTS", exceptionConstraints}
details = append(details, entry)

return renderOneLineCustomTable("POLICY DETAILS",
renderCustomTable([]string{}, details,
Expand All @@ -607,6 +617,16 @@ func buildPolicyDetailsTable(policy api.Policy) string {
)
}

func getPolicyExceptionConstraintsSlice(exceptionConfiguration map[string][]api.
PolicyExceptionConfigurationConstraints) []string {
var exceptionConstraints []string
constraintFields := exceptionConfiguration["constraintFields"]
for _, constraint := range constraintFields {
exceptionConstraints = append(exceptionConstraints, constraint.FieldKey)
}
return exceptionConstraints
}

func policyTagsTable(pt []string) (out [][]string) {
for _, tag := range pt {
out = append(out, []string{tag})
Expand Down