Skip to content

Commit

Permalink
send more upload information via headers
Browse files Browse the repository at this point in the history
  • Loading branch information
d047491 committed Aug 15, 2024
1 parent 94b9c67 commit 5e144b7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
29 changes: 29 additions & 0 deletions pkg/supply/env/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 8 additions & 1 deletion pkg/supply/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
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-Cf-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 5e144b7

Please sign in to comment.