Skip to content

Commit

Permalink
adds db status service -and converts to bsl
Browse files Browse the repository at this point in the history
  • Loading branch information
kezike committed Apr 20, 2024
1 parent 0c2f8b0 commit 0d9da47
Show file tree
Hide file tree
Showing 14 changed files with 580 additions and 217 deletions.
15 changes: 7 additions & 8 deletions .coordinator.env
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# default port is 4005
# PORT=4005
# PORT=4005

# ONLY for development when we need https; default is false
# ENABLE_HTTPS_FOR_DEV=false
# ENABLE_HTTPS_FOR_DEV=false

# default is false
# ENABLE_ACCESS_LOGGING=true
# default is false
ENABLE_STATUS_SERVICE=true
# default is false
ENABLE_STATUS_SERVICE=true

# set the service endpoints
# defaults are as follows
# STATUS_SERVICE_ENDPOINT=STATUS:4008
# SIGNING_SERVICE_ENDPOINT=SIGNER:4006
# defaults are as follows
# SIGNING_SERVICE=SIGNER:4006
# STATUS_SERVICE=STATUS:4008

# Tokens for protecting tenant endpoints.
# Add a token for any tenant name,
Expand All @@ -30,4 +30,3 @@ TENANT_TOKEN_RANDOM_TESTING=UNPROTECTED
# (for tenant name econ101):
# http://myhost.org/instance/econ101/credentials/issue
# http://myhost.org/instance/econ101/credentials/status

9 changes: 4 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
PORT=4005 #default port is 4005
ENABLE_HTTPS_FOR_DEV=false # ONLY for development when need https; default is false
ENABLE_ACCESS_LOGGING=true
ENABLE_HTTPS_FOR_DEV=false # ONLY for development when need https; default is false
ENABLE_ACCESS_LOGGING=true
ENABLE_STATUS_SERVICE=false

STATUS_SERVICE_ENDPOINT=localhost:4008
SIGNING_SERVICE_ENDPOINT=localhost:4006
SIGNING_SERVICE=localhost:4006
STATUS_SERVICE=localhost:4008

# Tokens for protecting tenant endpoints.
# Add a token for any tenant name,
Expand All @@ -18,4 +18,3 @@ TENANT_TOKEN_TESTING=ohno
# The tenant name is specified in the issuing/status invocations like so:
# http://myhost.org/instance/econ101/credentials/issue
# http://myhost.org/instance/econ101/credentials/status

6 changes: 3 additions & 3 deletions .signing-service.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#default port is 4006
#PORT=4006
#PORT=4006
# ONLY for dev when need https; default is false
#ENABLE_HTTPS_FOR_DEV=false

# DID seeds for generating signing keys.
# One seed per 'tenant'.
# Add DID SEEDS with the pattern TENANT_SEED_[tenant name]
Expand All @@ -15,4 +15,4 @@
# will be destroyed on restart
TENANT_SEED_UN_PROTECTED_TEST=z1AoLPRWHSKasPH1unbY1A6ZFF2Pdzzp7D2CkpK6YYYdKTN
TENANT_SEED_PROTECTED_TEST=z1AhT5czCXgNw8fjgz8y3s8AHjBYcpRKH8i9YYbjdCwVRak
TENANT_SEED_RANDOM_TESTING=generate
TENANT_SEED_RANDOM_TESTING=generate
259 changes: 150 additions & 109 deletions README.md

Large diffs are not rendered by default.

33 changes: 21 additions & 12 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,27 @@ import invalidPathHandler from './middleware/invalidPathHandler.js'
import verifyAuthHeader from './verifyAuthHeader.js'
import { getConfig } from './config.js'

