Files
objectstorage/ts/opsserver/handlers/policies.handler.ts

129 lines
4.7 KiB
TypeScript
Raw Normal View History

import * as plugins from '../../plugins.ts';
import type { OpsServer } from '../classes.opsserver.ts';
import * as interfaces from '../../../ts_interfaces/index.ts';
import { requireValidIdentity } from '../helpers/guards.ts';
export class PoliciesHandler {
public typedrouter = new plugins.typedrequest.TypedRouter();
constructor(private opsServerRef: OpsServer) {
this.opsServerRef.typedrouter.addTypedRouter(this.typedrouter);
this.registerHandlers();
}
private registerHandlers(): void {
const pm = () => this.opsServerRef.objectStorageRef.policyManager;
// List named policies
this.typedrouter.addTypedHandler(
new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_ListNamedPolicies>(
'listNamedPolicies',
async (dataArg) => {
await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
return { policies: pm().listPolicies() };
},
),
);
// Create named policy
this.typedrouter.addTypedHandler(
new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_CreateNamedPolicy>(
'createNamedPolicy',
async (dataArg) => {
await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
const policy = await pm().createPolicy(dataArg.name, dataArg.description, dataArg.statements);
return { policy };
},
),
);
// Update named policy
this.typedrouter.addTypedHandler(
new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_UpdateNamedPolicy>(
'updateNamedPolicy',
async (dataArg) => {
await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
const policy = await pm().updatePolicy(dataArg.policyId, dataArg.name, dataArg.description, dataArg.statements);
return { policy };
},
),
);
// Delete named policy
this.typedrouter.addTypedHandler(
new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_DeleteNamedPolicy>(
'deleteNamedPolicy',
async (dataArg) => {
await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
await pm().deletePolicy(dataArg.policyId);
return { ok: true };
},
),
);
// Get bucket named policies (attached + available)
this.typedrouter.addTypedHandler(
new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetBucketNamedPolicies>(
'getBucketNamedPolicies',
async (dataArg) => {
await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
return pm().getBucketAttachments(dataArg.bucketName);
},
),
);
// Attach policy to bucket
this.typedrouter.addTypedHandler(
new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_AttachPolicyToBucket>(
'attachPolicyToBucket',
async (dataArg) => {
await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
await pm().attachPolicyToBucket(dataArg.policyId, dataArg.bucketName);
return { ok: true };
},
),
);
// Detach policy from bucket
this.typedrouter.addTypedHandler(
new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_DetachPolicyFromBucket>(
'detachPolicyFromBucket',
async (dataArg) => {
await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
await pm().detachPolicyFromBucket(dataArg.policyId, dataArg.bucketName);
return { ok: true };
},
),
);
// Get policy buckets (attached + available)
this.typedrouter.addTypedHandler(
new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetPolicyBuckets>(
'getPolicyBuckets',
async (dataArg) => {
await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
const attachedBuckets = pm().getBucketsForPolicy(dataArg.policyId);
const allBuckets = await this.opsServerRef.objectStorageRef.listBuckets();
const attachedSet = new Set(attachedBuckets);
const availableBuckets = allBuckets
.map((b) => b.name)
.filter((name) => !attachedSet.has(name));
return { attachedBuckets, availableBuckets };
},
),
);
// Set policy buckets
this.typedrouter.addTypedHandler(
new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_SetPolicyBuckets>(
'setPolicyBuckets',
async (dataArg) => {
await requireValidIdentity(this.opsServerRef.adminHandler, dataArg);
await pm().setPolicyBuckets(dataArg.policyId, dataArg.bucketNames);
return { ok: true };
},
),
);
}
}