Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
)

require (
github.com/fastly/go-fastly/v7 v7.5.1
github.com/fastly/go-fastly/v7 v7.5.2
github.com/kennygrant/sanitize v1.2.4
github.com/mholt/archiver v3.1.1+incompatible
github.com/otiai10/copy v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj6
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/dustinkirkland/golang-petname v0.0.0-20191129215211-8e5a1ed0cff0 h1:90Ly+6UfUypEF6vvvW5rQIv9opIL8CbmW9FT20LDQoY=
github.com/dustinkirkland/golang-petname v0.0.0-20191129215211-8e5a1ed0cff0/go.mod h1:V+Qd57rJe8gd4eiGzZyg4h54VLHmYVVw54iMnlAMrF8=
github.com/fastly/go-fastly/v7 v7.5.1 h1:BkuUTHGgpuxb8/EDYDmgxyMUwP4JZOSO7cYZQFFpc9A=
github.com/fastly/go-fastly/v7 v7.5.1/go.mod h1:/Z2GD7NuxV3vVMAyrtckg1WvZVnCuM2Z8ok/z70XTEQ=
github.com/fastly/go-fastly/v7 v7.5.2 h1:6TfHEScwdP3ErhAZRcsh7VC55W1ssBML/nwvKWvOMOc=
github.com/fastly/go-fastly/v7 v7.5.2/go.mod h1:/Z2GD7NuxV3vVMAyrtckg1WvZVnCuM2Z8ok/z70XTEQ=
github.com/fastly/kingpin v2.1.12-0.20191105091915-95d230a53780+incompatible h1:FhrXlfhgGCS+uc6YwyiFUt04alnjpoX7vgDKJxS6Qbk=
github.com/fastly/kingpin v2.1.12-0.20191105091915-95d230a53780+incompatible/go.mod h1:U8UynVoU1SQaqD2I4ZqgYd5lx3A1ipQYn4aSt2Y5h6c=
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
Expand Down
83 changes: 18 additions & 65 deletions pkg/commands/ratelimit/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,87 +15,28 @@ import (
"github.com/fastly/go-fastly/v7/fastly"
)

// rateLimitActions is a list of supported actions.
// It is used to construct the API input.
// It is also used to construct input for the --action enum flag.
// We build the flag input dynamically so we can avoid hardcoding the values.
// This is in case the underlying values in go-fastly change between releases.
var rateLimitActions = []fastly.ERLAction{
fastly.ERLActionLogOnly,
fastly.ERLActionResponse,
fastly.ERLActionResponseObject,
}

// rateLimitActionFlagOpts is a string representation of rateLimitActions
// suitable for use within the enum flag definition below.
var rateLimitActionFlagOpts = func() (actions []string) {
for _, a := range rateLimitActions {
for _, a := range fastly.ERLActions {
actions = append(actions, string(a))
}
return actions
}()

// rateLimitLoggers is a list of supported logger types.
// It is used to construct the API input.
// It is also used to construct input for the --logger-type enum flag.
// We build the flag input dynamically so we can avoid hardcoding the values.
// This is in case the underlying values in go-fastly change between releases.
var rateLimitLoggers = []fastly.ERLLogger{
fastly.ERLLogAzureBlob,
fastly.ERLLogBigQuery,
fastly.ERLLogCloudFiles,
fastly.ERLLogDataDog,
fastly.ERLLogDigitalOcean,
fastly.ERLLogElasticSearch,
fastly.ERLLogFtp,
fastly.ERLLogGcs,
fastly.ERLLogGoogleAnalytics,
fastly.ERLLogHeroku,
fastly.ERLLogHoneycomb,
fastly.ERLLogHTTP,
fastly.ERLLogHTTPS,
fastly.ERLLogKafta,
fastly.ERLLogKinesis,
fastly.ERLLogLogEntries,
fastly.ERLLogLoggly,
fastly.ERLLogLogShuttle,
fastly.ERLLogNewRelic,
fastly.ERLLogOpenStack,
fastly.ERLLogPaperTrail,
fastly.ERLLogPubSub,
fastly.ERLLogS3,
fastly.ERLLogScalyr,
fastly.ERLLogSftp,
fastly.ERLLogSplunk,
fastly.ERLLogStackDriver,
fastly.ERLLogSumoLogic,
fastly.ERLLogSysLog,
}

// rateLimitLoggerFlagOpts is a string representation of rateLimitLoggers
// suitable for use within the enum flag definition below.
var rateLimitLoggerFlagOpts = func() (loggers []string) {
for _, l := range rateLimitLoggers {
for _, l := range fastly.ERLLoggers {
loggers = append(loggers, string(l))
}
return loggers
}()

// rateLimitWindowSizes is a list of supported time window sizes.
// It is used to construct the API input.
// It is also used to construct input for the --window-size enum flag.
// We build the flag input dynamically so we can avoid hardcoding the values.
// This is in case the underlying values in go-fastly change between releases.
var rateLimitWindowSizes = []fastly.ERLWindowSize{
fastly.ERLSize1,
fastly.ERLSize10,
fastly.ERLSize60,
}

// rateLimitWindowSizeFlagOpts is a string representation of rateLimitWindowSizes
// suitable for use within the enum flag definition below.
var rateLimitWindowSizeFlagOpts = func() (windowSizes []string) {
for _, w := range rateLimitWindowSizes {
for _, w := range fastly.ERLWindowSizes {
windowSizes = append(windowSizes, fmt.Sprint(w))
}
return windowSizes
Expand Down Expand Up @@ -127,13 +68,15 @@ func NewCreateCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *C
Dst: &c.autoClone.Value,
})
c.CmdClause.Flag("client-key", "Comma-separated list of VCL variable used to generate a counter key to identify a client").StringVar(&c.clientKeys)
c.CmdClause.Flag("feature-revision", "Revision number of the rate limiting feature implementation").IntVar(&c.featRevision)
c.CmdClause.Flag("http-methods", "Comma-separated list of HTTP methods to apply rate limiting to").StringVar(&c.httpMethods)
c.RegisterFlagBool(c.JSONFlag()) // --json
c.CmdClause.Flag("logger-type", "Name of the type of logging endpoint to be used when action is `log_only`").HintOptions(rateLimitLoggerFlagOpts...).EnumVar(&c.loggerType, rateLimitLoggerFlagOpts...)
c.CmdClause.Flag("name", "A human readable name for the rate limiting rule").StringVar(&c.name)
c.CmdClause.Flag("penalty-box-dur", "Length of time in minutes that the rate limiter is in effect after the initial violation is detected").IntVar(&c.penaltyDuration)
c.CmdClause.Flag("response-content", "HTTP response body data").StringVar(&c.responseContent)
c.CmdClause.Flag("response-content-type", "HTTP Content-Type (e.g. application/json)").StringVar(&c.responseContentType)
c.CmdClause.Flag("response-object-name", "Name of existing response object. Required if action is response_object").StringVar(&c.responseObjectName)
c.CmdClause.Flag("response-status", "HTTP response status code (e.g. 429)").IntVar(&c.responseStatus)
c.CmdClause.Flag("rps-limit", "Upper limit of requests per second allowed by the rate limiter").IntVar(&c.rpsLimit)
c.RegisterFlag(cmd.StringFlagOpts{
Expand Down Expand Up @@ -161,13 +104,15 @@ type CreateCommand struct {
action string
autoClone cmd.OptionalAutoClone
clientKeys string
featRevision int
httpMethods string
loggerType string
manifest manifest.Data
name string
penaltyDuration int
responseContent string
responseContentType string
responseObjectName string
responseStatus int
rpsLimit int
serviceName cmd.OptionalServiceNameID
Expand Down Expand Up @@ -236,7 +181,7 @@ func (c *CreateCommand) constructInput() *fastly.CreateERLInput {
var input fastly.CreateERLInput

if c.action != "" {
for _, a := range rateLimitActions {
for _, a := range fastly.ERLActions {
if c.action == string(a) {
input.Action = fastly.ERLActionPtr(a)
break
Expand All @@ -249,13 +194,17 @@ func (c *CreateCommand) constructInput() *fastly.CreateERLInput {
input.ClientKey = &clientKeys
}

if c.featRevision > 0 {
input.FeatureRevision = fastly.Int(c.featRevision)
}

if c.httpMethods != "" {
httpMethods := strings.Split(strings.ReplaceAll(c.httpMethods, " ", ""), ",")
input.HTTPMethods = &httpMethods
}

if c.loggerType != "" {
for _, l := range rateLimitLoggers {
for _, l := range fastly.ERLLoggers {
if c.loggerType == string(l) {
input.LoggerType = fastly.ERLLoggerPtr(l)
break
Expand All @@ -279,12 +228,16 @@ func (c *CreateCommand) constructInput() *fastly.CreateERLInput {
}
}

if c.responseObjectName != "" {
input.ResponseObjectName = fastly.String(c.responseObjectName)
}

if c.rpsLimit > 0 {
input.RpsLimit = fastly.Int(c.rpsLimit)
}

if c.windowSize != "" {
for _, w := range rateLimitWindowSizes {
for _, w := range fastly.ERLWindowSizes {
if c.windowSize == fmt.Sprint(w) {
input.WindowSize = fastly.ERLWindowSizePtr(w)
break
Expand Down
18 changes: 15 additions & 3 deletions pkg/commands/ratelimit/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ func NewUpdateCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *U
// Optional.
c.CmdClause.Flag("action", "The action to take when a rate limiter violation is detected").HintOptions(rateLimitActionFlagOpts...).EnumVar(&c.action, rateLimitActionFlagOpts...)
c.CmdClause.Flag("client-key", "Comma-separated list of VCL variable used to generate a counter key to identify a client").StringVar(&c.clientKeys)
c.CmdClause.Flag("feature-revision", "Revision number of the rate limiting feature implementation").IntVar(&c.featRevision)
c.CmdClause.Flag("http-methods", "Comma-separated list of HTTP methods to apply rate limiting to").StringVar(&c.httpMethods)
c.RegisterFlagBool(c.JSONFlag()) // --json
c.CmdClause.Flag("logger-type", "Name of the type of logging endpoint to be used when action is `log_only`").HintOptions(rateLimitLoggerFlagOpts...).EnumVar(&c.loggerType, rateLimitLoggerFlagOpts...)
c.CmdClause.Flag("name", "A human readable name for the rate limiting rule").StringVar(&c.name)
c.CmdClause.Flag("penalty-box-dur", "Length of time in minutes that the rate limiter is in effect after the initial violation is detected").IntVar(&c.penaltyDuration)
c.CmdClause.Flag("response-content", "HTTP response body data").StringVar(&c.responseContent)
c.CmdClause.Flag("response-content-type", "HTTP Content-Type (e.g. application/json)").StringVar(&c.responseContentType)
c.CmdClause.Flag("response-object-name", "Name of existing response object. Required if action is response_object").StringVar(&c.responseObjectName)
c.CmdClause.Flag("response-status", "HTTP response status code (e.g. 429)").IntVar(&c.responseStatus)
c.CmdClause.Flag("rps-limit", "Upper limit of requests per second allowed by the rate limiter").IntVar(&c.rpsLimit)
c.CmdClause.Flag("window-size", "Number of seconds during which the RPS limit must be exceeded in order to trigger a violation").HintOptions(rateLimitWindowSizeFlagOpts...).EnumVar(&c.windowSize, rateLimitWindowSizeFlagOpts...)
Expand All @@ -53,6 +55,7 @@ type UpdateCommand struct {

action string
clientKeys string
featRevision int
httpMethods string
id string
loggerType string
Expand All @@ -61,6 +64,7 @@ type UpdateCommand struct {
penaltyDuration int
responseContent string
responseContentType string
responseObjectName string
responseStatus int
rpsLimit int
windowSize string
Expand Down Expand Up @@ -107,7 +111,7 @@ func (c *UpdateCommand) constructInput() *fastly.UpdateERLInput {

// NOTE: rateLimitActions is defined in ./create.go
if c.action != "" {
for _, a := range rateLimitActions {
for _, a := range fastly.ERLActions {
if c.action == string(a) {
input.Action = fastly.ERLActionPtr(a)
break
Expand All @@ -120,14 +124,18 @@ func (c *UpdateCommand) constructInput() *fastly.UpdateERLInput {
input.ClientKey = &clientKeys
}

if c.featRevision > 0 {
input.FeatureRevision = fastly.Int(c.featRevision)
}

if c.httpMethods != "" {
httpMethods := strings.Split(strings.ReplaceAll(c.httpMethods, " ", ""), ",")
input.HTTPMethods = &httpMethods
}

// NOTE: rateLimitLoggers is defined in ./create.go
if c.loggerType != "" {
for _, l := range rateLimitLoggers {
for _, l := range fastly.ERLLoggers {
if c.loggerType == string(l) {
input.LoggerType = fastly.ERLLoggerPtr(l)
break
Expand All @@ -151,13 +159,17 @@ func (c *UpdateCommand) constructInput() *fastly.UpdateERLInput {
}
}

if c.responseObjectName != "" {
input.ResponseObjectName = fastly.String(c.responseObjectName)
}

if c.rpsLimit > 0 {
input.RpsLimit = fastly.Int(c.rpsLimit)
}

// NOTE: rateLimitWindowSizes is defined in ./create.go
if c.windowSize != "" {
for _, w := range rateLimitWindowSizes {
for _, w := range fastly.ERLWindowSizes {
if c.windowSize == fmt.Sprint(w) {
input.WindowSize = fastly.ERLWindowSizePtr(w)
break
Expand Down