Skip to content

Commit

Permalink
Suppressing 423 error in Terragrunt Provider Cache (#3453)
Browse files Browse the repository at this point in the history
* fix: host discovery

* fix: tests

* chore: shell package code refactoring

* chore: code comment

* chore: fix tests

* chore: fix tests

* chore: fix tests

* chore: fix tests

* chore: grammar

* fix: tests

* chore: fix test

* chore: fix test

* fix: signal package

* fix: lint

* chore: fix tests

* chore: fix strict lint

* chore: error displaying improvements

* chore: fix tests

* chore: fix grammar

* chore: fix tests

* chore: fix tests

* chore: fix tests

* chore: fix strict lint

* chore: code improvements

* chore: code improvements

* chore: fix tests
  • Loading branch information
levkohimins authored Oct 8, 2024
1 parent b7719d5 commit afd5d7f
Show file tree
Hide file tree
Showing 138 changed files with 2,041 additions and 1,555 deletions.
36 changes: 18 additions & 18 deletions awshelper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/gruntwork-io/go-commons/errors"
"github.com/gruntwork-io/terragrunt/internal/errors"
"github.com/gruntwork-io/terragrunt/options"
)

Expand Down Expand Up @@ -81,7 +81,7 @@ func CreateAwsSessionFromConfig(config *AwsSessionConfig, terragruntOptions *opt

sess, err := session.NewSessionWithOptions(sessionOptions)
if err != nil {
return nil, errors.WithStackTraceAndPrefix(err, "Error initializing session")
return nil, errors.Errorf("Error initializing session: %w", err)
}

sess.Handlers.Build.PushFrontNamed(addUserAgent)
Expand Down Expand Up @@ -131,7 +131,7 @@ func (f tokenFetcher) FetchToken(ctx credentials.Context) ([]byte, error) {

token, err := os.ReadFile(string(f))
if err != nil {
return nil, errors.WithStackTrace(err)
return nil, errors.New(err)
}

return token, nil
Expand Down Expand Up @@ -204,7 +204,7 @@ func CreateAwsSession(config *AwsSessionConfig, terragruntOptions *options.Terra

sess, err = session.NewSessionWithOptions(sessionOptions)
if err != nil {
return nil, errors.WithStackTrace(err)
return nil, errors.New(err)
}

sess.Handlers.Build.PushFrontNamed(addUserAgent)
Expand All @@ -223,7 +223,7 @@ func CreateAwsSession(config *AwsSessionConfig, terragruntOptions *options.Terra
} else {
sess, err = CreateAwsSessionFromConfig(config, terragruntOptions)
if err != nil {
return nil, errors.WithStackTrace(err)
return nil, errors.New(err)
}
}

Expand All @@ -234,7 +234,7 @@ func CreateAwsSession(config *AwsSessionConfig, terragruntOptions *options.Terra
msg = fmt.Sprintf("Error finding AWS credentials in file '%s' (did you set the correct file name and/or profile?)", config.CredsFilename)
}

return nil, errors.WithStackTraceAndPrefix(err, msg) //nolint:govet
return nil, errors.Errorf("%s: %w", msg, err)
}

return sess, nil
Expand All @@ -246,7 +246,7 @@ func AssumeIamRole(iamRoleOpts options.IAMRoleOptions) (*sts.Credentials, error)

sess, err := session.NewSessionWithOptions(sessionOptions)
if err != nil {
return nil, errors.WithStackTrace(err)
return nil, errors.New(err)
}

sess.Handlers.Build.PushFrontNamed(addUserAgent)
Expand All @@ -257,7 +257,7 @@ func AssumeIamRole(iamRoleOpts options.IAMRoleOptions) (*sts.Credentials, error)

_, err = sess.Config.Credentials.Get()
if err != nil {
return nil, errors.WithStackTraceAndPrefix(err, "Error finding AWS credentials (did you set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables?)")
return nil, errors.Errorf("error finding AWS credentials (did you set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables?): %w", err)
}

stsClient := sts.New(sess)
Expand All @@ -282,7 +282,7 @@ func AssumeIamRole(iamRoleOpts options.IAMRoleOptions) (*sts.Credentials, error)

output, err := stsClient.AssumeRole(&input)
if err != nil {
return nil, errors.WithStackTrace(err)
return nil, errors.New(err)
}

return output.Credentials, nil
Expand All @@ -296,7 +296,7 @@ func AssumeIamRole(iamRoleOpts options.IAMRoleOptions) (*sts.Credentials, error)
} else {
tb, err := os.ReadFile(iamRoleOpts.WebIdentityToken)
if err != nil {
return nil, errors.WithStackTrace(err)
return nil, errors.New(err)
}

token = string(tb)
Expand All @@ -314,7 +314,7 @@ func AssumeIamRole(iamRoleOpts options.IAMRoleOptions) (*sts.Credentials, error)
// N.B: copied from SDK implementation
req.RetryErrorCodes = append(req.RetryErrorCodes, sts.ErrCodeInvalidIdentityTokenException)
if err := req.Send(); err != nil {
return nil, errors.WithStackTrace(err)
return nil, errors.New(err)
}

return resp.Credentials, nil
Expand All @@ -324,12 +324,12 @@ func AssumeIamRole(iamRoleOpts options.IAMRoleOptions) (*sts.Credentials, error)
func GetAWSCallerIdentity(config *AwsSessionConfig, terragruntOptions *options.TerragruntOptions) (sts.GetCallerIdentityOutput, error) {
sess, err := CreateAwsSession(config, terragruntOptions)
if err != nil {
return sts.GetCallerIdentityOutput{}, errors.WithStackTrace(err)
return sts.GetCallerIdentityOutput{}, errors.New(err)
}

identity, err := sts.New(sess).GetCallerIdentity(nil)
if err != nil {
return sts.GetCallerIdentityOutput{}, errors.WithStackTrace(err)
return sts.GetCallerIdentityOutput{}, errors.New(err)
}

return *identity, nil
Expand All @@ -346,12 +346,12 @@ func ValidateAwsSession(config *AwsSessionConfig, terragruntOptions *options.Ter
func GetAWSPartition(config *AwsSessionConfig, terragruntOptions *options.TerragruntOptions) (string, error) {
identity, err := GetAWSCallerIdentity(config, terragruntOptions)
if err != nil {
return "", errors.WithStackTrace(err)
return "", errors.New(err)
}

arn, err := arn.Parse(*identity.Arn)
if err != nil {
return "", errors.WithStackTrace(err)
return "", errors.New(err)
}

return arn.Partition, nil
Expand All @@ -361,7 +361,7 @@ func GetAWSPartition(config *AwsSessionConfig, terragruntOptions *options.Terrag
func GetAWSAccountID(config *AwsSessionConfig, terragruntOptions *options.TerragruntOptions) (string, error) {
identity, err := GetAWSCallerIdentity(config, terragruntOptions)
if err != nil {
return "", errors.WithStackTrace(err)
return "", errors.New(err)
}

return *identity.Account, nil
Expand All @@ -371,7 +371,7 @@ func GetAWSAccountID(config *AwsSessionConfig, terragruntOptions *options.Terrag
func GetAWSIdentityArn(config *AwsSessionConfig, terragruntOptions *options.TerragruntOptions) (string, error) {
identity, err := GetAWSCallerIdentity(config, terragruntOptions)
if err != nil {
return "", errors.WithStackTrace(err)
return "", errors.New(err)
}

return *identity.Arn, nil
Expand All @@ -381,7 +381,7 @@ func GetAWSIdentityArn(config *AwsSessionConfig, terragruntOptions *options.Terr
func GetAWSUserID(config *AwsSessionConfig, terragruntOptions *options.TerragruntOptions) (string, error) {
identity, err := GetAWSCallerIdentity(config, terragruntOptions)
if err != nil {
return "", errors.WithStackTrace(err)
return "", errors.New(err)
}

return *identity.UserId, nil
Expand Down
59 changes: 32 additions & 27 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package cli

import (
"context"
goerrors "errors"
"fmt"
"os"
"path/filepath"
"sort"
"time"

"github.com/gruntwork-io/terragrunt/engine"
"github.com/gruntwork-io/terragrunt/internal/os/exec"
"github.com/gruntwork-io/terragrunt/internal/os/signal"
"github.com/gruntwork-io/terragrunt/pkg/log"
"github.com/gruntwork-io/terragrunt/pkg/log/format"
"github.com/gruntwork-io/terragrunt/pkg/log/hooks"
Expand All @@ -27,9 +27,9 @@ import (

"github.com/gruntwork-io/terragrunt/shell"

"github.com/gruntwork-io/go-commons/errors"
"github.com/gruntwork-io/go-commons/version"
"github.com/gruntwork-io/terragrunt/config"
"github.com/gruntwork-io/terragrunt/internal/errors"
"github.com/gruntwork-io/terragrunt/util"
hashicorpversion "github.com/hashicorp/go-version"

Expand All @@ -49,9 +49,6 @@ import (
"github.com/gruntwork-io/terragrunt/pkg/cli"
)

// forced shutdown interval after receiving an interrupt signal
const forceExitInterval = shell.SignalForwardingDelay * 2

func init() {
cli.AppVersionTemplate = AppVersionTemplate
cli.AppHelpTemplate = AppHelpTemplate
Expand Down Expand Up @@ -84,6 +81,7 @@ func NewApp(opts *options.TerragruntOptions) *App {
app.Before = beforeAction(opts)
app.DefaultCommand = terraformCmd.NewCommand(opts).WrapAction(WrapWithTelemetry(opts)) // by default, if no terragrunt command is specified, run the Terraform command
app.OsExiter = OSExiter
app.ExitErrHandler = ExitErrHandler

return &App{app, opts}
}
Expand All @@ -92,25 +90,26 @@ func (app *App) Run(args []string) error {
return app.RunContext(context.Background(), args)
}

func (app *App) registerGracefullyShutdown(ctx context.Context) context.Context {
ctx, cancel := context.WithCancelCause(ctx)

signal.NotifierWithContext(ctx, func(sig os.Signal) {
// Carriage return helps prevent "^C" from being printed
fmt.Fprint(app.Writer, "\r") //nolint:errcheck
app.opts.Logger.Infof("%s signal received. Gracefully shutting down...", cases.Title(language.English).String(sig.String()))

cancel(signal.NewContextCanceledError(sig))
}, signal.InterruptSignals...)

return ctx
}

func (app *App) RunContext(ctx context.Context, args []string) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

shell.RegisterSignalHandler(func(signal os.Signal) {
app.opts.Logger.Infof("%s signal received. Gracefully shutting down... (it can take up to %v)", cases.Title(language.English).String(signal.String()), shell.SignalForwardingDelay)
cancel()
ctx = app.registerGracefullyShutdown(ctx)

shell.RegisterSignalHandler(func(signal os.Signal) {
app.opts.Logger.Infof("Second %s signal received, force shutting down...", cases.Title(language.English).String(signal.String()))
os.Exit(1)
})

time.Sleep(forceExitInterval)
app.opts.Logger.Infof("Failed to gracefully shutdown within %v, force shutting down...", forceExitInterval)
os.Exit(1)
}, shell.InterruptSignals...)

// configure telemetry integration
err := telemetry.InitTelemetry(ctx, &telemetry.TelemetryOptions{
Vars: env.Parse(os.Environ()),
AppName: app.Name,
Expand All @@ -137,7 +136,7 @@ func (app *App) RunContext(ctx context.Context, args []string) error {
}
}(ctx)

if err := app.App.RunContext(ctx, args); err != nil && !goerrors.Is(err, context.Canceled) {
if err := app.App.RunContext(ctx, args); err != nil && !errors.IsContextCanceled(err) {
return err
}

Expand Down Expand Up @@ -282,7 +281,7 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {
if opts.WorkingDir == "" {
currentDir, err := os.Getwd()
if err != nil {
return errors.WithStackTrace(err)
return errors.New(err)
}

opts.WorkingDir = currentDir
Expand All @@ -292,7 +291,7 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {

workingDir, err := filepath.Abs(opts.WorkingDir)
if err != nil {
return errors.WithStackTrace(err)
return errors.New(err)
}

opts.Logger = opts.Logger.WithField(format.PrefixKeyName, workingDir)
Expand All @@ -315,7 +314,7 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {

downloadDir, err := filepath.Abs(opts.DownloadDir)
if err != nil {
return errors.WithStackTrace(err)
return errors.New(err)
}

opts.DownloadDir = filepath.ToSlash(downloadDir)
Expand All @@ -329,7 +328,7 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {

opts.TerragruntConfigPath, err = filepath.Abs(opts.TerragruntConfigPath)
if err != nil {
return errors.WithStackTrace(err)
return errors.New(err)
}

opts.TerraformPath = filepath.ToSlash(opts.TerraformPath)
Expand Down Expand Up @@ -361,7 +360,7 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {
if err != nil {
// Malformed Terragrunt version; set the version to 0.0
if terragruntVersion, err = hashicorpversion.NewVersion("0.0"); err != nil {
return errors.WithStackTrace(err)
return errors.New(err)
}
}

Expand All @@ -381,12 +380,18 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {

opts.RunTerragrunt = terraformCmd.Run

shell.PrepareConsole(opts)
exec.PrepareConsole(opts.Logger)

return nil
}

// OSExiter is an empty function that overrides the default behavior.
func OSExiter(exitCode int) {
// Do nothing. We just need to override this function, as the default value calls os.Exit, which
// kills the app (or any automated test) dead in its tracks.
}

// ExitErrHandler is an empty function that overrides the default behavior.
func ExitErrHandler(_ *cli.Context, err error) error {
return err
}
5 changes: 3 additions & 2 deletions cli/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"testing"

"github.com/gruntwork-io/go-commons/errors"
"github.com/gruntwork-io/terragrunt/cli"
"github.com/gruntwork-io/terragrunt/cli/commands"
awsproviderpatch "github.com/gruntwork-io/terragrunt/cli/commands/aws-provider-patch"
Expand All @@ -17,6 +16,7 @@ import (
runall "github.com/gruntwork-io/terragrunt/cli/commands/run-all"
terraformcmd "github.com/gruntwork-io/terragrunt/cli/commands/terraform"
"github.com/gruntwork-io/terragrunt/config"
"github.com/gruntwork-io/terragrunt/internal/errors"
"github.com/gruntwork-io/terragrunt/options"
cliPkg "github.com/gruntwork-io/terragrunt/pkg/cli"
"github.com/gruntwork-io/terragrunt/pkg/log"
Expand Down Expand Up @@ -225,7 +225,7 @@ func mockOptions(t *testing.T, terragruntConfigPath string, workingDir string, t

opts, err := options.NewTerragruntOptionsForTest(terragruntConfigPath)
if err != nil {
t.Fatalf("error: %v\n", errors.WithStackTrace(err))
t.Fatalf("error: %v\n", errors.New(err))
}

opts.WorkingDir = workingDir
Expand Down Expand Up @@ -498,6 +498,7 @@ func runAppTest(args []string, opts *options.TerragruntOptions) (*options.Terrag
terragruntCommands...).WrapAction(cli.WrapWithTelemetry(opts))
app.DefaultCommand = defaultCommand.WrapAction(cli.WrapWithTelemetry(opts))
app.OsExiter = cli.OSExiter
app.ExitErrHandler = cli.ExitErrHandler

err := app.Run(append([]string{"--"}, args...))
return opts, err
Expand Down
Loading

0 comments on commit afd5d7f

Please sign in to comment.