-
Notifications
You must be signed in to change notification settings - Fork 1
/
<2021-12-08T17:37:17.804Z>-keycloak.js
91 lines (82 loc) · 2.87 KB
/
<2021-12-08T17:37:17.804Z>-keycloak.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"use strict";
const { DOWN_MIGRATION } = require("../config/globals");
const waitOn = require("wait-on");
const path = require("path");
const {
setupKeyCloak,
cleanupKeyCloak,
KEYCLOAK_BASEURL,
KEYCLOAK_GIQL_CLIENT,
KEYCLOAK_SPA_CLIENT,
KEYCLOAK_GQL_CLIENT,
} = require("../utils/setup-keycloak");
const axios = require("axios");
/**
* @module - Migrations to create or to drop a table correpondant to a sequelize model.
*/
module.exports = {
/**
* up - configure the keycloak instance with zendro defaults
*
* @param {object} zendro initialized zendro object
*/
up: async (zendro) => {
// wait for keycloak service to be available
await waitOn({ resources: [KEYCLOAK_BASEURL] });
// setup default keycloak instance
try {
const {
KEYCLOAK_PUBLIC_KEY,
KEYCLOAK_GIQL_CLIENT_SECRET,
KEYCLOAK_SPA_CLIENT_SECRET,
} = await setupKeyCloak();
console.log(`Successfully created default keycloak zendro realm, client, roles.
A default user "zendro-admin" with password "admin" was created to login to the
zendro services. Please delete that user before publically deploying zendro.
To login to the keycloak admin console use credentials user: "admin"
pw: "admin" at "${KEYCLOAK_BASEURL}/auth". Change that user / password to your liking.
`);
// write ENV variables
// graphql-server
fs.appendFileSync(
path.resolve(__dirname, "../.env"),
`\nOAUTH2_PUBLIC_KEY="${KEYCLOAK_PUBLIC_KEY}"\nOAUTH2_CLIENT_ID=${KEYCLOAK_GQL_CLIENT}`
);
// graphiql-auth
fs.appendFileSync(
path.resolve(__dirname, "../../graphiql-auth/.env.development"),
`\nOAUTH2_CLIENT_SECRET=${KEYCLOAK_GIQL_CLIENT_SECRET}\nOAUTH2_CLIENT_ID=${KEYCLOAK_GIQL_CLIENT}`
);
fs.appendFileSync(
path.resolve(__dirname, "../../graphiql-auth/.env.production"),
`\nOAUTH2_CLIENT_SECRET=${KEYCLOAK_GIQL_CLIENT_SECRET}\nOAUTH2_CLIENT_ID=${KEYCLOAK_GIQL_CLIENT}`
);
// single-page-app
fs.appendFileSync(
path.resolve(__dirname, "../../single-page-app/.env.development"),
`\nOAUTH2_CLIENT_SECRET=${KEYCLOAK_SPA_CLIENT_SECRET}\nOAUTH2_CLIENT_ID=${KEYCLOAK_SPA_CLIENT}`
);
fs.appendFileSync(
path.resolve(__dirname, "../../single-page-app/.env.production"),
`\nOAUTH2_CLIENT_SECRET=${KEYCLOAK_SPA_CLIENT_SECRET}\nOAUTH2_CLIENT_ID=${KEYCLOAK_SPA_CLIENT}`
);
console.log(
"Successfully added OAuth2 keycloak PUBLIC_KEY, CLIENT_ID and CLIENT_SECRET environment variables."
);
} catch (error) {
throw new Error(error);
}
},
/**
* down - Drop a table.
*
* @param {object} zendro initialized zendro object
*/
down: async (zendro) => {
try {
await cleanupKeyCloak();
} catch (error) {
throw new Error(error);
}
},
};