107 lines
3.3 KiB
TypeScript
107 lines
3.3 KiB
TypeScript
import * as plugins from './smartacme.plugins.js';
|
|
import type { ICertManager } from './interfaces/certmanager.js';
|
|
import { SmartacmeCert } from './smartacme.classes.cert.js';
|
|
|
|
/**
|
|
* In-memory certificate manager for mongoless mode.
|
|
* Stores certificates in memory only and does not connect to MongoDB.
|
|
*/
|
|
export class MemoryCertManager implements ICertManager {
|
|
public interestMap: plugins.lik.InterestMap<string, SmartacmeCert>;
|
|
private certs: Map<string, SmartacmeCert> = new Map();
|
|
|
|
constructor() {
|
|
this.interestMap = new plugins.lik.InterestMap((domain) => domain);
|
|
}
|
|
|
|
public async init(): Promise<void> {
|
|
// no-op for in-memory store
|
|
}
|
|
|
|
public async retrieveCertificate(domainName: string): Promise<SmartacmeCert | null> {
|
|
return this.certs.get(domainName) ?? null;
|
|
}
|
|
|
|
public async storeCertificate(cert: SmartacmeCert): Promise<void> {
|
|
this.certs.set(cert.domainName, cert);
|
|
const interest = this.interestMap.findInterest(cert.domainName);
|
|
if (interest) {
|
|
interest.fullfillInterest(cert);
|
|
interest.markLost();
|
|
}
|
|
}
|
|
|
|
public async deleteCertificate(domainName: string): Promise<void> {
|
|
this.certs.delete(domainName);
|
|
}
|
|
|
|
public async close(): Promise<void> {
|
|
// no-op
|
|
}
|
|
/**
|
|
* Wipe all certificates from the in-memory store (for testing)
|
|
*/
|
|
public async wipe(): Promise<void> {
|
|
this.certs.clear();
|
|
// reset interest map
|
|
this.interestMap = new plugins.lik.InterestMap((domain) => domain);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* MongoDB-backed certificate manager using EasyStore from smartdata.
|
|
*/
|
|
export class MongoCertManager implements ICertManager {
|
|
public interestMap: plugins.lik.InterestMap<string, SmartacmeCert>;
|
|
private db: plugins.smartdata.SmartdataDb;
|
|
private store: plugins.smartdata.EasyStore<Record<string, any>>;
|
|
|
|
/**
|
|
* @param mongoDescriptor MongoDB connection settings
|
|
*/
|
|
constructor(mongoDescriptor: plugins.smartdata.IMongoDescriptor) {
|
|
this.db = new plugins.smartdata.SmartdataDb(mongoDescriptor);
|
|
// Use a single EasyStore document to hold all certs keyed by domainName
|
|
this.store = new plugins.smartdata.EasyStore<Record<string, any>>(
|
|
'smartacme-certs',
|
|
this.db,
|
|
);
|
|
this.interestMap = new plugins.lik.InterestMap((domain) => domain);
|
|
}
|
|
|
|
public async init(): Promise<void> {
|
|
await this.db.init();
|
|
}
|
|
|
|
public async retrieveCertificate(domainName: string): Promise<SmartacmeCert | null> {
|
|
const data = await this.store.readKey(domainName);
|
|
return data ? new SmartacmeCert(data) : null;
|
|
}
|
|
|
|
public async storeCertificate(cert: SmartacmeCert): Promise<void> {
|
|
// write plain object for persistence
|
|
await this.store.writeKey(cert.domainName, { ...cert });
|
|
const interest = this.interestMap.findInterest(cert.domainName);
|
|
if (interest) {
|
|
interest.fullfillInterest(cert);
|
|
interest.markLost();
|
|
}
|
|
}
|
|
|
|
public async deleteCertificate(domainName: string): Promise<void> {
|
|
await this.store.deleteKey(domainName);
|
|
}
|
|
|
|
public async close(): Promise<void> {
|
|
await this.db.close();
|
|
}
|
|
/**
|
|
* Wipe all certificates from the persistent store (for integration testing)
|
|
*/
|
|
public async wipe(): Promise<void> {
|
|
// clear all keys in the easy store
|
|
await this.store.wipe();
|
|
// reset interest map
|
|
this.interestMap = new plugins.lik.InterestMap((domain) => domain);
|
|
}
|
|
} |