Skip to content

Commit af04b7e

Browse files
authored
use schema level rest (#86)
* use schema level rest to better show how this would be used in ci/cd * Apply suggestions from code review
1 parent 7f9dab0 commit af04b7e

File tree

5 files changed

+75
-14
lines changed

5 files changed

+75
-14
lines changed

rest/tls/README.md

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ For more information on using TLS in your REST apis and other services [see our
77

88
This examples demonstrates a number of API Connect for GraphQL capabilities:
99
- Use of `@rest(tls:)`
10+
- Use of schema level `@rest`
1011
- stepzen service
1112
- Simple ecmascript capability for reshaping data.
13+
- config.yaml usage for `@rest` using process for clean CI/CD
1214

1315
## mTLS and certificates
1416

@@ -22,26 +24,56 @@ When the tls entry is given the name of a configuration entry, you can provide
2224
- `key` - the client certifcate key
2325
The data should be in PEM format.
2426

25-
In our examples, we have two configuration: `selfsign` with a `ca` entry and `selfsignedmtls` with all three entries.
27+
In our examples, we have two configuration: `myserver`(with a
28+
selfsigned cert) with a `ca` entry and `mymtlsserver` requiring
29+
mtls)with all three entries.
30+
31+
The same configuration entry is used
32+
for both tls and server configuration, so you'll see
33+
`@rest(tls:"myserver" configuration:"myserver")`.
34+
35+
If the tls `ca` was a shared private `ca`, you'd want to have a common
36+
`tls` config entry for all servers so you'd probably put them into two
37+
different configuration entries
2638

2739
TLS 1.2 and 1.3 are supported. TLS 1.0 and 1.1 are not supported.
2840

41+
### Schema `@rest`
42+
43+
You can definte a common `@rest(name:)` profile as a schema level definition to be shared as `@rest(use:)`.
44+
45+
This allows for common configuraiton to be creatd and shared.
46+
47+
```
48+
schema
49+
...
50+
@rest(name: "commonserver", endpoint: "$url;", tls: "myserver" configuration: "myserver") {
51+
query: Query
52+
}
53+
```
54+
create a `commonserver` profile with endpoint, tls, and configuration.
55+
56+
When `@rest(use:"commonserver")` is encountered, those arguments will be applied.
57+
58+
Since there is only a single `@rest` definition, this is a contrivied example, but the utility should be clear especially for CI/CD.
59+
60+
2961
## Try it out!
3062

31-
`rest_self` in tls.graphql provides an example of self signed certificates by pointing to the `selfsign` resource in config.yaml. That configuration contains `ca: STEPZEN_SERVER_CRT`
63+
`rest_self` in tls.graphql provides an example of self signed certificates by pointing to the `myserver` resource in config.yaml. That configuration contains `ca: STEPZEN_SERVER_CRT`
3264
During `stepzen deploy`, the `STEPZEN_SERVER_CRT` environment variable is expanded and the result will be a yaml that looks like:
3365
```
3466
configurationset:
3567
- configuration:
36-
name: selfsign
68+
name: myserver
3769
ca: |
3870
-----BEGIN CERTIFICATE-----
3971
MIIF5zCCA8+gAwIBAgIUS2BwtghuA7PREQ5AWzOeeT+tCe4wDQYJKoZIhvcNAQEL
4072
...
4173
-----END CERTIFICATE-----
4274
```
4375

44-
The `selfsignedmtls` configuration contains an example mutual TLS configuration.
76+
The `mymtlsserver` configuration contains an example mutual TLS configuration.
4577

4678
Two safe approaches are to set the environment variables from secrets or to have a `.env` file.
4779

@@ -88,6 +120,14 @@ By default, `rest_self` uses `host.docker.internal` which works in most modern
88120

89121
For Podman, you may need to change this to `host.containers.internal` or `localhost` depending on your podman defaults. You may also need to modify your podman default configuration to allow for such access.
90122

123+
We apply a layer of indirection for the endpoint. The actual value is held in the configuation as `url` and that is actually obtained from the environment variable `STEPZEN_SERVER_URL`. This allows for CI/CD process to inject the root URL by setting the env variable.
124+
125+
Here, this can be done by running, replacing the `SERVER_URL` with the value appropriate to your container runtime toolset.
126+
```
127+
(cd tests; make env SERVER_URL="http://localhost:8443")
128+
```
129+
This won't overwrite an existing .env, so remove it if already exists.
130+
91131
### env variables
92132

93133
You can set `STEPZEN_*` env variables in .env or using export.
@@ -136,11 +176,16 @@ that it is as you expected.
136176

137177
You are looking for something that looks like:
138178
```
139-
"configuration":{"configurationset":[{"configuration":{"name":"selfsign","ca":
179+
"configuration":{"configurationset":[{"configuration":{"name":"myserver","ca":
140180
"-----BEGIN CERTIFICATE-----\nMIIF5zCCA8+gAwIBAgIUS2BwtghuA7PREQ5AWzOeeT+tCe4wDQYJKoZIhvcNAQEL\nBQAwbTE...
141181
```
142182
notice the '\n'. If you see spaces you've done something wrong upsteram.
143183

184+
### Reserved environment variables
185+
186+
`STEPZEN_SERVER_URL` is used by the stepzen cli to identify the API Connect for GraphQL server and should be avoided to prevent stepzen cli issues.
187+
188+
Here's a full list of stepzen cli special variables you should avoid using.
144189

145190
### Debugging
146191
You can debug using stepzen request by adding `-H "stepzen-debug-level: 1"`

rest/tls/config.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
# provide values through environment variables to mimimize secret exposure and
2+
# adhere to clean ci/cd process. env variables/secrets should be injected
13
configurationset:
24
- configuration:
3-
name: selfsign
5+
name: myserver
46
ca: STEPZEN_SERVER_CRT
7+
url: STEPZEN_SERVICE_URL
58
- configuration:
6-
name: selfsignedmtls
9+
name: mymtlsserver
710
ca: STEPZEN_SERVER_CRT
811
cert: STEPZEN_CLIENT_CRT
912
key: STEPZEN_CLIENT_KEY
13+
url: STEPZEN_SERVICE_URL

rest/tls/index.graphql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
schema @sdl(files: ["tls.graphql"]) {
1+
schema
2+
@sdl(files: ["tls.graphql"])
3+
@rest(name: "commonserver", endpoint: "$url;", tls: "myserver" configuration: "myserver") {
24
query: Query
35
}

rest/tls/tests/Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Makefile to build and validate a pair of *example* self-signed certificates for *simple* tests
22

3+
# alternate endpoint settings for different container toolsets
4+
# hostname: "https://host.docker.internal:8443"
5+
# hostname: "https://localhost:8443"
6+
# hostname: "https://host.rancher-desktop.internal:8443"
7+
SERVER_URL:=https://host.docker.internal:8443
8+
39
# enable to debug ssl server
410
# DEBUG:=-debug
511
all: client.crt server.crt env
@@ -27,6 +33,7 @@ clean:
2733
env: ../.env
2834

2935
../.env: client.crt server.crt
30-
( echo STEPZEN_CLIENT_CRT=\""`cat client.crt`"\"; \
36+
( echo STEPZEN_SERVICE_URL=\"$(SERVER_URL)\"; \
37+
echo STEPZEN_CLIENT_CRT=\""`cat client.crt`"\"; \
3138
echo STEPZEN_CLIENT_KEY=\""`cat client.key`"\"; \
3239
echo STEPZEN_SERVER_CRT=\""`cat server.crt`"\") > ../.env

rest/tls/tls.graphql

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
type Query {
22
"""
3+
typically, there will be a common set of server configurations that are shared among many
4+
different `@rest` calls, so we actually specify the `@rest(tls:)` in a schema level
5+
`@rest` definition called `commonserver` in index.graphql
6+
7+
`@rest(use:"commonserver")` picks up the `@rest(tls:)` and the `@rest(endpoint:)`
8+
39
will contact localhost using host.docker.internal and 8443 and selfsign configuration
410
the ecmascript is used to repackage any content coming back (openssl s_server returns html)
511
"""
612
rest_self: JSON
713
@rest(
8-
endpoint: "https://host.docker.internal:8443/"
9-
# alternate endpoint settings for different container toolsets
10-
# endpoint: "https://localhost:8443/"
11-
# endpoint: "https://host.rancher-desktop.internal:8443/"
12-
tls: "selfsign"
14+
use: "commonserver"
15+
path: "/path"
1316
ecmascript: """
1417
function transformREST(s) {
1518
return JSON.stringify(

0 commit comments

Comments
 (0)