Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/image-builder-bob/cmd/proxy.go
2498 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"net/http"
9
"net/url"
10
"os"
11
12
"github.com/containerd/containerd/remotes/docker"
13
"github.com/distribution/reference"
14
"github.com/spf13/cobra"
15
16
log "github.com/gitpod-io/gitpod/common-go/log"
17
"github.com/gitpod-io/gitpod/image-builder/bob/pkg/proxy"
18
)
19
20
var proxyOpts struct {
21
BaseRef, TargetRef string
22
Auth string
23
AdditionalAuth string
24
}
25
26
// proxyCmd represents the build command
27
var proxyCmd = &cobra.Command{
28
Use: "proxy",
29
Short: "Runs an authenticating proxy",
30
Run: func(cmd *cobra.Command, args []string) {
31
log.Init("bob", "", true, os.Getenv("SUPERVISOR_DEBUG_ENABLE") == "true")
32
log := log.WithField("command", "proxy")
33
34
authP, err := proxy.NewAuthorizerFromDockerEnvVar(proxyOpts.Auth)
35
if err != nil {
36
log.WithError(err).WithField("auth", proxyOpts.Auth).Fatal("cannot unmarshal auth")
37
}
38
authA, err := proxy.NewAuthorizerFromEnvVar(proxyOpts.AdditionalAuth)
39
if err != nil {
40
log.WithError(err).WithField("additionalAuth", proxyOpts.AdditionalAuth).Fatal("cannot unmarshal additionalAuth")
41
}
42
authP = authP.AddIfNotExists(authA)
43
44
baseref, err := reference.ParseNormalizedNamed(proxyOpts.BaseRef)
45
if err != nil {
46
log.WithError(err).Fatal("cannot parse base ref")
47
}
48
var basetag string
49
if r, ok := baseref.(reference.NamedTagged); ok {
50
basetag = r.Tag()
51
}
52
targetref, err := reference.ParseNormalizedNamed(proxyOpts.TargetRef)
53
if err != nil {
54
log.WithError(err).Fatal("cannot parse target ref")
55
}
56
var targettag string
57
if r, ok := targetref.(reference.NamedTagged); ok {
58
targettag = r.Tag()
59
}
60
61
auth := func() docker.Authorizer { return docker.NewDockerAuthorizer(docker.WithAuthCreds(authP.Authorize)) }
62
mirrorAuth := func() docker.Authorizer { return docker.NewDockerAuthorizer(docker.WithAuthCreds(authA.Authorize)) }
63
prx, err := proxy.NewProxy(&url.URL{Host: "localhost:8080", Scheme: "http"}, map[string]proxy.Repo{
64
"base": {
65
Host: reference.Domain(baseref),
66
Repo: reference.Path(baseref),
67
Tag: basetag,
68
Auth: auth,
69
},
70
"target": {
71
Host: reference.Domain(targetref),
72
Repo: reference.Path(targetref),
73
Tag: targettag,
74
Auth: auth,
75
},
76
}, mirrorAuth)
77
if err != nil {
78
log.Fatal(err)
79
}
80
81
http.Handle("/", prx)
82
log.Info("starting bob proxy on :8080")
83
err = http.ListenAndServe(":8080", nil)
84
if err != nil {
85
log.Fatal(err)
86
}
87
},
88
}
89
90
func init() {
91
rootCmd.AddCommand(proxyCmd)
92
93
// These env vars start with `WORKSPACEKIT_` so that they aren't passed on to ring2
94
proxyCmd.Flags().StringVar(&proxyOpts.BaseRef, "base-ref", os.Getenv("WORKSPACEKIT_BOBPROXY_BASEREF"), "ref of the base image")
95
proxyCmd.Flags().StringVar(&proxyOpts.TargetRef, "target-ref", os.Getenv("WORKSPACEKIT_BOBPROXY_TARGETREF"), "ref of the target image")
96
proxyCmd.Flags().StringVar(&proxyOpts.Auth, "auth", os.Getenv("WORKSPACEKIT_BOBPROXY_AUTH"), "authentication to use")
97
proxyCmd.Flags().StringVar(&proxyOpts.AdditionalAuth, "additional-auth", os.Getenv("WORKSPACEKIT_BOBPROXY_ADDITIONALAUTH"), "additional authentication to use")
98
}
99
100