Skip to content

Commit

Permalink
adds service based environment variable files; updates readme instruc…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
kezike committed Jan 31, 2024
1 parent fa4bca6 commit d4e12a2
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 72 deletions.
18 changes: 18 additions & 0 deletions .env.db.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# see the README for an explanation of all env values

# General environment variables
CRED_STATUS_SERVICE=mongodb
CRED_STATUS_DID_SEED=z1AackbUm8U69ohKnihoRRFkXcXJd4Ra1PkAboQ2ZRy1ngB
PORT=4008 # default port is 4008
LOG_ALL_FILE=logs/all.log
ERROR_LOG_FILE=logs/error.log
CONSOLE_LOG_LEVEL=silly # default is silly, i.e. log everything - see the README for allowed levels
LOG_LEVEL=silly # default is silly

# Database specific environment variables
STATUS_CRED_SITE_ORIGIN=https://credentials.example.edu
CRED_STATUS_DB_URL=mongodb+srv://user:[email protected]?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
18 changes: 9 additions & 9 deletions .env.example → .env.git.example
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# see the README for an explanation of all env values

PORT=4008 # default port is 4008

# replace the following with your own values
# General environment variables
CRED_STATUS_SERVICE=github
CRED_STATUS_REPO_OWNER=digitalcredentials
CRED_STATUS_REPO_NAME=credential-status-test-jc
CRED_STATUS_META_REPO_NAME=credential-status-metadata-test-jc
CRED_STATUS_ACCESS_TOKEN=REPLACE_THIS_WITH_A_GITHUB_ACCESS_TOKEN
CRED_STATUS_DID_SEED=z1AackbUm8U69ohKnihoRRFkXcXJd4Ra1PkAboQ2ZRy1ngB

PORT=4008 # default port is 4008
LOG_ALL_FILE=logs/all.log
ERROR_LOG_FILE=logs/error.log
CONSOLE_LOG_LEVEL=silly # default is silly, i.e. log everything - see the README for allowed levels
LOG_LEVEL=silly # default is silly
LOG_LEVEL=silly # default is silly

