Skip to content

Commit 9eb6c7f

Browse files
committed
chart update, adding hub orgs plugin
1 parent 51727f1 commit 9eb6c7f

File tree

7 files changed

+171
-179
lines changed

7 files changed

+171
-179
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.4
21+
version: 0.1.5
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.4"
27+
appVersion: "0.1.5"
2828
sources:
2929
- https://github.com/holaplex/helm-charts
3030

charts/hub-gateway/plugins/graphql.lua

Lines changed: 0 additions & 138 deletions
This file was deleted.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
local core = require("apisix.core")
2+
local http = require("resty.http")
3+
local json = require("apisix.core.json")
4+
5+
local schema = {
6+
type = "object",
7+
properties = {
8+
host = {type = "string"},
9+
ssl_verify = {
10+
type = "boolean",
11+
default = true,
12+
},
13+
timeout = {
14+
type = "integer",
15+
minimum = 1,
16+
maximum = 60000,
17+
default = 3000,
18+
description = "timeout in milliseconds",
19+
},
20+
keepalive = {type = "boolean", default = true},
21+
keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
22+
keepalive_pool = {type = "integer", minimum = 1, default = 5},
23+
redirect_unauthorized = {type = "boolean", default = false},
24+
redirect_uri = {type = "string"},
25+
},
26+
required = {"host"}
27+
}
28+
29+
30+
local _M = {
31+
version = 0.1,
32+
priority = 3000,
33+
name = "hub-orgs",
34+
schema = schema,
35+
}
36+
37+
38+
function _M.check_schema(conf)
39+
return core.schema.check(schema, conf)
40+
end
41+
42+
local function build_json_error(code, status, reason)
43+
44+
core.response.set_header("content", "application/json")
45+
local res = {
46+
error = {
47+
code = code,
48+
status = status,
49+
reason = reason
50+
}
51+
}
52+
return json.encode(res)
53+
end
54+
55+
function _M.access(conf, ctx)
56+
local headers = core.request.headers();
57+
local user_id = ctx.var.kratos_user_id
58+
59+
if not user_id then
60+
local res = build_json_error(500, "Internal server error", "Unable to read user-id from kratos plugin")
61+
core.log.error("unable to read user-id from kratos plugin")
62+
return 500, res
63+
end
64+
-- Get Org data
65+
local params = {
66+
method = "GET",
67+
headers = {
68+
["X-USER-ID"] = user_id,
69+
["Content-Type"] = "application/json",
70+
["Accept"] = "application/json",
71+
},
72+
keepalive = conf.keepalive,
73+
ssl_verify = conf.ssl_verify
74+
}
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
89+
end
90+
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+
104+
-- make the call - get affiliations
105+
local endpoint = conf.host .. "/affiliations"
106+
local res, err = httpc:request_uri(endpoint, params)
107+
-- return 503 if error on response or when parsing
108+
if not res then
109+
local res = build_json_error(500, "Internal server error", "Unable to get affiliations")
110+
core.log.error("Failed to get affiliations. invalid response body: ", res.body, " err: ", err)
111+
return 500, res
112+
end
113+
114+
local affiliations, err = json.decode(res.body)
115+
if not affiliations then
116+
local res = build_json_error(404, "Not found", "No affiliations found for user id: " .. user_id)
117+
return res.status, res
118+
end
119+
120+
-- Expose org_id and affiliations on variables: org_id, hub_affiliations
121+
core.ctx.register_var("org_id", function(ctx)
122+
return org.id
123+
end)
124+
125+
local affiliations = ngx.encode_base64(res.body)
126+
core.ctx.register_var("hub_affiliations", function(ctx)
127+
return affiliations
128+
end)
129+
end
130+
131+
return _M

charts/hub-gateway/plugins/kratos.lua

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
--
12
-- Licensed to the Apache Software Foundation (ASF) under one or more
23
-- contributor license agreements. See the NOTICE file distributed with
34
-- this work for additional information regarding copyright ownership.
@@ -48,7 +49,7 @@ local schema = {
4849

4950
local _M = {
5051
version = 0.1,
51-
priority = 2000,
52+
priority = 4000,
5253
name = "kratos",
5354
schema = schema,
5455
}
@@ -60,7 +61,7 @@ end
6061

6162
local function build_json_error(code, status, reason)
6263

63-
core.request.set_header("content", "application/json")
64+
core.response.set_header(ctx, "content", "application/json")
6465
local res = {
6566
error = {
6667
code = code,
@@ -121,24 +122,22 @@ function _M.access(conf, ctx)
121122

122123
-- block by default when user is not found
123124
if not res then
124-
core.log.error("failed to get user identity, err: ", err)
125-
return 403
125+
return 403, res.body
126126
end
127127

128128
-- parse the user data
129129
local data, err = json.decode(res.body)
130130
if not data then
131-
core.log.error("invalid response body: ", res.body, " err: ", err)
132-
return 503
131+
return 503, res.body
133132
end
134133

135134
-- block if user id is not found
136135
if not data.id then
137136
local reason = res.body
138137
core.log.error(reason)
139-
if ret_code == 301 then
140-
core.response.set_header("Location", conf.redirect_uri)
141-
end
138+
if ret_code == 301 then
139+
core.response.set_header("Location", conf.redirect_uri)
140+
end
142141

143142
return ret_code, reason
144143
end
@@ -147,7 +146,7 @@ function _M.access(conf, ctx)
147146
if conf.expose_user_data then
148147
local user_data = ngx.encode_base64(res.body)
149148
if not user_data then
150-
return false, 'invalid response'
149+
return 503, res.body
151150
end
152151
core.ctx.register_var("kratos_user_data", function(ctx)
153152
return user_data
@@ -156,7 +155,7 @@ function _M.access(conf, ctx)
156155

157156
-- Expose user id on $kratos_user_id variable and X-USER-ID header
158157
if conf.expose_user_id then
159-
core.request.set_header("X-USER-ID", data.identity.id)
158+
core.request.set_header(ctx, "X-USER-ID", data.identity.id)
160159
core.response.set_header("X-USER-ID", data.identity.id)
161160
core.ctx.register_var("kratos_user_id", function(ctx)
162161
return data.identity.id

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
--
12
-- Licensed to the Apache Software Foundation (ASF) under one or more
23
-- contributor license agreements. See the NOTICE file distributed with
34
-- this work for additional information regarding copyright ownership.
@@ -124,8 +125,7 @@ function _M.access(conf, ctx)
124125

125126
-- block by default when decision is unavailable
126127
if not res then
127-
core.log.error("failed to process OPA decision, err: ", err)
128-
return 403
128+
return 403, err
129129
end
130130

131131
-- parse the results of the decision

0 commit comments

Comments
 (0)