Skip to content

Commit

Permalink
Add ams instance id header when uploading and downloading bundle (#57)
Browse files Browse the repository at this point in the history
* Add ams instance id header when uploading and downloading bundle

* Fix formatting

* Fix nolint

* Fix nolint

* Fix nolint
  • Loading branch information
f-blass authored Aug 26, 2022
1 parent a28a2cd commit 32285e3
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ issues:
- linters:
- revive
text: "^exported: "
- linters:
- staticcheck
text: "SA1019:"
# List of regexps of issue texts to exclude, empty list by default.
# But independently from this option we use default exclude patterns,
# it can be disabled by `exclude-use-default: false`. To list all
Expand Down
1 change: 1 addition & 0 deletions pkg/supply/env/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

const ServiceName = "authorization"
const HeaderInstanceID = "X-Ams-Instance-Id"

type Config struct {
Root string
Expand Down
12 changes: 7 additions & 5 deletions pkg/supply/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"path"
"path/filepath"

"github.com/SAP/cloud-authorization-buildpack/pkg/supply/env"
"github.com/SAP/cloud-authorization-buildpack/pkg/supply/services"
"github.com/cloudfoundry/libbuildpack"
"github.com/open-policy-agent/opa/download"
"github.com/open-policy-agent/opa/plugins/bundle"

"github.com/SAP/cloud-authorization-buildpack/pkg/supply/env"
"github.com/SAP/cloud-authorization-buildpack/pkg/supply/services"
"github.com/SAP/cloud-authorization-buildpack/pkg/uploader"
)

Expand Down Expand Up @@ -161,7 +161,8 @@ type Credentials struct {
}

type RestConfig struct {
URL string `json:"url"`
URL string `json:"url"`
Headers map[string]string
Credentials Credentials `json:"credentials"`
}

Expand Down Expand Up @@ -193,7 +194,7 @@ func (s *Supplier) writeProfileDFile(cfg env.Config, amsCreds services.AMSCreden
if err := os.MkdirAll(s.Stager.ProfileDir(), 0755); err != nil {
return fmt.Errorf("couldn't create profile dir: %w", err)
}
return os.WriteFile(path.Join(s.Stager.ProfileDir(), "0000_opa_env.sh"), b.Bytes(), 0755) // nolint
return os.WriteFile(path.Join(s.Stager.ProfileDir(), "0000_opa_env.sh"), b.Bytes(), 0755) //nolint
}

func (s *Supplier) writeOpaConfig(cred services.AMSCredentials, tlsCfg tlsConfig) error {
Expand Down Expand Up @@ -252,7 +253,8 @@ func (s *Supplier) createStorageGatewayConfig(cred services.AMSCredentials, cfg
}
svcs := make(map[string]RestConfig)
svcs[serviceKey] = RestConfig{
URL: cred.BundleURL,
URL: cred.BundleURL,
Headers: map[string]string{env.HeaderInstanceID: cred.InstanceID},
Credentials: Credentials{ClientTLS: &ClientTLS{
Cert: cfg.CertPath,
Key: cfg.KeyPath,
Expand Down
9 changes: 8 additions & 1 deletion pkg/supply/supply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"time"

"code.cloudfoundry.org/buildpackapplifecycle/buildpackrunner/resources"
"github.com/SAP/cloud-authorization-buildpack/pkg/uploader"
"github.com/cloudfoundry/libbuildpack"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
Expand All @@ -26,6 +25,9 @@ import (
"github.com/open-policy-agent/opa/plugins/rest"
"gopkg.in/yaml.v2"

"github.com/SAP/cloud-authorization-buildpack/pkg/supply/env"
"github.com/SAP/cloud-authorization-buildpack/pkg/uploader"

"github.com/SAP/cloud-authorization-buildpack/pkg/supply"
"github.com/SAP/cloud-authorization-buildpack/pkg/supply/testdata"
)
Expand Down Expand Up @@ -340,6 +342,11 @@ var _ = Describe("Supply", func() {
Expect(string(keySpy)).To(Equal("identity-key-payload"))
Expect(string(certSpy)).To(Equal("identity-cert-payload"))
})
It("sets the ams instance id http header when uploading the bundle", func() {
Expect(supplier.Run()).To(Succeed())
expectedValue := []string{"00000000-3b4d-4c41-9e5b-9aee7bfa6348"}
Expect(uploadReqSpy.Header).Should(HaveKeyWithValue(env.HeaderInstanceID, expectedValue))
})
It("should configure access to the gateway", func() {
Expect(supplier.Run()).To(Succeed())
rawConfig, err := os.ReadFile(filepath.Join(depDir, "opa_config.yml"))
Expand Down
5 changes: 2 additions & 3 deletions pkg/uploader/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"encoding/base64"
"io"
"io/ioutil"
"strings"

"os"
"path"
"path/filepath"
"strings"

"github.com/cloudfoundry/libbuildpack"
)
Expand Down Expand Up @@ -64,7 +63,7 @@ func CreateArchive(log *libbuildpack.Logger, root string) (*bytes.Buffer, error)
func crawlDCLs(fi os.FileInfo, file, root string) (*[]archiveContent, error) {
var archive []archiveContent
if fi.IsDir() {
content, err := ioutil.ReadDir(file) // nolint
content, err := ioutil.ReadDir(file) //nolint
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/uploader/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"time"

"github.com/cloudfoundry/libbuildpack"

"github.com/SAP/cloud-authorization-buildpack/pkg/supply/env"
)

type Uploader struct {
Expand Down Expand Up @@ -90,6 +92,7 @@ func (up *Uploader) do(ctx context.Context, dstURL string, body []byte) (*http.R
if err != nil {
return nil, fmt.Errorf("could not create DCL upload request %w", err)
}
r.Header.Set(env.HeaderInstanceID, up.AMSInstanceID)
r.Header.Set("Content-Type", "application/gzip")
return up.Client.Do(r)
}
Expand Down

0 comments on commit 32285e3

Please sign in to comment.