function IssuingException (code, message, error = null) {
this.code = code
this.error = error
this.message = message
class IssuingException extends Error {
constructor(code, message, error = null) {

Check failure on line 12 in src/app.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Missing space before function parentheses
super(message)
this.code = code
this.error = error
this.message = message
}
}

async function callService (endpoint, body) {
const { data } = await axios.post(endpoint, body)
return data
}

export async function build (opts = {}) {
const { enableStatusService, statusServiceEndpoint, signingServiceEndpoint } = getConfig()
const {
enableStatusService,
statusService,
signingService
} = getConfig()

const app = express()
// Add the middleware to write access logs
app.use(accessLogger())
Expand All @@ -30,7 +39,7 @@ export async function build (opts = {}) {
app.get('/', async function (req, res, next) {
if (enableStatusService) {
try {
await axios.get(`http://${statusServiceEndpoint}/`)
await axios.get(`http://${statusService}/`)
} catch (e) {
next({
message: 'status service is NOT running.',
Expand All @@ -40,7 +49,7 @@ export async function build (opts = {}) {
}
}
try {
await axios.get(`http://${signingServiceEndpoint}/`)
await axios.get(`http://${signingService}/`)
} catch (e) {
next({
message: 'signing service is NOT running.',
Expand All @@ -57,7 +66,7 @@ export async function build (opts = {}) {
})

app.get('/seedgen', async (req, res, next) => {
const response = await axios.get(`http://${signingServiceEndpoint}/seedgen`)
const response = await axios.get(`http://${signingService}/seedgen`)
return res.json(response.data)
})

Expand All @@ -72,9 +81,9 @@ export async function build (opts = {}) {
// NOTE: we throw the error here which will then be caught by middleware errorhandler
if (!unSignedVC || !Object.keys(unSignedVC).length) throw new IssuingException(400, 'A verifiable credential must be provided in the body')
const vcWithStatus = enableStatusService
? await callService(`http://${statusServiceEndpoint}/credentials/status/allocate`, unSignedVC)
? await callService(`http://${statusService}/credentials/status/allocate`, unSignedVC)
: unSignedVC
const signedVC = await callService(`http://${signingServiceEndpoint}/instance/${tenantName}/credentials/sign`, vcWithStatus)
const signedVC = await callService(`http://${signingService}/instance/${tenantName}/credentials/sign`, vcWithStatus)
return res.json(signedVC)
} catch (error) {
// have to catch async errors and forward error handling
Expand All @@ -84,7 +93,7 @@ export async function build (opts = {}) {
})

// updates the status
// the body will look like: {credentialId: '23kdr', credentialStatus: [{type: 'StatusList2021Credential', status: 'revoked'}]}
// the body will look like: {credentialId: '23kdr', credentialStatus: [{type: 'BitstringStatusListCredential', status: 'revoked'}]}
app.post('/instance/:tenantName/credentials/status',
async (req, res, next) => {
if (!enableStatusService) return res.status(405).send('The status service has not been enabled.')
Expand All @@ -95,7 +104,7 @@ export async function build (opts = {}) {
await verifyAuthHeader(authHeader, tenantName)
// NOTE: we throw the error here which will then be caught by middleware errorhandler
if (!statusUpdate || !Object.keys(statusUpdate).length) throw new IssuingException(400, 'A status update must be provided in the body.')
const updateResult = await callService(`http://${statusServiceEndpoint}/credentials/status`, statusUpdate)
const updateResult = await callService(`http://${statusService}/credentials/status`, statusUpdate)
return res.json(updateResult)
} catch (error) {
if (error.response?.status === 404) {
Expand Down
4 changes: 2 additions & 2 deletions src/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('api', () => {
// testDIDSeed = await decodeSeed(process.env.TENANT_SEED_TESTING)
testTenantToken = process.env.TENANT_TOKEN_PROTECTED_TEST
testTenantToken2 = process.env.TENANT_TOKEN_PROTECTED_TEST_2
statusUpdateBody = { credentialId: 'urn:uuid:951b475e-b795-43bc-ba8f-a2d01efd2eb1', credentialStatus: [{ type: 'StatusList2021Credential', status: 'revoked' }] }
statusUpdateBody = { credentialId: 'urn:uuid:951b475e-b795-43bc-ba8f-a2d01efd2eb1', credentialStatus: [{ type: 'BitstringStatusListCredential', status: 'revoked' }] }
})

after(() => {
Expand All @@ -39,7 +39,7 @@ describe('api', () => {
describe('GET /', () => {
it('GET / => hello', done => {
nock('http://localhost:4006').get('/').reply(200, 'signing-service server status: ok.')
nock('http://localhost:4008').get('/').reply(200, 'signing-service server status: ok.')
nock('http://localhost:4008').get('/').reply(200, 'status-service server status: ok.')

request(app)
.get('/')
Expand Down
8 changes: 4 additions & 4 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const randomTenantName = 'random'
const randtomTenantToken = 'UNPROTECTED'
const defaultTenantToken = 'UNPROTECTED'

const defaultStatusServiceEndpoint = 'STATUS:4008'
const defaultSigningServiceEndpoint = 'SIGNER:4006'
const defaultSigningService = 'SIGNER:4006'
const defaultStatusService = 'STATUS:4008'

// we set a default tenant
// It will be overwritten by whatever value is set for default in .env
Expand Down Expand Up @@ -37,8 +37,8 @@ function parseConfig () {
enableHttpsForDev: env.ENABLE_HTTPS_FOR_DEV?.toLowerCase() === 'true',
enableAccessLogging: env.ENABLE_ACCESS_LOGGING?.toLowerCase() === 'true',
enableStatusService: env.ENABLE_STATUS_SERVICE?.toLowerCase() === 'true',
statusServiceEndpoint: env.STATUS_SERVICE_ENDPOINT ? env.STATUS_SERVICE_ENDPOINT : defaultStatusServiceEndpoint,
signingServiceEndpoint: env.SIGNING_SERVICE_ENDPOINT ? env.SIGNING_SERVICE_ENDPOINT : defaultSigningServiceEndpoint,
signingService: env.SIGNING_SERVICE ?? defaultSigningService,
statusService: env.STATUS_SERVICE ?? defaultStatusService,
port: env.PORT ? parseInt(env.PORT) : defaultPort
})
return config
Expand Down
12 changes: 5 additions & 7 deletions src/test-fixtures/.env.testing
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@

#PORT=4007
#ENABLE_HTTPS_FOR_DEV=false
SIGNING_SERVICE_ENDPOINT=localhost:4006
STATUS_SERVICE_ENDPOINT=localhost:4008
#ENABLE_HTTPS_FOR_DEV=false
SIGNING_SERVICE=localhost:4006
STATUS_SERVICE=localhost:4008
ENABLE_STATUS_SERVICE=true


# we deliberately don't set a token for the third tenant to test that the call is still allowed
# i.e,. we want to allow some tenants to work without a token.
# we deliberately don't set a token for the third tenant to test that the call is still allowed
# i.e,. we want to allow some tenants to work without a token.
TENANT_TOKEN_UN_PROTECTED_TEST=UNPROTECTED
TENANT_TOKEN_PROTECTED_TEST=jds
TENANT_TOKEN_PROTECTED_TEST_2=hgf
Expand Down
Loading

0 comments on commit 0d9da47

Please sign in to comment.