Skip to content

Commit 1c97934

Browse files
committed
Proper implementation using the owner's server address
1 parent 102d705 commit 1c97934

File tree

1 file changed

+10
-21
lines changed
  • internal/http/services/opencloudmesh/ocmd

1 file changed

+10
-21
lines changed

internal/http/services/opencloudmesh/ocmd/shares.go

+10-21
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package ocmd
2020

2121
import (
22+
"context"
2223
"encoding/json"
2324
"fmt"
2425
"mime"
@@ -137,7 +138,7 @@ func (h *sharesHandler) CreateShare(w http.ResponseWriter, r *http.Request) {
137138
return
138139
}
139140

140-
protocols, err := getAndResolveProtocols(req.Protocols, r)
141+
protocols, err := getAndResolveProtocols(ctx, req.Protocols, owner.Idp)
141142
if err != nil {
142143
reqres.WriteError(w, r, reqres.APIErrorInvalidParameter, "error with protocols payload", err)
143144
return
@@ -246,7 +247,7 @@ func getOCMShareType(t string) ocm.ShareType {
246247
}
247248
}
248249

249-
func getAndResolveProtocols(p Protocols, r *http.Request) ([]*ocm.Protocol, error) {
250+
func getAndResolveProtocols(ctx context.Context, p Protocols, ownerServer string) ([]*ocm.Protocol, error) {
250251
protos := make([]*ocm.Protocol, 0, len(p))
251252
for _, data := range p {
252253
var uri string
@@ -270,8 +271,8 @@ func getAndResolveProtocols(p Protocols, r *http.Request) ([]*ocm.Protocol, erro
270271
protos = append(protos, ocmProto)
271272
continue
272273
}
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)
275276
if err != nil {
276277
return nil, err
277278
}
@@ -297,28 +298,15 @@ func getAndResolveProtocols(p Protocols, r *http.Request) ([]*ocm.Protocol, erro
297298
return protos, nil
298299
}
299300

300-
func discoverOcmRoot(r *http.Request, proto string) (string, error) {
301+
func discoverOcmRoot(ctx context.Context, ownerServer string, proto string) (string, error) {
301302
// implements the OCM discovery logic to fetch the root at the remote host that sent the share for the given proto, see
302303
// 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()
304304
log := appctx.GetLogger(ctx)
305305

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-
318306
ocmClient := NewClient(time.Duration(10)*time.Second, true)
319-
ocmCaps, err := ocmClient.Discover(ctx, senderURL)
307+
ocmCaps, err := ocmClient.Discover(ctx, "https://"+ownerServer)
320308
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")
322310
return "", err
323311
}
324312
for _, t := range ocmCaps.ResourceTypes {
@@ -329,10 +317,11 @@ func discoverOcmRoot(r *http.Request, proto string) (string, error) {
329317
u, _ := url.Parse(ocmCaps.Endpoint)
330318
u.Path = protoRoot
331319
u.RawQuery = ""
320+
log.Debug().Str("sender", ownerServer).Str("proto", proto).Str("URL", u.String()).Msg("resolved protocol URL")
332321
return u.String(), nil
333322
}
334323
}
335324

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")
337326
return "", errtypes.NotFound(fmt.Sprintf("root not found on OCM discovery for protocol %s", proto))
338327
}

0 commit comments

Comments
 (0)