Skip to content

Commit a48bc08

Browse files
authored
Merge pull request #18 from holaplex/mpw/gateway-hydra-plugin
add hydra plugin
2 parents c1e541b + 1c46563 commit a48bc08

File tree

6 files changed

+193
-24
lines changed

6 files changed

+193
-24
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.5.0"
21+
version: "0.6.0"
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.2"
27+
appVersion: "0.3"
2828
sources:
2929
- https://github.com/holaplex/helm-charts
3030

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
--
2+
-- Licensed to the Apache Software Foundation (ASF) under one or more
3+
-- contributor license agreements. See the NOTICE file distributed with
4+
-- this work for additional information regarding copyright ownership.
5+
-- The ASF licenses this file to You under the Apache License, Version 2.0
6+
-- (the "License"); you may not use this file except in compliance with
7+
-- the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing, software
12+
-- distributed under the License is distributed on an "AS IS" BASIS,
13+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
-- See the License for the specific language governing permissions and
15+
-- limitations under the License.
16+
--
17+
local core = require("apisix.core")
18+
local http = require("resty.http")
19+
local json = require("apisix.core.json")
20+
21+
local schema = {
22+
type = "object",
23+
properties = {
24+
host = {
25+
type = "string"
26+
},
27+
ssl_verify = {
28+
type = "boolean",
29+
default = true
30+
},
31+
timeout = {
32+
type = "integer",
33+
minimum = 1,
34+
maximum = 60000,
35+
default = 3000,
36+
description = "timeout in milliseconds"
37+
},
38+
keepalive = {
39+
type = "boolean",
40+
default = true
41+
},
42+
keepalive_timeout = {
43+
type = "integer",
44+
minimum = 1000,
45+
default = 60000
46+
},
47+
keepalive_pool = {
48+
type = "integer",
49+
minimum = 1,
50+
default = 5
51+
},
52+
expose_client_id = {
53+
type = "boolean",
54+
default = false
55+
},
56+
},
57+
required = {"host"}
58+
}
59+
60+
local _M = {
61+
version = 0.1,
62+
priority = 3,
63+
name = "oauth2",
64+
schema = schema
65+
}
66+
67+
function _M.check_schema(conf)
68+
return core.schema.check(schema, conf)
69+
end
70+
71+
function _M.access(conf, ctx)
72+
local api_token = core.request.header(ctx, "Authorization")
73+
74+
if not api_token then
75+
return 401, json.encode({
76+
message = "Authorization header not found"
77+
})
78+
end
79+
80+
local params = {
81+
method = "POST",
82+
body = "token=" .. api_token,
83+
headers = {
84+
["Content-Type"] = "application/x-www-form-urlencoded"
85+
},
86+
keepalive = conf.keepalive,
87+
ssl_verify = conf.ssl_verify
88+
}
89+
90+
if conf.keepalive then
91+
params.keepalive_timeout = conf.keepalive_timeout
92+
params.keepalive_pool = conf.keepalive_pool
93+
end
94+
95+
local endpoint = conf.host .. "/admin/oauth2/introspect"
96+
97+
local httpc = http.new()
98+
httpc:set_timeout(conf.timeout)
99+
local res, err = httpc:request_uri(endpoint, params)
100+
101+
-- block by default when introspection failed
102+
if not res then
103+
return 401, json.encode({
104+
message = err
105+
})
106+
end
107+
108+
-- parse the introspection data
109+
local data, err = json.decode(res.body)
110+
if not data then
111+
return 401, err
112+
end
113+
114+
-- block if token is not active
115+
if not data.active then
116+
return 401, json.encode({
117+
message = "Authorization token is not valid anymore. Please get a new one from the Hub web UI"
118+
})
119+
end
120+
121+
-- Expose hydra_client_id id on $hydra_client_id variable
122+
if conf.expose_client_id then
123+
core.request.set_header(ctx, "X-CLIENT-ID", data.client_id)
124+
core.response.set_header("X-CLIENT-ID", data.client_id)
125+
core.ctx.register_var("hydra_client_id", function(ctx)
126+
return data.client_id
127+
end)
128+
end
129+
end
130+
131+
return _M
File renamed without changes.

