Skip to content

Commit

Permalink
Set buildpack version as header on dcl upload (#93)
Browse files Browse the repository at this point in the history
* Set buildpack version as header on dcl upload

* Add userAgent field to uploader

* Remove param

* add unit test for setting the User-Agent Header in Base DCL upload

---------

Co-authored-by: d047491 <[email protected]>
  • Loading branch information
f-blass and d047491 authored Aug 14, 2024
1 parent f473bec commit 94b9c67
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/supply/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func main() {
logger.Error("Unable to load buildpack manifest: %s", err)
os.Exit(10)
}
version, err := manifest.Version()
if err != nil {
logger.Error("Unable to load buildpack version: %s", err)
os.Exit(20)
}

installer := libbuildpack.NewInstaller(manifest)

stager := libbuildpack.NewStager(os.Args[1:], logger, manifest)
Expand Down Expand Up @@ -74,6 +80,7 @@ func main() {
BuildpackDir: buildpackDir,
GetClient: uploader.GetClient,
CertCopierSourceDir: path.Join(buildpackDir, "bin"),
BuildpackVersion: version,
}

err = s.Run()
Expand Down
2 changes: 2 additions & 0 deletions pkg/supply/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Supplier struct {
BuildpackDir string
GetClient func(cert, key []byte) (uploader.AMSClient, error)
CertCopierSourceDir string
BuildpackVersion string
}

type Cloudfoundry struct {
Expand Down Expand Up @@ -284,6 +285,7 @@ func (s *Supplier) upload(creds *services.IASCredentials, tlsCfg tlsConfig, root
Root: path.Join(s.Stager.BuildDir(), rootDir),
Client: client,
AMSInstanceID: creds.AmsInstanceID,
UserAgent: fmt.Sprintf("cloud-authorization-buildpack/%s", s.BuildpackVersion),
}
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 @@ -97,6 +97,7 @@ var _ = Describe("Supply", func() {
Log: logger,
BuildpackDir: buildpackDir,
CertCopierSourceDir: certCopierDir,
BuildpackVersion: "UNIT-TEST",
GetClient: func(cert, key []byte) (uploader.AMSClient, error) {
certSpy = cert
keySpy = key
Expand Down Expand Up @@ -169,6 +170,11 @@ var _ = Describe("Supply", func() {
expectedValue := []string{"00000000-3b4d-4c41-9e5b-9aee7bfa6348"}
Expect(uploadReqSpy.Header).Should(HaveKeyWithValue(env.HeaderInstanceID, expectedValue))
})
It("sets the buildpack version as User-Agent Header", func() {
Expect(supplier.Run()).To(Succeed())
expectedValue := []string{"cloud-authorization-buildpack/UNIT-TEST"}
Expect(uploadReqSpy.Header).Should(HaveKeyWithValue("User-Agent", expectedValue))
})
It("creates a valid launch.yml", func() {
Expect(supplier.Run()).To(Succeed())
launchConfig, err := os.Open(filepath.Join(depDir, "launch.yml"))
Expand Down
2 changes: 2 additions & 0 deletions pkg/uploader/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Uploader struct {
Root string
Client AMSClient
AMSInstanceID string
UserAgent 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 @@ -91,6 +92,7 @@ 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")
return up.Client.Do(r)
}
Expand Down

0 comments on commit 94b9c67

Please sign in to comment.