19
19
package ocmd
20
20
21
21
import (
22
+ "context"
22
23
"encoding/json"
23
24
"fmt"
24
25
"mime"
@@ -137,7 +138,7 @@ func (h *sharesHandler) CreateShare(w http.ResponseWriter, r *http.Request) {
137
138
return
138
139
}
139
140
140
- protocols , err := getAndResolveProtocols (req .Protocols , r )
141
+ protocols , err := getAndResolveProtocols (ctx , req .Protocols , owner . Idp )
141
142
if err != nil {
142
143
reqres .WriteError (w , r , reqres .APIErrorInvalidParameter , "error with protocols payload" , err )
143
144
return
@@ -246,7 +247,7 @@ func getOCMShareType(t string) ocm.ShareType {
246
247
}
247
248
}
248
249
249
- func getAndResolveProtocols (p Protocols , r * http. Request ) ([]* ocm.Protocol , error ) {
250
+ func getAndResolveProtocols (ctx context. Context , p Protocols , ownerServer string ) ([]* ocm.Protocol , error ) {
250
251
protos := make ([]* ocm.Protocol , 0 , len (p ))
251
252
for _ , data := range p {
252
253
var uri string
@@ -270,8 +271,8 @@ func getAndResolveProtocols(p Protocols, r *http.Request) ([]*ocm.Protocol, erro
270
271
protos = append (protos , ocmProto )
271
272
continue
272
273
}
273
- // otherwise resolve the hostname using the OCM discovery endpoint
274
- remoteRoot , err := discoverOcmRoot (r , protocolName )
274
+ // otherwise use as endpoint the owner's server from the payload
275
+ remoteRoot , err := discoverOcmRoot (ctx , ownerServer , protocolName )
275
276
if err != nil {
276
277
return nil , err
277
278
}
@@ -297,28 +298,15 @@ func getAndResolveProtocols(p Protocols, r *http.Request) ([]*ocm.Protocol, erro
297
298
return protos , nil
298
299
}
299
300
300
- func discoverOcmRoot (r * http. Request , proto string ) (string , error ) {
301
+ func discoverOcmRoot (ctx context. Context , ownerServer string , proto string ) (string , error ) {
301
302
// implements the OCM discovery logic to fetch the root at the remote host that sent the share for the given proto, see
302
303
// https://cs3org.github.io/OCM-API/docs.html?branch=v1.1.0&repo=OCM-API&user=cs3org#/paths/~1ocm-provider/get
303
- ctx := r .Context ()
304
304
log := appctx .GetLogger (ctx )
305
305
306
- // assume the sender host is either given in the usual reverse proxy headers or as RemoteAddr, and that the
307
- // remote end listens on https regardless if the incoming connection got its TLS terminated upstream of us
308
- senderURL := r .Header .Get ("X-Real-Ip" )
309
- if senderURL == "" {
310
- senderURL = r .Header .Get ("X-Forwarded-For" )
311
- }
312
- if senderURL == "" {
313
- senderURL = r .RemoteAddr
314
- }
315
- senderURL = "https://" + senderURL [:strings .LastIndex (senderURL , ":" )]
316
- log .Debug ().Str ("sender" , senderURL ).Msg ("received OCM share, attempting to discover sender endpoint" )
317
-
318
306
ocmClient := NewClient (time .Duration (10 )* time .Second , true )
319
- ocmCaps , err := ocmClient .Discover (ctx , senderURL )
307
+ ocmCaps , err := ocmClient .Discover (ctx , "https://" + ownerServer )
320
308
if err != nil {
321
- log .Warn ().Str ("sender" , senderURL ).Err (err ).Msg ("failed to discover OCM sender" )
309
+ log .Warn ().Str ("sender" , ownerServer ).Err (err ).Msg ("failed to discover OCM sender" )
322
310
return "" , err
323
311
}
324
312
for _ , t := range ocmCaps .ResourceTypes {
@@ -329,10 +317,11 @@ func discoverOcmRoot(r *http.Request, proto string) (string, error) {
329
317
u , _ := url .Parse (ocmCaps .Endpoint )
330
318
u .Path = protoRoot
331
319
u .RawQuery = ""
320
+ log .Debug ().Str ("sender" , ownerServer ).Str ("proto" , proto ).Str ("URL" , u .String ()).Msg ("resolved protocol URL" )
332
321
return u .String (), nil
333
322
}
334
323
}
335
324
336
- log .Warn ().Str ("sender" , r . Host ).Interface ("response" , ocmCaps ).Msg ("missing root" )
325
+ log .Warn ().Str ("sender" , ownerServer ).Interface ("response" , ocmCaps ).Msg ("missing protocol root" )
337
326
return "" , errtypes .NotFound (fmt .Sprintf ("root not found on OCM discovery for protocol %s" , proto ))
338
327
}
0 commit comments