charts/hub-gateway/templates/apisixroute.yaml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,26 @@ spec:
2727
methods:
2828
{{- .methods | toYaml | nindent 8 }}
2929
plugins:
30-
{{- with .kratos }}
30+
{{- with .session }}
3131
{{- if .enabled | default false }}
32-
- name: kratos
32+
- name: session
3333
enable: true
3434
config:
35-
host: {{ print "http://" $apisixPlugins.kratos.serviceName "." $namespace ".svc:" $apisixPlugins.kratos.servicePort | quote }}
35+
host: {{ print "http://" $apisixPlugins.session.serviceName "." $namespace ".svc:" $apisixPlugins.session.servicePort | quote }}
3636
expose_user_data: true
3737
expose_user_id: true
3838
session_cookie_name: {{ $sessionCookie }}
3939
{{- end }}
4040
{{- end }}
41+
{{- with .oauth2 }}
42+
{{- if .enabled | default false }}
43+
- name: oauth2
44+
enable: true
45+
config:
46+
host: {{ print "http://" $apisixPlugins.oauth2.serviceName "." $namespace ".svc:" $apisixPlugins.oauth2.servicePort | quote }}
47+
expose_client_id: true
48+
{{- end }}
49+
{{- end }}
4150
{{- with .sessionRedirect }}
4251
{{- if .enabled }}
4352
- name: session-redirect
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
{{- $files := .Files }}
2+
{{- range $key, $val := .Values.apisixPlugins }}
13
apiVersion: v1
24
kind: ConfigMap
35
metadata:
4-
name: apisix-custom-plugins
6+
name: {{ print $key "-plugin" | kebabcase | lower }}
57
labels:
68
{{- include "hub-gateway.labels" $ | nindent 4 }}
79
data:
8-
{{- $files := .Files }}
9-
{{- range $key, $val := .Values.apisixPlugins }}
1010
{{- with $val.files}}
1111
{{- range . }}
1212
{{ (splitList "/" .) | last | nindent 2}}: |-
1313
{{ $.Files.Get . | nindent 8 }}
1414
{{- end }}
1515
{{- end }}
16-
{{- end }}
16+
---
17+
{{- end }}

charts/hub-gateway/values.yaml

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ routes:
1414
methods:
1515
- POST
1616
- OPTIONS
17-
kratos:
17+
oauth2:
1818
enabled: true
1919
regexUri:
2020
- "/graphql"
@@ -29,7 +29,7 @@ routes:
2929
methods:
3030
- POST
3131
- OPTIONS
32-
kratos:
32+
session:
3333
enabled: true
3434
regexUri:
3535
- "/graphql"
@@ -44,7 +44,7 @@ routes:
4444
methods:
4545
- POST
4646
- OPTIONS
47-
kratos:
47+
session:
4848
enabled: true
4949
sessionJson:
5050
enabled: true
@@ -61,7 +61,7 @@ routes:
6161
- /browser/organizations/*
6262
methods:
6363
- POST
64-
kratos:
64+
session:
6565
enabled: true
6666
sessionRedirect:
6767
enabled: true
@@ -70,7 +70,7 @@ routes:
7070
subdomain: hub
7171
serviceName: hub
7272
servicePort: 80
73-
kratos:
73+
session:
7474
enabled: true
7575
sessionRedirect:
7676
enabled: true
@@ -93,7 +93,7 @@ routes:
9393
subdomain: hub
9494
serviceName: hub
9595
servicePort: 80
96-
kratos:
96+
session:
9797
enabled: true
9898
sessionRedirect:
9999
enabled: true
@@ -124,12 +124,29 @@ routes:
124124
- /__nextjs_original-stack-frame
125125
- /api/.ory/*
126126

127+
- name: hub-auth-api
128+
serviceName: hydra-public
129+
servicePort: 4444
130+
subdomain: api
131+
paths:
132+
- /auth
133+
methods:
134+
- POST
135+
regexUri:
136+
- "/auth"
137+
- "/oauth2/token"
138+
127139
apisixPlugins:
128-
kratos:
140+
session:
129141
serviceName: kratos-public
130142
servicePort: 80
131143
files:
132-
- plugins/kratos.lua
144+
- plugins/session.lua
145+
oauth2:
146+
serviceName: hydra-admin
147+
servicePort: 4445
148+
files:
149+
- plugins/oauth2.lua
133150
sessionRedirect:
134151
files:
135152
- plugins/session-redirect.lua
@@ -190,9 +207,6 @@ apisix:
190207
viewer: VduoK1H4ujZB4nus9QEfOjgUjTGkmB
191208

192209
plugins:
193-
- kratos
194-
- session-redirect
195-
- session-json
196210
- mocking
197211
- cors
198212
- redirect
@@ -204,14 +218,28 @@ apisix:
204218
customPlugins:
205219
enabled: true
206220
plugins:
207-
- name: "kratos"
221+
- name: "session"
208222
configMap:
209-
name: "apisix-custom-plugins"
223+
name: "session-plugin"
224+
mounts:
225+
- key: "session.lua"
226+
path: "/opts/custom_plugins/apisix/plugins/session.lua"
227+
- name: "oauth2"
228+
configMap:
229+
name: "oauth2-plugin"
230+
mounts:
231+
- key: "oauth2.lua"
232+
path: "/opts/custom_plugins/apisix/plugins/oauth2.lua"
233+
- name: "session-redirect"
234+
configMap:
235+
name: "session-redirect-plugin"
210236
mounts:
211-
- key: "kratos.lua"
212-
path: "/opts/custom_plugins/apisix/plugins/kratos.lua"
213237
- key: "session-redirect.lua"
214238
path: "/opts/custom_plugins/apisix/plugins/session-redirect.lua"
239+
- name: "session-json"
240+
configMap:
241+
name: "session-json-plugin"
242+
mounts:
215243
- key: "session-json.lua"
216244
path: "/opts/custom_plugins/apisix/plugins/session-json.lua"
217245

0 commit comments

Comments
 (0)