Skip to content

Commit 63af99a

Browse files
committed
remove non secret token options
1 parent 5d7a77b commit 63af99a

12 files changed

+145
-328
lines changed

packages/backend/src/utils.ts

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { PrismaClient, Repo } from "@sourcebot/db";
66
import { decrypt } from "@sourcebot/crypto";
77
import { Token } from "@sourcebot/schemas/v3/shared.type";
88

9-
export const measure = async <T>(cb : () => Promise<T>) => {
9+
export const measure = async <T>(cb: () => Promise<T>) => {
1010
const start = Date.now();
1111
const data = await cb();
1212
const durationMs = Date.now() - start;
@@ -89,38 +89,26 @@ export const excludeReposByTopic = <T extends Repository>(repos: T[], excludedRe
8989
}
9090

9191
export const getTokenFromConfig = async (token: Token, orgId: number, db?: PrismaClient) => {
92-
if (typeof token === 'string') {
93-
return token;
92+
if (!db) {
93+
throw new Error(`Database connection required to retrieve secret`);
9494
}
95-
if ('env' in token) {
96-
const tokenValue = process.env[token.env];
97-
if (!tokenValue) {
98-
throw new Error(`The environment variable '${token.env}' was referenced in the config but was not set.`);
99-
}
100-
return tokenValue;
101-
} else if ('secret' in token) {
102-
if (!db) {
103-
throw new Error(`Database connection required to retrieve secret`);
104-
}
105-
106-
const secretKey = token.secret;
107-
const secret = await db.secret.findUnique({
108-
where: {
109-
orgId_key: {
110-
key: secretKey,
111-
orgId
112-
}
95+
96+
const secretKey = token.secret;
97+
const secret = await db.secret.findUnique({
98+
where: {
99+
orgId_key: {
100+
key: secretKey,
101+
orgId
113102
}
114-
});
115-
116-
if (!secret) {
117-
throw new Error(`Secret with key ${secretKey} not found for org ${orgId}`);
118103
}
104+
});
119105

120-
const decryptedSecret = decrypt(secret.iv, secret.encryptedValue);
121-
return decryptedSecret;
106+
if (!secret) {
107+
throw new Error(`Secret with key ${secretKey} not found for org ${orgId}`);
122108
}
123-
throw new Error(`Invalid token configuration in config`);
109+
110+
const decryptedSecret = decrypt(secret.iv, secret.encryptedValue);
111+
return decryptedSecret;
124112
}
125113

126114
export const isRemotePath = (path: string) => {
@@ -172,7 +160,7 @@ export const fetchWithRetry = async <T>(
172160
maxAttempts: number = 3
173161
): Promise<T> => {
174162
let attempts = 0;
175-
163+
176164
while (true) {
177165
try {
178166
return await fetchFn();

packages/schemas/src/v3/connection.schema.ts

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,17 @@ const schema = {
2020
"env": "ENV_VAR_CONTAINING_TOKEN"
2121
}
2222
],
23-
"anyOf": [
24-
{
25-
"type": "string"
26-
},
27-
{
28-
"type": "object",
29-
"properties": {
30-
"env": {
31-
"type": "string",
32-
"description": "The name of the environment variable that contains the token."
33-
}
34-
},
35-
"required": [
36-
"env"
37-
],
38-
"additionalProperties": false
39-
},
40-
{
41-
"type": "object",
42-
"properties": {
43-
"secret": {
44-
"type": "string",
45-
"description": "The name of the secret that contains the token."
46-
}
47-
},
48-
"required": [
49-
"secret"
50-
],
51-
"additionalProperties": false
23+
"type": "object",
24+
"properties": {
25+
"secret": {
26+
"type": "string",
27+
"description": "The name of the secret that contains the token."
5228
}
53-
]
29+
},
30+
"required": [
31+
"secret"
32+
],
33+
"additionalProperties": false
5434
},
5535
"url": {
5636
"type": "string",

packages/schemas/src/v3/connection.type.ts

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,7 @@ export interface GithubConnectionConfig {
1313
* GitHub Configuration
1414
*/
1515
type: "github";
16-
/**
17-
* A Personal Access Token (PAT).
18-
*/
19-
token?:
20-
| string
21-
| {
22-
/**
23-
* The name of the environment variable that contains the token.
24-
*/
25-
env: string;
26-
}
27-
| {
28-
/**
29-
* The name of the secret that contains the token.
30-
*/
31-
secret: string;
32-
};
16+
token?: Token;
3317
/**
3418
* The URL of the GitHub host. Defaults to https://github.com
3519
*/
@@ -85,6 +69,15 @@ export interface GithubConnectionConfig {
8569
};
8670
revisions?: GitRevisions;
8771
}
72+
/**
73+
* A Personal Access Token (PAT).
74+
*/
75+
export interface Token {
76+
/**
77+
* The name of the secret that contains the token.
78+
*/
79+
secret: string;
80+
}
8881
/**
8982
* The revisions (branches, tags) that should be included when indexing. The default branch (HEAD) is always indexed.
9083
*/
@@ -103,23 +96,7 @@ export interface GitlabConnectionConfig {
10396
* GitLab Configuration
10497
*/
10598
type: "gitlab";
106-
/**
107-
* An authentication token.
108-
*/
109-
token?:
110-
| string
111-
| {
112-
/**
113-
* The name of the environment variable that contains the token.
114-
*/
115-
env: string;
116-
}
117-
| {
118-
/**
119-
* The name of the secret that contains the token.
120-
*/
121-
secret: string;
122-
};
99+
token?: Token1;
123100
/**
124101
* The URL of the GitLab host. Defaults to https://gitlab.com
125102
*/
@@ -166,28 +143,21 @@ export interface GitlabConnectionConfig {
166143
};
167144
revisions?: GitRevisions;
168145
}
146+
/**
147+
* An authentication token.
148+
*/
149+
export interface Token1 {
150+
/**
151+
* The name of the secret that contains the token.
152+
*/
153+
secret: string;
154+
}
169155
export interface GiteaConnectionConfig {
170156
/**
171157
* Gitea Configuration
172158
*/
173159
type: "gitea";
174-
/**
175-
* A Personal Access Token (PAT).
176-
*/
177-
token?:
178-
| string
179-
| {
180-
/**
181-
* The name of the environment variable that contains the token.
182-
*/
183-
env: string;
184-
}
185-
| {
186-
/**
187-
* The name of the secret that contains the token.
188-
*/
189-
secret: string;
190-
};
160+
token?: Token2;
191161
/**
192162
* The URL of the Gitea host. Defaults to https://gitea.com
193163
*/
@@ -220,6 +190,15 @@ export interface GiteaConnectionConfig {
220190
};
221191
revisions?: GitRevisions;
222192
}
193+
/**
194+
* A Personal Access Token (PAT).
195+
*/
196+
export interface Token2 {
197+
/**
198+
* The name of the secret that contains the token.
199+
*/
200+
secret: string;
201+
}
223202
export interface GerritConnectionConfig {
224203
/**
225204
* Gerrit Configuration

packages/schemas/src/v3/gitea.schema.ts

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,17 @@ const schema = {
1616
"env": "ENV_VAR_CONTAINING_TOKEN"
1717
}
1818
],
19-
"anyOf": [
20-
{
21-
"type": "string"
22-
},
23-
{
24-
"type": "object",
25-
"properties": {
26-
"env": {
27-
"type": "string",
28-
"description": "The name of the environment variable that contains the token."
29-
}
30-
},
31-
"required": [
32-
"env"
33-
],
34-
"additionalProperties": false
35-
},
36-
{
37-
"type": "object",
38-
"properties": {
39-
"secret": {
40-
"type": "string",
41-
"description": "The name of the secret that contains the token."
42-
}
43-
},
44-
"required": [
45-
"secret"
46-
],
47-
"additionalProperties": false
19+
"type": "object",
20+
"properties": {
21+
"secret": {
22+
"type": "string",
23+
"description": "The name of the secret that contains the token."
4824
}
49-
]
25+
},
26+
"required": [
27+
"secret"
28+
],
29+
"additionalProperties": false
5030
},
5131
"url": {
5232
"type": "string",

packages/schemas/src/v3/gitea.type.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,7 @@ export interface GiteaConnectionConfig {
55
* Gitea Configuration
66
*/
77
type: "gitea";
8-
/**
9-
* A Personal Access Token (PAT).
10-
*/
11-
token?:
12-
| string
13-
| {
14-
/**
15-
* The name of the environment variable that contains the token.
16-
*/
17-
env: string;
18-
}
19-
| {
20-
/**
21-
* The name of the secret that contains the token.
22-
*/
23-
secret: string;
24-
};
8+
token?: Token;
259
/**
2610
* The URL of the Gitea host. Defaults to https://gitea.com
2711
*/
@@ -54,6 +38,15 @@ export interface GiteaConnectionConfig {
5438
};
5539
revisions?: GitRevisions;
5640
}
41+
/**
42+
* A Personal Access Token (PAT).
43+
*/
44+
export interface Token {
45+
/**
46+
* The name of the secret that contains the token.
47+
*/
48+
secret: string;
49+
}
5750
/**
5851
* The revisions (branches, tags) that should be included when indexing. The default branch (HEAD) is always indexed.
5952
*/

packages/schemas/src/v3/github.schema.ts

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,17 @@ const schema = {
1616
"env": "ENV_VAR_CONTAINING_TOKEN"
1717
}
1818
],
19-
"anyOf": [
20-
{
21-
"type": "string"
22-
},
23-
{
24-
"type": "object",
25-
"properties": {
26-
"env": {
27-
"type": "string",
28-
"description": "The name of the environment variable that contains the token."
29-
}
30-
},
31-
"required": [
32-
"env"
33-
],
34-
"additionalProperties": false
35-
},
36-
{
37-
"type": "object",
38-
"properties": {
39-
"secret": {
40-
"type": "string",
41-
"description": "The name of the secret that contains the token."
42-
}
43-
},
44-
"required": [
45-
"secret"
46-
],
47-
"additionalProperties": false
19+
"type": "object",
20+
"properties": {
21+
"secret": {
22+
"type": "string",
23+
"description": "The name of the secret that contains the token."
4824
}
49-
]
25+
},
26+
"required": [
27+
"secret"
28+
],
29+
"additionalProperties": false
5030
},
5131
"url": {
5232
"type": "string",

0 commit comments

Comments
 (0)