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

Allows users to set a dependency mirror #563

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions postal/fakes/mirror_resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fakes

import "sync"

type MirrorResolver struct {
FindDependencyMirrorCall struct {
mutex sync.Mutex
CallCount int
Receives struct {
Uri string
PlatformDir string
}
Returns struct {
String string
Error error
}
Stub func(string, string) (string, error)
}
}

func (f *MirrorResolver) FindDependencyMirror(param1 string, param2 string) (string, error) {
f.FindDependencyMirrorCall.mutex.Lock()
defer f.FindDependencyMirrorCall.mutex.Unlock()
f.FindDependencyMirrorCall.CallCount++
f.FindDependencyMirrorCall.Receives.Uri = param1
f.FindDependencyMirrorCall.Receives.PlatformDir = param2
if f.FindDependencyMirrorCall.Stub != nil {
return f.FindDependencyMirrorCall.Stub(param1, param2)
}
return f.FindDependencyMirrorCall.Returns.String, f.FindDependencyMirrorCall.Returns.Error
}
138 changes: 138 additions & 0 deletions postal/internal/dependency_mirror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package internal

import (
"fmt"
"net/url"
"os"
"strings"
)

type DependencyMirrorResolver struct {
bindingResolver BindingResolver
}

func NewDependencyMirrorResolver(bindingResolver BindingResolver) DependencyMirrorResolver {
return DependencyMirrorResolver{
bindingResolver: bindingResolver,
}
}

func formatAndVerifyMirror(mirror, uri string) (string, error) {
mirrorURL, err := url.Parse(mirror)
if err != nil {
return "", err
}

uriURL, err := url.Parse(uri)
if err != nil {
return "", err
}

if mirrorURL.Scheme != "https" && mirrorURL.Scheme != "file" {
return "", fmt.Errorf("invalid mirror scheme")
}

mirrorURL.Path = strings.Replace(mirrorURL.Path, "{originalHost}", uriURL.Host+uriURL.Path, 1)
return mirrorURL.String(), nil
}

func (d DependencyMirrorResolver) FindDependencyMirror(uri, platformDir string) (string, error) {
mirror, err := d.findMirrorFromEnv(uri)
if err != nil {
return "", err
}

if mirror != "" {
return formatAndVerifyMirror(mirror, uri)
}

mirror, err = d.findMirrorFromBinding(uri, platformDir)
if err != nil {
return "", err
}

if mirror != "" {
return formatAndVerifyMirror(mirror, uri)
}

return "", nil
}

func (d DependencyMirrorResolver) findMirrorFromEnv(uri string) (string, error) {
const DefaultMirror = "BP_DEPENDENCY_MIRROR"
const NonDefaultMirrorPrefix = "BP_DEPENDENCY_MIRROR_"
mirrors := make(map[string]string)
environmentVariables := os.Environ()
for _, ev := range environmentVariables {
pair := strings.SplitN(ev, "=", 2)
key := pair[0]
value := pair[1]

if !strings.Contains(key, DefaultMirror) {
continue
}

if key == DefaultMirror {
mirrors["default"] = value
continue
}

// convert key
hostname := strings.SplitN(key, NonDefaultMirrorPrefix, 2)[1]
hostname = strings.ReplaceAll(strings.ReplaceAll(hostname, "__", "-"), "_", ".")
hostname = strings.ToLower(hostname)
mirrors[hostname] = value

if !strings.Contains(uri, hostname) {
continue
}

return value, nil
}

if mirrorUri, ok := mirrors["default"]; ok {
return mirrorUri, nil
}

return "", nil
}

func (d DependencyMirrorResolver) findMirrorFromBinding(uri, platformDir string) (string, error) {
bindings, err := d.bindingResolver.Resolve("dependency-mirror", "", platformDir)
if err != nil {
return "", fmt.Errorf("failed to resolve 'dependency-mirror' binding: %w", err)
}

if len(bindings) > 1 {
return "", fmt.Errorf("cannot have multiple bindings of type 'dependency-mirror'")
}

if len(bindings) == 0 {
return "", nil
}

mirror := ""
entries := bindings[0].Entries
for hostname, entry := range entries {
if hostname == "default" {
mirror, err = entry.ReadString()
if err != nil {
return "", err
}
continue
}

if !strings.Contains(uri, hostname) {
continue
}

mirror, err = entry.ReadString()
if err != nil {
return "", err
}

return mirror, nil
}

return mirror, nil
}
Loading
Loading