Skip to content

Commit c817701

Browse files
committed
feat: registry using local db
1 parent e24b745 commit c817701

File tree

6 files changed

+82
-17
lines changed

6 files changed

+82
-17
lines changed

infrastructure/evault-provisioner/src/index.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import { provisionEVault } from "./templates/evault.nomad.js";
44
import dotenv from "dotenv";
55
import { W3IDBuilder } from "w3id";
66
import * as jose from "jose";
7+
import path from "path";
8+
import { fileURLToPath } from "url";
79

8-
dotenv.config();
10+
const __filename = fileURLToPath(import.meta.url);
11+
const __dirname = path.dirname(__filename);
12+
dotenv.config({ path: path.resolve(__dirname, "../../../.env") });
913

1014
const app = express();
11-
const port = process.env.PORT || 3000;
15+
const port = process.env.PORT || 3001;
1216

1317
app.use(express.json());
1418

@@ -37,6 +41,8 @@ app.post(
3741
res: Response<ProvisionResponse>,
3842
) => {
3943
try {
44+
45+
if (!process.env.REGISTRY_URI) throw new Error("REGISTRY_URI is not set");
4046
const { registryEntropy, namespace } = req.body;
4147

4248
if (!registryEntropy || !namespace) {
@@ -67,10 +73,22 @@ app.post(
6773
const uri = await provisionEVault(w3id, evaultId.id);
6874

6975

76+
await axios.post(new URL("/register", process.env.REGISTRY_URI).toString(), {
77+
ename: w3id,
78+
uri,
79+
evault: evaultId.id,
80+
}, {
81+
headers: {
82+
"Authorization": `Bearer ${process.env.REGISTRY_SHARED_SECRET}`
83+
}
84+
});
85+
7086
res.json({
7187
success: true,
7288
uri,
7389
});
90+
91+
7492
} catch (error) {
7593
const axiosError = error as AxiosError;
7694
res.status(500).json({

infrastructure/web3-adapter/src/__tests__/evault.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const EVaultEndpoint = "http://localhost:4000/graphql";
55

66
async function queryGraphQL(
77
query: string,
8-
variables: Record<string, any> = {},
8+
variables: Record<string, unknown> = {},
99
) {
1010
const response = await fetch(EVaultEndpoint, {
1111
method: "POST",

infrastructure/web3-adapter/src/adapter.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export type FieldMapping = {
22
sourceField: string;
33
targetField: string;
4-
transform?: (value: any) => any;
4+
transform?: (value: unknown) => unknown;
55
};
66

77
export class Web3Adapter {
@@ -17,14 +17,14 @@ export class Web3Adapter {
1717

1818
public toUniversal(
1919
platform: string,
20-
data: Record<string, any>,
21-
): Record<string, any> {
20+
data: Record<string, unknown>,
21+
): Record<string, unknown> {
2222
const mappings = this.mappings.get(platform);
2323
if (!mappings) {
2424
throw new Error(`No mappings found for platform: ${platform}`);
2525
}
2626

27-
const result: Record<string, any> = {};
27+
const result: Record<string, unknown> = {};
2828
for (const mapping of mappings) {
2929
if (data[mapping.sourceField] !== undefined) {
3030
const value = mapping.transform
@@ -38,14 +38,14 @@ export class Web3Adapter {
3838

3939
public fromUniversal(
4040
platform: string,
41-
data: Record<string, any>,
42-
): Record<string, any> {
41+
data: Record<string, unknown>,
42+
): Record<string, unknown> {
4343
const mappings = this.mappings.get(platform);
4444
if (!mappings) {
4545
throw new Error(`No mappings found for platform: ${platform}`);
4646
}
4747

48-
const result: Record<string, any> = {};
48+
const result: Record<string, unknown> = {};
4949
for (const mapping of mappings) {
5050
if (data[mapping.targetField] !== undefined) {
5151
const value = mapping.transform

platforms/registry/src/config/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as dotenv from "dotenv"
44
import { join } from "path"
55

66
// Load environment variables from root .env file
7-
dotenv.config({ path: join(__dirname, "../../../.env") })
7+
dotenv.config({ path: join(__dirname, "../../../../.env") })
88

99
export const AppDataSource = new DataSource({
1010
type: "postgres",

platforms/registry/src/index.ts

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,40 @@ const initializeDatabase = async () => {
2323
// Initialize VaultService
2424
const vaultService = new VaultService(AppDataSource.getRepository("Vault"));
2525

26+
// Middleware to check shared secret
27+
const checkSharedSecret = async (request: any, reply: any) => {
28+
const authHeader = request.headers.authorization;
29+
if (!authHeader || !authHeader.startsWith('Bearer ')) {
30+
return reply.status(401).send({ error: 'Missing or invalid authorization header' });
31+
}
32+
33+
const secret = authHeader.split(' ')[1];
34+
if (secret !== process.env.REGISTRY_SHARED_SECRET) {
35+
return reply.status(401).send({ error: 'Invalid shared secret' });
36+
}
37+
};
38+
39+
// Create a new vault entry
40+
server.post("/register", {
41+
preHandler: checkSharedSecret
42+
}, async (request, reply) => {
43+
try {
44+
const { ename, uri, evault } = request.body as { ename: string; uri: string; evault: string };
45+
46+
if (!ename || !uri || !evault) {
47+
return reply.status(400).send({
48+
error: "Missing required fields. Please provide ename, uri, and evault"
49+
});
50+
}
51+
52+
const vault = await vaultService.create(ename, uri, evault);
53+
return reply.status(201).send(vault);
54+
} catch (error) {
55+
server.log.error(error);
56+
reply.status(500).send({ error: "Failed to create vault entry" });
57+
}
58+
});
59+
2660
// Generate and return a signed JWT with entropy
2761
server.get("/entropy", async (request, reply) => {
2862
try {
@@ -53,16 +87,15 @@ server.get("/resolve", async (request, reply) => {
5387
return reply.status(400).send({ error: "w3id parameter is required" });
5488
}
5589

56-
const service = await vaultService.findByEname(w3id);
57-
58-
if (!service) {
90+
const vault = await vaultService.findByEname(w3id);
91+
if (!vault) {
5992
return reply.status(404).send({ error: "Service not found" });
6093
}
6194

6295
return {
63-
ename: service.ename,
64-
uri: service.uri,
65-
evault: service.evault
96+
ename: vault.ename,
97+
uri: vault.uri,
98+
evault: vault.evault
6699
};
67100
} catch (error) {
68101
server.log.error(error);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class Migration1748865359304 implements MigrationInterface {
4+
name = 'Migration1748865359304'
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`CREATE TABLE "vault" ("id" SERIAL NOT NULL, "ename" character varying NOT NULL, "uri" character varying NOT NULL, "evault" character varying NOT NULL, CONSTRAINT "PK_dd0898234c77f9d97585171ac59" PRIMARY KEY ("id"))`);
8+
}
9+
10+
public async down(queryRunner: QueryRunner): Promise<void> {
11+
await queryRunner.query(`DROP TABLE "vault"`);
12+
}
13+
14+
}

0 commit comments

Comments
 (0)