Skip to content

Commit 082b429

Browse files
committed
provision access point gw config
1 parent 0993f15 commit 082b429

File tree

1 file changed

+195
-1
lines changed

1 file changed

+195
-1
lines changed

microservices/gatewayApi/patterns/sdx/access_point_r1.py

Lines changed: 195 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
allow: [ ${mtls_allow_list} ]
3535
routes:
3636
- name: ${service_name}.DENY
37-
tags: [ns.${gateway}.${ns_qualifier}]
37+
tags: [ns.${gateway}.${ns_qualifier}, sdx]
3838
hosts:
3939
- ${route_host}
4040
paths:
@@ -57,6 +57,200 @@
5757
status_code: 401
5858
message: "Access Denied. Route not found."
5959
60+
- name: ${service_name}-CONSOLE
61+
url: http://sdx-demo-ui-lab-generic-api
62+
tags: [ns.${gateway}.${ns_qualifier}]
63+
routes:
64+
- name: ${service_name}-CONSOLE
65+
tags: [ns.${gateway}.${ns_qualifier}, sdx]
66+
hosts:
67+
- sdx.api.gov.bc.ca
68+
paths:
69+
- /console
70+
methods:
71+
- GET
72+
strip_path: true
73+
https_redirect_status_code: 426
74+
path_handling: v0
75+
request_buffering: true
76+
response_buffering: true
77+
78+
- name: ${service_name}-CONSOLE-DS
79+
url: https://api-gov-bc-ca-lab.dev.api.gov.bc.ca/
80+
tags: [ns.${gateway}.${ns_qualifier}]
81+
routes:
82+
- name: ${service_name}-CONSOLE-DS
83+
tags: [ns.${gateway}.${ns_qualifier}, sdx]
84+
hosts:
85+
- ${route_host}
86+
paths:
87+
- /api/ds
88+
methods: [GET,PUT,POST,DELETE]
89+
strip_path: true
90+
preserve_host: false
91+
92+
- name: ${service_name}-CONSOLE-RD
93+
url: https://bcgov.github.io/sdx-openapi/data/lab
94+
tags: [ns.${gateway}.${ns_qualifier}]
95+
routes:
96+
- name: ${service_name}-CONSOLE-RD
97+
tags: [ns.${gateway}.${ns_qualifier}, sdx]
98+
hosts:
99+
- ${route_host}
100+
paths:
101+
- /api/rd
102+
methods: [GET]
103+
strip_path: true
104+
preserve_host: false
105+
106+
plugins:
107+
- name: pre-function
108+
tags: [ns.${gateway}.${ns_qualifier}]
109+
config:
110+
access:
111+
- |
112+
-- Kong pre-function to rewrite the request path for /api/rd/{id} to /{id}.json
113+
-- This function captures the {id} parameter and rewrites the path accordingly
114+
115+
-- Get the original request path
116+
local original_path = ngx.var.request_uri
117+
118+
-- Use a pattern to extract the {id} from the path
119+
local id = original_path:match("/api/rd/(.+)")
120+
121+
if id then
122+
-- Construct the new path by appending .json to the extracted id
123+
-- prepend current service path
124+
local service = kong.router.get_service()
125+
126+
local new_path = service.path .. "/" .. id .. ".json"
127+
128+
kong.service.request.set_path(new_path)
129+
130+
-- Optionally, log the path rewrite for debugging
131+
ngx.log(ngx.WARN, "Rewritten path from ", original_path, " to ", new_path)
132+
else
133+
-- If no id is found, log a warning (optional)
134+
ngx.log(ngx.WARN, "No ID found in the request path: ", original_path)
135+
end
136+
137+
- name: ${service_name}-AUTH
138+
url: https://httpbin.org
139+
tags: [ns.${gateway}.${ns_qualifier}]
140+
tls_verify: false
141+
routes:
142+
- name: ${service_name}-AUTH
143+
tags: [ns.${gateway}.${ns_qualifier}, sdx]
144+
hosts:
145+
- ${route_host}
146+
paths:
147+
- /auth
148+
methods:
149+
- POST
150+
- OPTIONS
151+
strip_path: false
152+
preserve_host: false
153+
https_redirect_status_code: 426
154+
path_handling: v0
155+
request_buffering: true
156+
response_buffering: true
157+
plugins:
158+
- name: cors
159+
tags: [ns.${gateway}.${ns_qualifier}]
160+
enabled: true
161+
config:
162+
origins:
163+
- "*"
164+
methods:
165+
- GET
166+
- POST
167+
- OPTIONS
168+
headers:
169+
- Accept
170+
- Authorization
171+
- Content-Type
172+
- If-None-Match
173+
- X-Client-Id
174+
- DPoP
175+
176+
- name: pre-function
177+
tags: [ns.${gateway}.${ns_qualifier}]
178+
enabled: true
179+
config:
180+
access:
181+
- |
182+
local client_cert_path = "/etc/secrets/kong-client-tls/tls.crt"
183+
local client_key_path = "/etc/secrets/kong-client-tls/tls.key"
184+
185+
local io = require "io"
186+
local ssl = require('ngx.ssl')
187+
188+
local http = require "resty.http"
189+
local cjson = require "cjson.safe"
190+
191+
local httpc = http.new()
192+
local req_body = kong.request.get_raw_body()
193+
194+
if req_body then
195+
-- Process the raw body string
196+
kong.log.info("Request body: ", req_body)
197+
end
198+
199+
local function read_file(filename)
200+
local file = io.open(filename, "r")
201+
if not file then
202+
print("Error: Could not open file " .. filename)
203+
return nil
204+
end
205+
206+
local content = file:read("*all") -- Read entire file
207+
file:close()
208+
return content
209+
end
210+
211+
local config = {
212+
-- Server details
213+
host = "sdx-authz-apps-gov-bc-ca-lab.apps.gov.bc.ca",
214+
port = 443,
215+
path = "/auth/realms/sdx/protocol/openid-connect/token",
216+
217+
-- Client certificate files
218+
cert_file = assert(ssl.parse_pem_cert(read_file(client_cert_path))),
219+
key_file = assert(ssl.parse_pem_priv_key(read_file(client_key_path))),
220+
221+
-- Request data
222+
post_data = req_body,
223+
content_type = "application/x-www-form-urlencoded"
224+
}
225+
226+
if not config.cert_file or not config.key_file then
227+
print("Failed to load certificates as cdata")
228+
return nil
229+
end
230+
231+
local res, err = httpc:request_uri(
232+
"https://" .. config.host .. ":" .. config.port .. config.path,
233+
{
234+
method = "POST",
235+
headers = {
236+
["Content-Type"] = config.content_type,
237+
["Accept"] = "application/json",
238+
["DPoP"] = kong.request.get_header("DPoP")
239+
},
240+
body = config.post_data,
241+
ssl_verify = true,
242+
ssl_client_cert = config.cert_file,
243+
ssl_client_priv_key = config.key_file
244+
}
245+
)
246+
247+
if not res then
248+
return kong.response.exit(502, "Upstream request failed: " .. (err or "unknown error"))
249+
end
250+
251+
kong.response.set_header("Content-Type", res.headers["Content-Type"] or "application/json")
252+
return kong.response.exit(res.status, res.body)
253+
60254
""")
61255

62256
def eval_access_point_pattern (context):

0 commit comments

Comments
 (0)