From 80e23bdb506835cdfe26780abcfd977363bae015 Mon Sep 17 00:00:00 2001 From: Dan Luhring Date: Tue, 25 May 2021 15:01:02 -0400 Subject: [PATCH] Add config option for import timeout (#421) Signed-off-by: Dan Luhring --- cmd/packages.go | 10 ++++++++++ internal/anchore/import.go | 4 +++- internal/config/anchore.go | 2 ++ .../test-fixtures/snapshot/TestJSONPresenter.golden | 3 ++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cmd/packages.go b/cmd/packages.go index 09e21a9ae..4653cbd98 100644 --- a/cmd/packages.go +++ b/cmd/packages.go @@ -137,6 +137,11 @@ func setPackageFlags(flags *pflag.FlagSet) { "overwrite-existing-image", false, "overwrite an existing image during the upload to Anchore Enterprise", ) + + flags.Uint( + "import-timeout", 30, + "set a timeout duration (in seconds) for the upload to Anchore Enterprise", + ) } func bindPackagesConfigOptions(flags *pflag.FlagSet) error { @@ -172,6 +177,10 @@ func bindPackagesConfigOptions(flags *pflag.FlagSet) error { return err } + if err := viper.BindPFlag("anchore.import-timeout", flags.Lookup("import-timeout")); err != nil { + return err + } + return nil } @@ -262,6 +271,7 @@ func runPackageSbomUpload(src source.Source, s source.Metadata, catalog *pkg.Cat Dockerfile: dockerfileContents, OverwriteExistingUpload: appConfig.Anchore.OverwriteExistingImage, Scope: scope, + Timeout: appConfig.Anchore.ImportTimeout, } if err := c.Import(context.Background(), importCfg); err != nil { diff --git a/internal/anchore/import.go b/internal/anchore/import.go index 17a679879..b63c02371 100644 --- a/internal/anchore/import.go +++ b/internal/anchore/import.go @@ -27,6 +27,7 @@ type ImportConfig struct { Dockerfile []byte OverwriteExistingUpload bool Scope source.Scope + Timeout uint } func importProgress(source string) (*progress.Stage, *progress.Manual) { @@ -54,7 +55,8 @@ func importProgress(source string) (*progress.Stage, *progress.Manual) { func (c *Client) Import(ctx context.Context, cfg ImportConfig) error { stage, prog := importProgress(c.config.BaseURL) - ctxWithTimeout, cancel := context.WithTimeout(ctx, time.Second*30) + timeout := time.Duration(cfg.Timeout) * time.Second + ctxWithTimeout, cancel := context.WithTimeout(ctx, timeout) defer cancel() authedCtx := c.newRequestContext(ctxWithTimeout) diff --git a/internal/config/anchore.go b/internal/config/anchore.go index 430c3834b..7e3bc281c 100644 --- a/internal/config/anchore.go +++ b/internal/config/anchore.go @@ -12,6 +12,8 @@ type anchore struct { Password string `yaml:"-" json:"-" mapstructure:"password"` // -p , password to authenticate upload Dockerfile string `yaml:"dockerfile" json:"dockerfile" mapstructure:"dockerfile"` // -d , dockerfile to attach for upload OverwriteExistingImage bool `yaml:"overwrite-existing-image" json:"overwrite-existing-image" mapstructure:"overwrite-existing-image"` // --overwrite-existing-image , if any of the SBOM components have already been uploaded this flag will ensure they are overwritten with the current upload + ImportTimeout uint `yaml:"import-timeout" json:"import-timeout" mapstructure:"import-timeout"` // --import-timeout + // , customize the number of seconds within which the SBOM import must be completed or canceled } func (cfg anchore) loadDefaultValues(v *viper.Viper) { diff --git a/internal/presenter/poweruser/test-fixtures/snapshot/TestJSONPresenter.golden b/internal/presenter/poweruser/test-fixtures/snapshot/TestJSONPresenter.golden index 7067fa2bc..8fa206105 100644 --- a/internal/presenter/poweruser/test-fixtures/snapshot/TestJSONPresenter.golden +++ b/internal/presenter/poweruser/test-fixtures/snapshot/TestJSONPresenter.golden @@ -174,7 +174,8 @@ "host": "", "path": "", "dockerfile": "", - "overwrite-existing-image": false + "overwrite-existing-image": false, + "import-timeout": 0 }, "dev": { "profile-cpu": false,