Skip to content

Commit 6ba85b5

Browse files
committed
For secret env vars, don’t return the value
1 parent 78f8411 commit 6ba85b5

File tree

7 files changed

+86
-13
lines changed

7 files changed

+86
-13
lines changed

apps/webapp/app/routes/api.v1.projects.$projectRef.envvars.$slug.$name.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
132132
}
133133

134134
return json({
135+
name: environmentVariable.key,
135136
value: environmentVariable.value,
137+
isSecret: environmentVariable.isSecret,
136138
});
137139
}

apps/webapp/app/routes/api.v1.projects.$projectRef.envvars.$slug.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,11 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
8282

8383
const variables = await repository.getEnvironment(environment.project.id, environment.id);
8484

85-
return json(variables.map((variable) => ({ name: variable.key, value: variable.value })));
85+
return json(
86+
variables.map((variable) => ({
87+
name: variable.key,
88+
value: variable.value,
89+
isSecret: variable.isSecret,
90+
}))
91+
);
8692
}

apps/webapp/app/v3/environmentVariables/environmentVariablesRepository.server.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
DeleteEnvironmentVariable,
1616
DeleteEnvironmentVariableValue,
1717
EnvironmentVariable,
18+
EnvironmentVariableWithSecret,
1819
ProjectEnvironmentVariable,
1920
Repository,
2021
Result,
@@ -509,7 +510,10 @@ export class EnvironmentVariablesRepository implements Repository {
509510
return results;
510511
}
511512

