From f934314be3ff62225e33c737112fb6e67f7c28ee Mon Sep 17 00:00:00 2001 From: Sune Keller Date: Thu, 7 Jan 2021 18:15:53 +0100 Subject: [PATCH] feat(http): support custom headers in http upload (#2002) Signed-off-by: Sune Keller --- internal/http/http.go | 5 +++++ internal/http/http_test.go | 15 +++++++++++++++ pkg/config/config.go | 23 ++++++++++++----------- www/docs/customization/upload.md | 5 +++++ 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/internal/http/http.go b/internal/http/http.go index af6272551..15e7b949c 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -240,6 +240,11 @@ func uploadAsset(ctx *context.Context, upload *config.Upload, artifact *artifact log.Debugf("generated target url: %s", targetURL) var headers = map[string]string{} + if upload.CustomHeaders != nil { + for name, value := range upload.CustomHeaders { + headers[name] = value + } + } if upload.ChecksumHeader != "" { sum, err := artifact.Checksum("sha256") if err != nil { diff --git a/internal/http/http_test.go b/internal/http/http_test.go index b0a75a1cc..87a3b20ab 100644 --- a/internal/http/http_test.go +++ b/internal/http/http_test.go @@ -469,6 +469,21 @@ func TestUpload(t *testing.T) { }, checks(check{"/blah/2.1.0/a.ubi", "u2", "x", content, map[string]string{"-x-sha256": "5e2bf57d3f40c4b6df69daf1936cb766f832374b4fc0259a7cbff06e2f70f269"}}), }, + {"custom-headers", true, true, false, false, + func(s *httptest.Server) (*context.Context, config.Upload) { + return ctx, config.Upload{ + Mode: ModeBinary, + Name: "a", + Target: s.URL + "/{{.ProjectName}}/{{.Version}}/", + Username: "u2", + CustomHeaders: map[string]string{ + "x-custom-header-name": "custom-header-value", + }, + TrustedCerts: cert(s), + } + }, + checks(check{"/blah/2.1.0/a.ubi", "u2", "x", content, map[string]string{"x-custom-header-name": "custom-header-value"}}), + }, } uploadAndCheck := func(t *testing.T, setup func(*httptest.Server) (*context.Context, config.Upload), wantErrPlain, wantErrTLS bool, check func(r []*h.Request) error, srv *httptest.Server) { diff --git a/pkg/config/config.go b/pkg/config/config.go index f9cd30cf4..438d9bd3e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -543,17 +543,18 @@ type Blob struct { // Upload configuration. type Upload struct { - Name string `yaml:",omitempty"` - IDs []string `yaml:"ids,omitempty"` - Target string `yaml:",omitempty"` - Username string `yaml:",omitempty"` - Mode string `yaml:",omitempty"` - Method string `yaml:",omitempty"` - ChecksumHeader string `yaml:"checksum_header,omitempty"` - TrustedCerts string `yaml:"trusted_certificates,omitempty"` - Checksum bool `yaml:",omitempty"` - Signature bool `yaml:",omitempty"` - CustomArtifactName bool `yaml:"custom_artifact_name,omitempty"` + Name string `yaml:",omitempty"` + IDs []string `yaml:"ids,omitempty"` + Target string `yaml:",omitempty"` + Username string `yaml:",omitempty"` + Mode string `yaml:",omitempty"` + Method string `yaml:",omitempty"` + ChecksumHeader string `yaml:"checksum_header,omitempty"` + TrustedCerts string `yaml:"trusted_certificates,omitempty"` + Checksum bool `yaml:",omitempty"` + Signature bool `yaml:",omitempty"` + CustomArtifactName bool `yaml:"custom_artifact_name,omitempty"` + CustomHeaders map[string]string `yaml:"custom_headers,omitempty"` } // Publisher configuration. diff --git a/www/docs/customization/upload.md b/www/docs/customization/upload.md index 7fddba76d..46cc0557c 100644 --- a/www/docs/customization/upload.md +++ b/www/docs/customization/upload.md @@ -161,6 +161,11 @@ uploads: # Default is empty. checksum_header: -X-SHA256-Sum + # A map of custom headers e.g. to support required content types or auth schemes. + # Default is empty. + custom_headers: + JOB-TOKEN: {{ .Env.CI_JOB_TOKEN }} + # Upload checksums (defaults to false) checksum: true