Skip to content

Commit 5fb4bec

Browse files
authored
Merge pull request #12 from holaplex/mpw/disable-slugs-apisix-chart
use x-organization-id instead of slugs for hub-orgs plugin
2 parents b12d2c0 + db6ffc8 commit 5fb4bec

File tree

6 files changed

+70
-57
lines changed

6 files changed

+70
-57
lines changed

charts/hub-gateway/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ type: application
1818
# This is the chart version. This version number should be incremented each time you make changes
1919
# to the chart and its templates, including the app version.
2020
# Versions are expected to follow Semantic Versioning (https://semver.org/)
21-
version: 0.1.8
21+
version: 0.1.9
2222

2323
# This is the version number of the application being deployed. This version number should be
2424
# incremented each time you make changes to the application. Versions are not expected to
2525
# follow Semantic Versioning. They should reflect the version the application is using.
2626
# It is recommended to use it with quotes.
27-
appVersion: "0.1.8"
27+
appVersion: "0.1.9"
2828
sources:
2929
- https://github.com/holaplex/helm-charts
3030

charts/hub-gateway/plugins/hub-orgs.lua

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ local schema = {
2929

3030
local _M = {
3131
version = 0.1,
32-
priority = 3000,
32+
priority = 1005,
3333
name = "hub-orgs",
3434
schema = schema,
3535
}
@@ -72,38 +72,20 @@ function _M.access(conf, ctx)
7272
keepalive = conf.keepalive,
7373
ssl_verify = conf.ssl_verify
7474
}
75-
76-
-- Get slug from header
77-
local org_slug = string.lower(string.match(headers.host, "([^.]+)."))
78-
79-
-- make the call - get org id
80-
local endpoint = conf.host .. "/organizations/" .. org_slug
81-
local httpc = http.new()
82-
httpc:set_timeout(conf.timeout)
83-
local res, err = httpc:request_uri(endpoint, params)
84-
85-
-- return 503 if error on response or when parsing
86-
if not res then
87-
local res = build_json_error(500, "Internal server error", "Unable to get organizations")
88-
return 500, res
75+
local org_id = ngx.var['cookie__hub_org'] or headers['x-organization-id']
76+
if not org_id then
77+
local res = build_json_error(401, "Unauthorized", "X-Organization-Id header not found")
78+
core.log.error("Failed to get org id from header or cookie")
79+
return 401, res
8980
end
9081

91-
local org , err = json.decode(res.body)
92-
if not org then
93-
local res = build_json_error(404, "Not found", "No organization found with slug: " .. org_slug)
94-
core.log.error("Failed to parse organization data. invalid response body: ", res.body, " err: ", err)
95-
return 404, res
96-
end
97-
98-
if conf.keepalive then
99-
params.keepalive_timeout = conf.keepalive_timeout
100-
params.keepalive_pool = conf.keepalive_pool
101-
end
102-
103-
10482
-- make the call - get affiliations
10583
local endpoint = conf.host .. "/affiliations"
84+
local httpc = http.new()
85+
httpc:set_timeout(conf.timeout)
10686
local res, err = httpc:request_uri(endpoint, params)
87+
88+
core.log.error("Getting affiliations from hub-orgs for user: ", user_id)
10789
-- return 503 if error on response or when parsing
10890
if not res then
10991
local res = build_json_error(500, "Internal server error", "Unable to get affiliations")
@@ -119,13 +101,15 @@ function _M.access(conf, ctx)
119101

120102
-- Expose org_id and affiliations on variables: org_id, hub_affiliations
121103
core.ctx.register_var("org_id", function(ctx)
122-
return org.id
104+
return org_id
123105
end)
124106

125107
local affiliations = ngx.encode_base64(res.body)
126108
core.ctx.register_var("hub_affiliations", function(ctx)
127109
return affiliations
128110
end)
111+
112+
core.response.set_header("x-organization-id", org_id)
129113
end
130114

131115
return _M

charts/hub-gateway/plugins/kratos.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ local schema = {
4949

5050
local _M = {
5151
version = 0.1,
52-
priority = 4000,
52+
priority = 1030,
5353
name = "kratos",
5454
schema = schema,
5555
}
@@ -153,10 +153,10 @@ function _M.access(conf, ctx)
153153
end)
154154
end
155155

156-
-- Expose user id on $kratos_user_id variable and X-USER-ID header
156+
-- Expose user id on $kratos_user_id variable
157157
if conf.expose_user_id then
158-
core.request.set_header(ctx, "X-USER-ID", data.identity.id)
159-
core.response.set_header("X-USER-ID", data.identity.id)
158+
core.request.set_header(ctx, "x-user-id", data.identity.id)
159+
core.response.set_header("x-user-id", data.identity.id)
160160
core.ctx.register_var("kratos_user_id", function(ctx)
161161
return data.identity.id
162162
end)

