Skip to content

Commit 83379d9

Browse files
authored
Release/0.2.0 (#12)
* feat: ✨ Prepare release 0.2.0 Add example how to use constants.*.js, Add example how to implement Hasura Custom Action protector middleware, Add example how to parse cookies, Add ACL to non-public Swagger UI, fix Hlambda List of protected env names * feat: 🎨 Update Console UI * docs: 📝 Update CHANGELOG.md * fix: 📝 Update comment, update CHANGELOG.md * fix: 🐛 Update ACL to check for access value, and increase expiration time of the Swagger Console UI token.
1 parent 72d0e8f commit 83379d9

27 files changed

+558
-121
lines changed

.env.example

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ PRIVATE_KEY_CONFIGURATION="__INSERT_YOUR_PRIVATE_KEY_CONFIGURATION__"
3434
# Server max allowed body size from client that express app will support. (Main usecase is Apple Subscription Notifications)
3535
SERVER_BODY_SIZE="2mb"
3636

37+
# Constant reference in code: ENV_DISABLE_EXPRESS_BODY_PARSER | Default value: false
38+
# The Express body parser will be disabled for all routes except the ones required for Console.
39+
DISABLE_EXPRESS_BODY_PARSER="false"
40+
41+
# Constant reference in code: ENV_EXPRESS_BODY_PARSER_INCLUDE_RAW_BODY | Default value: false
42+
# In all requests you will have req.rawBody buffer available.
43+
EXPRESS_BODY_PARSER_INCLUDE_RAW_BODY="false"
44+
45+
# Constant reference in code: ENV_DISABLE_EXPRESS_COOKIE_PARSER | Default value: false
46+
# The Express cookie parser will be disabled for all routes except the ones required for Console.
47+
DISABLE_EXPRESS_COOKIE_PARSER="false"
48+
3749
# Constant reference in code: ENV_HLAMBDA_CORS_DOMAIN | Default value: *
3850
# By default, all CORS requests to the Hlambda server are allowed. To run with more restrictive CORS settings, use this env variable. Example: `https://*.foo.bar.com:8080, http://*.localhost, http://localhost:3000, http://example.com`
3951
HLAMBDA_CORS_DOMAIN="*"
@@ -110,7 +122,7 @@ HLAMBDA_ENVIRONMENT_BANNER_MESSAGE=""
110122
# Selects color of the environment banner.
111123
HLAMBDA_ENABLE_ENVIRONMENT_BANNER_COLOR="#fea300"
112124

113-
# Constant reference in code: ENV_HLAMBDA_LIST_OF_PROTECTED_ENV_VARIABLES | Default value: ENV_HLAMBDA_LIST_OF_PROTECTED_ENV_VARIABLES,HLAMBDA_DISABLE_CONSOLE,HLAMBDA_ADMIN_SECRET
125+
# Constant reference in code: ENV_HLAMBDA_LIST_OF_PROTECTED_ENV_VARIABLES | Default value: HLAMBDA_LIST_OF_PROTECTED_ENV_VARIABLES,HLAMBDA_DISABLE_CONSOLE,HLAMBDA_ADMIN_SECRET,SERVER_PORT
114126
# List of the env variable names that are protected from hlambda config override.
115-
HLAMBDA_LIST_OF_PROTECTED_ENV_VARIABLES="ENV_HLAMBDA_LIST_OF_PROTECTED_ENV_VARIABLES,HLAMBDA_DISABLE_CONSOLE,HLAMBDA_ADMIN_SECRET"
127+
HLAMBDA_LIST_OF_PROTECTED_ENV_VARIABLES="HLAMBDA_LIST_OF_PROTECTED_ENV_VARIABLES,HLAMBDA_DISABLE_CONSOLE,HLAMBDA_ADMIN_SECRET,SERVER_PORT"
116128

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# Release 0.2.0
2+
3+
- Updated Console UI (Logs: Auto scroll snap, Metadata: Action history dates)
4+
- Added example for use of constants in default metadata.
5+
- Added support for parsing cookies.
6+
- Added example to get and set cookies in default metadata.
7+
- Added ACL for Non-public Swagger UI in Console.
8+
- Fix HLAMBDA_LIST_OF_PROTECTED_ENV_VARIABLES, also add SERVER_PORT to HLAMBDA_LIST_OF_PROTECTED_ENV_VARIABLES.
9+
- Added support to get request raw body via new env variable `EXPRESS_BODY_PARSER_INCLUDE_RAW_BODY` default:false
10+
- Added Hasura Custom Action Middleware that checks for Hasura Web Hook Secret in request header to the example metadata.
11+
112
# Release 0.1.0
213

314
- Added support for JSON output to stdout, new env variable `JSON_STDOUT` default:false

data/self-update-history/.gitkeep

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import express from 'express';
2+
import asyncHandler from 'express-async-handler';
3+
4+
import cookieParser from 'cookie-parser';
5+
6+
import errors from './../errors.demo.js';
7+
8+
// Create express router
9+
const router = express.Router();
10+
11+
// router.use(cookieParser()); // Use this if you have set ENV_DISABLE_EXPRESS_COOKIE_PARSER to true.
12+
13+
router.get(
14+
'/cookie/set',
15+
asyncHandler((req, res) => {
16+
res.cookie('demo-jwt-token', 'token ey...', { maxAge: 10800 });
17+
res.send(`Demo token set!`);
18+
})
19+
);
20+
21+
router.get(
22+
'/cookie/get',
23+
asyncHandler((req, res) => {
24+
console.log('Cookies: ', req.cookies);
25+
res.send(JSON.stringify(req.cookies, null, 2));
26+
})
27+
);
28+
29+
export default router;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { createConstantsDescriptor } from 'hlambda';
2+
3+
export { isEnvTrue, getEnvValue } from 'hlambda';
4+
5+
// --- START SAFE TO EDIT ---
6+
7+
export const constantsGroupName = 'example-hasura-app';
8+
9+
export const constants = {
10+
ENV_HOOK_SECRET_HEADER_NAME: {
11+
name: 'HOOK_SECRET_HEADER_NAME',
12+
default: 'x-hook-secret', // Default value
13+
description: 'Name of the header where hook secret will be found.',
14+
},
15+
ENV_HOOK_SECRET: {
16+
name: 'HOOK_SECRET',
17+
default: 'you-must-change-me', // Default value
18+
description: 'Secret used to protect hooks.',
19+
},
20+
ENV_DANGEROUS_DISABLE_HOOK_SECRET_AND_CONTINUE: {
21+
name: 'DANGEROUS_DISABLE_HOOK_SECRET_AND_CONTINUE',
22+
default: 'false', // Default value
23+
description:
24+
'If set to true, HOOK_SECRET check will be disabled and anyone without hook secret can trigger the hook. (Default: false)',
25+
},
26+
};
27+
28+
// --- STOP SAFE TO EDIT ---
29+
30+
export const cd = createConstantsDescriptor(constants, constantsGroupName);
31+
32+
export default constants;

metadata/apps/example-hasura/errors.demo.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export const errors = {
1212
SOMETHING_WENT_TERRIBLY_WRONG: {
1313
message: 'Description of an error message...',
1414
},
15+
ERROR_INVALID_HOOK_SECRET: {
16+
message: 'Invalid hook secret.',
17+
},
1518
};
1619

1720
// --- STOP SAFE TO EDIT ---

metadata/apps/example-hasura/hlambda-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ env:
22
APP_VERSION: "v1.0.0"
33
envForce:
44
APP_VERSION: "v1.0.0"
5+
HOOK_SECRET_HEADER_NAME: "x-hook-secret"
6+
HOOK_SECRET: "hasura-hook-secret-SuPperSecr3t112"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import express from 'express';
2+
import asyncHandler from 'express-async-handler';
3+
4+
// Define errors
5+
import errors from './errors.demo.js';
6+
import { constants, isEnvTrue, getEnvValue } from './constants.demo.js';
7+
8+
// Create express router
9+
const router = express.Router();
10+
11+
// Middleware to handle authorization for the web hook.
12+
router.use(
13+
asyncHandler((req, res, next) => {
14+
const secret = req?.headers?.[getEnvValue(constants.ENV_HOOK_SECRET_HEADER_NAME)];
15+
// Check if protector is disabled. !!! Dangerous !!! But useful when testing in demo env.
16+
if (isEnvTrue(constants.ENV_DANGEROUS_DISABLE_HOOK_SECRET_AND_CONTINUE)) {
17+
next();
18+
return;
19+
}
20+
// Check if secret matches.
21+
if (secret !== getEnvValue(constants.ENV_HOOK_SECRET)) {
22+
throw new Error(errors.ERROR_INVALID_HOOK_SECRET);
23+
}
24+
next();
25+
})
26+
);
27+
28+
export default router;

metadata/apps/example-hasura/router.demo.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import asyncHandler from 'express-async-handler';
44
// Import our custom request logger
55
import hasuraRequestLogger from './hasura-request-logger.js';
66

7+
// Import protector for web hooks
8+
import hasuraWebHookProtector from './protector.js';
9+
710
// Import our errors definition
811
import errors from './errors.demo.js';
912

@@ -12,10 +15,21 @@ const router = express.Router();
1215

1316
router.use('/hasura-*', hasuraRequestLogger);
1417

18+
router.use('/hasura-*', hasuraWebHookProtector);
19+
1520
router.post(
1621
'/hasura-version',
1722
asyncHandler((req, res) => {
1823
console.log(`${process.env.APP_VERSION}`);
24+
res.json({
25+
version: `${process.env.APP_VERSION}`,
26+
});
27+
})
28+
);
29+
30+
router.post(
31+
'/hasura-version-error',
32+
asyncHandler((req, res) => {
1933
throw new Error(errors.SOMETHING_WENT_TERRIBLY_WRONG);
2034
// res.json({
2135
// version: `${process.env.APP_VERSION}`,

0 commit comments

Comments
 (0)