Skip to content

Commit 8acb353

Browse files
author
ikethecoder
authored
Edge route cleanup and gwa cli improvements (#4)
1 parent 55fb087 commit 8acb353

File tree

18 files changed

+581
-137
lines changed

18 files changed

+581
-137
lines changed

README.md

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ All APIs are protected by an OIDC JWT Token with the following claims:
1818
| -------- | ----------- | ------- |
1919
| `PORT` | Port | `2000` |
2020
| `LOG_LEVEL` | Log level for the application | `INFO` |
21-
| `ENVIRONMENT` | Indicates what environment config to use | `production` |
21+
| `ENVIRONMENT` | Indicates what environment config to use (development|test|production) | `production` |
2222
| `CONFIG_PATH` | Location of the config | `/tmp/production.json` |
2323
| `OIDC_BASE_URL` | Base url used for OIDC Discovery for getting the `jwks_uri` for the list of supported keys. | `https://keycloak.domain/auth/realms/abc`
2424
| `TOKEN_MATCH_AUD` | The `audience` that the token must match. | `gwa`
@@ -31,6 +31,9 @@ All APIs are protected by an OIDC JWT Token with the following claims:
3131
| `KC_USER_REALM` | Keycloak access for administrative rights to manage groups for namespaces | `master`
3232
| `KC_USERNAME` | Keycloak access for administrative rights to manage groups for namespaces | `kcadmin`
3333
| `KC_PASSWORD` | Keycloak access for administrative rights to manage groups for namespaces | `xxx`
34+
| `HOST_TRANSFORM_ENABLED` | For Dev and Test a way to transform the host for working in these environments | `false`
35+
| `HOST_TRANSFORM_BASE_URL` | For Dev and Test a way to transform the host for working in these environments |
36+
3437

3538
## Gateway API
3639

@@ -59,7 +62,7 @@ access: read, write
5962

6063
A `namespace` represents a collections of Kong Services and Routes that are managed independently.
6164

62-
To create a new namespace, go to the <a href="https://gwa-qwzrwc-dev.pathfinder.gov.bc.ca/int" target="_blank">API Services Portal</a>.
65+
To create a new namespace, go to the <a href="https://gwa-qwzrwc-test.pathfinder.gov.bc.ca/int" target="_blank">API Services Portal</a>.
6366

6467
After login and selection of an existing namespace, go to the `New Namespace` tab and click the `Create Namespace` button.
6568

@@ -72,31 +75,23 @@ Logout by clicking your username at the top right of the page. When you login a
7275
Go to the `Service Accounts` tab and click the `Create Service Account`. A new credential will be created - make a note of the `ID` and `Secret`.
7376

7477
With scopes:
75-
* admin:gateway
76-
* admin:acl
77-
* admin:catalog
78-
79-
## 3. Prepare and apply gateway configuration
80-
81-
The Swagger console for the `gwa-api` can be used to publish Kong Gateway configuration, or the `gwa-cli` can be used.
82-
83-
### Swagger Console
78+
* `admin:gateway` : Permission to publish gateway configuration to Kong
79+
* `admin:acl` : Permission to update the Access Control List for controlling access to viewing metrics, service configuration and service account management
80+
* `admin:catalog` : Permission to update BC Data Catalog datasets for describing APIs available for consumption
8481

85-
Go to <a href="https://gwa-api-qwzrwc-dev.pathfinder.gov.bc.ca/api/doc" target="_blank">gwa-api Swagger Console</a>.
82+
## 3. Prepare configuration
8683

87-
Select the `PUT` `/namespaces/{namespace}/gateway` API.
84+
The gateway configuration can be hand-crafted or you can use the `gwa` `new` command to walk you through the creation of the config.
8885

89-
The Service Account uses the OAuth2 Client Credentials Grant Flow. Click the `lock` link on the right and enter in the Service Account credentials that were generated in step #2.
90-
91-
For the `Parameter namespace`, enter the namespace that you created in step #1.
86+
To view a list of available plugins, you can run: `gwa plugins`.
9287

93-
Select `dryRun` to `true`.
88+
To view examples go [here](/docs/samples/service-plugins).
9489

95-
Select a `configFile` file. An example of a very minimal config is:
90+
**Simple Example**
9691

9792
```
9893
export NS="my_namespace"
99-
export NAME="some-service-name-$NS"
94+
export NAME="a-service-for-$NS"
10095
echo "
10196
services:
10297
- name: $NAME
@@ -109,16 +104,39 @@ services:
109104
- name: $NAME-route
110105
tags: [ ns.$NS ]
111106
hosts:
112-
- $NAME.api.333223.xyz
107+
- $NAME.api.189768.xyz
113108
paths:
114109
- /
115110
strip_path: false
116111
https_redirect_status_code: 426
117112
path_handling: v0
118113
" > sample.yaml
119-
120114
```
121115

116+
**gwa CLI Example**
117+
118+
Run: `gwa new` and follow the prompts.
119+
120+
## 4. Apply gateway configuration
121+
122+
The Swagger console for the `gwa-api` can be used to publish Kong Gateway configuration, or the `gwa-cli` can be used.
123+
124+
### Swagger Console
125+
126+
Go to <a href="https://gwa-api-qwzrwc-test.pathfinder.gov.bc.ca/api/doc" target="_blank">gwa-api Swagger Console</a>.
127+
128+
Select the `PUT` `/namespaces/{namespace}/gateway` API.
129+
130+
The Service Account uses the OAuth2 Client Credentials Grant Flow. Click the `lock` link on the right and enter in the Service Account credentials that were generated in step #2.
131+
132+
For the `Parameter namespace`, enter the namespace that you created in step #1.
133+
134+
Select `dryRun` to `true`.
135+
136+
Select a `configFile` file.
137+
138+
Send the request.
139+
122140
### Command Line
123141

124142
**Install**
@@ -131,42 +149,56 @@ npm run build
131149
npm link
132150
```
133151

152+
**Configure**
153+
134154
Create a `.env` file and update the CLIENT_ID and CLIENT_SECRET with the new credentials that were generated in step #2:
135155

136156
```
137157
echo "
138-
AUTHORIZATION_ENDPOINT=https://auth-qwzrwc-dev.pathfinder.gov.bc.ca/auth/realms/aps/protocol/openid-connect/token
139-
CLIENT_ID=sa-xxxx-g9ah2ess3y
140-
CLIENT_SECRET=0000-0000-0000-0000
141-
API_HOST=https://gwa-api-qwzrwc-dev.pathfinder.gov.bc.ca/v1
158+
GWA_NAMESPACE=$NS
159+
CLIENT_ID=<YOUR SERVICE ACCOUNT ID>
160+
CLIENT_SECRET=<YOUR SERVICE ACCOUNT SECRET>
161+
GWA_ENV=test
142162
" > .env
163+
164+
OR run:
165+
166+
gwa init -T --namespace=$NS --client-id=<YOUR SERVICE ACCOUNT ID> --client-secret=<YOUR SERVICE ACCOUNT SECRET>
167+
143168
```
144169

170+
**Publish**
171+
145172
```
146-
gwa pg --namespace $NS sample.yaml
173+
gwa pg sample.yaml
147174
```
148175

149-
## 4. Verify routes
176+
## 5. Verify routes
150177

151178
```
152-
curl https://$NAME.api.333223.xyz/headers
179+
curl https://$NAME.api.189768.xyz/headers
180+
181+
ab -n 20 -c 2 https://$NAME.api.189768.xyz/headers
182+
153183
```
154184

155-
## 5. View metrics
185+
## 6. View metrics
156186

157-
Go to <a href="https://grafana-qwzrwc-dev.pathfinder.gov.bc.ca/" target="_blank">Grafana</a> to view metrics for your configured services.
187+
Go to <a href="https://grafana-qwzrwc-test.pathfinder.gov.bc.ca/" target="_blank">Grafana</a> to view metrics for your configured services.
158188

159189

160-
## 6. Grant access to others
190+
## 7. Grant access to others
161191

162192
The `acl` command is an all-inclusive membership list, so the `--users` should have the full list of members. Any user that is a member but not in the `--users` list will be removed from the namespace.
163193

194+
For administrative privileges (such as managing Service Accounts), add the usernames to the `--managers` argument.
195+
164196
```
165-
gwa acl --namespace $NS --users acope@idir,jjones@idir
197+
gwa acl --managers acope@idir --users acope@idir jjones@idir
166198
```
167199

168-
## 7. Add to your CI/CD Pipeline
200+
## 8. Add to your CI/CD Pipeline
169201

170202
Update your CI/CD pipelines to run the `gwa-cli` to keep your services updated on the gateway.
171203

172-
204+
> TODO: Examples
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
services:
3+
- name: MY_REST_API
4+
tags: [ _NS_ ]
5+
plugins:
6+
- name: bcgov-gwa-endpoint
7+
tags: [ _NS_ ]
8+
enabled: true
9+
config:
10+
api_owners: [ CONSUMER_OWNER_1, CONSUMER_OWNER_2 ]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
services:
3+
- name: MY_REST_API
4+
tags: [ _NS_ ]
5+
plugins:
6+
- name: gwa-ip-anonymity
7+
tags: [ _NS_ ]
8+
enabled: true
9+
config:
10+
ipv4_mask: 0
11+
ipv6_mask: 0
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
services:
3+
- name: MY_REST_API
4+
tags: [ _NS_ ]
5+
plugins:
6+
- name: ip-restriction
7+
tags: [ _NS_ ]
8+
enabled: true
9+
config:
10+
allow: [ "10.10.10.0/24" ]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
services:
3+
- name: MY_REST_API
4+
tags: [ _NS_ ]
5+
plugins:
6+
- name: oidc
7+
tags: [ _NS_ ]
8+
enabled: true
9+
config:
10+
response_type: "code"
11+
introspection_endpoint: "https://{HOST}/auth/realms/{REALM}/protocol/openid-connect/token/introspect"
12+
filters: null
13+
bearer_only: "no"
14+
ssl_verify: "no"
15+
session_secret: null
16+
introspection_endpoint_auth_method: null
17+
realm: "kong"
18+
redirect_after_logout_uri: "/"
19+
scope: "openid"
20+
token_endpoint_auth_method: "client_secret_post"
21+
logout_path: "/logout"
22+
client_id: ""
23+
client_secret: ""
24+
discovery: "https://{HOST}/auth/realms/{REALM}/.well-known/openid-configuration"
25+
recovery_page_path: null
26+
redirect_uri_path: null
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
services:
3+
- name: MY_REST_API
4+
tags: [ _NS_ ]
5+
plugins:
6+
- name: rate-limiting
7+
tags: [ _NS_ ]
8+
enabled: true
9+
config:
10+
fault_tolerant: true
11+
hide_client_headers: false
12+
limit_by: consumer
13+
minute: 10
14+
policy: cluster
15+
header_name: null
16+
second: null
17+
hour: null
18+
day: null
19+
month: null
20+
year: null
21+
redis_database: 0
22+
redis_host: null
23+
redis_password: null
24+
redis_port: 6379
25+
redis_timeout: 2000
26+
enabled: true
27+
protocols:
28+
- http
29+
- https
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
services:
3+
- name: MY_REST_API
4+
tags: [ _NS_ ]
5+
plugins:
6+
- name: kong-spec-expose
7+
tags: [ _NS_ ]
8+
enabled: true
9+
config:
10+
spec_url: "https://raw.githubusercontent.com/bcgov/api-specs/master/gwells/gwells.yaml"

microservices/gatewayApi/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ docker run -ti --rm \
3636
-e KC_PASSWORD="SdufuSYnFAANnluWrAH0waHavE9YWdCu" \
3737
-e KC_USER_REALM=master \
3838
-e KC_CLIENT_ID=admin-cli \
39+
-v `pwd`/_tmp:/ssl \
40+
-v ~/.kube/config:/root/.kube/config \
3941
--add-host=docker:$hostip -p 2000:2000 gwa_kong_api
4042
```
4143

microservices/gatewayApi/auth/authz.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from flask import g, abort, make_response, jsonify
22

3-
def group_root_name():
3+
def users_group_root():
44
return 'ns'
55

6-
def group_root():
7-
return '/%s' % group_root_name()
6+
def admins_group_root():
7+
return 'ns-admins'
88

99
def ns_claim():
1010
return 'namespace'
@@ -18,7 +18,7 @@ def enforce_authorization(namespace):
1818
# Make sure namespace matches the one in the claim
1919
# It can be in two formats: '/ns/<namespace>' or '<namespace>'
2020
ns = g.principal[the_ns_claim]
21-
if ns != namespace and ns != ('%s/%s' % (group_root(), namespace)):
21+
if ns != namespace and ns != ('/%s/%s' % (users_group_root(), namespace)):
2222
abort(make_response(jsonify(error="Not authorized to use %s namespace." % namespace), 403))
2323

2424
def enforce_role_authorization(role):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
# Access the Kong Admin API for details about the Kong configuration
3+
#
4+
# Use the Route Hosts found in Kong to ensure there are no conflicts

0 commit comments

Comments
 (0)