Expand kv api

This commit is contained in:
2023-11-19 22:35:56 -07:00
parent 7c40650b9a
commit 5a54a9ca5b
9 changed files with 154 additions and 75 deletions

View File

@@ -30,6 +30,25 @@ export class KvService {
);
}
private generateSecretKey(): string {
return Math.random().toString(36).substring(2);
}
public async rotateSecretKey(
namespace: string,
secretKey: string,
): Promise<object> {
const metadata = await this.getMetadataFile().then((buffer) =>
JSON.parse(buffer.toString()),
);
if (metadata[namespace]?.secretKey !== secretKey) {
throw new Error('Secret key does not match');
}
metadata[namespace].secretKey = this.generateSecretKey();
await this.setMetadataFile(Buffer.from(JSON.stringify(metadata)));
return metadata[namespace];
}
public async queryObjectMetadata(
namespace: string,
key: string,
@@ -48,34 +67,35 @@ export class KvService {
return objectMetadata?.metaData;
}
public async get(namespace: string, key: string): Promise<string> {
public async get(namespace: string, key: string): Promise<Buffer> {
const objectMetadata = await this.minioService.getObjectMetadata(
this.kvBucketName,
this.generateFilePath(namespace, key),
);
console.log(objectMetadata);
if (objectMetadata?.metaData.public !== 'true') {
throw new Error('Object is not public');
}
return await this.minioService
.getBuffer(this.kvBucketName, this.generateFilePath(namespace, key))
.then((buffer) => buffer.toString());
return await this.minioService.getBuffer(
this.kvBucketName,
this.generateFilePath(namespace, key),
);
}
public async getWithSecretKey(
namespace: string,
key: string,
secretKey: string,
): Promise<string> {
): Promise<Buffer> {
const metadata = await this.getMetadataFile().then((buffer) =>
JSON.parse(buffer.toString()),
);
if (metadata[namespace]?.secretKey !== secretKey) {
throw new Error('Secret key does not match');
}
return await this.minioService
.getBuffer(this.kvBucketName, this.generateFilePath(namespace, key))
.then((buffer) => buffer.toString());
return await this.minioService.getBuffer(
this.kvBucketName,
this.generateFilePath(namespace, key),
);
}
public async claimNamespace(
@@ -90,7 +110,7 @@ export class KvService {
}
metadata[namespace] = {
...extraData,
secretKey: Math.random().toString(36).substring(2),
secretKey: this.generateSecretKey(),
claimed: new Date().toISOString(),
};
await this.setMetadataFile(Buffer.from(JSON.stringify(metadata)));
@@ -100,10 +120,11 @@ export class KvService {
public async set(
namespace: string,
key: string,
value: string,
value: Buffer,
secretKey: string,
options: {
public: boolean;
mimeType?: string;
} = {
public: false,
},
@@ -132,4 +153,24 @@ export class KvService {
this.generateFilePath(namespace, key),
);
}
public async shareFile(
namespace: string,
key: string,
secretKey: string,
// Default expiry is 7 days
expiry: number = 60 * 60 * 24 * 7,
): Promise<string> {
const metadata = await this.getMetadataFile().then((buffer) =>
JSON.parse(buffer.toString()),
);
if (metadata[namespace]?.secretKey !== secretKey) {
throw new Error('Incorrect secret key for namespace');
}
return await this.minioService.generatePresignedUrl(
this.kvBucketName,
this.generateFilePath(namespace, key),
expiry,
);
}
}