charts/hub-gateway/plugins/opa-mod.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ local schema = {
8585

8686
local _M = {
8787
version = 0.1,
88-
priority = 2002,
88+
priority = 1000,
8989
name = "opa-mod",
9090
schema = schema,
9191
}

charts/hub-gateway/templates/apisixroute.yaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,41 @@ metadata:
1313
{{- include "hub-gateway.labels" $ | nindent 4 }}
1414
spec:
1515
http:
16-
- name: {{ .name }}
16+
- name: {{ .name }}
1717
backends:
1818
- serviceName: {{ .serviceName }}
1919
servicePort: {{ .servicePort }}
2020
match:
2121
hosts:
22-
- {{ print .subdomain "." $domain | quote}}
22+
- {{ print .subdomain "." $domain | quote }}
2323
paths:
2424
{{ .paths | toYaml | nindent 7 }}
2525
methods:
2626
{{ .methods | toYaml | nindent 7 }}
2727
plugins:
28-
{{- if .require_auth}}
28+
{{- if .require_auth }}
2929
- name: kratos
3030
enable: true
3131
config:
3232
host: {{ print "http://" $apisixPlugins.kratos.serviceName "." $namespace ".svc:" $apisixPlugins.kratos.servicePort | quote }}
3333
expose_user_data: true
3434
expose_user_id: true
3535
session_cookie_name: {{ $sessionCookie }}
36+
{{- end }}
37+
{{- if .require_org }}
3638
- name: hub-orgs
3739
enable: true
3840
config:
3941
host: {{ print "http://" $apisixPlugins.hubOrgs.serviceName "." $namespace ".svc:" $apisixPlugins.hubOrgs.servicePort "/v1" | quote }}
40-
{{ end }}
41-
{{- if .regex_uri}}
42+
{{- end }}
43+
{{- if .regex_uri }}
4244
- name: proxy-rewrite
4345
enable: true
4446
config:
45-
headers:
46-
{{ .headers | toYaml | nindent 9 }}
4747
regex_uri:
4848
{{ .regex_uri | toYaml | nindent 9 }}
49-
{{ end }}
50-
{{- if .policy}}
49+
{{- end }}
50+
{{- if .policy }}
5151
- name: opa-mod
5252
enable: true
5353
config:
@@ -59,7 +59,7 @@ spec:
5959
org_id: $org_id
6060
project_id: $project_id
6161
affiliations: $hub_affiliations
62-
{{ end }}
62+
{{- end }}
6363
---
6464
{{- end }}
6565
{{- end }}

charts/hub-gateway/values.yaml

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,52 @@ routes:
77
- name: api
88
serviceName: hub-orgs
99
servicePort: 80
10-
subdomain: '*.api'
10+
subdomain: api
1111
paths:
12-
- /organization
13-
- /projects
14-
- /credentials
15-
- /webhooks/*
12+
- /v1/organization
13+
- /v1/organizations
14+
- /v1/projects
15+
- /v1/projects/*
16+
- /v1/affiliations
17+
- /v1/members
18+
- /v1/credentials
19+
- /v1/credentials/*
20+
- /v1/webhook/events
21+
- /v1/webhooks/*
22+
- /v1/members
23+
- /v1/members/*
24+
- /v1/invites
25+
- /v1/invites/*
1626
methods:
1727
- GET
1828
- POST
1929
- DELETE
2030
require_auth: true
21-
regex_uri: [ "^/(.*)", "/v1/$1" ]
22-
headers:
23-
X-Organization-Id: $org_id
24-
X-Project-Id: $project_id
25-
X-User-Id: $kratos_user_id
26-
#policy: "hub/utils/echo"
31+
require_org: true
32+
policy: "hub/api/orgs"
33+
34+
- name: hub-browser-api
35+
serviceName: hub-orgs
36+
servicePort: 80
37+
subdomain: hub
38+
paths:
39+
- /browser/login
40+
- /browser/organizations/*
41+
methods:
42+
- POST
43+
require_auth: true
44+
45+
- name: ui-private
46+
subdomain: hub
47+
serviceName: hub
48+
servicePort: 80
49+
require_auth: true
50+
require_org: true
51+
methods:
52+
- GET
53+
paths:
54+
- /organization/*
55+
- /projects/*
2756

2857
- name: ui-public
2958
subdomain: hub

0 commit comments

Comments
 (0)