# Git specific environment variables
CRED_STATUS_REPO_OWNER=digitalcredentials
CRED_STATUS_REPO_NAME=credential-status-test-jc
CRED_STATUS_META_REPO_NAME=credential-status-metadata-test-jc
CRED_STATUS_ACCESS_TOKEN=REPLACE_THIS_WITH_A_GITHUB_ACCESS_TOKEN
42 changes: 31 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,46 @@ The `/credentials/status` endpoint corresponds to the [VC-API /credentials/statu

## Environment Variables

There is a sample .env file provided called .env.example to help you get started with your own .env file. The supported fields:
We provide support for managing credential status in a variety of storage services. Currently, we support a [database integration](https://github.com/digitalcredentials/status-list-manager-db) for MongoDB and [Git integrations](https://github.com/digitalcredentials/status-list-manager-git) for GitHub and GitLab. For each service category, we have provided a sample `.env.*.example` file that you can use to initialize a `.env` file for your implementation.

#### General
Every credential status manager recognizes the following fields in an `.env` file:

| Key | Description | Default | Required |
| --- | --- | --- | --- |
| `CRED_STATUS_SERVICE` | name representing storage service used to manage credential status: `mongodb`, `github`, `gitlab` | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true |
| `CRED_STATUS_DID_SEED` | seed used to deterministically generate DID | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true |
| `PORT` | http port on which to run the express app | 4008 | no |
| `CRED_STATUS_OWNER` | name of the owner account (personal or organization) in the source control service that will host the credential status resources | no | yes if ENABLE_STATUS_ALLOCATION is true |
| `CRED_STATUS_REPO_NAME` | name of the credential status repository | no | yes if ENABLE_STATUS_ALLOCATION is true |
| `CRED_STATUS_META_REPO_NAME` | name of the credential status metadata repository | no | yes if ENABLE_STATUS_ALLOCATION is true |
| `CRED_STATUS_ACCESS_TOKEN` | Github access token for the credential status repositories | no | yes if ENABLE_STATUS_ALLOCATION is true |
| `CRED_STATUS_DID_SEED` | seed used to deterministically generate DID | no | yes if ENABLE_STATUS_ALLOCATION is true |
| `ERROR_LOG_FILE` | log file for all errors - see [Logging](#logging) | no | no |
| `LOG_ALL_FILE` | log file for everything - see [Logging](#logging) | no | no |
| `ERROR_LOG_FILE` | log file for all errors - see [Logging](#logging) | N/A | no |
| `LOG_ALL_FILE` | log file for everything - see [Logging](#logging) | N/A | no |
| `CONSOLE_LOG_LEVEL` | console log level - see [Logging](#logging) | silly | no |
| `LOG_LEVEL` | log level for application - see [Logging](#logging) | silly | no |

## Github Repositories
#### MongoDB
There is a sample `.env` file provided called `.env.db.example` to help you get started with your own `.env` file. In addition to the general fields, every Git credential status manager recognizes the following fields in an `.env` file:

| Key | Description | Default | Required |
| --- | --- | --- | --- |
| `STATUS_CRED_SITE_ORIGIN` | name of the owner account (personal or organization) in the source control service that will host the credential status resources | 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 |
| `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 |

You'll have to create two new github repositories that will be used exclusively to manage the status. Full details of the implementation are [here](https://github.com/digitalcredentials/status-list-manager-git)
#### Git
There is a sample `.env` file provided called `.env.git.example` to help you get started with your own `.env` file. In addition to the general fields, every Git credential status manager recognizes the following fields in an `.env` file:

For this MVP implementation of the issuer we've only exposed the github options, but if you would like to use gitlab instead, just let us know and we can expose those options.
| Key | Description | Default | Required |
| --- | --- | --- | --- |
| \*`CRED_STATUS_OWNER` | name of the owner account (personal or organization) in the source control service that will host the credential status resources | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true |
| \*`CRED_STATUS_REPO_NAME` | name of the credential status repository | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true |
| \*`CRED_STATUS_REPO_ID` | ID of the credential status repository | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true and if `CRED_STATUS_SERVICE` = `gitlab` |
| \*`CRED_STATUS_META_REPO_NAME` | name of the credential status metadata repository | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true |
| \*`CRED_STATUS_META_REPO_ID` | ID of the credential status metadata repository | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true and if `CRED_STATUS_SERVICE` = `gitlab` |
| `CRED_STATUS_ACCESS_TOKEN` | Github access token for the credential status repositories | N/A | yes if `ENABLE_STATUS_ALLOCATION` is true |

\* You'll have to create Git repositories for `CRED_STATUS_REPO_NAME` and `CRED_STATUS_META_REPO_NAME` under the ownership of `CRED_STATUS_OWNER`, as they will be used to manage credential status. Full details of the implementation are [here](https://github.com/digitalcredentials/status-list-manager-git).

## Signing key

Expand Down
39 changes: 18 additions & 21 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function setConfig() {
CONFIG = parseConfig();
}

function getCommonEnvs() {
function getGeneralEnvs() {
const env = process.env;
return {
enableHttpsForDev: env.ENABLE_HTTPS_FOR_DEV?.toLowerCase() === 'true',
Expand All @@ -22,6 +22,18 @@ function getCommonEnvs() {
};
}

function getMongoDbEnvs() {
const env = process.env;
return {
statusCredentialSiteOrigin: 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
};
}

function getGitHubEnvs() {
const env = process.env;
return {
Expand All @@ -42,37 +54,25 @@ function getGitLabEnvs() {
};
}

function getMongoDbEnvs() {
const env = process.env;
return {
statusCredentialSiteOrigin: 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
};
}

function parseConfig() {
const env = process.env
let serviceSpecificEnvs;
switch (env.CRED_STATUS_SERVICE) {
case 'mongodb':
serviceSpecificEnvs = getMongoDbEnvs();
break;
case 'github':
serviceSpecificEnvs = getGitHubEnvs();
break;
case 'gitlab':
serviceSpecificEnvs = getGitLabEnvs();
break;
case 'mongodb':
serviceSpecificEnvs = getMongoDbEnvs();
break;
default:
throw new Error('Encountered unsupported credential status service');
}
const commonEnvs = getCommonEnvs();
const generalEnvs = getGeneralEnvs();
const config = Object.freeze({
...commonEnvs,
...generalEnvs,
...serviceSpecificEnvs
});
return config
Expand All @@ -88,6 +88,3 @@ export function getConfig() {
export function resetConfig() {
CONFIG = null;
}



62 changes: 31 additions & 31 deletions src/status.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import {
createStatusManager as createStatusManagerGit
} from '@digitalcredentials/credential-status-manager-git';
import {
createStatusManager as createStatusManagerDb
} from '@digitalcredentials/credential-status-manager-db';
import {
createStatusManager as createStatusManagerGit
} from '@digitalcredentials/credential-status-manager-git';
import { getConfig } from './config.js';

const {
// Database env vars
statusCredentialSiteOrigin,
credStatusDatabaseUrl,
credStatusDatabaseHost,
credStatusDatabasePort,
credStatusDatabaseUsername,
credStatusDatabasePassword,

// Git env vars
credStatusService,
credStatusRepoName,
Expand All @@ -15,19 +23,27 @@ const {
credStatusMetaRepoId,
credStatusOwnerAccountName,
credStatusAccessToken,
credStatusDidSeed,

// Database env vars
statusCredentialSiteOrigin,
credStatusDatabaseUrl,
credStatusDatabaseHost,
credStatusDatabasePort,
credStatusDatabaseUsername,
credStatusDatabasePassword
credStatusDidSeed
} = getConfig();

let STATUS_LIST_MANAGER;

async function createDatabaseStatusManager() {
return createStatusManagerDb({
statusCredentialSiteOrigin,
databaseService: credStatusService,
databaseUrl: credStatusDatabaseUrl,
databaseHost: credStatusDatabaseHost,
databasePort: credStatusDatabasePort,
databaseUsername: credStatusDatabaseUsername,
databasePassword: credStatusDatabasePassword,
didMethod: 'key',
didSeed: credStatusDidSeed,
signUserCredential: false,
signStatusCredential: true
});
}

async function createGitHubStatusManager() {
return createStatusManagerGit({
service: credStatusService,
Expand Down Expand Up @@ -60,22 +76,6 @@ async function createGitLabStatusManager() {
});
}

async function createMongoDbStatusManager() {
return createStatusManagerDb({
statusCredentialSiteOrigin,
databaseService: credStatusService,
databaseUrl: credStatusDatabaseUrl,
databaseHost: credStatusDatabaseHost,
databasePort: credStatusDatabasePort,
databaseUsername: credStatusDatabaseUsername,
databasePassword: credStatusDatabasePassword,
didMethod: 'key',
didSeed: credStatusDidSeed,
signUserCredential: false,
signStatusCredential: true
});
}

/* we allow passing in a status manager, for testing */
async function initializeStatusManager(statusManager) {
if (statusManager) {
Expand All @@ -86,15 +86,15 @@ async function initializeStatusManager(statusManager) {
}

switch (credStatusService) {
case 'mongodb':
STATUS_LIST_MANAGER = await createDatabaseStatusManager();
break;
case 'github':
STATUS_LIST_MANAGER = await createGitHubStatusManager();
break;
case 'gitlab':
STATUS_LIST_MANAGER = await createGitLabStatusManager();
break;
case 'mongodb':
STATUS_LIST_MANAGER = await createMongoDbStatusManager();
break;
default:
throw new Error('Encountered unsupported credential status service');
}
Expand Down

0 comments on commit d4e12a2

Please sign in to comment.