Skip to content

Commit 55a0131

Browse files
Merge pull request #21 from FireTail-io/allow-undefined-routes
Allow undefined routes
2 parents d9aee41 + 6b709f2 commit 55a0131

File tree

5 files changed

+27
-47
lines changed

5 files changed

+27
-47
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ To setup the Firetail NGINX Module, you will need to modify your `nginx.conf` to
2929
load_module modules/ngx_firetail_module.so;
3030
```
3131

32-
You can then use the `firetail_api_token` directive to provide your Firetail logging API token inside a http block like so:
32+
You can then configure it using the following directives, which all belong in the `http` block of your NGINX configuration:
3333

34-
```
35-
firetail_api_token "YOUR-API-TOKEN";
36-
```
34+
| Directive | Description | Example |
35+
| --------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
36+
| `firetail_api_token` | Your API token from the FireTail platform | `PS-02-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx` |
37+
| `firetail_url` | The URL of the API endpoint the FireTail NGINX module will send logs to. | `https://api.logging.eu-west-1.prod.firetail.app/logs/bulk` |
38+
| `firetail_allow_undefined_routes` | If set to `1`, `t`, `T`, `TRUE`, `true`, or `True`, requests to routes not defined in your OpenAPI specification will not be blocked. | `1`, `t`, `T`, `TRUE`, `true`, `True`, `0`, `f`, `F`, `FALSE`, `false`, `False` |
3739

38-
See [dev/nginx.conf](./dev/nginx.conf) for an example of this in action.
40+
See [dev/nginx.conf](./dev/nginx.conf) for an example of these in use.
3941

