-
Notifications
You must be signed in to change notification settings - Fork 71
Adds account level and workspace level operations for ngwaf rules using json file inputs #1605
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c3bcdf2
feat(ngwaf/rules): adds account level and workspace level operations …
anthony-gomez-fastly b2813c3
fix lint
anthony-gomez-fastly 87705d0
add parsing support for different rule types
anthony-gomez-fastly 20b6c3b
add workspace level
anthony-gomez-fastly f9df697
lint
anthony-gomez-fastly 07582c4
add update
anthony-gomez-fastly 06523b6
fix typo
anthony-gomez-fastly 41c0070
fix error message
anthony-gomez-fastly 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 hidden or 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 hidden or 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 hidden or 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,192 @@ | ||
| package rule | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/fastly/go-fastly/v12/fastly" | ||
| "github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/rules" | ||
| "github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/scope" | ||
|
|
||
| "github.com/fastly/cli/pkg/argparser" | ||
| fsterr "github.com/fastly/cli/pkg/errors" | ||
| "github.com/fastly/cli/pkg/global" | ||
| "github.com/fastly/cli/pkg/text" | ||
| ) | ||
|
|
||
| // CreateCommand calls the Fastly API to create account-level rules. | ||
| type CreateCommand struct { | ||
| argparser.Base | ||
| argparser.JSONOutput | ||
|
|
||
| // Required. | ||
| path string | ||
| } | ||
|
|
||
| // NewCreateCommand returns a usable command registered under the parent. | ||
| func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand { | ||
| c := CreateCommand{ | ||
| Base: argparser.Base{ | ||
| Globals: g, | ||
| }, | ||
| } | ||
| c.CmdClause = parent.Command("create", "Create an account-level rule").Alias("add") | ||
|
|
||
| // Required. | ||
| c.CmdClause.Flag("path", "Path to a json file that contains the rule schema.").Required().StringVar(&c.path) | ||
|
|
||
| // Optional. | ||
| c.RegisterFlagBool(c.JSONFlag()) | ||
|
|
||
| return &c | ||
| } | ||
|
|
||
| // Exec invokes the application logic for the command. | ||
| func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { | ||
| if c.Globals.Verbose() && c.JSONOutput.Enabled { | ||
| return fsterr.ErrInvalidVerboseJSONCombo | ||
| } | ||
| var err error | ||
| rule := &rules.Rule{} | ||
| if c.path != "" { | ||
| path, err := filepath.Abs(c.path) | ||
| if err != nil { | ||
| return fmt.Errorf("error parsing path '%s': %q", c.path, err) | ||
| } | ||
|
|
||
| jsonFile, err := os.Open(path) | ||
| if err != nil { | ||
| return fmt.Errorf("error reading cert-path '%s': %q", c.path, err) | ||
| } | ||
| defer jsonFile.Close() | ||
|
|
||
| byteValue, err := io.ReadAll(jsonFile) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read json file: %v", err) | ||
| } | ||
|
|
||
| if err := json.Unmarshal(byteValue, rule); err != nil { | ||
| return fmt.Errorf("failed to unmarshal json data: %v", err) | ||
| } | ||
| } | ||
|
|
||
| input := &rules.CreateInput{ | ||
| Actions: []*rules.CreateAction{}, | ||
| Conditions: []*rules.CreateCondition{}, | ||
| Description: &rule.Description, | ||
| GroupConditions: []*rules.CreateGroupCondition{}, | ||
| MultivalConditions: []*rules.CreateMultivalCondition{}, | ||
| Enabled: &rule.Enabled, | ||
| Type: &rule.Type, | ||
| GroupOperator: &rule.GroupOperator, | ||
| RequestLogging: &rule.RequestLogging, | ||
| Scope: &scope.Scope{ | ||
| Type: scope.ScopeTypeAccount, | ||
| AppliesTo: []string{"*"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, action := range rule.Actions { | ||
| input.Actions = append(input.Actions, &rules.CreateAction{ | ||
| AllowInteractive: action.AllowInteractive, | ||
| DeceptionType: &action.DeceptionType, | ||
| RedirectURL: &action.RedirectURL, | ||
| ResponseCode: &action.ResponseCode, | ||
| Signal: &action.Signal, | ||
| Type: &action.Type, | ||
| }) | ||
| } | ||
|
|
||
| if rule.RateLimit != nil { | ||
| input.RateLimit = &rules.CreateRateLimit{ | ||
| ClientIdentifiers: []*rules.CreateClientIdentifier{}, | ||
| Duration: &rule.RateLimit.Duration, | ||
| Interval: &rule.RateLimit.Interval, | ||
| Signal: &rule.RateLimit.Signal, | ||
| Threshold: &rule.RateLimit.Threshold, | ||
| } | ||
|
|
||
| for _, rateLimit := range rule.RateLimit.ClientIdentifiers { | ||
| input.RateLimit.ClientIdentifiers = append(input.RateLimit.ClientIdentifiers, &rules.CreateClientIdentifier{ | ||
| Key: &rateLimit.Key, | ||
| Name: &rateLimit.Name, | ||
| Type: &rateLimit.Type, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| for _, jsonCondition := range rule.Conditions { | ||
| switch jsonCondition.Type { | ||
| case "single": | ||
| if sc, ok := jsonCondition.Fields.(rules.SingleCondition); ok { | ||
| input.Conditions = append(input.Conditions, &rules.CreateCondition{ | ||
| Field: &sc.Field, | ||
| Operator: &sc.Operator, | ||
| Value: &sc.Value, | ||
| }) | ||
| } else { | ||
| return fmt.Errorf("expected SingleCondition, got %T", jsonCondition.Fields) | ||
| } | ||
| case "group": | ||
| if gc, ok := jsonCondition.Fields.(rules.GroupCondition); ok { | ||
| parsedGroupCondition := &rules.CreateGroupCondition{ | ||
| GroupOperator: &gc.GroupOperator, | ||
| Conditions: []*rules.CreateCondition{}, | ||
| } | ||
| for _, groupSingleCondition := range gc.Conditions { | ||
| parsedGroupCondition.Conditions = append(parsedGroupCondition.Conditions, &rules.CreateCondition{ | ||
| Field: &groupSingleCondition.Field, | ||
| Operator: &groupSingleCondition.Operator, | ||
| Value: &groupSingleCondition.Value, | ||
| }) | ||
| } | ||
| input.GroupConditions = append(input.GroupConditions, parsedGroupCondition) | ||
| } else { | ||
| return fmt.Errorf("expected GroupCondition, got %T", jsonCondition.Fields) | ||
| } | ||
| case "multival": | ||
| if mvc, ok := jsonCondition.Fields.(rules.CreateMultivalCondition); ok { | ||
| parsedMultiValCondition := &rules.CreateMultivalCondition{ | ||
| Field: mvc.Field, | ||
| GroupOperator: mvc.GroupOperator, | ||
| Operator: mvc.Operator, | ||
| Conditions: []*rules.CreateConditionMult{}, | ||
| } | ||
| for _, multiSingleCondition := range mvc.Conditions { | ||
| parsedMultiValCondition.Conditions = append(parsedMultiValCondition.Conditions, &rules.CreateConditionMult{ | ||
| Field: multiSingleCondition.Field, | ||
| Operator: multiSingleCondition.Operator, | ||
| Value: multiSingleCondition.Value, | ||
| }) | ||
| } | ||
| input.MultivalConditions = append(input.MultivalConditions, parsedMultiValCondition) | ||
| } else { | ||
| return fmt.Errorf("expected MultivalCondition, got %T", jsonCondition.Fields) | ||
| } | ||
| default: | ||
| return fmt.Errorf("unknown condition type: %s", jsonCondition.Type) | ||
| } | ||
| } | ||
|
|
||
| fc, ok := c.Globals.APIClient.(*fastly.Client) | ||
| if !ok { | ||
| return errors.New("failed to convert interface to a fastly client") | ||
| } | ||
|
|
||
| data, err := rules.Create(context.TODO(), fc, input) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if ok, err := c.WriteJSON(out, data); ok { | ||
| return err | ||
| } | ||
|
|
||
| text.Success(out, "Created account-level rule with ID %s", data.RuleID) | ||
| return nil | ||
| } | ||
This file contains hidden or 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,84 @@ | ||
| package rule | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io" | ||
|
|
||
| "github.com/fastly/go-fastly/v12/fastly" | ||
|
|
||
| "github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/rules" | ||
| "github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/scope" | ||
|
|
||
| "github.com/fastly/cli/pkg/argparser" | ||
| fsterr "github.com/fastly/cli/pkg/errors" | ||
| "github.com/fastly/cli/pkg/global" | ||
| "github.com/fastly/cli/pkg/text" | ||
| ) | ||
|
|
||
| // DeleteCommand calls the Fastly API to delete an account-level rule. | ||
| type DeleteCommand struct { | ||
| argparser.Base | ||
| argparser.JSONOutput | ||
|
|
||
| // Required. | ||
| ruleID string | ||
| } | ||
|
|
||
| // NewDeleteCommand returns a usable command registered under the parent. | ||
| func NewDeleteCommand(parent argparser.Registerer, g *global.Data) *DeleteCommand { | ||
| c := DeleteCommand{ | ||
| Base: argparser.Base{ | ||
| Globals: g, | ||
| }, | ||
| } | ||
|
|
||
| c.CmdClause = parent.Command("delete", "Delete an account-level rule") | ||
|
|
||
| // Required. | ||
| c.CmdClause.Flag("rule-id", "Rule ID").Required().StringVar(&c.ruleID) | ||
|
|
||
| // Optional. | ||
| c.RegisterFlagBool(c.JSONFlag()) | ||
|
|
||
| return &c | ||
| } | ||
|
|
||
| // Exec invokes the application logic for the command. | ||
| func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error { | ||
| if c.Globals.Verbose() && c.JSONOutput.Enabled { | ||
| return fsterr.ErrInvalidVerboseJSONCombo | ||
| } | ||
|
|
||
| fc, ok := c.Globals.APIClient.(*fastly.Client) | ||
| if !ok { | ||
| return errors.New("failed to convert interface to a fastly client") | ||
| } | ||
|
|
||
| err := rules.Delete(context.TODO(), fc, &rules.DeleteInput{ | ||
| RuleID: &c.ruleID, | ||
| Scope: &scope.Scope{ | ||
| Type: scope.ScopeTypeAccount, | ||
| AppliesTo: []string{"*"}, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| c.Globals.ErrLog.Add(err) | ||
| return err | ||
| } | ||
|
|
||
| if c.JSONOutput.Enabled { | ||
| o := struct { | ||
| ID string `json:"id"` | ||
| Deleted bool `json:"deleted"` | ||
| }{ | ||
| c.ruleID, | ||
| true, | ||
| } | ||
| _, err := c.WriteJSON(out, o) | ||
| return err | ||
| } | ||
|
|
||
| text.Success(out, "Deleted account-level rule with id: %s", c.ruleID) | ||
| return nil | ||
| } |
This file contains hidden or 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,2 @@ | ||
| // Package rule contains commands to inspect and manipulate NGWAF account-level rules. | ||
| package rule |
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.
Uh oh!
There was an error while loading. Please reload this page.