Skip to content

Commit

Permalink
(feat) display more information about aws creds being used.
Browse files Browse the repository at this point in the history
  • Loading branch information
tphoney committed Oct 3, 2024
1 parent f35ef0b commit c1e00a4
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
path: .terraform
key: ${{ runner.os }}-${{ inputs.environment }}-${{ hashFiles('**/.terraform.lock.hcl') }}

- uses: hashicorp/setup-terraform@v2
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.5.7
terraform_wrapper: false
Expand Down Expand Up @@ -98,7 +98,7 @@ jobs:
path: .terraform
key: ${{ runner.os }}-${{ inputs.environment }}-${{ hashFiles('**/.terraform.lock.hcl') }}

- uses: hashicorp/setup-terraform@v2
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.5.7
terraform_wrapper: false
Expand Down
4 changes: 2 additions & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ builds:
# action yet.
# signs:
# - id: amd64
# signature: "overmind-cli-amd64.dmg"
# signature: "overmind-cli-amd64.dmg"
# ids:
# - overmind-macos # here we filter the macos only build id
# cmd: ./gon
Expand Down Expand Up @@ -147,7 +147,7 @@ checksum:
name_template: "checksums.txt"

snapshot:
name_template: "{{ incpatch .Version }}-next"
version_template: "{{ incpatch .Version }}-next"

