/
cf_cors.go
107 lines (92 loc) · 3.07 KB
/
cf_cors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package cf
import (
ar "github.com/Apicurio/apicurio-registry-operator/api/v1"
"github.com/Apicurio/apicurio-registry-operator/controllers/loop"
"github.com/Apicurio/apicurio-registry-operator/controllers/loop/context"
"github.com/Apicurio/apicurio-registry-operator/controllers/svc/env"
"github.com/Apicurio/apicurio-registry-operator/controllers/svc/resources"
"go.uber.org/zap"
"net/url"
)
var _ loop.ControlFunction = &CorsCF{}
const ENV_CORS = "CORS_ALLOWED_ORIGINS"
type CorsCF struct {
ctx context.LoopContext
log *zap.SugaredLogger
svcResourceCache resources.ResourceCache
targetCors string
existingCors string
overriddenCors string
}
// This CF makes sure the CORS_ALLOWED_ORIGINS env. variable is set properly
func NewCorsCF(ctx context.LoopContext) loop.ControlFunction {
res := &CorsCF{
ctx: ctx,
svcResourceCache: ctx.GetResourceCache(),
}
res.log = ctx.GetLog().Sugar().With("cf", res.Describe())
return res
}
func (this *CorsCF) Describe() string {
return "CorsCF"
}
func (this *CorsCF) Sense() {
this.targetCors = ""
this.overriddenCors = ""
if specEntry, exists := this.svcResourceCache.Get(resources.RC_KEY_SPEC); exists {
host := specEntry.GetValue().(*ar.ApicurioRegistry).Spec.Deployment.Host
if host != "" {
this.targetCors = "http://" + host + "," + "https://" + host
}
keycloak := specEntry.GetValue().(*ar.ApicurioRegistry).Spec.Configuration.Security.Keycloak.Url
if keycloak != "" {
if keycloakUrl, err := url.Parse(keycloak); err == nil {
if keycloakHost := keycloakUrl.Hostname(); keycloakHost != "" {
if this.targetCors != "" {
this.targetCors = this.targetCors + ","
}
this.targetCors = this.targetCors + keycloakUrl.Scheme + "://" + keycloakHost
} else {
this.log.With("keycloakUrl", keycloakUrl).
Infof("could not include Keycloak URL in %s, failed to get host. "+
"Make sure the URL is a valid URL with both a scheme and a host", ENV_CORS)
}
} else {
this.log.With("keycloakUrl", keycloakUrl, "error", err).
Infof("could not include Keycloak URL in %s, failed to parse URL", ENV_CORS)
}
}
envList := specEntry.GetValue().(*ar.ApicurioRegistry).Spec.Configuration.Env
for _, e := range envList {
if e.Name == ENV_CORS {
this.overriddenCors = e.Value
}
}
}
this.existingCors = ""
if entry, exists := this.ctx.GetEnvCache().Get(ENV_CORS); exists {
this.existingCors = entry.GetValue().Value
}
}
func (this *CorsCF) Compare() bool {
if this.overriddenCors != "" {
return this.existingCors != this.overriddenCors
} else {
return this.existingCors != this.targetCors
}
}
func (this *CorsCF) Respond() {
if this.overriddenCors != "" {
this.ctx.GetEnvCache().Set(env.NewSimpleEnvCacheEntryBuilder(ENV_CORS, this.overriddenCors).Build())
} else {
if this.targetCors != "" {
this.ctx.GetEnvCache().Set(env.NewSimpleEnvCacheEntryBuilder(ENV_CORS, this.targetCors).Build())
} else {
this.ctx.GetEnvCache().DeleteByName(ENV_CORS)
}
}
}
func (this *CorsCF) Cleanup() bool {
// No cleanup
return true
}