Path: blob/main/components/image-builder-bob/cmd/proxy.go
2498 views
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"net/http"8"net/url"9"os"1011"github.com/containerd/containerd/remotes/docker"12"github.com/distribution/reference"13"github.com/spf13/cobra"1415log "github.com/gitpod-io/gitpod/common-go/log"16"github.com/gitpod-io/gitpod/image-builder/bob/pkg/proxy"17)1819var proxyOpts struct {20BaseRef, TargetRef string21Auth string22AdditionalAuth string23}2425// proxyCmd represents the build command26var proxyCmd = &cobra.Command{27Use: "proxy",28Short: "Runs an authenticating proxy",29Run: func(cmd *cobra.Command, args []string) {30log.Init("bob", "", true, os.Getenv("SUPERVISOR_DEBUG_ENABLE") == "true")31log := log.WithField("command", "proxy")3233authP, err := proxy.NewAuthorizerFromDockerEnvVar(proxyOpts.Auth)34if err != nil {35log.WithError(err).WithField("auth", proxyOpts.Auth).Fatal("cannot unmarshal auth")36}37authA, err := proxy.NewAuthorizerFromEnvVar(proxyOpts.AdditionalAuth)38if err != nil {39log.WithError(err).WithField("additionalAuth", proxyOpts.AdditionalAuth).Fatal("cannot unmarshal additionalAuth")40}41authP = authP.AddIfNotExists(authA)4243baseref, err := reference.ParseNormalizedNamed(proxyOpts.BaseRef)44if err != nil {45log.WithError(err).Fatal("cannot parse base ref")46}47var basetag string48if r, ok := baseref.(reference.NamedTagged); ok {49basetag = r.Tag()50}51targetref, err := reference.ParseNormalizedNamed(proxyOpts.TargetRef)52if err != nil {53log.WithError(err).Fatal("cannot parse target ref")54}55var targettag string56if r, ok := targetref.(reference.NamedTagged); ok {57targettag = r.Tag()58}5960auth := func() docker.Authorizer { return docker.NewDockerAuthorizer(docker.WithAuthCreds(authP.Authorize)) }61mirrorAuth := func() docker.Authorizer { return docker.NewDockerAuthorizer(docker.WithAuthCreds(authA.Authorize)) }62prx, err := proxy.NewProxy(&url.URL{Host: "localhost:8080", Scheme: "http"}, map[string]proxy.Repo{63"base": {64Host: reference.Domain(baseref),65Repo: reference.Path(baseref),66Tag: basetag,67Auth: auth,68},69"target": {70Host: reference.Domain(targetref),71Repo: reference.Path(targetref),72Tag: targettag,73Auth: auth,74},75}, mirrorAuth)76if err != nil {77log.Fatal(err)78}7980http.Handle("/", prx)81log.Info("starting bob proxy on :8080")82err = http.ListenAndServe(":8080", nil)83if err != nil {84log.Fatal(err)85}86},87}8889func init() {90rootCmd.AddCommand(proxyCmd)9192// These env vars start with `WORKSPACEKIT_` so that they aren't passed on to ring293proxyCmd.Flags().StringVar(&proxyOpts.BaseRef, "base-ref", os.Getenv("WORKSPACEKIT_BOBPROXY_BASEREF"), "ref of the base image")94proxyCmd.Flags().StringVar(&proxyOpts.TargetRef, "target-ref", os.Getenv("WORKSPACEKIT_BOBPROXY_TARGETREF"), "ref of the target image")95proxyCmd.Flags().StringVar(&proxyOpts.Auth, "auth", os.Getenv("WORKSPACEKIT_BOBPROXY_AUTH"), "authentication to use")96proxyCmd.Flags().StringVar(&proxyOpts.AdditionalAuth, "additional-auth", os.Getenv("WORKSPACEKIT_BOBPROXY_ADDITIONALAUTH"), "additional authentication to use")97}9899100