changelog:
sort: asc
Expand Down
46 changes: 29 additions & 17 deletions cmd/explore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/google/uuid"
"github.com/overmindtech/aws-source/proc"
"github.com/overmindtech/cli/tfutils"
"github.com/overmindtech/discovery"
"github.com/overmindtech/pterm"
"github.com/overmindtech/sdp-go"
stdlibsource "github.com/overmindtech/stdlib-source/sources"
stdlibSource "github.com/overmindtech/stdlib-source/sources"
"github.com/pkg/browser"
log "github.com/sirupsen/logrus"
"github.com/sourcegraph/conc/pool"
Expand All @@ -37,11 +38,17 @@ var exploreCmd = &cobra.Command{
// any query or request during the runtime of the CLI. for proper cleanup,
// execute the returned function. The method returns once the sources are
// started. Progress is reported into the provided multi printer.
func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oauth2.Token, tfArgs []string, multi *pterm.MultiPrinter) (func(), error) {
func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oauth2.Token, tfArgs []string, failOverToAws bool) (func(), error) {
var err error

multi := pterm.DefaultMultiPrinter
_, _ = multi.Start()
defer func() {
_, _ = multi.Stop()
}()
stdlibSpinner, _ := pterm.DefaultSpinner.WithWriter(multi.NewWriter()).Start("Starting stdlib source engine")
awsSpinner, _ := pterm.DefaultSpinner.WithWriter(multi.NewWriter()).Start("Starting AWS source engine")
statusArea := pterm.DefaultParagraph.WithWriter(multi.NewWriter())

natsOptions := natsOptions(ctx, oi, token)
heartbeatOptions := heartbeatOptions(oi, token)
Expand All @@ -54,7 +61,7 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut
p := pool.NewWithResults[*discovery.Engine]().WithErrors()

p.Go(func() (*discovery.Engine, error) {
stdlibEngine, err := stdlibsource.InitializeEngine(
stdlibEngine, err := stdlibSource.InitializeEngine(
natsOptions,
fmt.Sprintf("stdlib-source-%v", hostname),
fmt.Sprintf("cli-%v", cliVersion),
Expand All @@ -67,7 +74,6 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut
stdlibSpinner.Fail("Failed to initialize stdlib source engine")
return nil, fmt.Errorf("failed to initialize stdlib source engine: %w", err)
}

// todo: pass in context with timeout to abort timely and allow Ctrl-C to work
err = stdlibEngine.Start()
if err != nil {
Expand All @@ -91,7 +97,6 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut
return nil, fmt.Errorf("failed to parse providers: %w", err)
}
configs := []aws.Config{}

for _, p := range providers {
if p.Error != nil {
// skip providers that had errors. This allows us to use
Expand All @@ -104,9 +109,19 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut
awsSpinner.Fail("Error when converting provider to config")
return nil, fmt.Errorf("error when converting provider to config: %w", err)
}
credentials, _ := c.Credentials.Retrieve(ctx)
statusArea.Println(fmt.Sprintf("Using AWS provider in %s with %s.", p.FilePath, credentials.Source))
configs = append(configs, c)
}

if len(configs) == 0 && failOverToAws {
userConfig, err := config.LoadDefaultConfig(ctx)
if err != nil {
awsSpinner.Fail("Failed to load default AWS config")
return nil, fmt.Errorf("failed to load default AWS config: %w", err)
}
statusArea.Println("Using default AWS CLI config. No AWS terraform providers found.")
configs = append(configs, userConfig)
}
awsEngine, err := proc.InitializeAwsSourceEngine(
ctx,
fmt.Sprintf("aws-source-%v", hostname),
Expand Down Expand Up @@ -155,27 +170,24 @@ func Explore(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

multi := pterm.DefaultMultiPrinter

_, _ = multi.Start()

ctx, oi, token, err := login(ctx, cmd, []string{"request:receive"}, multi.NewWriter())
_, _ = multi.Start() // multi-printer controls the lifecycle of screen output, it needs to be stopped before printing anything else
defer func() {
_, _ = multi.Stop()
}()
ctx, oi, token, err := login(ctx, cmd, []string{"request:receive", "api:read"}, multi.NewWriter())
_, _ = multi.Stop()
if err != nil {
return err
}

cleanup, err := StartLocalSources(ctx, oi, token, args, &multi)
cleanup, err := StartLocalSources(ctx, oi, token, args, true)
if err != nil {
return err
}
defer cleanup()

_, _ = multi.Stop()

exploreURL := fmt.Sprintf("%v/explore", oi.FrontendUrl)
err = browser.OpenURL(exploreURL)
if err != nil {
pterm.Error.Printf("Unable to open browser: %v", err)
}
_ = browser.OpenURL(exploreURL) // ignore error, we can't do anything about it

pterm.Println()
pterm.Println(fmt.Sprintf("Explore your infrastructure graph at %s", exploreURL))
Expand Down
4 changes: 2 additions & 2 deletions cmd/pterm.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ func StartSources(ctx context.Context, cmd *cobra.Command, args []string) (conte
_, _ = multi.Stop()
}()

ctx, oi, token, err := login(ctx, cmd, []string{"explore:read", "changes:write", "config:write", "request:receive"}, multi.NewWriter())
ctx, oi, token, err := login(ctx, cmd, []string{"explore:read", "changes:write", "config:write", "request:receive", "api:read", "sources:read"}, multi.NewWriter())
if err != nil {
return ctx, sdp.OvermindInstance{}, nil, nil, err
}

cleanup, err := StartLocalSources(ctx, oi, token, args, &multi)
cleanup, err := StartLocalSources(ctx, oi, token, args, false)
if err != nil {
return ctx, sdp.OvermindInstance{}, nil, nil, err
}
Expand Down
5 changes: 1 addition & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,7 @@ func getOauthToken(ctx context.Context, oi sdp.OvermindInstance, requiredScopes

var token *oauth2.Token

err = browser.OpenURL(deviceCode.VerificationURI)
if err != nil {
pterm.Error.Printf("Unable to open browser: %v", err)
}
_ = browser.OpenURL(deviceCode.VerificationURI) // nolint:errcheck // we don't care if the browser fails to open
pterm.Print(
markdownToString(MAX_TERMINAL_WIDTH, fmt.Sprintf(
beginAuthMessage,
Expand Down
11 changes: 5 additions & 6 deletions cmd/terraform_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TerraformApply(cmd *cobra.Command, args []string) error {
PTermSetup()

hasPlanSet := false
autoapprove := false
autoApprove := false
planFile := "overmind.plan"
if len(args) >= 1 {
f, err := os.Stat(args[len(args)-1])
Expand All @@ -51,7 +51,7 @@ func TerraformApply(cmd *cobra.Command, args []string) error {
}
if hasPlanSet {
planFile = args[len(args)-1]
autoapprove = true
autoApprove = true
}
}

Expand All @@ -76,18 +76,18 @@ func TerraformApply(cmd *cobra.Command, args []string) error {
// therefore we only check for the flag when no plan file is supplied
for _, a := range args {
if a == "-auto-approve" || a == "-auto-approve=true" || a == "-auto-approve=TRUE" || a == "--auto-approve" || a == "--auto-approve=true" || a == "--auto-approve=TRUE" {
autoapprove = true
autoApprove = true
}
if a == "-auto-approve=false" || a == "-auto-approve=FALSE" || a == "--auto-approve=false" || a == "--auto-approve=FALSE" {
autoapprove = false
autoApprove = false
}
}
}

args = append([]string{"apply"}, args...)

needPlan := !hasPlanSet
needApproval := !autoapprove
needApproval := !autoApprove

ctx, oi, _, cleanup, err := StartSources(ctx, cmd, args)
if err != nil {
Expand All @@ -103,7 +103,6 @@ func TerraformApply(cmd *cobra.Command, args []string) error {
}

if needApproval {

pterm.Println("")
pterm.Println("Do you want to perform these actions?")
pterm.Println("")
Expand Down
2 changes: 1 addition & 1 deletion cmd/terraform_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func TerraformPlanImpl(ctx context.Context, cmd *cobra.Command, oi sdp.OvermindI
blastRadiusSpinner.UpdateText(fmt.Sprintf("Calculating Blast Radius: %v", snapshotDetail(stateLabel, blastRadiusItems, blastRadiusEdges)))
}
if resultStream.Err() != nil {
blastRadiusSpinner.Fail(fmt.Sprintf("Calculating Blast Radius: error streaming results: %v", err))
blastRadiusSpinner.Fail(fmt.Sprintf("Calculating Blast Radius: error streaming results: %v", resultStream.Err()))
return nil
}
blastRadiusSpinner.Success("Calculating Blast Radius: done")
Expand Down

0 comments on commit c1e00a4

Please sign in to comment.