512-
async getEnvironment(projectId: string, environmentId: string): Promise<EnvironmentVariable[]> {
513+
async getEnvironment(
514+
projectId: string,
515+
environmentId: string
516+
): Promise<EnvironmentVariableWithSecret[]> {
513517
const project = await this.prismaClient.project.findFirst({
514518
where: {
515519
id: projectId,
@@ -531,7 +535,36 @@ export class EnvironmentVariablesRepository implements Repository {
531535
return [];
532536
}
533537

534-
return this.getEnvironmentVariables(projectId, environmentId);
538+
// Get the keys of all secret variables
539+
const secretValues = await this.prismaClient.environmentVariableValue.findMany({
540+
where: {
541+
environmentId,
542+
isSecret: true,
543+
},
544+
select: {
545+
variable: {
546+
select: {
547+
key: true,
548+
},
549+
},
550+
},
551+
});
552+
const secretVarKeys = secretValues.map((r) => r.variable.key);
553+
554+
const variables = await this.getEnvironmentVariables(projectId, environmentId);
555+
556+
// Filter out secret variables if includeSecrets is false
557+
return variables.map((v) => {
558+
if (secretVarKeys.includes(v.key)) {
559+
return {
560+
key: v.key,
561+
value: "<redacted>",
562+
isSecret: true,
563+
};
564+
}
565+
566+
return { key: v.key, value: v.value, isSecret: false };
567+
});
535568
}
536569

537570
async #getSecretEnvironmentVariables(

apps/webapp/app/v3/environmentVariables/repository.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,25 @@ export type EnvironmentVariable = {
7979
value: string;
8080
};
8181

82+
export type EnvironmentVariableWithSecret = EnvironmentVariable & {
83+
isSecret: boolean;
84+
};
85+
8286
export interface Repository {
8387
create(projectId: string, options: CreateEnvironmentVariables): Promise<CreateResult>;
8488
edit(projectId: string, options: EditEnvironmentVariable): Promise<Result>;
8589
editValue(projectId: string, options: EditEnvironmentVariableValue): Promise<Result>;
8690
getProject(projectId: string): Promise<ProjectEnvironmentVariable[]>;
87-
getEnvironment(projectId: string, environmentId: string): Promise<EnvironmentVariable[]>;
91+
/**
92+
* Get the environment variables for a given environment, it does NOT return values for secret variables
93+
*/
94+
getEnvironment(
95+
projectId: string,
96+
environmentId: string
97+
): Promise<EnvironmentVariableWithSecret[]>;
98+
/**
99+
* Return all env vars, including secret variables with values. Should only be used for executing tasks.
100+
*/
88101
getEnvironmentVariables(projectId: string, environmentId: string): Promise<EnvironmentVariable[]>;
89102
delete(projectId: string, options: DeleteEnvironmentVariable): Promise<Result>;
90103
deleteValue(projectId: string, options: DeleteEnvironmentVariableValue): Promise<Result>;

packages/core/src/v3/apiClient/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
DeletedScheduleObject,
1818
EnvironmentVariableResponseBody,
1919
EnvironmentVariableValue,
20+
EnvironmentVariableWithSecret,
2021
EnvironmentVariables,
2122
ListQueueOptions,
2223
ListRunResponseItem,
@@ -549,7 +550,7 @@ export class ApiClient {
549550

550551
listEnvVars(projectRef: string, slug: string, requestOptions?: ZodFetchOptions) {
551552
return zodfetch(
552-
EnvironmentVariables,
553+
z.array(EnvironmentVariableWithSecret),
553554
`${this.baseUrl}/api/v1/projects/${projectRef}/envvars/${slug}`,
554555
{
555556
method: "GET",
@@ -579,7 +580,7 @@ export class ApiClient {
579580

580581
retrieveEnvVar(projectRef: string, slug: string, key: string, requestOptions?: ZodFetchOptions) {
581582
return zodfetch(
582-
EnvironmentVariableValue,
583+
EnvironmentVariableWithSecret,
583584
`${this.baseUrl}/api/v1/projects/${projectRef}/envvars/${slug}/${key}`,
584585
{
585586
method: "GET",

packages/core/src/v3/schemas/api.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,11 +806,26 @@ export const EnvironmentVariable = z.object({
806806
name: z.string(),
807807
value: z.string(),
808808
});
809-
810809
export const EnvironmentVariables = z.array(EnvironmentVariable);
811810

812811
export type EnvironmentVariables = z.infer<typeof EnvironmentVariables>;
813812

813+
export const EnvironmentVariableWithSecret = z.object({
814+
/** The name of the env var, e.g. `DATABASE_URL` */
815+
name: z.string(),
816+
/** The value of the env var. If it's a secret, this will be a redacted value, not the real value. */
817+
value: z.string(),
818+
/**
819+
* Whether the env var is a secret or not.
820+
* When you create env vars you can mark them as secrets.
821+
*
822+
* You can't view the value of a secret env var after setting it initially.
823+
* For a secret env var, the value will be redacted.
824+
*/
825+
isSecret: z.boolean(),
826+
});
827+
export type EnvironmentVariableWithSecret = z.infer<typeof EnvironmentVariableWithSecret>;
828+
814829
export const UpdateMetadataRequestBody = FlushedRunMetadata;
815830

816831
export type UpdateMetadataRequestBody = z.infer<typeof UpdateMetadataRequestBody>;

packages/trigger-sdk/src/v3/envvars.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
CreateEnvironmentVariableParams,
55
EnvironmentVariableResponseBody,
66
EnvironmentVariableValue,
7+
EnvironmentVariableWithSecret,
78
EnvironmentVariables,
89
ImportEnvironmentVariablesParams,
910
UpdateEnvironmentVariableParams,
@@ -84,13 +85,15 @@ export function list(
8485
projectRef: string,
8586
slug: string,
8687
requestOptions?: ApiRequestOptions
87-
): ApiPromise<EnvironmentVariables>;
88-
export function list(requestOptions?: ApiRequestOptions): ApiPromise<EnvironmentVariables>;
88+
): ApiPromise<EnvironmentVariableWithSecret[]>;
89+
export function list(
90+
requestOptions?: ApiRequestOptions
91+
): ApiPromise<EnvironmentVariableWithSecret[]>;
8992
export function list(
9093
projectRefOrRequestOptions?: string | ApiRequestOptions,
9194
slug?: string,
9295
requestOptions?: ApiRequestOptions
93-
): ApiPromise<EnvironmentVariables> {
96+
): ApiPromise<EnvironmentVariableWithSecret[]> {
9497
const $projectRef = !isRequestOptions(projectRefOrRequestOptions)
9598
? projectRefOrRequestOptions
9699
: taskContext.ctx?.project.ref;
@@ -188,17 +191,17 @@ export function retrieve(
188191
slug: string,
189192
name: string,
190193
requestOptions?: ApiRequestOptions
191-
): ApiPromise<EnvironmentVariableValue>;
194+
): ApiPromise<EnvironmentVariableWithSecret>;
192195
export function retrieve(
193196
name: string,
194197
requestOptions?: ApiRequestOptions
195-
): ApiPromise<EnvironmentVariableValue>;
198+
): ApiPromise<EnvironmentVariableWithSecret>;
196199
export function retrieve(
197200
projectRefOrName: string,
198201
slugOrRequestOptions?: string | ApiRequestOptions,
199202
name?: string,
200203
requestOptions?: ApiRequestOptions
201-
): ApiPromise<EnvironmentVariableValue> {
204+
): ApiPromise<EnvironmentVariableWithSecret> {
202205
let $projectRef: string;
203206
let $slug: string;
204207
let $name: string;

0 commit comments

Comments
 (0)