@kosha-ai/server 0.0.5 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +7 -0
- package/dist/config/kosha-config.d.ts +21 -0
- package/dist/config/kosha-config.d.ts.map +1 -1
- package/dist/config/kosha-config.js +31 -2
- package/dist/credentials/credentials.module.d.ts +3 -0
- package/dist/credentials/credentials.module.d.ts.map +1 -0
- package/dist/credentials/credentials.module.js +15 -0
- package/dist/credentials/credentials.service.d.ts +55 -0
- package/dist/credentials/credentials.service.d.ts.map +1 -0
- package/dist/credentials/credentials.service.js +108 -0
- package/dist/egress/ca.service.d.ts +36 -0
- package/dist/egress/ca.service.d.ts.map +1 -0
- package/dist/egress/ca.service.js +117 -0
- package/dist/egress/egress.controller.d.ts +24 -0
- package/dist/egress/egress.controller.d.ts.map +1 -0
- package/dist/egress/egress.controller.js +59 -0
- package/dist/egress/egress.listener.d.ts +37 -0
- package/dist/egress/egress.listener.d.ts.map +1 -0
- package/dist/egress/egress.listener.js +324 -0
- package/dist/egress/egress.module.d.ts +7 -0
- package/dist/egress/egress.module.d.ts.map +1 -0
- package/dist/egress/egress.module.js +23 -0
- package/dist/egress/grants.service.d.ts +132 -0
- package/dist/egress/grants.service.d.ts.map +1 -0
- package/dist/egress/grants.service.js +219 -0
- package/dist/egress/host-rules.service.d.ts +35 -0
- package/dist/egress/host-rules.service.d.ts.map +1 -0
- package/dist/egress/host-rules.service.js +48 -0
- package/dist/generated/prisma/browser.d.ts +5 -0
- package/dist/generated/prisma/browser.d.ts.map +1 -1
- package/dist/generated/prisma/client.d.ts +5 -0
- package/dist/generated/prisma/client.d.ts.map +1 -1
- package/dist/generated/prisma/internal/class.d.ts +11 -0
- package/dist/generated/prisma/internal/class.d.ts.map +1 -1
- package/dist/generated/prisma/internal/class.js +4 -4
- package/dist/generated/prisma/internal/prismaNamespace.d.ts +91 -1
- package/dist/generated/prisma/internal/prismaNamespace.d.ts.map +1 -1
- package/dist/generated/prisma/internal/prismaNamespace.js +15 -1
- package/dist/generated/prisma/internal/prismaNamespaceBrowser.d.ts +15 -0
- package/dist/generated/prisma/internal/prismaNamespaceBrowser.d.ts.map +1 -1
- package/dist/generated/prisma/internal/prismaNamespaceBrowser.js +15 -1
- package/dist/generated/prisma/models/EgressGrant.d.ts +1538 -0
- package/dist/generated/prisma/models/EgressGrant.d.ts.map +1 -0
- package/dist/generated/prisma/models/EgressGrant.js +1 -0
- package/dist/generated/prisma/models/EndUser.d.ts +127 -0
- package/dist/generated/prisma/models/EndUser.d.ts.map +1 -1
- package/dist/generated/prisma/models/Organization.d.ts +147 -0
- package/dist/generated/prisma/models/Organization.d.ts.map +1 -1
- package/dist/generated/prisma/models.d.ts +1 -0
- package/dist/generated/prisma/models.d.ts.map +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/kosha.module.d.ts.map +1 -1
- package/dist/kosha.module.js +4 -0
- package/dist/proxy/proxy.module.d.ts.map +1 -1
- package/dist/proxy/proxy.module.js +2 -3
- package/dist/proxy/proxy.service.d.ts +3 -19
- package/dist/proxy/proxy.service.d.ts.map +1 -1
- package/dist/proxy/proxy.service.js +9 -89
- package/package.json +6 -4
- package/prisma/migrations/20260921000000_add_egress_grant/migration.sql +31 -0
- package/prisma/schema.prisma +30 -0
package/dist/bootstrap.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bootstrap.d.ts","sourceRoot":"","sources":["../src/bootstrap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"bootstrap.d.ts","sourceRoot":"","sources":["../src/bootstrap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAI/D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAI5D;;;;;;GAMG;AACH,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,gBAAgB,CAAC,CA0B3B;AAED,gCAAgC;AAChC,wBAAsB,gBAAgB,CACpC,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,gBAAgB,CAAC,CAkB3B"}
|
package/dist/bootstrap.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Logger } from '@nestjs/common';
|
|
2
2
|
import { NestFactory } from '@nestjs/core';
|
|
3
|
+
import express from 'express';
|
|
3
4
|
import { createAdminProxy } from './admin-proxy.js';
|
|
4
5
|
import { resolveConfig } from './config/kosha-config.js';
|
|
5
6
|
import { KoshaModule } from './kosha.module.js';
|
|
@@ -15,6 +16,12 @@ export async function createKoshaServer(config) {
|
|
|
15
16
|
const app = await NestFactory.create(KoshaModule.forRoot(resolved), {
|
|
16
17
|
bodyParser: false,
|
|
17
18
|
});
|
|
19
|
+
const expressApp = app.getHttpAdapter().getInstance();
|
|
20
|
+
for (const integration of resolved.integrations) {
|
|
21
|
+
if (integration.docsPath) {
|
|
22
|
+
expressApp.use(`/${resolved.server.globalPrefix}/integrations/${integration.key}/docs`, express.static(integration.docsPath));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
18
25
|
const cors = resolved.server.cors;
|
|
19
26
|
app.enableCors({
|
|
20
27
|
origin: cors?.origin ?? resolved.auth.trustedOrigins ?? [],
|
|
@@ -35,6 +35,22 @@ export interface KoshaServerConfig {
|
|
|
35
35
|
export interface KoshaAdminConfig {
|
|
36
36
|
port?: number;
|
|
37
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* The egress broker: a forward CONNECT proxy that lends a connection's identity
|
|
40
|
+
* to a process Kosha does not control. Off by default, because a forward proxy
|
|
41
|
+
* opened by accident is an open relay rather than a broken feature.
|
|
42
|
+
*/
|
|
43
|
+
export interface KoshaEgressConfig {
|
|
44
|
+
enabled?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Its own port, never the API's. Whatever can reach this can ask for a
|
|
47
|
+
* credential, so it is the port a network policy opens to untrusted
|
|
48
|
+
* workloads; sharing `server.port` would expose every other route too.
|
|
49
|
+
*/
|
|
50
|
+
port?: number;
|
|
51
|
+
/** Validity of the per-host leaf certificates the broker mints. */
|
|
52
|
+
leafCertDays?: number;
|
|
53
|
+
}
|
|
38
54
|
export interface KoshaEncryptionConfig {
|
|
39
55
|
/**
|
|
40
56
|
* Base64-encoded 32-byte key for AES-256-GCM. Every provider token and
|
|
@@ -51,12 +67,15 @@ export interface KoshaConfig {
|
|
|
51
67
|
server?: KoshaServerConfig;
|
|
52
68
|
/** The Next.js admin UI `kosha start` boots alongside the server. */
|
|
53
69
|
admin?: KoshaAdminConfig;
|
|
70
|
+
/** The sandbox egress broker. Off unless `enabled`. */
|
|
71
|
+
egress?: KoshaEgressConfig;
|
|
54
72
|
integrations?: KoshaIntegration[];
|
|
55
73
|
}
|
|
56
74
|
/** A config with every optional section filled in. */
|
|
57
75
|
export interface ResolvedKoshaConfig extends KoshaConfig {
|
|
58
76
|
server: Required<Pick<KoshaServerConfig, 'port' | 'globalPrefix' | 'publicUrl'>> & KoshaServerConfig;
|
|
59
77
|
admin: Required<Pick<KoshaAdminConfig, 'port'>> & KoshaAdminConfig;
|
|
78
|
+
egress: Required<KoshaEgressConfig>;
|
|
60
79
|
integrations: KoshaIntegration[];
|
|
61
80
|
}
|
|
62
81
|
export declare class KoshaConfigError extends Error {
|
|
@@ -65,6 +84,8 @@ export declare class KoshaConfigError extends Error {
|
|
|
65
84
|
export declare const DEFAULT_PORT = 3333;
|
|
66
85
|
export declare const DEFAULT_GLOBAL_PREFIX = "api";
|
|
67
86
|
export declare const DEFAULT_ADMIN_PORT = 3000;
|
|
87
|
+
export declare const DEFAULT_EGRESS_PORT = 3128;
|
|
88
|
+
export declare const DEFAULT_LEAF_CERT_DAYS = 1;
|
|
68
89
|
/**
|
|
69
90
|
* Identity function that gives an app's `kosha.config.ts` its types. Validation
|
|
70
91
|
* is deliberately deferred to {@link resolveConfig} at boot, so editing a
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kosha-config.d.ts","sourceRoot":"","sources":["../../src/config/kosha-config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAElE,MAAM,WAAW,mBAAmB;IAClC,kCAAkC;IAClC,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,eAAe;IAC9B,gFAAgF;IAChF,MAAM,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,6DAA6D;IAC7D,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,gBAAgB,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;CAC1C;AAED,MAAM,WAAW,iBAAiB;IAChC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,+DAA+D;IAC/D,IAAI,CAAC,EAAE;QAAE,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;QAAC,WAAW,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;CAC7D;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,mBAAmB,CAAC;IAC9B,IAAI,EAAE,eAAe,CAAC;IACtB,wDAAwD;IACxD,UAAU,CAAC,EAAE,qBAAqB,CAAC;IACnC,MAAM,CAAC,EAAE,iBAAiB,CAAC;IAC3B,qEAAqE;I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AAY,OAAO,CAAC;AACjC,eAAO,MAAM,qBAAqB,QAAQ,CAAC;AAC3C,eAAO,MAAM,kBAAkB,OAAO,CAAC;AACvC,eAAO,MAAM,mBAAmB,OAAO,CAAC;AACxC,eAAO,MAAM,sBAAsB,IAAI,CAAC;AAExC;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,WAAW,GAAG,WAAW,CAE7D;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,WAAW,GAAG,mBAAmB,CAqGtE"}
|
|
@@ -7,6 +7,8 @@ export class KoshaConfigError extends Error {
|
|
|
7
7
|
export const DEFAULT_PORT = 3333;
|
|
8
8
|
export const DEFAULT_GLOBAL_PREFIX = 'api';
|
|
9
9
|
export const DEFAULT_ADMIN_PORT = 3000;
|
|
10
|
+
export const DEFAULT_EGRESS_PORT = 3128;
|
|
11
|
+
export const DEFAULT_LEAF_CERT_DAYS = 1;
|
|
10
12
|
/**
|
|
11
13
|
* Identity function that gives an app's `kosha.config.ts` its types. Validation
|
|
12
14
|
* is deliberately deferred to {@link resolveConfig} at boot, so editing a
|
|
@@ -44,6 +46,29 @@ export function resolveConfig(config) {
|
|
|
44
46
|
!/^https?:\/\//.test(config.server.publicUrl)) {
|
|
45
47
|
problems.push(`server.publicUrl must be an absolute http(s) URL, got ${config.server.publicUrl}`);
|
|
46
48
|
}
|
|
49
|
+
const port = config?.server?.port ?? DEFAULT_PORT;
|
|
50
|
+
const adminPort = config?.admin?.port ?? DEFAULT_ADMIN_PORT;
|
|
51
|
+
const egressPort = config?.egress?.port ?? DEFAULT_EGRESS_PORT;
|
|
52
|
+
if (config?.egress?.enabled) {
|
|
53
|
+
if (!Number.isInteger(egressPort) || egressPort < 1 || egressPort > 65535) {
|
|
54
|
+
problems.push(`egress.port must be a port number, got ${egressPort}`);
|
|
55
|
+
}
|
|
56
|
+
// Sharing a listener would expose every API route to whatever the egress
|
|
57
|
+
// port is opened to, which is by definition untrusted.
|
|
58
|
+
if (egressPort === port) {
|
|
59
|
+
problems.push('egress.port must differ from server.port');
|
|
60
|
+
}
|
|
61
|
+
if (egressPort === adminPort) {
|
|
62
|
+
problems.push('egress.port must differ from admin.port');
|
|
63
|
+
}
|
|
64
|
+
if (!config.encryption?.key) {
|
|
65
|
+
problems.push('encryption.key is required when egress.enabled: each grant owns a certificate authority whose private key is stored encrypted');
|
|
66
|
+
}
|
|
67
|
+
const days = config.egress.leafCertDays ?? DEFAULT_LEAF_CERT_DAYS;
|
|
68
|
+
if (!Number.isInteger(days) || days < 1) {
|
|
69
|
+
problems.push(`egress.leafCertDays must be a positive integer, got ${days}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
47
72
|
const integrations = config?.integrations ?? [];
|
|
48
73
|
const seen = new Map();
|
|
49
74
|
for (const integration of integrations) {
|
|
@@ -57,13 +82,17 @@ export function resolveConfig(config) {
|
|
|
57
82
|
if (problems.length > 0) {
|
|
58
83
|
throw new KoshaConfigError(`Invalid kosha.config:\n - ${problems.join('\n - ')}`);
|
|
59
84
|
}
|
|
60
|
-
const port = config.server?.port ?? DEFAULT_PORT;
|
|
61
85
|
const globalPrefix = config.server?.globalPrefix ?? DEFAULT_GLOBAL_PREFIX;
|
|
62
86
|
const publicUrl = (config.server?.publicUrl ?? `http://localhost:${port}`).replace(/\/$/, '');
|
|
63
87
|
return {
|
|
64
88
|
...config,
|
|
65
89
|
integrations,
|
|
66
90
|
server: { ...config.server, port, globalPrefix, publicUrl },
|
|
67
|
-
admin: { ...config.admin, port:
|
|
91
|
+
admin: { ...config.admin, port: adminPort },
|
|
92
|
+
egress: {
|
|
93
|
+
enabled: config.egress?.enabled ?? false,
|
|
94
|
+
port: egressPort,
|
|
95
|
+
leafCertDays: config.egress?.leafCertDays ?? DEFAULT_LEAF_CERT_DAYS,
|
|
96
|
+
},
|
|
68
97
|
};
|
|
69
98
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentials.module.d.ts","sourceRoot":"","sources":["../../src/credentials/credentials.module.ts"],"names":[],"mappings":"AAKA,qBAKa,iBAAiB;CAAG"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { Module } from '@nestjs/common';
|
|
3
|
+
import { ConfigurationsModule } from '../configurations/configurations.module.js';
|
|
4
|
+
import { ConnectModule } from '../connect/connect.module.js';
|
|
5
|
+
import { CredentialsService } from './credentials.service.js';
|
|
6
|
+
let CredentialsModule = class CredentialsModule {
|
|
7
|
+
};
|
|
8
|
+
CredentialsModule = __decorate([
|
|
9
|
+
Module({
|
|
10
|
+
imports: [ConnectModule, ConfigurationsModule],
|
|
11
|
+
providers: [CredentialsService],
|
|
12
|
+
exports: [CredentialsService],
|
|
13
|
+
})
|
|
14
|
+
], CredentialsModule);
|
|
15
|
+
export { CredentialsModule };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ConfigurationsService } from '../configurations/configurations.service.js';
|
|
2
|
+
import { ConnectService } from '../connect/connect.service.js';
|
|
3
|
+
import { CryptoService } from '../crypto/crypto.service.js';
|
|
4
|
+
import { IntegrationRegistry } from '../integrations/integration.registry.js';
|
|
5
|
+
import { PrismaService } from '../prisma/prisma.service.js';
|
|
6
|
+
/**
|
|
7
|
+
* Turning a stored connection into a usable access token.
|
|
8
|
+
*
|
|
9
|
+
* Its own service rather than a private method on the proxy, because the REST
|
|
10
|
+
* proxy and the egress broker both need this and two copies could disagree
|
|
11
|
+
* about when a connection is dead.
|
|
12
|
+
*/
|
|
13
|
+
export interface ResolvedCredential {
|
|
14
|
+
accessToken: string;
|
|
15
|
+
tokenType: string | null;
|
|
16
|
+
expiresAt: Date | null;
|
|
17
|
+
/** True when we minted a new token, so the old one is now dead. */
|
|
18
|
+
refreshed: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface ResolvableConnection {
|
|
21
|
+
id: string;
|
|
22
|
+
configurationId: string;
|
|
23
|
+
configuration: {
|
|
24
|
+
provider: {
|
|
25
|
+
key: string;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
credential: {
|
|
29
|
+
accessToken: string;
|
|
30
|
+
refreshToken: string | null;
|
|
31
|
+
tokenType: string | null;
|
|
32
|
+
expiresAt: Date | null;
|
|
33
|
+
} | null;
|
|
34
|
+
}
|
|
35
|
+
export declare class CredentialsService {
|
|
36
|
+
private readonly prisma;
|
|
37
|
+
private readonly crypto;
|
|
38
|
+
private readonly registry;
|
|
39
|
+
private readonly connect;
|
|
40
|
+
private readonly configurations;
|
|
41
|
+
private readonly logger;
|
|
42
|
+
constructor(prisma: PrismaService, crypto: CryptoService, registry: IntegrationRegistry, connect: ConnectService, configurations: ConfigurationsService);
|
|
43
|
+
/**
|
|
44
|
+
* Returns a usable access token, refreshing first when the stored one has
|
|
45
|
+
* expired and the provider supports refresh.
|
|
46
|
+
*
|
|
47
|
+
* Without this an expired token is a dead end for the end user: they have to
|
|
48
|
+
* walk the whole consent flow again for a token the provider was willing to
|
|
49
|
+
* renew.
|
|
50
|
+
*/
|
|
51
|
+
resolveCredential(connection: ResolvableConnection): Promise<ResolvedCredential>;
|
|
52
|
+
private runTokenRefreshedHook;
|
|
53
|
+
private markExpired;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=credentials.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentials.service.d.ts","sourceRoot":"","sources":["../../src/credentials/credentials.service.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,qBAAqB,EAAE,MAAM,6CAA6C,CAAC;AACpF,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,yCAAyC,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D;;;;;;GAMG;AAEH,MAAM,WAAW,kBAAkB;IACjC,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,SAAS,EAAE,IAAI,GAAG,IAAI,CAAC;IACvB,mEAAmE;IACnE,SAAS,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,WAAW,oBAAoB;IACnC,EAAE,EAAE,MAAM,CAAC;IACX,eAAe,EAAE,MAAM,CAAC;IACxB,aAAa,EAAE;QAAE,QAAQ,EAAE;YAAE,GAAG,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,CAAC;IAC7C,UAAU,EAAE;QACV,WAAW,EAAE,MAAM,CAAC;QACpB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;QAC5B,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;QACzB,SAAS,EAAE,IAAI,GAAG,IAAI,CAAC;KACxB,GAAG,IAAI,CAAC;CACV;AAED,qBACa,kBAAkB;IAI3B,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,cAAc;IAPjC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuC;gBAG3C,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,aAAa,EACrB,QAAQ,EAAE,mBAAmB,EAC7B,OAAO,EAAE,cAAc,EACvB,cAAc,EAAE,qBAAqB;IAGxD;;;;;;;OAOG;IACG,iBAAiB,CACrB,UAAU,EAAE,oBAAoB,GAC/B,OAAO,CAAC,kBAAkB,CAAC;YAsEhB,qBAAqB;YAqBrB,WAAW;CAM1B"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
var CredentialsService_1;
|
|
2
|
+
import { __decorate, __metadata } from "tslib";
|
|
3
|
+
import { ForbiddenException, Injectable, Logger, UnauthorizedException, } from '@nestjs/common';
|
|
4
|
+
import { ConfigurationsService } from '../configurations/configurations.service.js';
|
|
5
|
+
import { ConnectService } from '../connect/connect.service.js';
|
|
6
|
+
import { CryptoService } from '../crypto/crypto.service.js';
|
|
7
|
+
import { IntegrationRegistry } from '../integrations/integration.registry.js';
|
|
8
|
+
import { PrismaService } from '../prisma/prisma.service.js';
|
|
9
|
+
let CredentialsService = CredentialsService_1 = class CredentialsService {
|
|
10
|
+
prisma;
|
|
11
|
+
crypto;
|
|
12
|
+
registry;
|
|
13
|
+
connect;
|
|
14
|
+
configurations;
|
|
15
|
+
logger = new Logger(CredentialsService_1.name);
|
|
16
|
+
constructor(prisma, crypto, registry, connect, configurations) {
|
|
17
|
+
this.prisma = prisma;
|
|
18
|
+
this.crypto = crypto;
|
|
19
|
+
this.registry = registry;
|
|
20
|
+
this.connect = connect;
|
|
21
|
+
this.configurations = configurations;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Returns a usable access token, refreshing first when the stored one has
|
|
25
|
+
* expired and the provider supports refresh.
|
|
26
|
+
*
|
|
27
|
+
* Without this an expired token is a dead end for the end user: they have to
|
|
28
|
+
* walk the whole consent flow again for a token the provider was willing to
|
|
29
|
+
* renew.
|
|
30
|
+
*/
|
|
31
|
+
async resolveCredential(connection) {
|
|
32
|
+
const credential = connection.credential;
|
|
33
|
+
if (!credential) {
|
|
34
|
+
throw new ForbiddenException('Connection has no stored credential');
|
|
35
|
+
}
|
|
36
|
+
const expired = credential.expiresAt !== null && credential.expiresAt <= new Date();
|
|
37
|
+
if (!expired) {
|
|
38
|
+
return {
|
|
39
|
+
accessToken: this.crypto.decrypt(credential.accessToken),
|
|
40
|
+
expiresAt: credential.expiresAt,
|
|
41
|
+
refreshed: false,
|
|
42
|
+
tokenType: credential.tokenType,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const integration = this.registry.get(connection.configuration.provider.key);
|
|
46
|
+
const canRefresh = typeof integration.auth.refresh === 'function' && credential.refreshToken;
|
|
47
|
+
if (!canRefresh) {
|
|
48
|
+
await this.markExpired(connection.id, 'Access token expired');
|
|
49
|
+
throw new UnauthorizedException('Connection token expired; the end user must reconnect');
|
|
50
|
+
}
|
|
51
|
+
const configuration = await this.configurations.getWithDecryptedSecret(connection.configurationId);
|
|
52
|
+
try {
|
|
53
|
+
const tokens = await integration.auth.refresh?.({
|
|
54
|
+
client: {
|
|
55
|
+
clientId: configuration.clientId ?? '',
|
|
56
|
+
clientSecret: configuration.clientSecret,
|
|
57
|
+
redirectUri: configuration.redirectUri ??
|
|
58
|
+
this.configurations.callbackUrlFor(configuration.provider.key),
|
|
59
|
+
scopes: configuration.scopes,
|
|
60
|
+
},
|
|
61
|
+
refreshToken: this.crypto.decrypt(credential.refreshToken),
|
|
62
|
+
});
|
|
63
|
+
if (!tokens)
|
|
64
|
+
throw new Error('Refresh returned no tokens');
|
|
65
|
+
await this.connect.storeCredential(connection.id, tokens);
|
|
66
|
+
await this.runTokenRefreshedHook(connection.id, integration, tokens);
|
|
67
|
+
return {
|
|
68
|
+
accessToken: tokens.accessToken,
|
|
69
|
+
expiresAt: tokens.expiresInSeconds
|
|
70
|
+
? new Date(Date.now() + tokens.expiresInSeconds * 1000)
|
|
71
|
+
: null,
|
|
72
|
+
refreshed: true,
|
|
73
|
+
tokenType: tokens.tokenType ?? credential.tokenType,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
const message = error instanceof Error ? error.message : 'Refresh failed';
|
|
78
|
+
await this.markExpired(connection.id, `Token refresh failed: ${message}`);
|
|
79
|
+
throw new UnauthorizedException('Connection token expired and could not be refreshed; the end user must reconnect');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async runTokenRefreshedHook(connectionId, integration, tokens) {
|
|
83
|
+
const hook = integration.hooks?.onTokenRefreshed;
|
|
84
|
+
if (!hook)
|
|
85
|
+
return;
|
|
86
|
+
try {
|
|
87
|
+
await hook({ connectionId, tokens });
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
this.logger.error(`onTokenRefreshed hook failed for "${integration.key}": ${error instanceof Error ? error.message : error}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async markExpired(connectionId, reason) {
|
|
94
|
+
await this.prisma.connection.update({
|
|
95
|
+
where: { id: connectionId },
|
|
96
|
+
data: { status: 'EXPIRED', errorMessage: reason },
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
CredentialsService = CredentialsService_1 = __decorate([
|
|
101
|
+
Injectable(),
|
|
102
|
+
__metadata("design:paramtypes", [PrismaService,
|
|
103
|
+
CryptoService,
|
|
104
|
+
IntegrationRegistry,
|
|
105
|
+
ConnectService,
|
|
106
|
+
ConfigurationsService])
|
|
107
|
+
], CredentialsService);
|
|
108
|
+
export { CredentialsService };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ResolvedKoshaConfig } from '../config/kosha-config.js';
|
|
2
|
+
/**
|
|
3
|
+
* The per-grant certificate authority, and the leaf certificates it signs.
|
|
4
|
+
*
|
|
5
|
+
* Injecting a credential means terminating TLS, which means presenting a
|
|
6
|
+
* certificate for a host we are not. So each grant gets a CA of its own, stored
|
|
7
|
+
* in its row and worthless once it is revoked. One shared CA would let a key
|
|
8
|
+
* extracted from any sandbox forge certificates for every other sandbox.
|
|
9
|
+
*/
|
|
10
|
+
export interface CertificateAuthority {
|
|
11
|
+
readonly certPem: string;
|
|
12
|
+
readonly keyPem: string;
|
|
13
|
+
}
|
|
14
|
+
export interface LeafCertificate {
|
|
15
|
+
readonly cert: string;
|
|
16
|
+
readonly key: string;
|
|
17
|
+
}
|
|
18
|
+
export declare class CaService {
|
|
19
|
+
private readonly config;
|
|
20
|
+
private readonly leaves;
|
|
21
|
+
constructor(config: ResolvedKoshaConfig);
|
|
22
|
+
/**
|
|
23
|
+
* A fresh CA. 2048-bit because this runs synchronously on the request that
|
|
24
|
+
* mints a grant and 4096 costs seconds there, for a key discarded in hours.
|
|
25
|
+
*/
|
|
26
|
+
createCertificateAuthority(): CertificateAuthority;
|
|
27
|
+
/** A leaf for `host`, signed by this grant's CA. Cached per grant and host. */
|
|
28
|
+
leafFor(grant: {
|
|
29
|
+
id: string;
|
|
30
|
+
caPem: string;
|
|
31
|
+
caKeyPem: string;
|
|
32
|
+
}, host: string): LeafCertificate;
|
|
33
|
+
/** Drops every leaf a grant's CA signed. Called when the grant is revoked. */
|
|
34
|
+
forgetLeaves(grantId: string): void;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=ca.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ca.service.d.ts","sourceRoot":"","sources":["../../src/egress/ca.service.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAGrE;;;;;;;GAOG;AAEH,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAaD,qBACa,SAAS;IAOI,OAAO,CAAC,QAAQ,CAAC,MAAM;IAH/C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAsC;gBAGpB,MAAM,EAAE,mBAAmB;IAGpE;;;OAGG;IACH,0BAA0B,IAAI,oBAAoB;IAiClD,+EAA+E;IAC/E,OAAO,CACL,KAAK,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,EACtD,IAAI,EAAE,MAAM,GACX,eAAe;IAyDlB,8EAA8E;IAC9E,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;CAKpC"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
2
|
+
import { Inject, Injectable } from '@nestjs/common';
|
|
3
|
+
import forge from 'node-forge';
|
|
4
|
+
import { KOSHA_CONFIG } from '../config/tokens.js';
|
|
5
|
+
/** Bounded so a long-lived process cannot accumulate one entry per host seen. */
|
|
6
|
+
const MAX_LEAVES = 512;
|
|
7
|
+
/**
|
|
8
|
+
* Picks the SAN type for the leaf. Node rejects a certificate whose only SAN is
|
|
9
|
+
* a DNS entry when the client connected to an address.
|
|
10
|
+
*/
|
|
11
|
+
function isIpLiteral(host) {
|
|
12
|
+
return /^[0-9.]+$/.test(host) || host.includes(':');
|
|
13
|
+
}
|
|
14
|
+
let CaService = class CaService {
|
|
15
|
+
config;
|
|
16
|
+
// Signing is ~10ms of RSA and git opens a connection per operation, so a
|
|
17
|
+
// clone would pay for it repeatedly. Keyed by grant and host, because a leaf
|
|
18
|
+
// from one grant's CA is worthless under another's.
|
|
19
|
+
leaves = new Map();
|
|
20
|
+
constructor(config) {
|
|
21
|
+
this.config = config;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A fresh CA. 2048-bit because this runs synchronously on the request that
|
|
25
|
+
* mints a grant and 4096 costs seconds there, for a key discarded in hours.
|
|
26
|
+
*/
|
|
27
|
+
createCertificateAuthority() {
|
|
28
|
+
const keys = forge.pki.rsa.generateKeyPair(2048);
|
|
29
|
+
const cert = forge.pki.createCertificate();
|
|
30
|
+
cert.publicKey = keys.publicKey;
|
|
31
|
+
cert.serialNumber = `00${forge.util.bytesToHex(forge.random.getBytesSync(16))}`;
|
|
32
|
+
cert.validity.notBefore = new Date(Date.now() - 5 * 60_000);
|
|
33
|
+
cert.validity.notAfter = new Date(Date.now() + 30 * 24 * 60 * 60_000);
|
|
34
|
+
const attrs = [
|
|
35
|
+
{ name: 'commonName', value: 'Kosha Egress Broker' },
|
|
36
|
+
{ name: 'organizationName', value: 'Kosha' },
|
|
37
|
+
];
|
|
38
|
+
cert.setSubject(attrs);
|
|
39
|
+
cert.setIssuer(attrs);
|
|
40
|
+
cert.setExtensions([
|
|
41
|
+
{
|
|
42
|
+
cA: true,
|
|
43
|
+
critical: true,
|
|
44
|
+
name: 'basicConstraints',
|
|
45
|
+
pathLenConstraint: 0,
|
|
46
|
+
},
|
|
47
|
+
{ critical: true, keyCertSign: true, name: 'keyUsage' },
|
|
48
|
+
]);
|
|
49
|
+
cert.sign(keys.privateKey, forge.md.sha256.create());
|
|
50
|
+
return {
|
|
51
|
+
certPem: forge.pki.certificateToPem(cert),
|
|
52
|
+
keyPem: forge.pki.privateKeyToPem(keys.privateKey),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/** A leaf for `host`, signed by this grant's CA. Cached per grant and host. */
|
|
56
|
+
leafFor(grant, host) {
|
|
57
|
+
const cacheKey = `${grant.id}:${host}`;
|
|
58
|
+
const hit = this.leaves.get(cacheKey);
|
|
59
|
+
if (hit !== undefined)
|
|
60
|
+
return hit;
|
|
61
|
+
const caCert = forge.pki.certificateFromPem(grant.caPem);
|
|
62
|
+
const caKey = forge.pki.privateKeyFromPem(grant.caKeyPem);
|
|
63
|
+
const keys = forge.pki.rsa.generateKeyPair(2048);
|
|
64
|
+
const cert = forge.pki.createCertificate();
|
|
65
|
+
cert.publicKey = keys.publicKey;
|
|
66
|
+
cert.serialNumber = `00${forge.util.bytesToHex(forge.random.getBytesSync(16))}`;
|
|
67
|
+
cert.validity.notBefore = new Date(Date.now() - 5 * 60_000);
|
|
68
|
+
cert.validity.notAfter = new Date(Date.now() + this.config.egress.leafCertDays * 24 * 60 * 60_000);
|
|
69
|
+
cert.setSubject([{ name: 'commonName', value: host }]);
|
|
70
|
+
cert.setIssuer(caCert.subject.attributes);
|
|
71
|
+
cert.setExtensions([
|
|
72
|
+
{ cA: false, critical: true, name: 'basicConstraints' },
|
|
73
|
+
{
|
|
74
|
+
critical: true,
|
|
75
|
+
digitalSignature: true,
|
|
76
|
+
keyEncipherment: true,
|
|
77
|
+
name: 'keyUsage',
|
|
78
|
+
},
|
|
79
|
+
{ name: 'extKeyUsage', serverAuth: true },
|
|
80
|
+
{
|
|
81
|
+
// The SAN is what clients actually check; commonName alone has not
|
|
82
|
+
// been accepted for years.
|
|
83
|
+
altNames: [
|
|
84
|
+
isIpLiteral(host) ? { ip: host, type: 7 } : { type: 2, value: host },
|
|
85
|
+
],
|
|
86
|
+
name: 'subjectAltName',
|
|
87
|
+
},
|
|
88
|
+
]);
|
|
89
|
+
cert.sign(caKey, forge.md.sha256.create());
|
|
90
|
+
const leaf = {
|
|
91
|
+
cert: forge.pki.certificateToPem(cert),
|
|
92
|
+
key: forge.pki.privateKeyToPem(keys.privateKey),
|
|
93
|
+
};
|
|
94
|
+
// Map preserves insertion order, so dropping the oldest is enough to bound
|
|
95
|
+
// a cache.
|
|
96
|
+
if (this.leaves.size >= MAX_LEAVES) {
|
|
97
|
+
const oldest = this.leaves.keys().next();
|
|
98
|
+
if (!oldest.done)
|
|
99
|
+
this.leaves.delete(oldest.value);
|
|
100
|
+
}
|
|
101
|
+
this.leaves.set(cacheKey, leaf);
|
|
102
|
+
return leaf;
|
|
103
|
+
}
|
|
104
|
+
/** Drops every leaf a grant's CA signed. Called when the grant is revoked. */
|
|
105
|
+
forgetLeaves(grantId) {
|
|
106
|
+
for (const key of this.leaves.keys()) {
|
|
107
|
+
if (key.startsWith(`${grantId}:`))
|
|
108
|
+
this.leaves.delete(key);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
CaService = __decorate([
|
|
113
|
+
Injectable(),
|
|
114
|
+
__param(0, Inject(KOSHA_CONFIG)),
|
|
115
|
+
__metadata("design:paramtypes", [Object])
|
|
116
|
+
], CaService);
|
|
117
|
+
export { CaService };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type ApiKeyRequest } from '../auth/api-key.guard.js';
|
|
2
|
+
import { GrantsService } from './grants.service.js';
|
|
3
|
+
export interface MintGrantBody {
|
|
4
|
+
endUserExternalId?: string;
|
|
5
|
+
/** Narrows the grant. Omit for every provider this end user has connected. */
|
|
6
|
+
providerKeys?: string[];
|
|
7
|
+
expiresAt?: string;
|
|
8
|
+
label?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Minting and revoking egress grants. `ApiKeyGuard` rather than `OrgAuthGuard`
|
|
12
|
+
* because handing a sandbox an identity is an errand for an app's backend, not
|
|
13
|
+
* for a browser session.
|
|
14
|
+
*/
|
|
15
|
+
export declare class EgressController {
|
|
16
|
+
private readonly grants;
|
|
17
|
+
constructor(grants: GrantsService);
|
|
18
|
+
mint(request: ApiKeyRequest, body: MintGrantBody): Promise<import("./grants.service.js").MintedGrant>;
|
|
19
|
+
revoke(request: ApiKeyRequest, id: string): Promise<{
|
|
20
|
+
grantId: string;
|
|
21
|
+
revoked: boolean;
|
|
22
|
+
}>;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=egress.controller.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"egress.controller.d.ts","sourceRoot":"","sources":["../../src/egress/egress.controller.ts"],"names":[],"mappings":"AAYA,OAAO,EAAe,KAAK,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,MAAM,WAAW,aAAa;IAC5B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,8EAA8E;IAC9E,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;GAIG;AACH,qBAGa,gBAAgB;IACf,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,aAAa;IAI5C,IAAI,CAAQ,OAAO,EAAE,aAAa,EAAU,IAAI,EAAE,aAAa;IAkB/D,MAAM,CAAQ,OAAO,EAAE,aAAa,EAAe,EAAE,EAAE,MAAM;;;;CAIpE"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
2
|
+
import { BadRequestException, Body, Controller, Delete, HttpCode, Param, Post, Req, UseGuards, } from '@nestjs/common';
|
|
3
|
+
import { AllowAnonymous } from '@thallesp/nestjs-better-auth';
|
|
4
|
+
import { ApiKeyGuard } from '../auth/api-key.guard.js';
|
|
5
|
+
import { GrantsService } from './grants.service.js';
|
|
6
|
+
/**
|
|
7
|
+
* Minting and revoking egress grants. `ApiKeyGuard` rather than `OrgAuthGuard`
|
|
8
|
+
* because handing a sandbox an identity is an errand for an app's backend, not
|
|
9
|
+
* for a browser session.
|
|
10
|
+
*/
|
|
11
|
+
let EgressController = class EgressController {
|
|
12
|
+
grants;
|
|
13
|
+
constructor(grants) {
|
|
14
|
+
this.grants = grants;
|
|
15
|
+
}
|
|
16
|
+
async mint(request, body) {
|
|
17
|
+
if (!body?.expiresAt) {
|
|
18
|
+
throw new BadRequestException('expiresAt is required');
|
|
19
|
+
}
|
|
20
|
+
const expiresAt = new Date(body.expiresAt);
|
|
21
|
+
if (Number.isNaN(expiresAt.getTime())) {
|
|
22
|
+
throw new BadRequestException('expiresAt must be an ISO date');
|
|
23
|
+
}
|
|
24
|
+
return this.grants.mint(request.organizationId, {
|
|
25
|
+
endUserExternalId: body.endUserExternalId ?? '',
|
|
26
|
+
expiresAt,
|
|
27
|
+
label: body.label,
|
|
28
|
+
providerKeys: body.providerKeys,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
async revoke(request, id) {
|
|
32
|
+
const revoked = await this.grants.revoke(request.organizationId, id);
|
|
33
|
+
return { grantId: id, revoked };
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
__decorate([
|
|
37
|
+
Post('grants'),
|
|
38
|
+
HttpCode(201),
|
|
39
|
+
__param(0, Req()),
|
|
40
|
+
__param(1, Body()),
|
|
41
|
+
__metadata("design:type", Function),
|
|
42
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
43
|
+
__metadata("design:returntype", Promise)
|
|
44
|
+
], EgressController.prototype, "mint", null);
|
|
45
|
+
__decorate([
|
|
46
|
+
Delete('grants/:id'),
|
|
47
|
+
__param(0, Req()),
|
|
48
|
+
__param(1, Param('id')),
|
|
49
|
+
__metadata("design:type", Function),
|
|
50
|
+
__metadata("design:paramtypes", [Object, String]),
|
|
51
|
+
__metadata("design:returntype", Promise)
|
|
52
|
+
], EgressController.prototype, "revoke", null);
|
|
53
|
+
EgressController = __decorate([
|
|
54
|
+
Controller('egress'),
|
|
55
|
+
AllowAnonymous(),
|
|
56
|
+
UseGuards(ApiKeyGuard),
|
|
57
|
+
__metadata("design:paramtypes", [GrantsService])
|
|
58
|
+
], EgressController);
|
|
59
|
+
export { EgressController };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type OnApplicationBootstrap, type OnApplicationShutdown } from '@nestjs/common';
|
|
2
|
+
import { CaService } from './ca.service.js';
|
|
3
|
+
import type { ResolvedKoshaConfig } from '../config/kosha-config.js';
|
|
4
|
+
import { CredentialsService } from '../credentials/credentials.service.js';
|
|
5
|
+
import { GrantsService } from './grants.service.js';
|
|
6
|
+
import { HostRulesService } from './host-rules.service.js';
|
|
7
|
+
export declare class EgressListener implements OnApplicationBootstrap, OnApplicationShutdown {
|
|
8
|
+
private readonly config;
|
|
9
|
+
private readonly grants;
|
|
10
|
+
private readonly hostRules;
|
|
11
|
+
private readonly ca;
|
|
12
|
+
private readonly credentials;
|
|
13
|
+
private readonly logger;
|
|
14
|
+
/** Reused so a clone's many requests do not each pay for a TLS handshake. */
|
|
15
|
+
private readonly upstreamAgent;
|
|
16
|
+
private server?;
|
|
17
|
+
private mitmServer?;
|
|
18
|
+
private pruneTimer?;
|
|
19
|
+
constructor(config: ResolvedKoshaConfig, grants: GrantsService, hostRules: HostRulesService, ca: CaService, credentials: CredentialsService);
|
|
20
|
+
onApplicationBootstrap(): Promise<void>;
|
|
21
|
+
onApplicationShutdown(): void;
|
|
22
|
+
private createProxyServer;
|
|
23
|
+
private handleConnect;
|
|
24
|
+
/** Copies bytes both ways and gets out of the way. TLS is never inspected. */
|
|
25
|
+
private tunnel;
|
|
26
|
+
/** Terminates TLS with a leaf from the grant's CA and feeds it to the parser. */
|
|
27
|
+
private intercept;
|
|
28
|
+
/**
|
|
29
|
+
* The HTTP server that sees decrypted requests. `emit('connection', socket)`
|
|
30
|
+
* hands Node's own parser a socket it did not accept, which buys keep-alive,
|
|
31
|
+
* chunked bodies and pipelining for free; the per-connection facts ride on
|
|
32
|
+
* the socket under `CONTEXT`.
|
|
33
|
+
*/
|
|
34
|
+
private createMitmServer;
|
|
35
|
+
private forward;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=egress.listener.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"egress.listener.d.ts","sourceRoot":"","sources":["../../src/egress/egress.listener.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC3B,MAAM,gBAAgB,CAAC;AAWxB,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAc,MAAM,qBAAqB,CAAC;AAChE,OAAO,EACL,gBAAgB,EAEjB,MAAM,yBAAyB,CAAC;AAgGjC,qBACa,cACX,YAAW,sBAAsB,EAAE,qBAAqB;IAahC,OAAO,CAAC,QAAQ,CAAC,MAAM;IAC7C,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,EAAE;IACnB,OAAO,CAAC,QAAQ,CAAC,WAAW;IAf9B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAmC;I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