Skip to content

Commit 6351d47

Browse files
committed
Refactor resource directory structure
To avoid circular dependencies in the future, refactor the directory structure of the resources (such as user, project, etc) by identifying types of objects owned by resources: - GraphQL Resolvers (resources/X/resolvers.js) - SQL queries (resources/X/sql.js) - Helpers (resources/X/helpers.js) - Keycloak operations (resources/X/keycloak.js) This also removes the abstraction and indirection caused by the DAO pattern.
1 parent 8b9fa5b commit 6351d47

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+3189
-3176
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ node_modules/
1212
build/*
1313
!build/.gitkeep
1414
openshift
15+
!services/api/src/resources/openshift
1516
.loopback
1617
secrets
1718
docs/_build

local-dev/api-data/01-populate-api-data.gql

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ mutation PopulateApi {
242242
}
243243

244244
# Projects for CI
245-
CiGithub: addProject(
245+
CiGithub1: addProject(
246246
input: {
247247
id: 3
248248
name: "ci-github"

services/api/src/app.js

-29
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,11 @@ const createRouter = require('./routes');
1010
const { authKeycloakMiddleware } = require('./authKeycloakMiddleware');
1111
const { createAuthMiddleware } = require('./authMiddleware');
1212

13-
const Dao = require('./dao');
14-
1513
/* ::
16-
import type MariaSQL from 'mariasql';
17-
import type elasticsearch from 'elasticsearch';
18-
1914
type CreateAppArgs = {
2015
store?: Object,
2116
jwtSecret: string,
2217
jwtAudience: string,
23-
sqlClient: MariaSQL,
24-
esClient: elasticsearch.Client,
25-
keycloakClient: Object,
26-
searchguardClient: Object,
27-
kibanaClient: Object
2818
};
2919
*/
3020

@@ -33,28 +23,9 @@ const createApp = (args /* : CreateAppArgs */) => {
3323
// store,
3424
jwtSecret,
3525
jwtAudience,
36-
sqlClient,
37-
esClient,
38-
keycloakClient,
39-
searchguardClient,
40-
kibanaClient,
4126
} = args;
4227
const app = express();
4328

44-
const dao = Dao.make(
45-
sqlClient,
46-
esClient,
47-
keycloakClient,
48-
searchguardClient,
49-
kibanaClient,
50-
);
51-
52-
app.set('context', {
53-
sqlClient,
54-
esClient,
55-
dao,
56-
});
57-
5829
// Use compression (gzip) for responses.
5930
app.use(compression());
6031

services/api/src/authKeycloakMiddleware.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ const authWithKeycloak /* : AuthWithKeycloakFn */ = async (req, res, next) => {
6363
return;
6464
}
6565

66-
const ctx = req.app.get('context');
67-
const dao = ctx.dao;
68-
6966
try {
7067
// Admins have full access and don't need a list of permissions
7168
if (
@@ -85,7 +82,7 @@ const authWithKeycloak /* : AuthWithKeycloakFn */ = async (req, res, next) => {
8582
},
8683
} = req.kauth.grant.access_token;
8784

88-
const permissions = await getPermissionsForUser(dao, userId);
85+
const permissions = await getPermissionsForUser(userId);
8986

9087
if (R.isEmpty(permissions)) {
9188
res.status(401).send({

services/api/src/authMiddleware.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ const createAuthMiddleware /* : CreateAuthMiddlewareFn */ = ({
6666
jwtSecret,
6767
jwtAudience,
6868
}) => async (req, res, next) => {
69-
const ctx = req.app.get('context');
70-
const dao = ctx.dao;
71-
7269
// Allow access to status without auth
7370
if (req.url === '/status') {
7471
next();
@@ -126,7 +123,7 @@ const createAuthMiddleware /* : CreateAuthMiddlewareFn */ = ({
126123
let nonAdminCreds = {};
127124

128125
if (role !== 'admin') {
129-
const permissions = await getPermissionsForUser(dao, userId);
126+
const permissions = await getPermissionsForUser(userId);
130127

131128
if (R.isEmpty(permissions)) {
132129
res.status(401).send({

services/api/src/clients/esClient.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @flow
2+
3+
const elasticsearch = require('elasticsearch');
4+
5+
const { LOGSDB_ADMIN_PASSWORD } = process.env;
6+
7+
const esClient = new elasticsearch.Client({
8+
host: 'logs-db:9200',
9+
log: 'warning',
10+
httpAuth: `admin:${LOGSDB_ADMIN_PASSWORD || '<password not set>'}`,
11+
});
12+
13+
module.exports = esClient;
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @flow
2+
3+
const KeycloakAdminClient = require('keycloak-admin').default;
4+
5+
// Must be initialized with `waitAndInitKeycloak`
6+
const keycloakClient = new KeycloakAdminClient({
7+
baseUrl: 'http://keycloak:8080/auth',
8+
realmName: 'master',
9+
});
10+
11+
module.exports = keycloakClient;
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @flow
2+
3+
const got = require('got');
4+
5+
const { LOGSDB_ADMIN_PASSWORD } = process.env;
6+
7+
const kibanaClient = got.extend({
8+
baseUrl: 'http://logs-db-ui:5601/api/',
9+
auth: `admin:${LOGSDB_ADMIN_PASSWORD || '<password not set>'}`,
10+
json: true,
11+
headers: {
12+
'kbn-xsrf': 'true',
13+
},
14+
});
15+
16+
module.exports = kibanaClient;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @flow
2+
const got = require('got');
3+
4+
const { LOGSDB_ADMIN_PASSWORD } = process.env;
5+
6+
const searchguardClient = got.extend({
7+
baseUrl: 'http://logs-db:9200/_searchguard/api/',
8+
json: true,
9+
auth: `admin:${LOGSDB_ADMIN_PASSWORD || '<password not set>'}`,
10+
});
11+
12+
module.exports = searchguardClient;

services/api/src/clients/sqlClient.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @flow
2+
3+
const MariaSQL = require('mariasql');
4+
const logger = require('../logger');
5+
6+
const sqlClient = new MariaSQL({
7+
host: 'api-db',
8+
port: 3306,
9+
user: 'api',
10+
password: 'api',
11+
db: 'infrastructure',
12+
});
13+
14+
sqlClient.on('error', error => {
15+
logger.error(error);
16+
});
17+
18+
module.exports = sqlClient;

services/api/src/dao/attrFilter.js

-24
This file was deleted.

services/api/src/dao/attrFilter.test.js

-10
This file was deleted.

0 commit comments

Comments
 (0)