4042
You should use a module such as the [ngx_http_lua_module](https://github.com/openresty/lua-nginx-module) to avoid placing plaintext credentials in your `nginx.conf`, and instead make use of [system environment variables](https://github.com/openresty/lua-nginx-module#system-environment-variable-support).
4143

dev/nginx.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ http {
1111
# You should use lua-nginx-module to pull the API token in from an environment variable here
1212
firetail_api_token "YOUR-API-TOKEN";
1313
firetail_url "https://api.logging.eu-west-1.prod.firetail.app/logs/bulk";
14+
firetail_allow_undefined_routes "true";
1415

1516
server {
1617
listen 80;

src/validator/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ require (
1616
gopkg.in/yaml.v3 v3.0.1 // indirect
1717
)
1818

19-
replace github.com/FireTail-io/firetail-go-lib => github.com/FireTail-io/firetail-go-lib v0.2.1
19+
replace github.com/FireTail-io/firetail-go-lib => github.com/FireTail-io/firetail-go-lib v0.2.3

src/validator/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/FireTail-io/firetail-go-lib v0.2.1 h1:Jf3C1mAtCHAHHqSeuaMx6sSRmUS7OjgY4B2nY8XfibU=
22
github.com/FireTail-io/firetail-go-lib v0.2.1/go.mod h1:PH4aGBwry6z/3vzXEdcMaxK22E3xqPq2+w2y3FzETj4=
3+
github.com/FireTail-io/firetail-go-lib v0.2.3 h1:MDWnEpa/cyqc5sfhyHLjSXz8C+510nmHvpVxiOXnFAg=
4+
github.com/FireTail-io/firetail-go-lib v0.2.3/go.mod h1:PH4aGBwry6z/3vzXEdcMaxK22E3xqPq2+w2y3FzETj4=
35
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
46
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
57
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

src/validator/main.go

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,21 @@ func ValidateRequestBody(
3333
log.Println("✅ Validating request body...")
3434
// Create the middleware if it hasn't already been done
3535
if firetailRequestMiddleware == nil {
36-
var err error
36+
allowUndefinedRoutesBool, err := strconv.ParseBool(
37+
string(C.GoBytes(allowUndefinedRoutes, allowUndefinedRoutesLength)),
38+
)
39+
if err != nil {
40+
log.Println("Failed to initialise Firetail middleware, err:", err.Error())
41+
}
42+
3743
firetailRequestMiddleware, err = firetail.GetMiddleware(&firetail.Options{
3844
OpenapiSpecPath: "/etc/nginx/appspec.yml",
3945
LogsApiToken: "",
4046
LogsApiUrl: "",
4147
DebugErrs: true,
4248
EnableRequestValidation: true,
4349
EnableResponseValidation: false,
50+
AllowUndefinedRoutes: allowUndefinedRoutesBool,
4451
})
4552
if err != nil {
4653
log.Println("Failed to initialise Firetail middleware, err:", err.Error())
@@ -90,26 +97,6 @@ func ValidateRequestBody(
9097

9198
// If the body differs after being passed through the middleware then we'll just infer it doesn't match the spec
9299
if string(middlewareResponseBodyBytes) != string(placeholderResponse) {
93-
// If allowing undefined routes, then we need to check if the response is a 404 from the middleware. If it is,
94-
// we return success.
95-
allowUndefinedRoutesBool, err := strconv.ParseBool(string(C.GoBytes(allowUndefinedRoutes, allowUndefinedRoutesLength)))
96-
if err != nil {
97-
return 1, responseCString // return 1 is error by convention
98-
}
99-
if allowUndefinedRoutesBool {
100-
response_json := map[string]interface{}{}
101-
if err := json.Unmarshal(middlewareResponseBodyBytes, &response_json); err != nil {
102-
return 1, responseCString // return 1 is error by convention
103-
}
104-
if code, ok := response_json["code"]; ok {
105-
if code_float, ok := code.(float64); ok {
106-
if code_float == 404 {
107-
return 0, responseCString // return 0 is success by convention
108-
}
109-
}
110-
}
111-
112-
}
113100
return 1, responseCString // return 1 is error by convention
114101
}
115102

@@ -131,14 +118,21 @@ func ValidateResponseBody(
131118
methodCharPtr unsafe.Pointer, methodLength C.int,
132119
) (C.int, *C.char) {
133120
if firetailResponseMiddleware == nil {
134-
var err error
121+
allowUndefinedRoutesBool, err := strconv.ParseBool(
122+
string(C.GoBytes(allowUndefinedRoutes, allowUndefinedRoutesLength)),
123+
)
124+
if err != nil {
125+
log.Println("Failed to initialise Firetail middleware, err:", err.Error())
126+
}
127+
135128
firetailResponseMiddleware, err = firetail.GetMiddleware(&firetail.Options{
136129
OpenapiSpecPath: "/etc/nginx/appspec.yml",
137130
LogsApiToken: strings.TrimSpace(string(C.GoBytes(tokenCharPtr, tokenLength))),
138131
LogsApiUrl: strings.TrimSpace(string(C.GoBytes(urlCharPtr, urlLength))),
139132
DebugErrs: true,
140133
EnableRequestValidation: false,
141134
EnableResponseValidation: true,
135+
AllowUndefinedRoutes: allowUndefinedRoutesBool,
142136
})
143137
if err != nil {
144138
log.Println("Failed to initialise Firetail middleware, err:", err.Error())
@@ -204,25 +198,6 @@ func ValidateResponseBody(
204198

205199
// If the body differs after being passed through the middleware then we'll just infer it doesn't match the spec
206200
if string(middlewareResponseBodyBytes) != string(resBodySlice) || localResponseWriter.Code != int(statusCode) {
207-
// If allowing undefined routes, then we need to check if the response is a 404 from the middleware. If it is,
208-
// we return success.
209-
allowUndefinedRoutesBool, err := strconv.ParseBool(string(C.GoBytes(allowUndefinedRoutes, allowUndefinedRoutesLength)))
210-
if err != nil {
211-
return 1, responseCString // return 1 is error by convention
212-
}
213-
if allowUndefinedRoutesBool {
214-
response_json := map[string]interface{}{}
215-
if err := json.Unmarshal(middlewareResponseBodyBytes, &response_json); err != nil {
216-
return 1, responseCString // return 1 is error by convention
217-
}
218-
if code, ok := response_json["code"]; ok {
219-
if code_float, ok := code.(float64); ok {
220-
if code_float == 404 {
221-
return 0, C.CString(string(resBodySlice)) // return 0 is success by convention
222-
}
223-
}
224-
}
225-
}
226201
return 1, responseCString // return 1 is error by convention
227202
}
228203

0 commit comments

Comments
 (0)