Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Env var support in agent configs #89

Draft
wants to merge 3 commits into
base: feat/VDX-308
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions packages/agent/conf/demos/dbc/oid4vci_metadata/dbc.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"correlationId": "https://ssi.dutchblockchaincoalition.org/agent",
"correlationId": "${AGENT_ENDPOINT_DBC ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"overwriteExisting": true,
"metadata": {
"credential_issuer": "https://ssi.dutchblockchaincoalition.org/agent",
"credential_endpoint": "https://ssi.dutchblockchaincoalition.org/agent/credentials",
"credential_issuer": "${AGENT_ENDPOINT_DBC ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"credential_endpoint": "${AGENT_ENDPOINT_DBC ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}/credentials",
"display": [
{
"name": "Dutch Blockchain Coalition",
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/conf/demos/dbc/oid4vci_options/dbc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"definitionId": "dbc2023",
"correlationId": "https://ssi.dutchblockchaincoalition.org/agent",
"correlationId": "${AGENT_ENDPOINT_DBC ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"issuerOpts": {
"didOpts": {
"checkLinkedDomains": "if_present"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"correlationId": "https://ssi.energyshr.test.sphereon.com/issuer",
"correlationId": "${AGENT_ENDPOINT_ESHR ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"overwriteExisting": true,
"metadata": {
"credential_issuer": "https://ssi.energyshr.test.sphereon.com/issuer",
"credential_endpoint": "https://ssi.energyshr.test.sphereon.com/issuer/credentials",
"credential_issuer": "${AGENT_ENDPOINT_ESHR ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"credential_endpoint": "${AGENT_ENDPOINT_ESHR ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}/credentials",
"display": [
{
"name": "EnergySHR",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"definitionId": "energySHR",
"correlationId": "https://ssi.energyshr.test.sphereon.com/issuer",
"correlationId": "${AGENT_ENDPOINT_ESHR ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"issuerOpts": {
"didOpts": {
"checkLinkedDomains": "if_present"
Expand Down
6 changes: 3 additions & 3 deletions packages/agent/conf/demos/fmdm/oid4vci_metadata/fmdm.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"correlationId": "https://ssi.future-mobility-alliance.org/issuer",
"correlationId": "${AGENT_ENDPOINT_FMDM ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"overwriteExisting": true,
"metadata": {
"credential_issuer": "https://ssi.future-mobility-alliance.org/issuer",
"credential_endpoint": "https://ssi.future-mobility-alliance.org/issuer/credentials",
"credential_issuer": "${AGENT_ENDPOINT_FMDM ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"credential_endpoint": "${AGENT_ENDPOINT_FMDM ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}/credentials",
"display": [
{
"name": "Future Mobility Alliance",
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/conf/demos/fmdm/oid4vci_options/fmdm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"definitionId": "fmdm2023",
"correlationId": "https://ssi.future-mobility-alliance.org/issuer",
"correlationId": "${AGENT_ENDPOINT_FMDM ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"issuerOpts": {
"didOpts": {
"checkLinkedDomains": "if_present"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"correlationId": "https://notary.demo.sphereon.com/agent",
"correlationId": "${AGENT_ENDPOINT_NOTARY ?? AGENT_ENDPOINT ?? http://localhost:5000'}",
"overwriteExisting": true,
"metadata": {
"credential_issuer": "https://notary.demo.sphereon.com/agent",
"credential_endpoint": "https://notary.demo.sphereon.com/agent/credentials",
"credential_issuer": "${AGENT_ENDPOINT_NOTARY ?? AGENT_ENDPOINT ?? http://localhost:5000'}",
"credential_endpoint": "${AGENT_ENDPOINT_NOTARY ?? AGENT_ENDPOINT ?? http://localhost:5000'}/credentials",
"display": [
{
"name": "Van Auckland notaries",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"definitionId": "knb-auckland",
"correlationId": "https://notary.demo.sphereon.com/agent",
"correlationId": "${AGENT_ENDPOINT_NOTARY ?? AGENT_ENDPOINT ?? http://localhost:5000'}",
"issuerOpts": {
"didOpts": {
"checkLinkedDomains": "if_present"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"correlationId": "https://bank.demo.sphereon.com/agent",
"correlationId": "${AGENT_ENDPOINT_BANK ?? AGENT_ENDPOINT ?? http://localhost:5000'}",
"overwriteExisting": true,
"metadata": {
"credential_issuer": "https://bank.demo.sphereon.com/agent",
"credential_endpoint": "https://bank.demo.sphereon.com/agent/credentials",
"credential_issuer": "${AGENT_ENDPOINT_BANK ?? AGENT_ENDPOINT ?? http://localhost:5000'}",
"credential_endpoint": "${AGENT_ENDPOINT_BANK ?? AGENT_ENDPOINT ?? http://localhost:5000'}/credentials",
"display": [
{
"name": "Babylon Bank",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"definitionId": "knb-bank",
"correlationId": "https://bank.demo.sphereon.com/agent",
"correlationId": "${AGENT_ENDPOINT_BANK ?? AGENT_ENDPOINT ?? http://localhost:5000'}",
"issuerOpts": {
"didOpts": {
"checkLinkedDomains": "if_present"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"definitionId": "knb-bank",
"correlationId": "https://bank.demo.sphereon.com/agent",
"correlationId": "${AGENT_ENDPOINT_BANK ?? AGENT_ENDPOINT ?? http://localhost:5000'}",
"issuerOpts": {
"didOpts": {
"checkLinkedDomains": "if_present"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"correlationId": "https://kvk.demo.sphereon.com/agent",
"correlationId": "${AGENT_ENDPOINT_KVK ?? AGENT_ENDPOINT ?? http://localhost:5000'}",
"overwriteExisting": true,
"metadata": {
"credential_issuer": "https://kvk.demo.sphereon.com/agent",
"credential_endpoint": "https://kvk.demo.sphereon.com/agent/credentials",
"credential_issuer": "${AGENT_ENDPOINT_KVK ?? AGENT_ENDPOINT ?? http://localhost:5000'}",
"credential_endpoint": "${AGENT_ENDPOINT_KVK ?? AGENT_ENDPOINT ?? http://localhost:5000'}/credentials",
"display": [
{
"name": "Kamer van koophandel",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"definitionId": "knb-kvk",
"correlationId": "https://kvk.demo.sphereon.com/agent",
"correlationId": "${AGENT_ENDPOINT_KVK ?? AGENT_ENDPOINT ?? http://localhost:5000'}",
"issuerOpts": {
"didOpts": {
"checkLinkedDomains": "if_present"
Expand Down
6 changes: 3 additions & 3 deletions packages/agent/conf/demos/pf3/oid4vci_metadata/pf3.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"correlationId": "https://ssi.sphereon.com/pf3",
"correlationId": "${AGENT_ENDPOINT_PF3 ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"overwriteExisting": true,
"metadata": {
"credential_issuer": "https://ssi.sphereon.com/pf3",
"credential_endpoint": "https://ssi.sphereon.com/pf3/credentials",
"credential_issuer": "${AGENT_ENDPOINT_PF3 ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"credential_endpoint": "${AGENT_ENDPOINT_PF3 ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}/credentials",
"display": [
{
"name": "Sphereon",
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/conf/demos/pf3/oid4vci_options/pf3.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"definitionId": "pf3",
"correlationId": "https://ssi.sphereon.com/pf3",
"correlationId": "${AGENT_ENDPOINT_PF3 ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"issuerOpts": {
"didOpts": {
"checkLinkedDomains": "if_present"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"correlationId": "http://internet.or.LAN.host:5000/sphereonGuest",
"correlationId": "${AGENT_ENDPOINT_SPHEREONGUEST ?? AGENT_ENDPOINT ?? 'http://localhost:5000' }",
"overwriteExisting": true,
"metadata": {
"credential_issuer": "http://internet.or.LAN.host:5000/sphereonGuest",
"credential_endpoint": "http://internet.or.LAN.host:5000/sphereonGuest/credentials",
"credential_issuer": "${AGENT_ENDPOINT_SPHEREONGUEST ?? AGENT_ENDPOINT ?? 'http://localhost:5000' }",
"credential_endpoint": "${AGENT_ENDPOINT_SPHEREONGUEST ?? AGENT_ENDPOINT ?? 'http://localhost:5000' }/credentials",
"display": [
{
"name": "Sphereon International",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"definitionId": "sphereonGuest",
"correlationId": "http://internet.or.LAN.host:5000/sphereonGuest",
"correlationId": "${AGENT_ENDPOINT_SPHEREONGUEST ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"issuerOpts": {
"didOpts": {
"checkLinkedDomains": "if_present"
Expand Down
6 changes: 3 additions & 3 deletions packages/agent/conf/demos/triall/oid4vci_metadata/triall.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"correlationId": "https://ssi.triall.io/issuer",
"correlationId": "${AGENT_ENDPOINT_TRIALL ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"overwriteExisting": true,
"metadata": {
"credential_issuer": "https://ssi.triall.io/issuer",
"credential_endpoint": "https://ssi.triall.io/issuer/credentials",
"credential_issuer": "${AGENT_ENDPOINT_TRIALL ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"credential_endpoint": "${AGENT_ENDPOINT_TRIALL ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}/credentials",
"display": [
{
"name": "Triall",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"definitionId": "triall2023",
"correlationId": "https://ssi.triall.io/issuer",
"correlationId": "${AGENT_ENDPOINT_TRIALL ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"issuerOpts": {
"didOpts": {
"checkLinkedDomains": "if_present"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"correlationId": "https://agent.banqup.demo.sphereon.com",
"correlationId": "${AGENT_ENDPOINT_BANQUP ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"overwriteExisting": true,
"metadata": {
"credential_issuer": "https://agent.banqup.demo.sphereon.com",
"credential_endpoint": "https://agent.banqup.demo.sphereon.com/credentials",
"credential_issuer": "${AGENT_ENDPOINT_BANQUP ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"credential_endpoint": "${AGENT_ENDPOINT_BANQUP ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}/credentials",
"display": [
{
"name": "Banqup",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"definitionId": "banqup",
"correlationId": "https://agent.banqup.demo.sphereon.com",
"correlationId": "${AGENT_ENDPOINT_BANQUP ?? AGENT_ENDPOINT ?? 'http://localhost:5000'}",
"issuerOpts": {
"didOpts": {
"checkLinkedDomains": "if_present"
Expand Down
39 changes: 30 additions & 9 deletions packages/agent/src/utils/generic.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import fs from "fs";
import fs from 'fs'

export function loadJsonFiles<T>({path}: { path: string }): {
names: string[],
fileNames: string[],
asObject: Record<string, T>
asObject: Record<string, T>,
asArray: T[]

} {
const fileNames = fs.readdirSync(path).filter(file => file.match(/\.json$/))
const names: string[] = []
Expand All @@ -20,8 +19,12 @@ export function loadJsonFiles<T>({path}: { path: string }): {
names.push(name)
files.push(fileName)
const jsonFilePath = `${path}/${fileName}`

try {
const object = JSON.parse(fs.readFileSync(jsonFilePath, 'utf8').toString()) as T
let content = fs.readFileSync(jsonFilePath, 'utf8').toString()
content = substituteEnvVars(content)

const object = JSON.parse(content) as T
asObject[name] = object
asArray.push(object)
} catch (e) {
Expand All @@ -32,28 +35,46 @@ export function loadJsonFiles<T>({path}: { path: string }): {
}
}
})

return {names, fileNames: files, asObject, asArray}
}

function substituteEnvVars(content: string): string {
const envVarRegex = /\$\{([^}]+)}/g;
return content.replace(envVarRegex, (_, vars) => {
const options = vars.split(/(?:\|\|)|(?:\?\?)/).map((opt: string) => opt.trim()).filter(Boolean);
for (const option of options) {
if (option.startsWith("'") && option.endsWith("'")) {
return option.slice(1, -1);
}
const envValue = process.env[option];
if (envValue !== undefined && envValue !== null && envValue.trim() !== '') {
return envValue;
}
}
return '';
});
}

/**
* The function builds a file path without missing or excess slashes
* @param segments
*/
export function normalizeFilePath(...segments: (string | null | undefined)[]): string {
let result = '';
let result = ''

for (let i = 0; i < segments.length; i++) {
const segment = segments[i];
const segment = segments[i]

if (segment !== null && segment !== undefined && segment !== '') {
if (i === 0) {
// For the first non-null and non-empty segment, remove the trailing slash if it exists
result += segment.replace(/\/$/, '');
result += segment.replace(/\/$/, '')
} else {
// For subsequent segments, ensure all slashes are present
result += `/${segment.replace(/^\//, '').replace(/\/$/, '')}`;
result += `/${segment.replace(/^\//, '').replace(/\/$/, '')}`
}
}
}
return result;
return result
}