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