1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
23 "git.arvados.org/arvados.git/lib/cmd"
24 "git.arvados.org/arvados.git/lib/config"
25 "git.arvados.org/arvados.git/sdk/go/arvados"
26 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
27 "git.arvados.org/arvados.git/sdk/go/ctxlog"
28 "git.arvados.org/arvados.git/sdk/go/health"
29 "git.arvados.org/arvados.git/sdk/go/httpserver"
30 "git.arvados.org/arvados.git/sdk/go/keepclient"
31 "github.com/coreos/go-systemd/daemon"
32 "github.com/ghodss/yaml"
33 "github.com/gorilla/mux"
34 lru "github.com/hashicorp/golang-lru"
35 "github.com/sirupsen/logrus"
45 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
47 func configure(args []string) (*arvados.Cluster, logrus.FieldLogger, error) {
49 flags := flag.NewFlagSet(prog, flag.ContinueOnError)
51 dumpConfig := flags.Bool("dump-config", false, "write current configuration to stdout and exit")
52 getVersion := flags.Bool("version", false, "Print version information and exit.")
54 initLogger := logrus.New()
55 initLogger.Formatter = &logrus.JSONFormatter{
56 TimestampFormat: rfc3339NanoFixed,
58 var logger logrus.FieldLogger = initLogger
60 loader := config.NewLoader(os.Stdin, logger)
61 loader.SetupFlags(flags)
62 args = loader.MungeLegacyConfigArgs(logger, args[1:], "-legacy-keepproxy-config")
64 if ok, code := cmd.ParseFlags(flags, prog, args, "", os.Stderr); !ok {
66 } else if *getVersion {
67 fmt.Printf("keepproxy %s\n", version)
68 return nil, logger, nil
71 cfg, err := loader.Load()
73 return nil, logger, err
75 cluster, err := cfg.GetCluster("")
77 return nil, logger, err
80 logger = ctxlog.New(os.Stderr, cluster.SystemLogs.Format, cluster.SystemLogs.LogLevel).WithFields(logrus.Fields{
81 "ClusterID": cluster.ClusterID,
86 out, err := yaml.Marshal(cfg)
88 return nil, logger, err
90 if _, err := os.Stdout.Write(out); err != nil {
91 return nil, logger, err
93 return nil, logger, nil
96 return cluster, logger, nil
100 cluster, logger, err := configure(os.Args)
108 logger.Printf("keepproxy %s started", version)
110 if err := run(logger, cluster); err != nil {
114 logger.Println("shutting down")
117 func run(logger logrus.FieldLogger, cluster *arvados.Cluster) error {
118 client, err := arvados.NewClientFromConfig(cluster)
122 client.AuthToken = cluster.SystemRootToken
124 arv, err := arvadosclient.New(client)
126 return fmt.Errorf("Error setting up arvados client %v", err)
129 // If a config file is available, use the keepstores defined there
130 // instead of the legacy autodiscover mechanism via the API server
131 for k := range cluster.Services.Keepstore.InternalURLs {
132 arv.KeepServiceURIs = append(arv.KeepServiceURIs, strings.TrimRight(k.String(), "/"))
135 if cluster.SystemLogs.LogLevel == "debug" {
136 keepclient.DebugPrintf = logger.Printf
138 kc, err := keepclient.MakeKeepClient(arv)
140 return fmt.Errorf("Error setting up keep client %v", err)
142 keepclient.RefreshServiceDiscoveryOnSIGHUP()
144 if cluster.Collections.DefaultReplication > 0 {
145 kc.Want_replicas = cluster.Collections.DefaultReplication
148 var listen arvados.URL
149 for listen = range cluster.Services.Keepproxy.InternalURLs {
154 listener, lErr = net.Listen("tcp", listen.Host)
156 return fmt.Errorf("listen(%s): %v", listen.Host, lErr)
159 if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
160 logger.Printf("Error notifying init daemon: %v", err)
162 logger.Println("listening at", listener.Addr())
164 // Shut down the server gracefully (by closing the listener)
165 // if SIGTERM is received.
166 term := make(chan os.Signal, 1)
167 go func(sig <-chan os.Signal) {
169 logger.Println("caught signal:", s)
172 signal.Notify(term, syscall.SIGTERM)
173 signal.Notify(term, syscall.SIGINT)
175 // Start serving requests.
176 router, err = MakeRESTRouter(kc, time.Duration(keepclient.DefaultProxyRequestTimeout), cluster, logger)
180 server := http.Server{
181 Handler: httpserver.AddRequestIDs(httpserver.LogRequests(router)),
182 BaseContext: func(net.Listener) context.Context {
183 return ctxlog.Context(context.Background(), logger)
186 return server.Serve(listener)
189 type TokenCacheEntry struct {
194 type APITokenCache struct {
195 tokens *lru.TwoQueueCache
199 // RememberToken caches the token and set an expire time. If the
200 // token is already in the cache, it is not updated.
201 func (cache *APITokenCache) RememberToken(token string, user *arvados.User) {
202 now := time.Now().Unix()
203 _, ok := cache.tokens.Get(token)
205 cache.tokens.Add(token, TokenCacheEntry{
206 expire: now + cache.expireTime,
212 // RecallToken checks if the cached token is known and still believed to be
214 func (cache *APITokenCache) RecallToken(token string) (bool, *arvados.User) {
215 val, ok := cache.tokens.Get(token)
220 cacheEntry := val.(TokenCacheEntry)
221 now := time.Now().Unix()
222 if now < cacheEntry.expire {
223 // Token is known and still valid
224 return true, cacheEntry.user
227 cache.tokens.Remove(token)
232 // GetRemoteAddress returns a string with the remote address for the request.
233 // If the X-Forwarded-For header is set and has a non-zero length, it returns a
234 // string made from a comma separated list of all the remote addresses,
235 // starting with the one(s) from the X-Forwarded-For header.
236 func GetRemoteAddress(req *http.Request) string {
237 if xff := req.Header.Get("X-Forwarded-For"); xff != "" {
238 return xff + "," + req.RemoteAddr
240 return req.RemoteAddr
243 func (h *proxyHandler) CheckAuthorizationHeader(req *http.Request) (pass bool, tok string, user *arvados.User) {
244 parts := strings.SplitN(req.Header.Get("Authorization"), " ", 2)
245 if len(parts) < 2 || !(parts[0] == "OAuth2" || parts[0] == "Bearer") || len(parts[1]) == 0 {
246 return false, "", nil
250 // Tokens are validated differently depending on what kind of
251 // operation is being performed. For example, tokens in
252 // collection-sharing links permit GET requests, but not
255 if req.Method == "GET" || req.Method == "HEAD" {
261 if ok, user := h.APITokenCache.RecallToken(op + ":" + tok); ok {
262 // Valid in the cache, short circuit
263 return true, tok, user
267 arv := *h.KeepClient.Arvados
269 arv.RequestID = req.Header.Get("X-Request-Id")
270 user = &arvados.User{}
271 userCurrentError := arv.Call("GET", "users", "", "current", nil, user)
272 err = userCurrentError
273 if err != nil && op == "read" {
274 apiError, ok := err.(arvadosclient.APIServerError)
275 if ok && apiError.HttpStatusCode == http.StatusForbidden {
276 // If it was a scoped "sharing" token it will
277 // return 403 instead of 401 for the current
278 // user check. If it is a download operation
279 // and they have permission to read the
280 // keep_services table, we can allow it.
281 err = arv.Call("HEAD", "keep_services", "", "accessible", nil, nil)
285 ctxlog.FromContext(req.Context()).Printf("%s: CheckAuthorizationHeader error: %v", GetRemoteAddress(req), err)
286 return false, "", nil
289 if userCurrentError == nil && user.IsAdmin {
290 // checking userCurrentError is probably redundant,
291 // IsAdmin would be false anyway. But can't hurt.
292 if op == "read" && !h.cluster.Collections.KeepproxyPermission.Admin.Download {
293 return false, "", nil
295 if op == "write" && !h.cluster.Collections.KeepproxyPermission.Admin.Upload {
296 return false, "", nil
299 if op == "read" && !h.cluster.Collections.KeepproxyPermission.User.Download {
300 return false, "", nil
302 if op == "write" && !h.cluster.Collections.KeepproxyPermission.User.Upload {
303 return false, "", nil
307 // Success! Update cache
308 h.APITokenCache.RememberToken(op+":"+tok, user)
310 return true, tok, user
313 // We need to make a private copy of the default http transport early
314 // in initialization, then make copies of our private copy later. It
315 // won't be safe to copy http.DefaultTransport itself later, because
316 // its private mutexes might have already been used. (Without this,
317 // the test suite sometimes panics "concurrent map writes" in
318 // net/http.(*Transport).removeIdleConnLocked().)
319 var defaultTransport = *(http.DefaultTransport.(*http.Transport))
321 type proxyHandler struct {
323 *keepclient.KeepClient
325 timeout time.Duration
326 transport *http.Transport
327 logger logrus.FieldLogger
328 cluster *arvados.Cluster
331 // MakeRESTRouter returns an http.Handler that passes GET and PUT
332 // requests to the appropriate handlers.
333 func MakeRESTRouter(kc *keepclient.KeepClient, timeout time.Duration, cluster *arvados.Cluster, logger logrus.FieldLogger) (http.Handler, error) {
334 rest := mux.NewRouter()
336 transport := defaultTransport
337 transport.DialContext = (&net.Dialer{
338 Timeout: keepclient.DefaultConnectTimeout,
339 KeepAlive: keepclient.DefaultKeepAlive,
342 transport.TLSClientConfig = arvadosclient.MakeTLSConfig(kc.Arvados.ApiInsecure)
343 transport.TLSHandshakeTimeout = keepclient.DefaultTLSHandshakeTimeout
345 cacheQ, err := lru.New2Q(500)
347 return nil, fmt.Errorf("Error from lru.New2Q: %v", err)
354 transport: &transport,
355 APITokenCache: &APITokenCache{
363 rest.HandleFunc(`/{locator:[0-9a-f]{32}\+.*}`, h.Get).Methods("GET", "HEAD")
364 rest.HandleFunc(`/{locator:[0-9a-f]{32}}`, h.Get).Methods("GET", "HEAD")
367 rest.HandleFunc(`/index`, h.Index).Methods("GET")
369 // List blocks whose hash has the given prefix
370 rest.HandleFunc(`/index/{prefix:[0-9a-f]{0,32}}`, h.Index).Methods("GET")
372 rest.HandleFunc(`/{locator:[0-9a-f]{32}\+.*}`, h.Put).Methods("PUT")
373 rest.HandleFunc(`/{locator:[0-9a-f]{32}}`, h.Put).Methods("PUT")
374 rest.HandleFunc(`/`, h.Put).Methods("POST")
375 rest.HandleFunc(`/{any}`, h.Options).Methods("OPTIONS")
376 rest.HandleFunc(`/`, h.Options).Methods("OPTIONS")
378 rest.Handle("/_health/{check}", &health.Handler{
379 Token: cluster.ManagementToken,
383 rest.NotFoundHandler = InvalidPathHandler{}
387 var errLoopDetected = errors.New("loop detected")
389 func (h *proxyHandler) checkLoop(resp http.ResponseWriter, req *http.Request) error {
390 if via := req.Header.Get("Via"); strings.Index(via, " "+viaAlias) >= 0 {
391 h.logger.Printf("proxy loop detected (request has Via: %q): perhaps keepproxy is misidentified by gateway config as an external client, or its keep_services record does not have service_type=proxy?", via)
392 http.Error(resp, errLoopDetected.Error(), http.StatusInternalServerError)
393 return errLoopDetected
398 func SetCorsHeaders(resp http.ResponseWriter) {
399 resp.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, OPTIONS")
400 resp.Header().Set("Access-Control-Allow-Origin", "*")
401 resp.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Length, Content-Type, X-Keep-Desired-Replicas")
402 resp.Header().Set("Access-Control-Max-Age", "86486400")
405 type InvalidPathHandler struct{}
407 func (InvalidPathHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
408 ctxlog.FromContext(req.Context()).Printf("%s: %s %s unroutable", GetRemoteAddress(req), req.Method, req.URL.Path)
409 http.Error(resp, "Bad request", http.StatusBadRequest)
412 func (h *proxyHandler) Options(resp http.ResponseWriter, req *http.Request) {
413 ctxlog.FromContext(req.Context()).Printf("%s: %s %s", GetRemoteAddress(req), req.Method, req.URL.Path)
417 var errBadAuthorizationHeader = errors.New("Missing or invalid Authorization header, or method not allowed")
418 var errContentLengthMismatch = errors.New("Actual length != expected content length")
419 var errMethodNotSupported = errors.New("Method not supported")
421 var removeHint, _ = regexp.Compile("\\+K@[a-z0-9]{5}(\\+|$)")
423 func (h *proxyHandler) Get(resp http.ResponseWriter, req *http.Request) {
424 if err := h.checkLoop(resp, req); err != nil {
428 resp.Header().Set("Via", req.Proto+" "+viaAlias)
430 locator := mux.Vars(req)["locator"]
433 var expectLength, responseLength int64
437 h.logger.Println(GetRemoteAddress(req), req.Method, req.URL.Path, status, expectLength, responseLength, proxiedURI, err)
438 if status != http.StatusOK {
439 http.Error(resp, err.Error(), status)
443 kc := h.makeKeepClient(req)
447 var user *arvados.User
448 if pass, tok, user = h.CheckAuthorizationHeader(req); !pass {
449 status, err = http.StatusForbidden, errBadAuthorizationHeader
453 // Copy ArvadosClient struct and use the client's API token
454 arvclient := *kc.Arvados
455 arvclient.ApiToken = tok
456 kc.Arvados = &arvclient
458 var reader io.ReadCloser
460 locator = removeHint.ReplaceAllString(locator, "$1")
463 parts := strings.SplitN(locator, "+", 3)
467 logger = logger.WithField("user_uuid", user.UUID).
468 WithField("user_full_name", user.FullName)
470 logger.WithField("locator", fmt.Sprintf("%s+%s", parts[0], parts[1])).Infof("Block download")
476 expectLength, proxiedURI, err = kc.Ask(locator)
478 reader, expectLength, proxiedURI, err = kc.Get(locator)
483 status, err = http.StatusNotImplemented, errMethodNotSupported
487 if expectLength == -1 {
488 h.logger.Println("Warning:", GetRemoteAddress(req), req.Method, proxiedURI, "Content-Length not provided")
491 switch respErr := err.(type) {
493 status = http.StatusOK
494 resp.Header().Set("Content-Length", fmt.Sprint(expectLength))
499 responseLength, err = io.Copy(resp, reader)
500 if err == nil && expectLength > -1 && responseLength != expectLength {
501 err = errContentLengthMismatch
504 case keepclient.Error:
505 if respErr == keepclient.BlockNotFound {
506 status = http.StatusNotFound
507 } else if respErr.Temporary() {
508 status = http.StatusBadGateway
513 status = http.StatusInternalServerError
517 var errLengthRequired = errors.New(http.StatusText(http.StatusLengthRequired))
518 var errLengthMismatch = errors.New("Locator size hint does not match Content-Length header")
520 func (h *proxyHandler) Put(resp http.ResponseWriter, req *http.Request) {
521 if err := h.checkLoop(resp, req); err != nil {
525 resp.Header().Set("Via", "HTTP/1.1 "+viaAlias)
527 kc := h.makeKeepClient(req)
530 var expectLength int64
531 var status = http.StatusInternalServerError
532 var wroteReplicas int
533 var locatorOut string = "-"
536 h.logger.Println(GetRemoteAddress(req), req.Method, req.URL.Path, status, expectLength, kc.Want_replicas, wroteReplicas, locatorOut, err)
537 if status != http.StatusOK {
538 http.Error(resp, err.Error(), status)
542 locatorIn := mux.Vars(req)["locator"]
544 // Check if the client specified storage classes
545 if req.Header.Get("X-Keep-Storage-Classes") != "" {
547 for _, sc := range strings.Split(req.Header.Get("X-Keep-Storage-Classes"), ",") {
548 scl = append(scl, strings.Trim(sc, " "))
550 kc.SetStorageClasses(scl)
553 _, err = fmt.Sscanf(req.Header.Get("Content-Length"), "%d", &expectLength)
554 if err != nil || expectLength < 0 {
555 err = errLengthRequired
556 status = http.StatusLengthRequired
561 var loc *keepclient.Locator
562 if loc, err = keepclient.MakeLocator(locatorIn); err != nil {
563 status = http.StatusBadRequest
565 } else if loc.Size > 0 && int64(loc.Size) != expectLength {
566 err = errLengthMismatch
567 status = http.StatusBadRequest
574 var user *arvados.User
575 if pass, tok, user = h.CheckAuthorizationHeader(req); !pass {
576 err = errBadAuthorizationHeader
577 status = http.StatusForbidden
581 // Copy ArvadosClient struct and use the client's API token
582 arvclient := *kc.Arvados
583 arvclient.ApiToken = tok
584 kc.Arvados = &arvclient
586 // Check if the client specified the number of replicas
587 if desiredReplicas := req.Header.Get(keepclient.XKeepDesiredReplicas); desiredReplicas != "" {
589 _, err := fmt.Sscanf(desiredReplicas, "%d", &r)
595 // Now try to put the block through
597 bytes, err2 := ioutil.ReadAll(req.Body)
599 err = fmt.Errorf("Error reading request body: %s", err2)
600 status = http.StatusInternalServerError
603 locatorOut, wroteReplicas, err = kc.PutB(bytes)
605 locatorOut, wroteReplicas, err = kc.PutHR(locatorIn, req.Body, expectLength)
608 if locatorOut != "" {
609 parts := strings.SplitN(locatorOut, "+", 3)
613 logger = logger.WithField("user_uuid", user.UUID).
614 WithField("user_full_name", user.FullName)
616 logger.WithField("locator", fmt.Sprintf("%s+%s", parts[0], parts[1])).Infof("Block upload")
620 // Tell the client how many successful PUTs we accomplished
621 resp.Header().Set(keepclient.XKeepReplicasStored, fmt.Sprintf("%d", wroteReplicas))
625 status = http.StatusOK
626 if len(kc.StorageClasses) > 0 {
627 // A successful PUT request with storage classes means that all
628 // storage classes were fulfilled, so the client will get a
629 // confirmation via the X-Storage-Classes-Confirmed header.
632 for _, sc := range kc.StorageClasses {
634 hdr = fmt.Sprintf("%s=%d", sc, wroteReplicas)
637 hdr += fmt.Sprintf(", %s=%d", sc, wroteReplicas)
640 resp.Header().Set(keepclient.XKeepStorageClassesConfirmed, hdr)
642 _, err = io.WriteString(resp, locatorOut)
643 case keepclient.OversizeBlockError:
645 status = http.StatusRequestEntityTooLarge
646 case keepclient.InsufficientReplicasError:
647 status = http.StatusServiceUnavailable
649 status = http.StatusBadGateway
653 // ServeHTTP implementation for IndexHandler
654 // Supports only GET requests for /index/{prefix:[0-9a-f]{0,32}}
655 // For each keep server found in LocalRoots:
656 // Invokes GetIndex using keepclient
657 // Expects "complete" response (terminating with blank new line)
658 // Aborts on any errors
659 // Concatenates responses from all those keep servers and returns
660 func (h *proxyHandler) Index(resp http.ResponseWriter, req *http.Request) {
663 prefix := mux.Vars(req)["prefix"]
668 if status != http.StatusOK {
669 http.Error(resp, err.Error(), status)
673 kc := h.makeKeepClient(req)
674 ok, token, _ := h.CheckAuthorizationHeader(req)
676 status, err = http.StatusForbidden, errBadAuthorizationHeader
680 // Copy ArvadosClient struct and use the client's API token
681 arvclient := *kc.Arvados
682 arvclient.ApiToken = token
683 kc.Arvados = &arvclient
685 // Only GET method is supported
686 if req.Method != "GET" {
687 status, err = http.StatusNotImplemented, errMethodNotSupported
691 // Get index from all LocalRoots and write to resp
693 for uuid := range kc.LocalRoots() {
694 reader, err = kc.GetIndex(uuid, prefix)
696 status = http.StatusBadGateway
700 _, err = io.Copy(resp, reader)
702 status = http.StatusBadGateway
707 // Got index from all the keep servers and wrote to resp
708 status = http.StatusOK
709 resp.Write([]byte("\n"))
712 func (h *proxyHandler) makeKeepClient(req *http.Request) *keepclient.KeepClient {
714 kc.RequestID = req.Header.Get("X-Request-Id")
715 kc.HTTPClient = &proxyClient{
716 client: &http.Client{
718 Transport: h.transport,