Skip to content

Commit

Permalink
fix: application-server module various fixes
Browse files Browse the repository at this point in the history
- rename "application-server" in "administration-console-api"
- administration-console-api uses matrix application server instance of tom-server instead of extend matrix-application-server package
- use postgresql instead of sqlite for administration-console-api integration tests
  • Loading branch information
Jordy Cabannes committed Apr 12, 2024
1 parent 9c0d6fe commit 042f8fc
Show file tree
Hide file tree
Showing 45 changed files with 1,917 additions and 1,395 deletions.
57 changes: 26 additions & 31 deletions packages/matrix-application-server/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,14 @@ export default class MASRouter {
* 500:
* $ref: '#/components/responses/InternalServerError'
*/
this.routes
.route('/_matrix/app/v1/transactions/:txnId')
.put(
this._middlewares(
transaction(this._appServer),
validation(Endpoints.TRANSACTIONS),
this.defaultAuthMiddleware
)
)
.all(allowCors, methodNotAllowed, errorMiddleware)
this.addRoute(
this.routes,
'/_matrix/app/v1/transactions/:txnId',
EHttpMethod.PUT,
transaction(this._appServer),
validation(Endpoints.TRANSACTIONS),
this.defaultAuthMiddleware
)

/**
* @openapi
Expand Down Expand Up @@ -116,16 +114,14 @@ export default class MASRouter {
* 500:
* $ref: '#/components/responses/InternalServerError'
*/
this.routes
.route('/_matrix/app/v1/users/:userId')
.get(
this._middlewares(
query,
validation(Endpoints.USERS),
this.defaultAuthMiddleware
)
)
.all(allowCors, methodNotAllowed, errorMiddleware)
this.addRoute(
this.routes,
'/_matrix/app/v1/users/:userId',
EHttpMethod.GET,
query,
validation(Endpoints.USERS),
this.defaultAuthMiddleware
)

/**
* @openapi
Expand Down Expand Up @@ -157,16 +153,14 @@ export default class MASRouter {
* 500:
* $ref: '#/components/responses/InternalServerError'
*/
this.routes
.route('/_matrix/app/v1/rooms/:roomAlias')
.get(
this._middlewares(
query,
validation(Endpoints.ROOMS),
this.defaultAuthMiddleware
)
)
.all(allowCors, methodNotAllowed, errorMiddleware)
this.addRoute(
this.routes,
'/_matrix/app/v1/rooms/:roomAlias',
EHttpMethod.GET,
query,
validation(Endpoints.ROOMS),
this.defaultAuthMiddleware
)

this.routes
.route(/^\/(users|rooms|transactions)\/[a-zA-Z0-9]*/g)
Expand All @@ -192,13 +186,14 @@ export default class MASRouter {
}

public addRoute(
router: Router,
path: string,
method: EHttpMethod,
controller: expressAppHandler,
validators: ValidationChain[],
authMiddleware?: expressAppHandler
): void {
const route: IRoute = this.routes.route(path)
const route: IRoute = router.route(path)
switch (method) {
case EHttpMethod.DELETE:
route.delete(this._middlewares(controller, validators, authMiddleware))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('MASRouter', () => {
const keys = Object.keys(newRoutes)
keys.forEach((method) => {
router.addRoute(
router.routes,
newRoutes[method].path,
method as EHttpMethod,
(req, res, next) => {},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import sqlite3 from 'sqlite3'
import { type Config } from '../../types'

let created = false

const createQuery =
'CREATE TABLE users (uid varchar(8), mobile varchar(12), mail varchar(32), sn varchar(32))'
const insertQueries = [
"INSERT INTO users VALUES('dwho', '33612345678', '[email protected]', 'Dwho')",
"INSERT INTO users VALUES('rtyler', '33687654321', '[email protected]', 'Rtyler')"
]

// eslint-disable-next-line @typescript-eslint/promise-function-async
export const buildUserDB = (conf: Config): Promise<void> => {
if (created) return Promise.resolve()
return new Promise((resolve, reject) => {
const matrixDb = new sqlite3.Database(conf.matrix_database_host)
matrixDb.run(
'CREATE TABLE users (name text, desactivated text, admin integer)',
(err) => {
if (err != null) {
reject(err)
} else {
matrixDb.run(
"INSERT INTO users VALUES('@dwho:example.com', '', 0)",
(err) => {
if (err != null) {
reject(err)
} else {
matrixDb.close((err) => {
/* istanbul ignore if */
if (err != null) {
console.error(err)
reject(err)
} else {
const userDb = new sqlite3.Database(
conf.userdb_host as string
)
userDb.run(createQuery, (err) => {
if (err != null) {
reject(err)
} else {
Promise.all(
insertQueries.map(
// eslint-disable-next-line @typescript-eslint/promise-function-async
(query) =>
new Promise((_resolve, _reject) => {
userDb.run(query, (err) => {
err != null ? _reject(err) : _resolve(true)
})
})
)
)
.then(() => {
userDb.close((err) => {
/* istanbul ignore if */
if (err != null) {
console.error(err)
reject(err)
} else {
created = true
resolve()
}
})
})

.catch(reject)
}
})
}
})
}
}
)
}
}
)
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/sh
set -e

DATABASE=${PG_DATABASE:-lemonldapng}
USER=${PG_USER:-lemonldap}
PASSWORD=${PG_PASSWORD:-lemonldap}
TABLE=${PG_TABLE:-lmConfig}
PTABLE=${PG_PERSISTENT_SESSIONS_TABLE:-psessions}
STABLE=${PG_SESSIONS_TABLE:-sessions}
SAMLTABLE=${PG_SAML_TABLE:-samlsessions}
OIDCTABLE=${PG_OIDC_TABLE:-oidcsessions}
CASTABLE=${PG_CAS_TABLE:-cassessions}

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER $USER PASSWORD '$PASSWORD';
CREATE DATABASE $DATABASE;
EOSQL
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$DATABASE" <<-EOSQL
CREATE TABLE $TABLE (
cfgNum integer not null primary key,
data text
);
GRANT ALL PRIVILEGES ON TABLE $TABLE TO $USER;
CREATE TABLE $PTABLE (
id varchar(64) not null primary key,
a_session jsonb
);
CREATE INDEX i_p__session_kind ON psessions ((a_session ->> '_session_kind'));
CREATE INDEX i_p__httpSessionType ON psessions ((a_session ->> '_httpSessionType'));
CREATE INDEX i_p__session_uid ON psessions ((a_session ->> '_session_uid'));
CREATE INDEX i_p_ipAddr ON psessions ((a_session ->> 'ipAddr'));
CREATE INDEX i_p__whatToTrace ON psessions ((a_session ->> '_whatToTrace'));
GRANT ALL PRIVILEGES ON TABLE $PTABLE TO $USER;
CREATE UNLOGGED TABLE $STABLE (
id varchar(64) not null primary key,
a_session jsonb
);
CREATE INDEX i_s__whatToTrace ON sessions ((a_session ->> '_whatToTrace'));
CREATE INDEX i_s__session_kind ON sessions ((a_session ->> '_session_kind'));
CREATE INDEX i_s__utime ON sessions ((cast (a_session ->> '_utime' as bigint)));
CREATE INDEX i_s_ipAddr ON sessions ((a_session ->> 'ipAddr'));
CREATE INDEX i_s__httpSessionType ON sessions ((a_session ->> '_httpSessionType'));
CREATE INDEX i_s_user ON sessions ((a_session ->> 'user'));
GRANT ALL PRIVILEGES ON TABLE $STABLE TO $USER;
CREATE UNLOGGED TABLE $SAMLTABLE (
id varchar(64) not null primary key,
a_session jsonb
);
CREATE INDEX i_a__session_kind ON $SAMLTABLE ((a_session ->> '_session_kind'));
CREATE INDEX i_a__utime ON $SAMLTABLE ((cast(a_session ->> '_utime' as bigint)));
CREATE INDEX i_a_ProxyID ON $SAMLTABLE ((a_session ->> 'ProxyID'));
CREATE INDEX i_a__nameID ON $SAMLTABLE ((a_session ->> '_nameID'));
CREATE INDEX i_a__assert_id ON $SAMLTABLE ((a_session ->> '_assert_id'));
CREATE INDEX i_a__art_id ON $SAMLTABLE ((a_session ->> '_art_id'));
CREATE INDEX i_a__saml_id ON $SAMLTABLE ((a_session ->> '_saml_id'));
GRANT ALL PRIVILEGES ON TABLE $SAMLTABLE TO $USER;
CREATE UNLOGGED TABLE $OIDCTABLE (
id varchar(64) not null primary key,
a_session jsonb
);
CREATE INDEX i_o__session_kind ON $OIDCTABLE ((a_session ->> '_session_kind'));
CREATE INDEX i_o__utime ON $OIDCTABLE ((cast(a_session ->> '_utime' as bigint )));
GRANT ALL PRIVILEGES ON TABLE $OIDCTABLE TO $USER;
CREATE UNLOGGED TABLE $CASTABLE (
id varchar(64) not null primary key,
a_session jsonb
);
CREATE INDEX i_c__session_kind ON $CASTABLE ((a_session ->> '_session_kind'));
CREATE INDEX i_c__utime ON $CASTABLE ((cast(a_session ->> '_utime' as bigint)));
CREATE INDEX i_c__cas_id ON $CASTABLE ((a_session ->> '_cas_id'));
CREATE INDEX i_c_pgtIou ON $CASTABLE ((a_session ->> 'pgtIou'));
GRANT ALL PRIVILEGES ON TABLE $CASTABLE TO $USER;
EOSQL

if test -e /llng-conf/conf.json; then
SERIALIZED=`perl -MJSON -e '$/=undef;
open F, "/llng-conf/conf.json" or die $!;
$a=JSON::from_json(<F>);
$a->{cfgNum}=1;
$a=JSON::to_json($a);
$a=~s/'\''/'\'\''/g;
$a =~ s/\\\\/\\\\\\\\/g;
print $a;'`
echo "set val '$SERIALIZED'" >&2
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$DATABASE" <<-EOSQL
\\set val '$SERIALIZED'
INSERT INTO $TABLE (cfgNum, data) VALUES (1, :'val');
\\unset val
EOSQL
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh

psql -U postgres <<-EOSQL
CREATE USER synapse PASSWORD 'synapse!1';
CREATE DATABASE synapse TEMPLATE='template0' LOCALE='C' ENCODING='UTF8' OWNER='synapse';
EOSQL
psql -v ON_ERROR_STOP=1 --username "synapse" --dbname "synapse" <<-EOSQL
CREATE TABLE users (
name text,
password_hash text,
creation_ts bigint,
admin smallint DEFAULT 0 NOT NULL,
upgrade_ts bigint,
is_guest smallint DEFAULT 0 NOT NULL,
appservice_id text,
consent_version text,
consent_server_notice_sent text,
user_type text,
deactivated smallint DEFAULT 0 NOT NULL,
shadow_banned boolean,
consent_ts bigint
);
EOSQL
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

psql -U postgres <<-EOSQL
CREATE USER synapse PASSWORD 'synapse!1';
CREATE DATABASE synapse TEMPLATE='template0' LOCALE='C' ENCODING='UTF8' OWNER='synapse';
EOSQL
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

psql -U postgres <<-EOSQL
CREATE USER twake PASSWORD 'twake!1';
CREATE DATABASE twakedb TEMPLATE='template0' LOCALE='C' ENCODING='UTF8' OWNER='twake';
EOSQL
Loading

0 comments on commit 042f8fc

Please sign in to comment.