-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add support for ddosx waf logs * add tests
- Loading branch information
Showing
10 changed files
with
591 additions
and
3 deletions.
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,18 @@ | ||
package ddosx | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/ukfast/cli/internal/pkg/factory" | ||
) | ||
|
||
func ddosxWAFRootCmd(f factory.ClientFactory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "waf", | ||
Short: "sub-commands relating to web application filewalls", | ||
} | ||
|
||
// Child root commands | ||
cmd.AddCommand(ddosxWAFLogRootCmd(f)) | ||
|
||
return cmd | ||
} |
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,108 @@ | ||
package ddosx | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/ukfast/cli/internal/pkg/factory" | ||
"github.com/ukfast/cli/internal/pkg/helper" | ||
"github.com/ukfast/cli/internal/pkg/output" | ||
"github.com/ukfast/sdk-go/pkg/service/ddosx" | ||
) | ||
|
||
func ddosxWAFLogRootCmd(f factory.ClientFactory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "log", | ||
Short: "sub-commands relating to domain web application firewall logs", | ||
} | ||
|
||
// Child root commands | ||
cmd.AddCommand(ddosxWAFLogMatchRootCmd(f)) | ||
|
||
// Child commands | ||
cmd.AddCommand(ddosxWAFLogListCmd(f)) | ||
cmd.AddCommand(ddosxWAFLogShowCmd(f)) | ||
|
||
return cmd | ||
} | ||
|
||
func ddosxWAFLogListCmd(f factory.ClientFactory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "Lists WAF logs", | ||
Long: "This command lists WAF logs", | ||
Example: "ukfast ddosx waf log list", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
c, err := f.NewClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ddosxWAFLogList(c.DDoSXService(), cmd, args) | ||
}, | ||
} | ||
|
||
cmd.Flags().String("domain", "", "Domain name for filtering") | ||
|
||
return cmd | ||
} | ||
|
||
func ddosxWAFLogList(service ddosx.DDoSXService, cmd *cobra.Command, args []string) error { | ||
params, err := helper.GetAPIRequestParametersFromFlags(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if cmd.Flags().Changed("domain") { | ||
filterDomain, _ := cmd.Flags().GetString("domain") | ||
params.WithFilter(helper.GetFilteringInferOperator("domain", filterDomain)) | ||
} | ||
|
||
logs, err := service.GetWAFLogs(params) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving WAF logs: %s", err) | ||
} | ||
|
||
return output.CommandOutput(cmd, OutputDDoSXWAFLogsProvider(logs)) | ||
} | ||
|
||
func ddosxWAFLogShowCmd(f factory.ClientFactory) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "show <request: id>...", | ||
Short: "Shows WAF logs", | ||
Long: "This command shows a WAF log", | ||
Example: "ukfast ddosx waf log show 2d8556677081cecf112b555c359a78c6", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("Missing request") | ||
} | ||
|
||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
c, err := f.NewClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ddosxWAFLogShow(c.DDoSXService(), cmd, args) | ||
}, | ||
} | ||
} | ||
|
||
func ddosxWAFLogShow(service ddosx.DDoSXService, cmd *cobra.Command, args []string) error { | ||
var logs []ddosx.WAFLog | ||
|
||
for _, arg := range args { | ||
log, err := service.GetWAFLog(arg) | ||
if err != nil { | ||
output.OutputWithErrorLevelf("Error retrieving WAF log [%s]: %s", arg, err.Error()) | ||
continue | ||
} | ||
|
||
logs = append(logs, log) | ||
} | ||
|
||
return output.CommandOutput(cmd, OutputDDoSXWAFLogsProvider(logs)) | ||
} |
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,114 @@ | ||
package ddosx | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/ukfast/cli/internal/pkg/factory" | ||
"github.com/ukfast/cli/internal/pkg/helper" | ||
"github.com/ukfast/cli/internal/pkg/output" | ||
"github.com/ukfast/sdk-go/pkg/service/ddosx" | ||
) | ||
|
||
func ddosxWAFLogMatchRootCmd(f factory.ClientFactory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "match", | ||
Short: "sub-commands relating to domain web application firewall log matches", | ||
} | ||
|
||
// Child commands | ||
cmd.AddCommand(ddosxWAFLogMatchListCmd(f)) | ||
cmd.AddCommand(ddosxWAFLogMatchShowCmd(f)) | ||
|
||
return cmd | ||
} | ||
|
||
func ddosxWAFLogMatchListCmd(f factory.ClientFactory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "Lists WAF log matches", | ||
Long: "This command lists WAF log matches", | ||
Example: "ukfast ddosx waf log match list", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
c, err := f.NewClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ddosxWAFLogMatchList(c.DDoSXService(), cmd, args) | ||
}, | ||
} | ||
|
||
cmd.Flags().String("request", "", "Show matches for specific request") | ||
|
||
return cmd | ||
} | ||
|
||
func ddosxWAFLogMatchList(service ddosx.DDoSXService, cmd *cobra.Command, args []string) error { | ||
params, err := helper.GetAPIRequestParametersFromFlags(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var matches []ddosx.WAFLogMatch | ||
|
||
if cmd.Flags().Changed("request") { | ||
request, _ := cmd.Flags().GetString("request") | ||
matches, err = service.GetWAFLogRequestMatches(request, params) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving WAF log request matches: %s", err) | ||
} | ||
} else { | ||
matches, err = service.GetWAFLogMatches(params) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving WAF log matches: %s", err) | ||
} | ||
} | ||
|
||
return output.CommandOutput(cmd, OutputDDoSXWAFLogMatchesProvider(matches)) | ||
} | ||
|
||
func ddosxWAFLogMatchShowCmd(f factory.ClientFactory) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "show <request: id> <match: id>...", | ||
Short: "Shows WAF log request matches", | ||
Long: "This command shows a WAF log request matches", | ||
Example: "ukfast ddosx waf log match show 2d8556677081cecf112b555c359a78c6 123456", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("Missing request") | ||
} | ||
|
||
if len(args) < 2 { | ||
return errors.New("Missing match") | ||
} | ||
|
||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
c, err := f.NewClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ddosxWAFLogMatchShow(c.DDoSXService(), cmd, args) | ||
}, | ||
} | ||
} | ||
|
||
func ddosxWAFLogMatchShow(service ddosx.DDoSXService, cmd *cobra.Command, args []string) error { | ||
var logs []ddosx.WAFLogMatch | ||
|
||
for _, arg := range args[1:] { | ||
log, err := service.GetWAFLogRequestMatch(args[0], arg) | ||
if err != nil { | ||
output.OutputWithErrorLevelf("Error retrieving WAF log matches [%s]: %s", arg, err.Error()) | ||
continue | ||
} | ||
|
||
logs = append(logs, log) | ||
} | ||
|
||
return output.CommandOutput(cmd, OutputDDoSXWAFLogMatchesProvider(logs)) | ||
} |
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,117 @@ | ||
package ddosx | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
gomock "github.com/golang/mock/gomock" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/ukfast/cli/test/mocks" | ||
"github.com/ukfast/cli/test/test_output" | ||
"github.com/ukfast/sdk-go/pkg/service/ddosx" | ||
) | ||
|
||
func Test_ddosxWAFLogMatchList(t *testing.T) { | ||
t.Run("WithoutRequest_ExpectedCalls", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockDDoSXService(mockCtrl) | ||
|
||
service.EXPECT().GetWAFLogMatches(gomock.Any()).Return([]ddosx.WAFLogMatch{}, nil).Times(1) | ||
|
||
ddosxWAFLogMatchList(service, ddosxWAFLogMatchListCmd(nil), []string{}) | ||
}) | ||
|
||
t.Run("WithRequest_ExpectedCalls", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
cmd := ddosxWAFLogMatchListCmd(nil) | ||
cmd.Flags().Set("request", "abcdef") | ||
|
||
service := mocks.NewMockDDoSXService(mockCtrl) | ||
|
||
service.EXPECT().GetWAFLogRequestMatches("abcdef", gomock.Any()).Return([]ddosx.WAFLogMatch{}, nil).Times(1) | ||
|
||
ddosxWAFLogMatchList(service, cmd, []string{}) | ||
}) | ||
|
||
t.Run("GetWAFLogRequestMatches_ReturnsError", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
cmd := ddosxWAFLogMatchListCmd(nil) | ||
cmd.Flags().Set("request", "abcdef") | ||
|
||
service := mocks.NewMockDDoSXService(mockCtrl) | ||
|
||
service.EXPECT().GetWAFLogRequestMatches("abcdef", gomock.Any()).Return([]ddosx.WAFLogMatch{}, errors.New("test error")) | ||
|
||
err := ddosxWAFLogMatchList(service, cmd, []string{}) | ||
|
||
assert.Equal(t, "Error retrieving WAF log request matches: test error", err.Error()) | ||
}) | ||
|
||
t.Run("GetWAFLogMatches_ReturnsError", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockDDoSXService(mockCtrl) | ||
|
||
service.EXPECT().GetWAFLogMatches(gomock.Any()).Return([]ddosx.WAFLogMatch{}, errors.New("test error")) | ||
|
||
err := ddosxWAFLogMatchList(service, ddosxWAFLogMatchListCmd(nil), []string{}) | ||
|
||
assert.Equal(t, "Error retrieving WAF log matches: test error", err.Error()) | ||
}) | ||
} | ||
|
||
func Test_ddosxWAFLogMatchShowCmd_Args(t *testing.T) { | ||
t.Run("ValidArgs_NoError", func(t *testing.T) { | ||
err := ddosxWAFLogMatchShowCmd(nil).Args(nil, []string{"2d8556677081cecf112b555c359a78c6", "abcdef"}) | ||
|
||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("MissingRequest_Error", func(t *testing.T) { | ||
err := ddosxWAFLogMatchShowCmd(nil).Args(nil, []string{}) | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, "Missing request", err.Error()) | ||
}) | ||
|
||
t.Run("MissingMatch_Error", func(t *testing.T) { | ||
err := ddosxWAFLogMatchShowCmd(nil).Args(nil, []string{"2d8556677081cecf112b555c359a78c6"}) | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, "Missing match", err.Error()) | ||
}) | ||
} | ||
|
||
func Test_ddosxWAFLogMatchShow(t *testing.T) { | ||
t.Run("SingleLog", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockDDoSXService(mockCtrl) | ||
|
||
service.EXPECT().GetWAFLogRequestMatch("2d8556677081cecf112b555c359a78c6", "abcdef").Return(ddosx.WAFLogMatch{}, nil).Times(1) | ||
|
||
ddosxWAFLogMatchShow(service, &cobra.Command{}, []string{"2d8556677081cecf112b555c359a78c6", "abcdef"}) | ||
}) | ||
|
||
t.Run("GetWAFLogMatchError_OutputsError", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockDDoSXService(mockCtrl) | ||
|
||
service.EXPECT().GetWAFLogRequestMatch("2d8556677081cecf112b555c359a78c6", "abcdef").Return(ddosx.WAFLogMatch{}, errors.New("test error")) | ||
|
||
test_output.AssertErrorOutput(t, "Error retrieving WAF log matches [abcdef]: test error\n", func() { | ||
ddosxWAFLogMatchShow(service, &cobra.Command{}, []string{"2d8556677081cecf112b555c359a78c6", "abcdef"}) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.