-
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.
[POA-2129] Separate out common
apidump
flags (#49)
* In this PR, we have moved out common `apidump` flags from `apidump.go` to `common_flags.go`. * `common_flags.go` have two functions: * `AddCommonApiDumpFlags()`: Add the flags to cobra.Command object. It returns the objects to which flag values will be assigned. * `ConvertCommonApiDumpFlagsToArgs()`: Returns an array of strings (like `--flag value`). It is added to the apidump command in different platform installation configurations. * The `ecs` and `ec2` commands will read these values and then convert them to flags for `apidump` commands and add them to the definition file (`ecs`) or systems service file (`ec2`) * The `apidump` command will use these flag values to set the main `apidump` package variables.
- Loading branch information
1 parent
f2cc043
commit ac38940
Showing
7 changed files
with
170 additions
and
126 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,130 @@ | ||
package apidump | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/postmanlabs/postman-insights-agent/apispec" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type CommonApidumpFlags struct { | ||
Filter string | ||
HostAllowlist []string | ||
HostExclusions []string | ||
Interfaces []string | ||
PathAllowlist []string | ||
PathExclusions []string | ||
RandomizedStart int | ||
RateLimit float64 | ||
SendWitnessPayloads bool | ||
} | ||
|
||
func AddCommonApiDumpFlags(cmd *cobra.Command) (flags CommonApidumpFlags) { | ||
cmd.Flags().StringVar( | ||
&flags.Filter, | ||
"filter", | ||
"", | ||
"Used to match packets going to and coming from your API service.", | ||
) | ||
|
||
cmd.Flags().StringSliceVar( | ||
&flags.HostAllowlist, | ||
"host-allow", | ||
nil, | ||
"Allows only HTTP hosts matching regular expressions.", | ||
) | ||
|
||
cmd.Flags().StringSliceVar( | ||
&flags.HostExclusions, | ||
"host-exclusions", | ||
nil, | ||
"Removes HTTP hosts matching regular expressions.", | ||
) | ||
|
||
cmd.Flags().StringSliceVar( | ||
&flags.Interfaces, | ||
"interfaces", | ||
nil, | ||
"List of network interfaces to listen on. Defaults to all interfaces on host.", | ||
) | ||
|
||
cmd.Flags().StringSliceVar( | ||
&flags.PathAllowlist, | ||
"path-allow", | ||
nil, | ||
"Allows only HTTP paths matching regular expressions.", | ||
) | ||
|
||
cmd.Flags().StringSliceVar( | ||
&flags.PathExclusions, | ||
"path-exclusions", | ||
nil, | ||
"Removes HTTP paths matching regular expressions.", | ||
) | ||
|
||
cmd.Flags().IntVar( | ||
&flags.RandomizedStart, | ||
"randomized-start", | ||
100, | ||
"Probability that the apidump command will start intercepting traffic.", | ||
) | ||
_ = cmd.Flags().MarkHidden("randomized-start") | ||
|
||
cmd.Flags().Float64Var( | ||
&flags.RateLimit, | ||
"rate-limit", | ||
apispec.DefaultRateLimit, | ||
"Number of requests per minute to capture.", | ||
) | ||
|
||
cmd.Flags().BoolVar( | ||
&flags.SendWitnessPayloads, | ||
"send-witness-payloads", | ||
false, | ||
"Send request and response payloads to Postman", | ||
) | ||
_ = cmd.Flags().MarkHidden("send-witness-payloads") | ||
|
||
return flags | ||
} | ||
|
||
func ConvertCommonApiDumpFlagsToArgs(flags CommonApidumpFlags) []string { | ||
commonApidumpArgs := []string{} | ||
|
||
if flags.Filter != "" { | ||
commonApidumpArgs = append(commonApidumpArgs, "--filter", flags.Filter) | ||
} | ||
|
||
if flags.RandomizedStart != 100 { | ||
commonApidumpArgs = append(commonApidumpArgs, "--randomized-start", strconv.Itoa(flags.RandomizedStart)) | ||
} | ||
|
||
if flags.RateLimit != apispec.DefaultRateLimit { | ||
commonApidumpArgs = append(commonApidumpArgs, "--rate-limit", strconv.FormatFloat(flags.RateLimit, 'f', -1, 64)) | ||
} | ||
|
||
if flags.SendWitnessPayloads { | ||
commonApidumpArgs = append(commonApidumpArgs, "--send-witness-payloads") | ||
} | ||
|
||
// Add slice type flags to the entry point. | ||
// Flags: --host-allow, --host-exclusions, --interfaces, --path-allow, --path-exclusions | ||
// Added them separately instead of joining with comma(,) to avoid any regex parsing issues. | ||
for _, host := range flags.HostAllowlist { | ||
commonApidumpArgs = append(commonApidumpArgs, "--host-allow", host) | ||
} | ||
for _, host := range flags.HostExclusions { | ||
commonApidumpArgs = append(commonApidumpArgs, "--host-exclusions", host) | ||
} | ||
for _, interfaceFlag := range flags.Interfaces { | ||
commonApidumpArgs = append(commonApidumpArgs, "--interfaces", interfaceFlag) | ||
} | ||
for _, path := range flags.PathAllowlist { | ||
commonApidumpArgs = append(commonApidumpArgs, "--path-allow", path) | ||
} | ||
for _, path := range flags.PathExclusions { | ||
commonApidumpArgs = append(commonApidumpArgs, "--path-exclusions", path) | ||
} | ||
|
||
return commonApidumpArgs | ||
} |
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
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
Oops, something went wrong.