From 5e144b7546c35fafaeba6b4e677603f8e6891887 Mon Sep 17 00:00:00 2001 From: d047491 Date: Thu, 15 Aug 2024 12:58:23 +0000 Subject: [PATCH] send more upload information via headers --- pkg/supply/env/config.go | 29 +++++++++++++++++++++++++++++ pkg/supply/supply.go | 9 ++++++++- pkg/supply/supply_test.go | 6 ++++++ pkg/uploader/uploader.go | 7 +++++-- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/pkg/supply/env/config.go b/pkg/supply/env/config.go index b4cdc7c..c3cbbb4 100644 --- a/pkg/supply/env/config.go +++ b/pkg/supply/env/config.go @@ -20,6 +20,35 @@ type amsDataDeprecated struct { Root string `json:"root"` } +type VcapApplication struct { + ApplicationID string `json:"application_id"` + ApplicationName string `json:"application_name"` + ApplicationUris []string `json:"application_uris"` + CfAPI string `json:"cf_api"` + Limits struct { + Fds int `json:"fds"` + } `json:"limits"` + Name string `json:"name"` + OrganizationID string `json:"organization_id"` + OrganizationName string `json:"organization_name"` + SpaceID string `json:"space_id"` + SpaceName string `json:"space_name"` + Uris []string `json:"uris"` + Users any `json:"users"` +} + +func LoadVcapApplication(log *libbuildpack.Logger) (VcapApplication, error) { + vcapStr, vcapSet := os.LookupEnv("VCAP_APPLICATION") + var result VcapApplication + if vcapSet { + err := json.Unmarshal([]byte(vcapStr), &result) + if err != nil { + log.Error("error parsing VCAP_APPLICATION value %s : %v", vcapStr, err) + } + } + return result, nil +} + func LoadBuildpackConfig(log *libbuildpack.Logger) (Config, error) { // Deprecated compatibility coding to support AMS_DATA for now (AMS_DATA.serviceNname will be ignored, because its not supposed to be supported by stakeholders) amsData, amsDataSet := os.LookupEnv("AMS_DATA") diff --git a/pkg/supply/supply.go b/pkg/supply/supply.go index aa5338a..f839d03 100644 --- a/pkg/supply/supply.go +++ b/pkg/supply/supply.go @@ -280,12 +280,19 @@ func (s *Supplier) upload(creds *services.IASCredentials, tlsCfg tlsConfig, root if err != nil { return fmt.Errorf("unable to create AMS client: %s", err) } + vcapApp, err := env.LoadVcapApplication(s.Log) + if err != nil { + return err + } u := uploader.Uploader{ Log: s.Log, Root: path.Join(s.Stager.BuildDir(), rootDir), Client: client, AMSInstanceID: creds.AmsInstanceID, - UserAgent: fmt.Sprintf("cloud-authorization-buildpack/%s", s.BuildpackVersion), + ExtraHeaders: map[string]string{ + "User-Agent": fmt.Sprintf("cloud-authorization-buildpack/%s", s.BuildpackVersion), + "X-Cf-Appname": vcapApp.ApplicationName, + }, } return u.Do(context.Background(), creds.AmsServerURL) } diff --git a/pkg/supply/supply_test.go b/pkg/supply/supply_test.go index e02a58c..51790c3 100644 --- a/pkg/supply/supply_test.go +++ b/pkg/supply/supply_test.go @@ -158,6 +158,7 @@ var _ = Describe("Supply", func() { BeforeEach(func() { vcapServices = testdata.EnvWithIASAuthX509 os.Setenv("AMS_DCL_ROOT", "/policies") + os.Setenv("VCAP_APPLICATION", "{\"application_name\":\"unit-tests-appname\"}") }) It("should succeed", func() { Expect(supplier.Run()).To(Succeed()) @@ -175,6 +176,11 @@ var _ = Describe("Supply", func() { expectedValue := []string{"cloud-authorization-buildpack/UNIT-TEST"} Expect(uploadReqSpy.Header).Should(HaveKeyWithValue("User-Agent", expectedValue)) }) + It("sets the cf app name as upload header", func() { + Expect(supplier.Run()).To(Succeed()) + expectedValue := []string{"unit-tests-appname"} + Expect(uploadReqSpy.Header).Should(HaveKeyWithValue("X-Cf-Appname", expectedValue)) + }) It("creates a valid launch.yml", func() { Expect(supplier.Run()).To(Succeed()) launchConfig, err := os.Open(filepath.Join(depDir, "launch.yml")) diff --git a/pkg/uploader/uploader.go b/pkg/uploader/uploader.go index e7518ff..2bf58be 100644 --- a/pkg/uploader/uploader.go +++ b/pkg/uploader/uploader.go @@ -21,7 +21,7 @@ type Uploader struct { Root string Client AMSClient AMSInstanceID string - UserAgent string + ExtraHeaders map[string]string } //go:generate mockgen --build_flags=--mod=mod --destination=../supply/client_mock_test.go --package=supply_test github.com/SAP/cloud-authorization-buildpack/pkg/uploader AMSClient @@ -92,8 +92,11 @@ func (up *Uploader) do(ctx context.Context, dstURL string, body []byte) (*http.R return nil, fmt.Errorf("could not create DCL upload request %w", err) } r.Header.Set(env.HeaderInstanceID, up.AMSInstanceID) - r.Header.Set("User-Agent", up.UserAgent) r.Header.Set("Content-Type", "application/gzip") + + for key, value := range up.ExtraHeaders { + r.Header.Set(key, value) + } return up.Client.Do(r) }