1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.arvados.org/arvados.git/sdk/go/arvados"
18 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
19 "git.arvados.org/arvados.git/sdk/go/auth"
20 "git.arvados.org/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, volmgr *RRVolumeManager) {
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:],
53 VolumeManager: volmgr,
58 var remoteClient *keepclient.KeepClient
60 for i, part := range strings.Split(r.URL.Path[1:], "+") {
63 // don't try to parse hash part as hint
64 case strings.HasPrefix(part, "A"):
65 // drop local permission hint
67 case len(part) > 7 && part[0] == 'R' && part[6] == '-':
69 remote, ok := cluster.RemoteClusters[remoteID]
71 http.Error(w, "remote cluster not configured", http.StatusBadRequest)
74 kc, err := rp.remoteClient(remoteID, remote, token)
75 if err == auth.ErrObsoleteToken {
76 http.Error(w, err.Error(), http.StatusBadRequest)
78 } else if err != nil {
79 http.Error(w, err.Error(), http.StatusInternalServerError)
85 parts = append(parts, part)
87 if remoteClient == nil {
88 http.Error(w, "bad request", http.StatusBadRequest)
91 locator := strings.Join(parts, "+")
92 rdr, _, _, err := remoteClient.Get(locator)
97 case *keepclient.ErrNotFound:
98 http.Error(w, err.Error(), http.StatusNotFound)
100 http.Error(w, err.Error(), http.StatusBadGateway)
104 func (rp *remoteProxy) remoteClient(remoteID string, remoteCluster arvados.RemoteCluster, token string) (*keepclient.KeepClient, error) {
106 kc, ok := rp.clients[remoteID]
109 c := &arvados.Client{
110 APIHost: remoteCluster.Host,
112 Insecure: remoteCluster.Insecure,
114 ac, err := arvadosclient.New(c)
118 kc, err = keepclient.MakeKeepClient(ac)
124 if rp.clients == nil {
125 rp.clients = map[string]*keepclient.KeepClient{remoteID: kc}
127 rp.clients[remoteID] = kc
131 accopy := *kc.Arvados
132 accopy.ApiToken = token
134 kccopy.Arvados = &accopy
135 token, err := auth.SaltToken(token, remoteID)
139 kccopy.Arvados.ApiToken = token
143 var localOrRemoteSignature = regexp.MustCompile(`\+[AR][^\+]*`)
145 // remoteResponseCacher wraps http.ResponseWriter. It buffers the
146 // response data in the provided buffer, writes/touches a copy on a
147 // local volume, adds a response header with a locally-signed locator,
148 // and finally writes the data through.
149 type remoteResponseCacher struct {
153 Context context.Context
154 Cluster *arvados.Cluster
155 VolumeManager *RRVolumeManager
160 func (rrc *remoteResponseCacher) Write(p []byte) (int, error) {
161 if len(rrc.Buffer)+len(p) > cap(rrc.Buffer) {
162 return 0, errors.New("buffer full")
164 rrc.Buffer = append(rrc.Buffer, p...)
168 func (rrc *remoteResponseCacher) WriteHeader(statusCode int) {
169 rrc.statusCode = statusCode
172 func (rrc *remoteResponseCacher) Close() error {
173 if rrc.statusCode == 0 {
174 rrc.statusCode = http.StatusOK
175 } else if rrc.statusCode != http.StatusOK {
176 rrc.ResponseWriter.WriteHeader(rrc.statusCode)
177 rrc.ResponseWriter.Write(rrc.Buffer)
180 _, err := PutBlock(rrc.Context, rrc.VolumeManager, rrc.Buffer, rrc.Locator[:32], nil)
181 if rrc.Context.Err() != nil {
182 // If caller hung up, log that instead of subsequent/misleading errors.
183 http.Error(rrc.ResponseWriter, rrc.Context.Err().Error(), http.StatusGatewayTimeout)
186 if err == RequestHashError {
187 http.Error(rrc.ResponseWriter, "checksum mismatch in remote response", http.StatusBadGateway)
190 if err, ok := err.(*KeepError); ok {
191 http.Error(rrc.ResponseWriter, err.Error(), err.HTTPCode)
195 http.Error(rrc.ResponseWriter, err.Error(), http.StatusBadGateway)
199 unsigned := localOrRemoteSignature.ReplaceAllLiteralString(rrc.Locator, "")
200 expiry := time.Now().Add(rrc.Cluster.Collections.BlobSigningTTL.Duration())
201 signed := SignLocator(rrc.Cluster, unsigned, rrc.Token, expiry)
202 if signed == unsigned {
203 err = errors.New("could not sign locator")
204 http.Error(rrc.ResponseWriter, err.Error(), http.StatusInternalServerError)
207 rrc.Header().Set("X-Keep-Locator", signed)
208 rrc.ResponseWriter.WriteHeader(rrc.statusCode)
209 _, err = rrc.ResponseWriter.Write(rrc.Buffer)