refact: cleanup
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
import type { Statement } from './access.schemas.ts';
|
||||
|
||||
type AccessProvider = {
|
||||
getAccess: (token: string) => Promise<{
|
||||
statements: Statement[];
|
||||
}>;
|
||||
};
|
||||
|
||||
export type { AccessProvider };
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from './access.session.ts';
|
||||
export * from './access.token.ts';
|
||||
@@ -1,8 +1,9 @@
|
||||
import { z } from 'zod';
|
||||
import jwt from 'jsonwebtoken';
|
||||
|
||||
import { statementSchema } from './access.schemas.ts';
|
||||
import type { AccessProvider } from './access.provider.ts';
|
||||
import { statementSchema } from './auth.schemas.ts';
|
||||
import type { AuthProvider } from './auth.provider.ts';
|
||||
|
||||
import type { Services } from '#root/utils/services.ts';
|
||||
import { Config } from '#root/config/config.ts';
|
||||
|
||||
@@ -12,7 +13,7 @@ const tokenBodySchema = z.object({
|
||||
|
||||
type TokenBody = z.infer<typeof tokenBodySchema>;
|
||||
|
||||
class AccessTokens implements AccessProvider {
|
||||
class JwtAuth implements AuthProvider {
|
||||
#services: Services;
|
||||
|
||||
constructor(services: Services) {
|
||||
@@ -41,4 +42,4 @@ class AccessTokens implements AccessProvider {
|
||||
};
|
||||
}
|
||||
|
||||
export { AccessTokens };
|
||||
export { JwtAuth };
|
||||
@@ -1,18 +1,18 @@
|
||||
import { KubernetesObjectApi, type KubernetesObject } from '@kubernetes/client-node';
|
||||
|
||||
import { K8sResources } from './k8s.resources.ts';
|
||||
import type { K8sBackboneClient } from './k8s.schemas.ts';
|
||||
import type { AuthProvider } from './auth.provider.ts';
|
||||
import type { Statement } from './auth.schemas.ts';
|
||||
|
||||
import type { AccessProvider } from '#root/access/access.provider.ts';
|
||||
import type { Statement } from '#root/access/access.schemas.ts';
|
||||
import type { Services } from '#root/utils/services.ts';
|
||||
import { K8sConfig } from './k8s.config.ts';
|
||||
import { K8sResources } from '#root/services/k8s/k8s.resources.ts';
|
||||
import type { K8sBackboneClient } from '#root/services/k8s/k8s.schemas.ts';
|
||||
import { K8sConfig } from '#root/services/k8s/k8s.config.ts';
|
||||
|
||||
type K8sClient = {
|
||||
statements: Statement[];
|
||||
};
|
||||
|
||||
class K8sClients implements AccessProvider {
|
||||
class K8sAuth implements AuthProvider {
|
||||
#services: Services;
|
||||
#clients: Map<string, K8sClient>;
|
||||
|
||||
@@ -65,4 +65,4 @@ class K8sClients implements AccessProvider {
|
||||
};
|
||||
}
|
||||
|
||||
export { K8sClients };
|
||||
export { K8sAuth };
|
||||
@@ -1,8 +1,9 @@
|
||||
import jwt from 'jsonwebtoken';
|
||||
|
||||
import type { AccessProvider } from '#root/access/access.provider.ts';
|
||||
import type { Statement } from './auth.schemas.ts';
|
||||
import type { AuthProvider } from './auth.provider.ts';
|
||||
|
||||
import type { Services } from '#root/utils/services.ts';
|
||||
import type { Statement } from '#root/access/access.schemas.ts';
|
||||
import { Config } from '#root/config/config.ts';
|
||||
|
||||
const adminStatements: Statement[] = [
|
||||
@@ -27,7 +28,7 @@ const readerStatements: Statement[] = [
|
||||
},
|
||||
];
|
||||
|
||||
class OidcHandler implements AccessProvider {
|
||||
class OidcAuth implements AuthProvider {
|
||||
#services: Services;
|
||||
|
||||
constructor(services: Services) {
|
||||
@@ -63,4 +64,4 @@ class OidcHandler implements AccessProvider {
|
||||
};
|
||||
}
|
||||
|
||||
export { OidcHandler };
|
||||
export { OidcAuth };
|
||||
9
src/auth/auth.provider.ts
Normal file
9
src/auth/auth.provider.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { Statement } from './auth.schemas.ts';
|
||||
|
||||
type AuthProvider = {
|
||||
getAccess: (token: string) => Promise<{
|
||||
statements: Statement[];
|
||||
}>;
|
||||
};
|
||||
|
||||
export type { AuthProvider };
|
||||
@@ -1,4 +1,4 @@
|
||||
import z from 'zod';
|
||||
import { z } from 'zod';
|
||||
|
||||
const statementSchema = z.object({
|
||||
effect: z.enum(['allow', 'disallow']),
|
||||
@@ -1,9 +1,10 @@
|
||||
import { AccessHandler } from './access/access.handler.ts';
|
||||
import { AccessTokens } from './access/access.token.ts';
|
||||
import { JwtAuth } from './auth/auth.jwt.ts';
|
||||
import { K8sAuth } from './auth/auth.k8s.ts';
|
||||
import { OidcAuth } from './auth/auth.oidc.ts';
|
||||
import { Config } from './config/config.ts';
|
||||
import { K8sService } from './k8s/k8s.ts';
|
||||
import { OidcHandler } from './oidc/oidc.handler.ts';
|
||||
import { MqttServer } from './server/server.ts';
|
||||
import { K8sService } from './services/k8s/k8s.ts';
|
||||
import { SessionProvider } from './services/sessions/sessions.provider.ts';
|
||||
import { TopicsHandler } from './topics/topics.handler.ts';
|
||||
import { Services } from './utils/services.ts';
|
||||
|
||||
@@ -26,8 +27,8 @@ class Backbone {
|
||||
return this.#services.get(MqttServer);
|
||||
}
|
||||
|
||||
public get accessHandler() {
|
||||
return this.#services.get(AccessHandler);
|
||||
public get sessionProvider() {
|
||||
return this.#services.get(SessionProvider);
|
||||
}
|
||||
|
||||
public get topicsHandler() {
|
||||
@@ -41,7 +42,7 @@ class Backbone {
|
||||
public start = async () => {
|
||||
if (this.config.k8s.enabled) {
|
||||
await this.k8s.setup();
|
||||
this.accessHandler.register('k8s', this.k8s.clients);
|
||||
this.sessionProvider.register('k8s', this.#services.get(K8sAuth));
|
||||
}
|
||||
if (this.config.http.enabled) {
|
||||
console.log('starting http');
|
||||
@@ -53,10 +54,10 @@ class Backbone {
|
||||
tcp.listen(this.config.tcp.port);
|
||||
}
|
||||
if (this.config.oidc.enabled) {
|
||||
this.accessHandler.register('oidc', this.#services.get(OidcHandler));
|
||||
this.sessionProvider.register('oidc', this.#services.get(OidcAuth));
|
||||
}
|
||||
if (this.config.tokenSecret) {
|
||||
this.accessHandler.register('token', this.#services.get(AccessTokens));
|
||||
this.sessionProvider.register('token', this.#services.get(JwtAuth));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -3,6 +3,10 @@ class Config {
|
||||
return process.env.TOKEN_SECRET;
|
||||
}
|
||||
|
||||
public get adminToken() {
|
||||
return process.env.ADMIN_TOKEN;
|
||||
}
|
||||
|
||||
public get oidc() {
|
||||
const enabled = process.env.OIDC_ENABLED === 'true';
|
||||
const discoveryUrl = process.env.OIDC_DISCOVERY_URL;
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import type { Services } from '#root/utils/services.ts';
|
||||
import { ApiException, ApiextensionsV1Api } from '@kubernetes/client-node';
|
||||
import { z, type ZodType } from 'zod';
|
||||
|
||||
import { K8sConfig } from './k8s.config.ts';
|
||||
|
||||
import type { Services } from '#root/utils/services.ts';
|
||||
|
||||
type CreateCrdOptions = {
|
||||
kind: string;
|
||||
apiVersion: string;
|
||||
@@ -2,9 +2,10 @@ import { V1Secret, type KubernetesObject } from '@kubernetes/client-node';
|
||||
|
||||
import { K8sWatcher } from './k8s.watcher.ts';
|
||||
import type { K8sBackboneClient, K8sBackboneTopic } from './k8s.schemas.ts';
|
||||
import type { Services } from '#root/utils/services.ts';
|
||||
import { K8sConfig } from './k8s.config.ts';
|
||||
|
||||
import type { Services } from '#root/utils/services.ts';
|
||||
|
||||
class K8sResources {
|
||||
#services: Services;
|
||||
#secrets?: K8sWatcher<V1Secret>;
|
||||
@@ -1,6 +1,6 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import { statementSchema } from '#root/access/access.schemas.ts';
|
||||
import { statementSchema } from '#root/auth/auth.schemas.ts';
|
||||
|
||||
const k8sBackboneClientSchema = z.object({
|
||||
statements: z.array(statementSchema),
|
||||
@@ -1,9 +1,6 @@
|
||||
import { KubeConfig } from '@kubernetes/client-node';
|
||||
|
||||
import { K8sResources } from './k8s.resources.ts';
|
||||
import { K8sCrds } from './k8s.crd.ts';
|
||||
import { k8sBackboneClientSchema, k8sBackboneTopicSchema } from './k8s.schemas.ts';
|
||||
import { K8sClients } from './k8s.clients.ts';
|
||||
|
||||
import { API_VERSION } from '#root/utils/consts.ts';
|
||||
import type { Services } from '#root/utils/services.ts';
|
||||
@@ -19,10 +16,6 @@ class K8sService {
|
||||
return this.#services.get(K8sResources);
|
||||
}
|
||||
|
||||
public get clients() {
|
||||
return this.#services.get(K8sClients);
|
||||
}
|
||||
|
||||
public setup = async () => {
|
||||
const crds = this.#services.get(K8sCrds);
|
||||
await crds.install({
|
||||
@@ -1,13 +1,13 @@
|
||||
import type { AccessProvider } from './access.provider.ts';
|
||||
import type { AuthProvider } from '#root/auth/auth.provider.ts';
|
||||
|
||||
class AccessHandler {
|
||||
#handlers: Map<string, AccessProvider>;
|
||||
class SessionProvider {
|
||||
#handlers: Map<string, AuthProvider>;
|
||||
|
||||
constructor() {
|
||||
this.#handlers = new Map();
|
||||
}
|
||||
|
||||
public register = (name: string, provider: AccessProvider) => {
|
||||
public register = (name: string, provider: AuthProvider) => {
|
||||
this.#handlers.set(name, provider);
|
||||
};
|
||||
|
||||
@@ -20,4 +20,4 @@ class AccessHandler {
|
||||
};
|
||||
}
|
||||
|
||||
export { AccessHandler };
|
||||
export { SessionProvider };
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { Statement } from './access.schemas.ts';
|
||||
import { validate } from './access.utils.ts';
|
||||
import { validate } from './sessions.utils.ts';
|
||||
|
||||
import type { Statement } from '#root/auth/auth.schemas.ts';
|
||||
|
||||
type SessionOptions = {
|
||||
statements: Statement[];
|
||||
@@ -1,6 +1,6 @@
|
||||
import micromatch from 'micromatch';
|
||||
|
||||
import type { Statement } from './access.schemas.ts';
|
||||
import type { Statement } from '#root/auth/auth.schemas.ts';
|
||||
|
||||
type ValidateOptions = {
|
||||
action: string;
|
||||
@@ -1,14 +1,11 @@
|
||||
import mqtt, { connectAsync, MqttClient } from 'mqtt';
|
||||
import { connectAsync, MqttClient } from 'mqtt';
|
||||
import getPort from 'get-port';
|
||||
|
||||
import { AccessHandler } from '#root/access/access.handler.ts';
|
||||
import { type Statement } from '#root/access/access.schemas.ts';
|
||||
import { AccessTokens } from '#root/access/access.token.ts';
|
||||
import { MqttServer } from '#root/server/server.ts';
|
||||
import type { TopicDefinition } from '#root/topics/topcis.schemas.ts';
|
||||
import { TopicsHandler } from '#root/topics/topics.handler.ts';
|
||||
import { TopicsStore } from '#root/topics/topics.store.ts';
|
||||
import { Backbone } from '#root/backbone.ts';
|
||||
import { JwtAuth } from '#root/auth/auth.jwt.ts';
|
||||
import type { Statement } from '#root/auth/auth.schemas.ts';
|
||||
|
||||
type CreateSocketOptions = {
|
||||
port: number;
|
||||
@@ -32,11 +29,8 @@ type WorldOptions = {
|
||||
const createWorld = async (options: WorldOptions) => {
|
||||
const { topics = [] } = options;
|
||||
const backbone = new Backbone();
|
||||
const secret = 'test';
|
||||
const accessTokens = new AccessTokens({
|
||||
secret,
|
||||
});
|
||||
backbone.accessHandler.register('token', accessTokens);
|
||||
const accessTokens = backbone.services.get(JwtAuth);
|
||||
backbone.sessionProvider.register('token', accessTokens);
|
||||
const topicsStore = new TopicsStore();
|
||||
topicsStore.register(...topics);
|
||||
backbone.topicsHandler.register(topicsStore);
|
||||
|
||||
Reference in New Issue
Block a user