From 39e527de679348930f54936c6812284431d1c19c Mon Sep 17 00:00:00 2001 From: Kayode Ezike Date: Fri, 1 Mar 2024 04:55:18 -0500 Subject: [PATCH] configures database and table names for db status manager; enables status credential retrieval for all status managers --- .env.db.example | 9 +++++++-- README.md | 17 +++++++++++------ server.js | 10 +++++----- src/app.js | 15 ++++----------- src/config.js | 9 +++++++-- src/status.js | 50 +++++++++++++++++++++++++++++++++++-------------- 6 files changed, 70 insertions(+), 40 deletions(-) diff --git a/.env.db.example b/.env.db.example index ee6a830..c2750c0 100644 --- a/.env.db.example +++ b/.env.db.example @@ -14,5 +14,10 @@ STATUS_CRED_SITE_ORIGIN=https://credentials.example.edu CRED_STATUS_DB_URL=mongodb+srv://user:pass@domain.mongodb.net?retryWrites=false CRED_STATUS_DB_HOST=domain.mongodb.net # ignored if CRED_STATUS_DB_URL is configured CRED_STATUS_DB_PORT=27017 # ignored if CRED_STATUS_DB_URL is configured -CRED_STATUS_DB_USER=user # ignored if CRED_STATUS_DB_URL is configured -CRED_STATUS_DB_PASS=pass # ignored if CRED_STATUS_DB_URL is configured +CRED_STATUS_DB_USER=testuser # ignored if CRED_STATUS_DB_URL is configured +CRED_STATUS_DB_PASS=testpass # ignored if CRED_STATUS_DB_URL is configured +CRED_STATUS_DB_NAME= +STATUS_CRED_TABLE_NAME= +CONFIG_TABLE_NAME= +EVENT_TABLE_NAME= +CRED_EVENT_TABLE_NAME= diff --git a/README.md b/README.md index 5755347..45757a8 100644 --- a/README.md +++ b/README.md @@ -53,12 +53,17 @@ There is a sample `.env` file provided called `.env.db.example` to help you get | Key | Description | Default | Required | | --- | --- | --- | --- | -| \* `STATUS_CRED_SITE_ORIGIN` | Base URL of status credentials managed by this service | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true | -| `CRED_STATUS_DB_URL` | URL of the database instance used to manage the credential status repository | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true and if the other set of `CRED_STATUS_DB_*` fields are not set | -| `CRED_STATUS_DB_HOST` | host of the database instance used to manage the credential status repository | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true and if `CRED_STATUS_DB_URL` is not set | -| `CRED_STATUS_DB_PORT` | port of the database instance used to manage the credential status repository | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true and if `CRED_STATUS_DB_URL` is not set | -| `CRED_STATUS_DB_USER` | username of user with read/write privileges on the database instance used to manage the credential status repository | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true and if `CRED_STATUS_DB_URL` is not set | +| \* `STATUS_CRED_SITE_ORIGIN` | base URL of status credentials managed by a given deployment | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true | +| `CRED_STATUS_DB_URL` | URL of the database instance used to manage credential status data | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true and if the other set of `CRED_STATUS_DB_*` fields are not set | +| `CRED_STATUS_DB_HOST` | host of the database instance used to manage credential status data | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true and if `CRED_STATUS_DB_URL` is not set | +| `CRED_STATUS_DB_PORT` | port of the database instance used to manage credential status data | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true and if `CRED_STATUS_DB_URL` is not set | +| `CRED_STATUS_DB_USER` | username of user with read/write privileges on the database instance used to manage credential status data | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true and if `CRED_STATUS_DB_URL` is not set | | `CRED_STATUS_DB_PASS` | password associated with `CRED_STATUS_DB_USER` | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true and if `CRED_STATUS_DB_URL` is not set | +| `CRED_STATUS_DB_NAME` | name of the database instance used to manage credential status data | `credentialStatus` | no | +| `STATUS_CRED_TABLE_NAME` | name of the database table used to manage status credentials | `StatusCredential` | no | +| `CONFIG_TABLE_NAME` | name of the database table used to manage application configuration | `Config` | no | +| `EVENT_TABLE_NAME` | name of the database table used to manage credential status events | `Event` | no | +| `CRED_EVENT_TABLE_NAME` | name of the database table used to manage the latest status event for a given credential | `CredentialEvent` | no | \* In order for credential status verification to work, you will need to use a publicly accessible URL for `STATUS_CRED_SITE_ORIGIN`, so that the verifier can access the status data. If you would like to spin up this service at a public URL, consider using a traffic forwarding tool like [localtunnel](https://www.npmjs.com/package/localtunnel). Once you have installed it, follow these simple steps to run the service: 1. Run `lt --port $PORT` @@ -225,7 +230,7 @@ NOTE: CURL can get a bit clunky if you want to experiment more (like say by chan ### Revoke -Revocation is fully explained in the StatusList2021 specifivation and the git status repo implemenation but amounts to POSTing an object to the revocation endpoint, like so: +Revocation is fully explained in the Status List 2021 specification and the git status repo implemenation but amounts to POSTing an object to the revocation endpoint, like so: ``` {credentialId: '23kdr', credentialStatus: [{type: 'StatusList2021Credential', status: 'revoked'}]} diff --git a/server.js b/server.js index e6f3714..c88c242 100644 --- a/server.js +++ b/server.js @@ -1,12 +1,12 @@ import { build } from './src/app.js' -import { getConfig, setConfig } from "./src/config.js"; -import http from "http" +import { getConfig, setConfig } from './src/config.js'; +import http from 'http'; const run = async () => { await setConfig() - const { port, enableHttpsForDev } = getConfig(); + const { port } = getConfig(); const app = await build(); - http.createServer(app).listen(port, () => console.log(`Server running on port ${port}`)) + http.createServer(app).listen(port, () => console.log(`Server running on port ${port}`)); }; -run(); \ No newline at end of file +run(); diff --git a/src/app.js b/src/app.js index caea1cf..b18c3ea 100644 --- a/src/app.js +++ b/src/app.js @@ -1,7 +1,5 @@ import express from 'express'; -import logger from 'morgan'; import cors from 'cors'; -import { getConfig } from './config.js'; import status from './status.js'; import revoke from './revoke.js' import allocateStatus from './allocateStatus.js' @@ -11,8 +9,6 @@ import errorLogger from './middleware/errorLogger.js'; import invalidPathHandler from './middleware/invalidPathHandler.js'; export async function build(opts = {}) { - const { credStatusService } = getConfig(); - await status.initializeStatusManager(); const app = express(); @@ -29,9 +25,6 @@ export async function build(opts = {}) { // get status credential app.get('/:statusCredentialId', async (req, res, next) => { - if (credStatusService !== 'mongodb') { - return null; - } const statusCredentialId = req.params.statusCredentialId; try { const statusCredential = await status.getStatusCredential(statusCredentialId); @@ -104,10 +97,10 @@ export async function build(opts = {}) { } }); - // Attach the error handling middleware calls, in the order that they should run - app.use(errorLogger); - app.use(errorHandler); - app.use(invalidPathHandler); + // Attach the error handling middleware calls, in the order that they should run + app.use(errorLogger); + app.use(errorHandler); + app.use(invalidPathHandler); return app; } diff --git a/src/config.js b/src/config.js index f8fc582..64c98fd 100644 --- a/src/config.js +++ b/src/config.js @@ -25,12 +25,17 @@ function getGeneralEnvs() { function getMongoDbEnvs() { const env = process.env; return { - statusCredentialSiteOrigin: env.STATUS_CRED_SITE_ORIGIN, + statusCredSiteOrigin: env.STATUS_CRED_SITE_ORIGIN, credStatusDatabaseUrl: env.CRED_STATUS_DB_URL, credStatusDatabaseHost: env.CRED_STATUS_DB_HOST, credStatusDatabasePort: env.CRED_STATUS_DB_PORT, credStatusDatabaseUsername: env.CRED_STATUS_DB_USER, - credStatusDatabasePassword: env.CRED_STATUS_DB_PASS + credStatusDatabasePassword: env.CRED_STATUS_DB_PASS, + credStatusDatabaseName: env.CRED_STATUS_DB_NAME, + statusCredTableName: env.STATUS_CRED_TABLE_NAME, + configTableName: env.CONFIG_TABLE_NAME, + eventTableName: env.EVENT_TABLE_NAME, + credEventTableName: env.CRED_EVENT_TABLE_NAME }; } diff --git a/src/status.js b/src/status.js index 18e9480..d149ac5 100644 --- a/src/status.js +++ b/src/status.js @@ -8,12 +8,17 @@ import { getConfig } from './config.js'; const { // Database env vars - statusCredentialSiteOrigin, + statusCredSiteOrigin, credStatusDatabaseUrl, credStatusDatabaseHost, credStatusDatabasePort, credStatusDatabaseUsername, credStatusDatabasePassword, + credStatusDatabaseName, + statusCredTableName, + configTableName, + eventTableName, + credEventTableName, // Git env vars credStatusService, @@ -30,23 +35,35 @@ let STATUS_LIST_MANAGER; async function createDatabaseStatusManager() { return createStatusManagerDb({ - statusCredentialSiteOrigin, + statusCredentialSiteOrigin: statusCredSiteOrigin, databaseService: credStatusService, databaseUrl: credStatusDatabaseUrl, databaseHost: credStatusDatabaseHost, databasePort: credStatusDatabasePort, databaseUsername: credStatusDatabaseUsername, databasePassword: credStatusDatabasePassword, + databaseName: credStatusDatabaseName, + statusCredentialTableName: statusCredTableName, + configTableName, + eventTableName, + credentialEventTableName: credEventTableName, didMethod: 'key', didSeed: credStatusDidSeed, - signUserCredential: false, - signStatusCredential: true + // This is the already the default value, + // but setting here to be explicit + autoDeployDatabase: true, + // This is the already the default value, + // but setting here to be explicit + signStatusCredential: true, + // This is the already the default value, + // but setting here to be explicit + signUserCredential: false }); } async function createGitHubStatusManager() { return createStatusManagerGit({ - service: credStatusService, + gitService: credStatusService, repoName: credStatusRepoName, metaRepoName: credStatusMetaRepoName, ownerAccountName: credStatusOwnerAccountName, @@ -54,14 +71,18 @@ async function createGitHubStatusManager() { metaRepoAccessToken: credStatusAccessToken, didMethod: 'key', didSeed: credStatusDidSeed, - signUserCredential: false, - signStatusCredential: true + // This is the already the default value, + // but setting here to be explicit + signStatusCredential: true, + // This is the already the default value, + // but setting here to be explicit + signUserCredential: false }); } async function createGitLabStatusManager() { return createStatusManagerGit({ - service: credStatusService, + gitService: credStatusService, repoName: credStatusRepoName, repoId: credStatusRepoId, metaRepoName: credStatusMetaRepoName, @@ -71,8 +92,12 @@ async function createGitLabStatusManager() { metaRepoAccessToken: credStatusAccessToken, didMethod: 'key', didSeed: credStatusDidSeed, - signUserCredential: false, - signStatusCredential: true + // This is the already the default value, + // but setting here to be explicit + signStatusCredential: true, + // This is the already the default value, + // but setting here to be explicit + signUserCredential: false }); } @@ -106,11 +131,8 @@ async function getStatusManager() { } async function getStatusCredential(statusCredentialId) { - if (credStatusService !== 'mongodb') { - return null; - } const statusManager = await getStatusManager(); return statusManager.getStatusCredential(statusCredentialId); } -export default { initializeStatusManager, getStatusManager, getStatusCredential }; \ No newline at end of file +export default { initializeStatusManager, getStatusManager, getStatusCredential };