-
Notifications
You must be signed in to change notification settings - Fork 0
feat: audit command #98
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
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
14f7d9e
o
keroxp 3073a8f
a
keroxp 544e3b6
a
keroxp f1b9b85
a
keroxp adacf58
Update scan.go
keroxp 97f4461
test
keroxp 1526180
Update printer.go
keroxp 7b54f9d
Update cli/cage/scan/printer.go
keroxp 822c10a
test
keroxp 7264d28
Merge branch 'feat-scan' of https://github.com/loilo-inc/canarycage i…
keroxp bc34201
Update printer_test.go
keroxp fa5cd67
test
keroxp 268af2b
tets
keroxp 92965c9
a
keroxp 732c6a0
w
keroxp 50c9d81
test
keroxp 43f643f
Update types_test.go
keroxp 74b2585
a
keroxp 7a31f5a
a
keroxp 7f960ae
a
keroxp cbba2b7
PR
keroxp b11b96d
a
keroxp 43824a3
a
keroxp 5d24307
add package name
keroxp 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
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,65 @@ | ||
| package cageapp | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/aws" | ||
| "github.com/aws/aws-sdk-go-v2/config" | ||
| "github.com/aws/aws-sdk-go-v2/service/ec2" | ||
| "github.com/aws/aws-sdk-go-v2/service/ecr" | ||
| "github.com/aws/aws-sdk-go-v2/service/ecs" | ||
| "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2" | ||
| cage "github.com/loilo-inc/canarycage" | ||
| "github.com/loilo-inc/canarycage/cli/cage/scan" | ||
| "github.com/loilo-inc/canarycage/env" | ||
| "github.com/loilo-inc/canarycage/key" | ||
| "github.com/loilo-inc/canarycage/logger" | ||
| "github.com/loilo-inc/canarycage/task" | ||
| "github.com/loilo-inc/canarycage/timeout" | ||
| "github.com/loilo-inc/canarycage/types" | ||
| "github.com/loilo-inc/logos/di" | ||
| "golang.org/x/xerrors" | ||
| ) | ||
|
|
||
| func ProvideCageCli(envars *env.Envars) (types.Cage, error) { | ||
| conf, err := loadAwsConfig(envars.Region) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| d := di.NewDomain(func(b *di.B) { | ||
| b.Set(key.Env, envars) | ||
| b.Set(key.EcsCli, ecs.NewFromConfig(conf)) | ||
| b.Set(key.EcrCli, ecr.NewFromConfig(conf)) | ||
| b.Set(key.Ec2Cli, ec2.NewFromConfig(conf)) | ||
| b.Set(key.AlbCli, elasticloadbalancingv2.NewFromConfig(conf)) | ||
| b.Set(key.TaskFactory, task.NewFactory(b.Future())) | ||
| b.Set(key.Time, &timeout.Time{}) | ||
| }) | ||
| cagecli := cage.NewCage(d) | ||
| return cagecli, nil | ||
| } | ||
|
|
||
| func ProvideScanDI(region string) (*di.D, error) { | ||
| conf, err := loadAwsConfig(region) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| d := di.NewDomain(func(b *di.B) { | ||
| ecsCli := ecs.NewFromConfig(conf) | ||
| ecrCli := ecr.NewFromConfig(conf) | ||
| b.Set(key.Scanner, scan.NewScanner(ecsCli, ecrCli)) | ||
| b.Set(key.Logger, logger.DefaultLogger(os.Stdout)) | ||
| }) | ||
| return d, nil | ||
| } | ||
|
|
||
| func loadAwsConfig(region string) (aws.Config, error) { | ||
| conf, err := config.LoadDefaultConfig( | ||
| context.Background(), | ||
| config.WithRegion(region)) | ||
| if err != nil { | ||
| return aws.Config{}, xerrors.Errorf("failed to load aws config: %w", err) | ||
| } | ||
| return conf, 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,81 @@ | ||
| package cageapp_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/loilo-inc/canarycage/cli/cage/cageapp" | ||
| "github.com/loilo-inc/canarycage/env" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestProvideCageCli(t *testing.T) { | ||
| t.Run("successfully creates cage cli with valid region", func(t *testing.T) { | ||
| envars := &env.Envars{ | ||
| Region: "us-east-1", | ||
| } | ||
|
|
||
| cage, err := cageapp.ProvideCageCli(envars) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, cage) | ||
| }) | ||
|
|
||
| t.Run("returns error with invalid region", func(t *testing.T) { | ||
| envars := &env.Envars{ | ||
| Region: "", | ||
| } | ||
|
|
||
| cage, err := cageapp.ProvideCageCli(envars) | ||
| if err != nil { | ||
| assert.Nil(t, cage, "expected cage to be nil when error occurs") | ||
| return | ||
| } | ||
| assert.NotNil(t, cage, "expected cage to be non-nil when no error") | ||
| }) | ||
|
|
||
| t.Run("handles nil envars", func(t *testing.T) { | ||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| return | ||
| } | ||
| }() | ||
|
|
||
| cage, err := cageapp.ProvideCageCli(nil) | ||
| if err == nil { | ||
| assert.NotNil(t, cage, "expected cage to be non-nil when no error") | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestProvideScanDI(t *testing.T) { | ||
| t.Run("successfully creates scan DI with valid region", func(t *testing.T) { | ||
| region := "us-east-1" | ||
|
|
||
| d, err := cageapp.ProvideScanDI(region) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, d) | ||
| }) | ||
|
|
||
| t.Run("returns error with invalid region", func(t *testing.T) { | ||
| region := "" | ||
|
|
||
| d, err := cageapp.ProvideScanDI(region) | ||
| if err != nil { | ||
| assert.Nil(t, d, "expected DI domain to be nil when error occurs") | ||
| return | ||
| } | ||
| assert.NotNil(t, d, "expected DI domain to be non-nil when no error") | ||
| }) | ||
|
|
||
| t.Run("creates DI domain with different regions", func(t *testing.T) { | ||
| regions := []string{"us-west-2", "eu-west-1", "ap-northeast-1"} | ||
|
|
||
| for _, region := range regions { | ||
| d, err := cageapp.ProvideScanDI(region) | ||
| if err != nil { | ||
| t.Logf("region %s returned error: %v", region, err) | ||
| continue | ||
| } | ||
| assert.NotNil(t, d, "expected DI domain to be non-nil for region %s", region) | ||
| } | ||
| }) | ||
| } |
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,46 @@ | ||
| package commands_test | ||
|
|
||
| import ( | ||
| "io" | ||
| "testing" | ||
|
|
||
| "github.com/loilo-inc/canarycage/cli/cage/cageapp" | ||
| "github.com/loilo-inc/canarycage/cli/cage/commands" | ||
| "github.com/loilo-inc/canarycage/env" | ||
| "github.com/loilo-inc/canarycage/mocks/mock_types" | ||
| "github.com/loilo-inc/canarycage/types" | ||
| "github.com/urfave/cli/v2" | ||
| "go.uber.org/mock/gomock" | ||
| ) | ||
|
|
||
| var stdinService = "ap-notheast-1\ncluster\nservice\nyes\n" | ||
| var stdinTask = "ap-notheast-1\ncluster\nyes\n" | ||
|
|
||
| func setup(t *testing.T, input io.Reader) (*cli.App, *mock_types.MockCage) { | ||
| ctrl := gomock.NewController(t) | ||
| cagecli := mock_types.NewMockCage(ctrl) | ||
| cageapp := &cageapp.App{Stdin: input} | ||
| app := cli.NewApp() | ||
| cmds := commands.NewCageCommands(func(envars *env.Envars) (types.Cage, error) { | ||
| return cagecli, nil | ||
| }) | ||
| app.Commands = []*cli.Command{ | ||
| cmds.Up(cageapp), | ||
| cmds.RollOut(cageapp), | ||
| cmds.Run(cageapp), | ||
| } | ||
| app.Flags = []cli.Flag{ | ||
| &cli.BoolFlag{ | ||
| Name: "ci", | ||
| Destination: &cageapp.CI, | ||
| Value: false, | ||
| }, | ||
| } | ||
| return app, cagecli | ||
| } | ||
|
|
||
| type errorReader struct{} | ||
|
|
||
| func (e *errorReader) Read(p []byte) (n int, err error) { | ||
| return 0, io.EOF | ||
| } |
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
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.