Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for overriding default dependency source #315

Merged
merged 9 commits into from
Mar 19, 2024
33 changes: 28 additions & 5 deletions dependency_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,17 @@ type RequestModifierFunc func(request *http.Request) (*http.Request, error)
func (d *DependencyCache) Artifact(dependency BuildpackDependency, mods ...RequestModifierFunc) (*os.File, error) {

var (
actual BuildpackDependency
artifact string
file string
uri = dependency.URI
urlP *url.URL
actual BuildpackDependency
artifact string
file string
isBinding bool
uri = dependency.URI
urlP *url.URL
)

for d, u := range d.Mappings {
if d == dependency.SHA256 {
isBinding = true
uri = u
break
}
Expand All @@ -184,6 +186,10 @@ func (d *DependencyCache) Artifact(dependency BuildpackDependency, mods ...Reque
return nil, fmt.Errorf("unable to parse URI. see DEBUG log level")
}

if !isBinding {
d.overrideDependencySource(urlP)
}

if dependency.SHA256 == "" {
d.Logger.Headerf("%s Dependency has no SHA256. Skipping cache.",
color.New(color.FgYellow, color.Bold).Sprint("Warning:"))
Expand Down Expand Up @@ -363,3 +369,20 @@ func (DependencyCache) verify(path string, expected string) error {

return nil
}

func (d DependencyCache) overrideDependencySource(urlD *url.URL) {
dependencySourceOverride := sherpa.GetEnvWithDefault("BP_DEPENDENCY_SOURCE_OVERRIDE", "")
if dependencySourceOverride != "" {
d.Logger.Infof("variable BP_DEPENDENCY_SOURCE_OVERRIDE found. overriding download uri.")
urlOverride, err := url.ParseRequestURI(dependencySourceOverride)
if err == nil {
urlD.Scheme = urlOverride.Scheme
urlD.User = urlOverride.User
urlD.Host = urlOverride.Host
urlD.Path = urlOverride.Path + urlD.Path
} else {
d.Logger.Debugf("environment variable BP_DEPENDENCY_SOURCE_OVERRIDE could not be parsed. value: %s\n%w", dependencySourceOverride, err)
d.Logger.Infof("ignoring invalid variable BP_DEPENDENCY_SOURCE_OVERRIDE. continuing without override...")
}
}
}
22 changes: 22 additions & 0 deletions dependency_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,28 @@ func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
})
})

context("source is overridden", func() {
serverOverride := ghttp.NewServer()
url, _ := url.Parse(serverOverride.URL())

it.Before(func() {
t.Setenv("BP_DEPENDENCY_SOURCE_OVERRIDE", url.Scheme+"://"+"username:password@"+url.Host+"/foo/bar")
})

it("downloads from override source", func() {
serverOverride.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyBasicAuth("username", "password"),
ghttp.VerifyRequest(http.MethodGet, "/foo/bar/test-path", ""),
ghttp.RespondWith(http.StatusOK, "test-fixture"),
))

a, err := dependencyCache.Artifact(dependency)
Expect(err).NotTo(HaveOccurred())

Expect(io.ReadAll(a)).To(Equal([]byte("test-fixture")))
})
})

it("fails with invalid SHA256", func() {
server.AppendHandlers(ghttp.RespondWith(http.StatusOK, "invalid-fixture"))

Expand Down
Loading