Skip to content

Commit d873eba

Browse files
committed
fix: preserve external_host path prefixes in generated public URLs
1 parent 93ff899 commit d873eba

23 files changed

Lines changed: 339 additions & 32 deletions

containers/aggregator-server/config/description.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package config
33
import (
44
"aggregator/model"
55
"encoding/json"
6-
"fmt"
76
"net/http"
87
)
98

@@ -38,11 +37,11 @@ func handleServerDescription(w http.ResponseWriter, r *http.Request) {
3837
}
3938

4039
desc := AggregatorServerDescription{
41-
RegistrationEndpoint: fmt.Sprintf("%s://%s/registration", model.Protocol, model.ExternalHost),
40+
RegistrationEndpoint: model.PublicURL("/registration"),
4241
SupportedRegistrationTypes: supported,
43-
Version: "1.0.0",
44-
ClientIdentifier: fmt.Sprintf("%s://%s/client.json", model.Protocol, model.ExternalHost),
45-
TransformationCatalog: fmt.Sprintf("%s://%s/config/transformations", model.Protocol, model.ExternalHost),
42+
Version: "1.0.0",
43+
ClientIdentifier: model.PublicURL("/client.json"),
44+
TransformationCatalog: model.PublicURL("/config/transformations"),
4645
}
4746

4847
w.Header().Set("Content-Type", "application/json")

