|
| 1 | +// Create a forwarding NewSingleHostReverseProxy for local services that |
| 2 | +// supports admission control, including access token validation and utilization |
| 3 | +// based "tx controller". |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "crypto/tls" |
| 9 | + "errors" |
| 10 | + "flag" |
| 11 | + "log" |
| 12 | + "net/http" |
| 13 | + "net/http/httputil" |
| 14 | + "net/url" |
| 15 | + "os" |
| 16 | + "strings" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/gorilla/handlers" |
| 20 | + |
| 21 | + "github.com/m-lab/access/controller" |
| 22 | + "github.com/m-lab/access/token" |
| 23 | + |
| 24 | + "github.com/m-lab/go/flagx" |
| 25 | + "github.com/m-lab/go/httpx" |
| 26 | + "github.com/m-lab/go/rtx" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + tokenVerifyKey = flagx.FileBytesArray{} |
| 31 | + tokenRequired bool |
| 32 | + tokenMachine string |
| 33 | + certFile string |
| 34 | + keyFile string |
| 35 | + forward = forwardURLs{} |
| 36 | +) |
| 37 | + |
| 38 | +type ForwardURL struct { |
| 39 | + From *url.URL |
| 40 | + Target *url.URL |
| 41 | +} |
| 42 | + |
| 43 | +type forwardURLs []ForwardURL |
| 44 | + |
| 45 | +func (f forwardURLs) String() string { |
| 46 | + s := "" |
| 47 | + for i := range f { |
| 48 | + s += f[i].From.String() + "@" + f[i].Target.String() |
| 49 | + } |
| 50 | + return s |
| 51 | +} |
| 52 | + |
| 53 | +func (f *forwardURLs) Get() string { |
| 54 | + return f.String() |
| 55 | +} |
| 56 | + |
| 57 | +func (f *forwardURLs) Set(s string) error { |
| 58 | + fields := strings.Split(s, "@") |
| 59 | + if len(fields) != 2 { |
| 60 | + return errors.New("from-url@target-url") |
| 61 | + } |
| 62 | + from, err := url.Parse(fields[0]) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + target, err := url.Parse(fields[1]) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + x := ForwardURL{ |
| 71 | + From: from, |
| 72 | + Target: target, |
| 73 | + } |
| 74 | + *f = append(*f, x) |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +func init() { |
| 79 | + flag.Var(&forward, "forward", "listen on from and forward to target url") |
| 80 | + flag.Var(&tokenVerifyKey, "token.verify-key", "Public key for verifying access tokens") |
| 81 | + flag.BoolVar(&tokenRequired, "token.required", false, "Require access token for requests") |
| 82 | + flag.StringVar(&tokenMachine, "token.machine", "", "Use given machine name to verify token claims") |
| 83 | + flag.StringVar(&certFile, "cert", "", "TLS certificate for envelope server") |
| 84 | + flag.StringVar(&keyFile, "key", "", "TLS key for envelope server") |
| 85 | +} |
| 86 | + |
| 87 | +var mainCtx, mainCancel = context.WithCancel(context.Background()) |
| 88 | + |
| 89 | +func EarlyRequestLogger(next http.Handler) http.Handler { |
| 90 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 91 | + log.Println(r.RemoteAddr, time.Now(), r.Method, r.URL, r.Proto, r.RequestURI) |
| 92 | + // Clone the request with the context provided by isVerified. |
| 93 | + next.ServeHTTP(w, r) |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +func main() { |
| 98 | + defer mainCancel() |
| 99 | + flag.Parse() |
| 100 | + |
| 101 | + v, err := token.NewVerifier(tokenVerifyKey.Get()...) |
| 102 | + if tokenRequired && err != nil { |
| 103 | + rtx.Must(err, "Failed to load verifier for when tokens are required") |
| 104 | + } |
| 105 | + ac, _ := controller.Setup(mainCtx, v, tokenRequired, tokenMachine) |
| 106 | + ac = ac.Append(EarlyRequestLogger) |
| 107 | + |
| 108 | + for i := range forward { |
| 109 | + rp := httputil.NewSingleHostReverseProxy(forward[i].Target) |
| 110 | + rp.Transport = &http.Transport{ |
| 111 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 112 | + } |
| 113 | + h := handlers.LoggingHandler(os.Stderr, rp) |
| 114 | + smx := http.NewServeMux() |
| 115 | + smx.Handle("/", ac.Then(h)) |
| 116 | + |
| 117 | + listenAddr := forward[i].From.Host |
| 118 | + s := &http.Server{ |
| 119 | + Addr: listenAddr, |
| 120 | + Handler: smx, |
| 121 | + // NOTE: set absolute read and write timeouts for server connections. |
| 122 | + ReadTimeout: time.Minute, // TODO: make configurable. |
| 123 | + WriteTimeout: time.Minute, // TODO: make configurable. |
| 124 | + } |
| 125 | + |
| 126 | + switch forward[i].From.Scheme { |
| 127 | + case "https": |
| 128 | + log.Println("Listening for secure access requests on " + listenAddr + " to " + forward[i].Target.String()) |
| 129 | + rtx.Must(httpx.ListenAndServeTLSAsync(s, certFile, keyFile), "Could not start envelop server") |
| 130 | + case "http": |
| 131 | + log.Println("Listening for INSECURE access requests on " + listenAddr + " to " + forward[i].Target.String()) |
| 132 | + rtx.Must(httpx.ListenAndServeAsync(s), "Could not start envelop server") |
| 133 | + default: |
| 134 | + panic("unknown forward scheme") |
| 135 | + } |
| 136 | + defer s.Close() |
| 137 | + } |
| 138 | + <-mainCtx.Done() |
| 139 | +} |
0 commit comments