Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions 40 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,28 @@ Create `config.json` in the data directory and set its content to:
```
{
"$schema": "../../src/schemas/config.json",
"apiPort": 3000,
"p2pPort": 3001,
"api": {
"hostname": "localhost",
"port": 3000
},
"p2p": {
"hostname": "localhost",
"port": 3001
},
"apiKey": "xxxxx",
"peers": [
{
"name": "org-b",
"id": "org-b",
"endpoint": "https://localhost:4001"
}
]
}
```

Based on this configuration:
- Port 3000 will be used to access the API
- Port 3001 will be used for P2P communications
- The API key will be set to `xxxxx`
- API will be accessed via localhost:3000
- P2P communications will use localhost:3001
- The API key will be set to `xxxxx` (this is optional)
- There is one peer named `org-b` whose P2P endpoint is `https://localhost:4001`

#### Generate certificate
Expand All @@ -63,22 +69,28 @@ export LOG_LEVEL=info
```
{
"$schema": "../../src/schemas/config.json",
"apiPort": 4000,
"p2pPort": 4001,
"apiKey": "yyyyy",
"api": {
"hostname": "localhost",
"port": 4000
},
"p2p": {
"hostname": "localhost",
"port": 4001
},
"apiKey": "xxxxx",
"peers": [
{
"name": "org-b",
"endpoint": "https://localhost:4001"
"id": "org-b",
"endpoint": "https://localhost:3001"
}
]
}
```

Based on this configuration:
- Port 4000 will be used to access the API
- Port 4001 will be used for P2P communications
- The API key will be set to `yyyyy`
- API will be accessed via localhost:4000
- P2P communications will use localhost:4001
- The API key will be set to `xxxxx` (this is optional)
- There is one peer named `org-a` whose P2P endpoint is `https://localhost:3001`


