move to postgres, adds user checks to content creation

This commit is contained in:
2024-08-20 09:55:49 -06:00
parent 545656cf22
commit e42a938b13
15 changed files with 266 additions and 245 deletions

View File

@@ -1,5 +1,7 @@
// import { mkdirSync, readFileSync, writeFileSync } from "fs";
// import { writeFile } from "fs/promises";
import { writeFile } from "fs/promises";
import { mkdirSync, readFileSync } from "fs";
export class DHSecretClient {
private token!: Promise<string>; //Set by init
@@ -13,14 +15,10 @@ export class DHSecretClient {
* @param dhBaseUri uri for hosted Dragon's Hoard instance
* @param cacheDir path to cache dir
*/
constructor(
private dhBaseUri: string,
private cacheDir: string,
) {
constructor(private dhBaseUri: string, private cacheDir: string) {
this.cacheLocation = this.cacheDir.trim().replace(/\/^/, "") + "/.dh_cache";
// mkdirSync(this.cacheDir, { recursive: true });
// writeFileSync(this.cacheLocation, "{}", { encoding: "utf-8", flag: "wx" });
// this.readDiskCache();
mkdirSync(this.cacheDir, { recursive: true });
this.readDiskCache();
this.token = this.fetchToken();
}
@@ -43,19 +41,22 @@ export class DHSecretClient {
return token;
}
// private readDiskCache() {
// const cache = readFileSync(this.cacheLocation, "utf-8");
private readDiskCache() {
const cache = readFileSync(this.cacheLocation, "utf-8");
// this.cache = JSON.parse(cache || "{}");
// }
// private async writeDiskCache() {
// await writeFile(this.cacheLocation, JSON.stringify(this.cache), "utf-8");
// }
if (!cache) {
this.cache = {};
this.writeDiskCache();
} else this.cache = JSON.parse(cache || "{}");
}
private async writeDiskCache() {
await writeFile(this.cacheLocation, JSON.stringify(this.cache), "utf-8");
}
private writeCache(key: string, value: string, expires?: number) {
this.cache[key] = { value, expires };
// this.writeDiskCache();
this.writeDiskCache();
}
private readCache(key: string) {
@@ -73,7 +74,10 @@ export class DHSecretClient {
}
async fetchSecret(secret_name: string, environment?: string) {
const uri = this.dhBaseUri + "/api/keys/" + secret_name +
const uri =
this.dhBaseUri +
"/api/keys/" +
secret_name +
(environment ? "?env=" + environment : "");
const cached = this.readCache(secret_name);