Skip to content

Commit

Permalink
Send appname as header on DCL upload (#94)
Browse files Browse the repository at this point in the history
* send more upload information via headers

* implement review suggestions
  • Loading branch information
d047491 authored Aug 20, 2024
1 parent 94b9c67 commit 133075c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
16 changes: 16 additions & 0 deletions pkg/supply/env/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ type amsDataDeprecated struct {
Root string `json:"root"`
}

type VcapApplication struct {
ApplicationName string `json:"application_name"`
}

func LoadVcapApplication(log *libbuildpack.Logger) VcapApplication {
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
}

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")
Expand Down
7 changes: 6 additions & 1 deletion pkg/supply/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,17 @@ func (s *Supplier) upload(creds *services.IASCredentials, tlsCfg tlsConfig, root
if err != nil {
return fmt.Errorf("unable to create AMS client: %s", err)
}
vcapApp := env.LoadVcapApplication(s.Log)

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-Appname": vcapApp.ApplicationName,
},
}
return u.Do(context.Background(), creds.AmsServerURL)
}
6 changes: 6 additions & 0 deletions pkg/supply/supply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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-Appname", expectedValue))
})
It("creates a valid launch.yml", func() {
Expect(supplier.Run()).To(Succeed())
launchConfig, err := os.Open(filepath.Join(depDir, "launch.yml"))
Expand Down
7 changes: 5 additions & 2 deletions pkg/uploader/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 133075c

Please sign in to comment.