containers/aggregator-server/config/transformations.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type TransformationsConfigData struct {
1717
func InitTransformationsConfiguration(mux *http.ServeMux) error {
1818
logrus.Info("Initializing transformations configuration")
1919

20-
transformations := fmt.Sprintf(hardcodedAvailableTransformationsTemplate, model.ExternalHost)
20+
transformations := fmt.Sprintf(hardcodedAvailableTransformationsTemplate, model.PublicURL("/config/transformations"))
2121

2222
config := TransformationsConfigData{
2323
etagTransformations: 0,
@@ -78,7 +78,7 @@ func (config *TransformationsConfigData) getAvailableTransformations(w http.Resp
7878
}
7979

8080
const hardcodedAvailableTransformationsTemplate = `
81-
@base <http://%s/config/transformations#> .
81+
@base <%s#> .
8282
@prefix fno: <https://w3id.org/function/ontology#> .
8383
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
8484
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

containers/aggregator-server/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ func main() {
5353

5454
model.Protocol = "http"
5555
model.ExternalHost = externalBase
56+
model.ExternalBasePath = ""
5657
if parsed, err := url.Parse(externalBase); err == nil && parsed.Scheme != "" {
5758
model.Protocol = strings.ToLower(parsed.Scheme)
5859
model.ExternalHost = parsed.Host
60+
model.ExternalBasePath = strings.TrimSuffix(parsed.Path, "/")
5961
}
6062

6163
model.ProvisionClientID = strings.TrimSpace(os.Getenv("PROVISION_CLIENT_ID"))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package model
2+
3+
import "strings"
4+
5+
func PublicURL(path string) string {
6+
base := strings.TrimSuffix(ExternalBasePath, "/")
7+
relative := "/" + strings.TrimLeft(path, "/")
8+
return Protocol + "://" + ExternalHost + base + relative
9+
}
10+
11+
func PublicBaseURL() string {
12+
base := strings.TrimSuffix(ExternalBasePath, "/")
13+
return Protocol + "://" + ExternalHost + base
14+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package model
2+
3+
import "testing"
4+
5+
func TestPublicURL(t *testing.T) {
6+
originalProtocol := Protocol
7+
originalHost := ExternalHost
8+
originalBasePath := ExternalBasePath
9+
t.Cleanup(func() {
10+
Protocol = originalProtocol
11+
ExternalHost = originalHost
12+
ExternalBasePath = originalBasePath
13+
})
14+
15+
Protocol = "https"
16+
ExternalHost = "example.com"
17+
ExternalBasePath = "/aggregator"
18+
19+
got := PublicURL("/registration")
20+
want := "https://example.com/aggregator/registration"
21+
if got != want {
22+
t.Fatalf("expected %q, got %q", want, got)
23+
}
24+
}
25+
26+
func TestPublicURLNoBasePath(t *testing.T) {
27+
originalProtocol := Protocol
28+
originalHost := ExternalHost
29+
originalBasePath := ExternalBasePath
30+
t.Cleanup(func() {
31+
Protocol = originalProtocol
32+
ExternalHost = originalHost
33+
ExternalBasePath = originalBasePath
34+
})
35+
36+
Protocol = "https"
37+
ExternalHost = "example.com"
38+
ExternalBasePath = ""
39+
40+
got := PublicURL("registration")
41+
want := "https://example.com/registration"
42+
if got != want {
43+
t.Fatalf("expected %q, got %q", want, got)
44+
}
45+
}
46+
47+
func TestPublicBaseURL(t *testing.T) {
48+
originalProtocol := Protocol
49+
originalHost := ExternalHost
50+
originalBasePath := ExternalBasePath
51+
t.Cleanup(func() {
52+
Protocol = originalProtocol
53+
ExternalHost = originalHost
54+
ExternalBasePath = originalBasePath
55+
})
56+
57+
Protocol = "https"
58+
ExternalHost = "example.com"
59+
ExternalBasePath = "/aggregator/"
60+
61+
got := PublicBaseURL()
62+
want := "https://example.com/aggregator"
63+
if got != want {
64+
t.Fatalf("expected %q, got %q", want, got)
65+
}
66+
}

containers/aggregator-server/model/user.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package model
22

33
import (
4-
"fmt"
54
"time"
65
)
76

@@ -33,11 +32,11 @@ type User struct {
3332

3433
func (u *User) ConfigEndpoints() map[string]string {
3534
return map[string]string{
36-
"services": fmt.Sprintf("http://%s/config/%s/services", ExternalHost, u.Namespace),
35+
"services": PublicURL("/config/" + u.Namespace + "/services"),
3736
}
3837
}
3938

4039
// GetAggregatorURL returns the aggregator description URL for a namespace.
4140
func GetAggregatorURL(namespace string) string {
42-
return fmt.Sprintf("%s://%s/config/%s", Protocol, ExternalHost, namespace)
41+
return PublicURL("/config/" + namespace)
4342
}

containers/aggregator-server/model/vars.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
var Protocol string
1010
var ExternalHost string
11+
var ExternalBasePath string
1112

1213
var ClientId string
1314
var ClientSecret string

containers/aggregator-server/registration/namespace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ func deployAggregatorResources(namespace string, tokenEndpoint string, accessTok
490490
{ContainerPort: 5000},
491491
},
492492
Env: []corev1.EnvVar{
493-
{Name: "AGGREGATOR_EXTERNAL_HOST", Value: fmt.Sprintf("%s://%s", model.Protocol, model.ExternalHost)},
493+
{Name: "AGGREGATOR_EXTERNAL_HOST", Value: model.PublicBaseURL()},
494494
{Name: "CLIENT_ID", Value: model.ClientId},
495495
{Name: "CLIENT_SECRET", Value: model.ClientSecret},
496496
{Name: "LOG_LEVEL", Value: model.LogLevel.String()},

containers/aggregator-server/registration/registration_client_credentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func handleClientCredentialsFlow(w http.ResponseWriter, req model.RegistrationRe
122122
return
123123
}
124124

125-
clientURI := fmt.Sprintf("%s://%s", model.Protocol, model.ExternalHost)
125+
clientURI := model.PublicBaseURL()
126126
rsClientID, rsClientSecret, pat, err := ensurePATForUMA(configCtx, umaConfig, tokenResp.AccessToken, clientURI)
127127
if err != nil {
128128
logrus.WithError(err).Error("Unable to obtain UMA protection API token")

containers/aggregator-server/registration/registration_provision.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func handleProvisionFlow(w http.ResponseWriter, req model.RegistrationRequest, o
128128
return
129129
}
130130

131-
clientURI := fmt.Sprintf("%s://%s", model.Protocol, model.ExternalHost)
131+
clientURI := model.PublicBaseURL()
132132
rsClientID, rsClientSecret, pat, err := ensurePATForUMA(configCtx, umaConfig, tokenResp.AccessToken, clientURI)
133133
if err != nil {
134134
logrus.WithError(err).Error("Unable to obtain UMA protection API token")

0 commit comments

Comments
 (0)