1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.curoverse.com/arvados.git/sdk/go/arvados"
18 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
19 "git.curoverse.com/arvados.git/sdk/go/auth"
20 "git.curoverse.com/arvados.git/sdk/go/keepclient"
23 type remoteProxy struct {
24 clients map[string]*keepclient.KeepClient
28 func (rp *remoteProxy) Get(ctx context.Context, w http.ResponseWriter, r *http.Request, cluster *arvados.Cluster) {
29 // Intervening proxies must not return a cached GET response
30 // to a prior request if a X-Keep-Signature request header has
31 // been added or changed.
32 w.Header().Add("Vary", "X-Keep-Signature")
34 token := GetAPIToken(r)
36 http.Error(w, "no token provided in Authorization header", http.StatusUnauthorized)
39 if strings.SplitN(r.Header.Get("X-Keep-Signature"), ",", 2)[0] == "local" {
40 buf, err := getBufferWithContext(ctx, bufs, BlockSize)
42 http.Error(w, err.Error(), http.StatusServiceUnavailable)
46 rrc := &remoteResponseCacher{
47 Locator: r.URL.Path[1:],
56 var remoteClient *keepclient.KeepClient
58 for i, part := range strings.Split(r.URL.Path[1:], "+") {
61 // don't try to parse hash part as hint
62 case strings.HasPrefix(part, "A"):
63 // drop local permission hint
65 case len(part) > 7 && part[0] == 'R' && part[6] == '-':
67 remote, ok := cluster.RemoteClusters[remoteID]
69 http.Error(w, "remote cluster not configured", http.StatusBadGateway)
72 kc, err := rp.remoteClient(remoteID, remote, token)
73 if err == auth.ErrObsoleteToken {
74 http.Error(w, err.Error(), http.StatusBadRequest)
76 } else if err != nil {
77 http.Error(w, err.Error(), http.StatusInternalServerError)
83 parts = append(parts, part)
85 if remoteClient == nil {
86 http.Error(w, "bad request", http.StatusBadRequest)
89 locator := strings.Join(parts, "+")
90 rdr, _, _, err := remoteClient.Get(locator)
95 case *keepclient.ErrNotFound:
96 http.Error(w, err.Error(), http.StatusNotFound)
98 http.Error(w, err.Error(), http.StatusBadGateway)
102 func (rp *remoteProxy) remoteClient(remoteID string, remoteCluster arvados.RemoteCluster, token string) (*keepclient.KeepClient, error) {
104 kc, ok := rp.clients[remoteID]
107 c := &arvados.Client{
108 APIHost: remoteCluster.Host,
110 Insecure: remoteCluster.Insecure,
112 ac, err := arvadosclient.New(c)
116 kc, err = keepclient.MakeKeepClient(ac)
122 if rp.clients == nil {
123 rp.clients = map[string]*keepclient.KeepClient{remoteID: kc}
125 rp.clients[remoteID] = kc
129 accopy := *kc.Arvados
130 accopy.ApiToken = token
132 kccopy.Arvados = &accopy
133 token, err := auth.SaltToken(token, remoteID)
137 kccopy.Arvados.ApiToken = token
141 var localOrRemoteSignature = regexp.MustCompile(`\+[AR][^\+]*`)
143 // remoteResponseCacher wraps http.ResponseWriter. It buffers the
144 // response data in the provided buffer, writes/touches a copy on a
145 // local volume, adds a response header with a locally-signed locator,
146 // and finally writes the data through.
147 type remoteResponseCacher struct {
151 Context context.Context
156 func (rrc *remoteResponseCacher) Write(p []byte) (int, error) {
157 if len(rrc.Buffer)+len(p) > cap(rrc.Buffer) {
158 return 0, errors.New("buffer full")
160 rrc.Buffer = append(rrc.Buffer, p...)
164 func (rrc *remoteResponseCacher) WriteHeader(statusCode int) {
165 rrc.statusCode = statusCode
168 func (rrc *remoteResponseCacher) Close() error {
169 if rrc.statusCode == 0 {
170 rrc.statusCode = http.StatusOK
171 } else if rrc.statusCode != http.StatusOK {
172 rrc.ResponseWriter.WriteHeader(rrc.statusCode)
173 rrc.ResponseWriter.Write(rrc.Buffer)
176 _, err := PutBlock(rrc.Context, rrc.Buffer, rrc.Locator[:32])
177 if err == RequestHashError {
178 http.Error(rrc.ResponseWriter, "checksum mismatch in remote response", http.StatusBadGateway)
181 if err, ok := err.(*KeepError); ok {
182 http.Error(rrc.ResponseWriter, err.Error(), err.HTTPCode)
186 http.Error(rrc.ResponseWriter, err.Error(), http.StatusBadGateway)
190 unsigned := localOrRemoteSignature.ReplaceAllLiteralString(rrc.Locator, "")
191 signed := SignLocator(unsigned, rrc.Token, time.Now().Add(theConfig.BlobSignatureTTL.Duration()))
192 if signed == unsigned {
193 err = errors.New("could not sign locator")
194 http.Error(rrc.ResponseWriter, err.Error(), http.StatusInternalServerError)
197 rrc.Header().Set("X-Keep-Locator", signed)
198 rrc.ResponseWriter.WriteHeader(rrc.statusCode)
199 _, err = rrc.ResponseWriter.Write(rrc.Buffer)