-
Notifications
You must be signed in to change notification settings - Fork 216
/
repository.go
78 lines (69 loc) · 2.05 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package helm
import (
"crypto/subtle"
"net/http"
"strings"
"time"
"github.com/rancher/fleet/integrationtests/cli"
)
const (
username = "user"
password = "pass"
)
type repository struct {
server *http.Server
port string
}
// starts a helm repository on localhost:3000. It contains all repositories that are located in the assets/helmrepository folder.
// basic auth is enabled is authEnabled is true.
func (r *repository) startRepository(authEnabled bool) {
r.server = &http.Server{Addr: r.port, ReadHeaderTimeout: 1 * time.Second}
r.server.Handler = getHandler(authEnabled)
go func() {
const maxAttempts = 10
currentAttempt := 0
for {
err := r.server.ListenAndServe()
// It is possible that the previous repository is still closing as it takes a few extra milliseconds to fully close
// retry after 100 milliseconds if "address already in use" error is returned
if err != nil && strings.Contains(err.Error(), "address already in use") {
if currentAttempt == maxAttempts {
panic("Max number of attempts reached: error creating helm repository: " + err.Error())
}
currentAttempt++
time.Sleep(100 * time.Millisecond)
continue
}
if err != nil && err != http.ErrServerClosed {
panic("error creating helm repository: " + err.Error())
}
break
}
}()
}
func getHandler(authEnabled bool) http.Handler {
fs := http.FileServer(http.Dir(cli.AssetsPath + "helmrepository"))
if !authEnabled {
return fs
}
return &authHandler{fs: fs}
}
func (r *repository) stopRepository() error {
return r.server.Close()
}
// handler with basic authentication enabled
type authHandler struct {
fs http.Handler
}
func (h *authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok || subtle.ConstantTimeCompare([]byte(strings.TrimSpace(user)), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(strings.TrimSpace(pass)), []byte(password)) != 1 {
w.WriteHeader(401)
_, err := w.Write([]byte("Unauthorised."))
if err != nil {
return
}
return
}
h.fs.ServeHTTP(w, r)
}