Expand Down
12 changes: 9 additions & 3 deletions 12 data/member-a/config.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{
"$schema": "../../src/schemas/config.json",
"apiPort": 3000,
"p2pPort": 3001,
"api": {
"hostname": "localhost",
"port": 3000
},
"p2p": {
"hostname": "localhost",
"port": 3001
},
"apiKey": "xxxxx",
"peers": [
{
"name": "org-b",
"id": "org-b",
"endpoint": "https://localhost:4001"
}
]
Expand Down
14 changes: 10 additions & 4 deletions 14 data/member-b/config.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{
"$schema": "../../src/schemas/config.json",
"apiPort": 4000,
"p2pPort": 4001,
"apiKey": "yyyyy",
"api": {
"hostname": "localhost",
"port": 4000
},
"p2p": {
"hostname": "localhost",
"port": 4001
},
"apiKey": "xxxxx",
"peers": [
{
"name": "org-a",
"id": "org-b",
"endpoint": "https://localhost:3001"
}
]
Expand Down
17 changes: 14 additions & 3 deletions 17 package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion 4 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
"author": "",
"license": "ISC",
"dependencies": {
"ajv": "^8.4.0",
"ajv": "^8.5.0",
"axios": "^0.21.1",
"bunyan": "^1.8.15",
"busboy": "^0.3.1",
"express": "^4.17.1",
"form-data": "^4.0.0",
"jsrsasign": "^10.3.0",
"swagger-ui-express": "^4.1.6",
"ts-node": "^9.1.1",
"typescript": "^4.2.4",
Expand All @@ -31,6 +32,7 @@
"@types/bunyan": "^1.8.6",
"@types/busboy": "^0.2.3",
"@types/express": "^4.17.11",
"@types/jsrsasign": "^8.0.12",
"@types/node": "^15.0.3",
"@types/swagger-ui-express": "^4.1.2",
"@types/ws": "^7.4.4",
Expand Down
17 changes: 9 additions & 8 deletions 17 src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ export const start = async () => {
p2pEventEmitter.addListener('event', event => eventsHandler.queueEvent(event));
blobsEventEmitter.addListener('event', event => eventsHandler.queueEvent(event));
messagesEventEmitter.addListener('event', event => eventsHandler.queueEvent(event));

eventsHandler.eventEmitter.addListener('event', event => {
if(delegatedWebSocket !== undefined) {
if (delegatedWebSocket !== undefined) {
delegatedWebSocket.send(JSON.stringify(event));
}
});

const assignWebSocketDelegate = (webSocket: WebSocket) => {
delegatedWebSocket = webSocket;
const event = eventsHandler.getCurrentEvent();
Expand All @@ -96,7 +96,7 @@ export const start = async () => {
};

wss.on('connection', (webSocket: WebSocket) => {
if(delegatedWebSocket === undefined) {
if (delegatedWebSocket === undefined) {
assignWebSocketDelegate(webSocket);
}
});
Expand All @@ -107,7 +107,7 @@ export const start = async () => {
if (req.path === '/') {
res.redirect('/swagger');
} else {
if (req.headers['x-api-key'] !== config.apiKey) {
if (config.apiKey !== undefined && req.headers['x-api-key'] !== config.apiKey) {
next(new RequestError('Unauthorized', 401));
} else {
next();
Expand All @@ -123,9 +123,10 @@ export const start = async () => {
p2pApp.use('/api/v1', p2pRouter);
p2pApp.use(errorHandler);

const apiServerPromise = new Promise<void>(resolve => apiServer.listen(config.apiPort, () => resolve()));
const p2pServerPromise = new Promise<void>(resolve => p2pServer.listen(config.p2pPort, () => resolve()));
const apiServerPromise = new Promise<void>(resolve => apiServer.listen(config.api.port, config.api.hostname, () => resolve()));
const p2pServerPromise = new Promise<void>(resolve => p2pServer.listen(config.p2p.port, config.p2p.hostname, () => resolve()));
await Promise.all([apiServerPromise, p2pServerPromise]);
log.info(`Data exchange listening on ports ${config.apiPort} (API) and ${config.p2pPort} (P2P) - log level "${utils.constants.LOG_LEVEL}"`);
log.info(`Data exchange running on http://${config.api.hostname}:${config.api.port} (API) and ` +
`https://${config.p2p.hostname}:${config.p2p.port} (P2P) - log level "${utils.constants.LOG_LEVEL}"`);

};
1 change: 1 addition & 0 deletions 1 src/custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ declare global{
getPeerCertificate: () => {
issuer: {
O: string
OU: string
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions 3 src/lib/cert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ const log = createLogger({ name: 'lib/certs.ts', level: utils.constants.LOG_LEVE
export let key: string;
export let cert: string;
export let ca: string[] = [];
export let peerID: string;

export const init = async () => {
key = (await fs.readFile(path.join(utils.constants.DATA_DIRECTORY, utils.constants.KEY_FILE))).toString();
cert = (await fs.readFile(path.join(utils.constants.DATA_DIRECTORY, utils.constants.CERT_FILE))).toString();
const certData = utils.getCertData(cert);
peerID = utils.getPeerID(certData.organization, certData.organizationUnit);
await loadCAs();
};

Expand Down
21 changes: 16 additions & 5 deletions 21 src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@
// limitations under the License.

export interface IConfig {
apiPort: number
p2pPort: number
apiKey: string
api: {
hostname: string
port: number
}
p2p: {
hostname: string
port: number
}
apiKey?: string
peers: {
name: string
id: string
endpoint: string
}[]
}
Expand Down Expand Up @@ -107,7 +113,12 @@ export type BlobTask = {
export interface IStatus {
messageQueueSize: number
peers: {
name: string
id: string
available: boolean
}[]
}

export interface ICertData {
organization?: string
organizationUnit?: string
}
33 changes: 32 additions & 1 deletion 33 src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

import { Request } from 'express';
import { promises as fs } from 'fs';
import { IFile } from './interfaces';
import { ICertData, IFile } from './interfaces';
import RequestError from './request-error';
import Busboy from 'busboy';
import axios, { AxiosRequestConfig } from 'axios';
import { createLogger, LogLevelString } from 'bunyan';
import { X509 } from 'jsrsasign';

export const constants = {
LOG_LEVEL: process.env.LOG_LEVEL || 'info',
Expand Down Expand Up @@ -115,3 +116,33 @@ export const axiosWithRetry = async (config: AxiosRequestConfig) => {
}
throw currentError;
};

export const getPeerID = (organization: string | undefined, organizationUnit: string | undefined) => {
if(organization !== undefined) {
if(organizationUnit !== undefined) {
return `${organization}-${organizationUnit}`;
} else {
return organization;
}
} else if(organizationUnit !== undefined) {
return organizationUnit;
} else {
throw new Error('Invalid peer');
}
};

export const getCertData = (cert: string): ICertData => {
const x509 = new X509();
x509.readCertPEM(cert);
const subject = x509.getSubjectString();
const o = subject.match(/O=(.+[^/])/);
let certData: ICertData = {};
if(o !== null) {
certData.organization = o[1];
}
const ou = subject.match(/OU=(.+[^/])/);
if(ou !== null) {
certData.organizationUnit = ou[1];
}
return certData;
};
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.