Skip to content

Commit

Permalink
Ddosx waf log commands (#61)
Browse files Browse the repository at this point in the history
* add support for ddosx waf logs

* add tests
  • Loading branch information
0x4c6565 committed Jul 27, 2020
1 parent 3df1eea commit b8db429
Show file tree
Hide file tree
Showing 10 changed files with 591 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/ddosx/ddosx.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func DDoSXRootCmd(f factory.ClientFactory, fs afero.Fs) *cobra.Command {
cmd.AddCommand(ddosxDomainRootCmd(f, fs))
cmd.AddCommand(ddosxRecordRootCmd(f))
cmd.AddCommand(ddosxSSLRootCmd(f, fs))
cmd.AddCommand(ddosxWAFRootCmd(f))

return cmd
}
18 changes: 18 additions & 0 deletions cmd/ddosx/ddosx_waf.go
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
}
108 changes: 108 additions & 0 deletions cmd/ddosx/ddosx_waf_log.go
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))
}
114 changes: 114 additions & 0 deletions cmd/ddosx/ddosx_waf_log_match.go
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))
}
117 changes: 117 additions & 0 deletions cmd/ddosx/ddosx_waf_log_match_test.go
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"})
})
})
}
Loading

0 comments on commit b8db429

Please sign in to comment.