environment API uses a write-only model for the tls key and hawser token

This commit is contained in:
Jarek Krochmalski committed 2026-08-27 07:34:37 +02:00
1 parent bf518f98aa
commit 58f712d432
6 files changed
+125 -17

No files matched your search

+17 -6
View File
@@ -10,6 +10,7 @@ import { authorize } from '$lib/server/authorize';
import { auditEnvironment } from '$lib/server/audit';
import { refreshSubprocessEnvironments } from '$lib/server/subprocess-manager';
import { resetHostDetection, detectHostDataDir } from '$lib/server/host-path';
import { redactEnvironment } from '$lib/server/environment-redact';
import { serializeLabels, parseLabels, MAX_LABELS } from '$lib/utils/label-colors';
import { cleanPem } from '$lib/utils/pem';
import { validateEnvName } from '$lib/utils/env-name';
@@ -22,7 +23,8 @@ import { deleteEnvironmentIcon } from '$lib/server/env-icons';
* @openapi
* summary: Get a single environment by id, including its parsed labels and public IP
* path: id:integer! Environment id (from GET /api/environments)
* resp-200: {id:integer!, name:string!, connectionType:string!, labels:array<string>, publicIp:string}
* resp-200: {id:integer!, name:string!, connectionType:string!, labels:array<string>, publicIp:string, hasTlsKey:boolean, hasHawserToken:boolean}
* resp-200-desc: The tlsKey (private TLS client key) and hawserToken secrets are NEVER returned; hasTlsKey / hasHawserToken indicate whether one is stored.
* resp-403: Permission denied (RBAC 'environments:view' missing)
* resp-404: Environment not found
* resp-500: Unexpected error while loading the environment
@@ -47,7 +49,7 @@ export const GET: RequestHandler = async ({ params, cookies }) => {
// Parse labels from JSON string to array
return json({
...env,
...redactEnvironment(env),
labels: parseLabels(env.labels as string | null),
publicIp
});
@@ -62,8 +64,10 @@ export const GET: RequestHandler = async ({ params, cookies }) => {
* summary: Update an environment; renaming also renames its on-disk stacks/git-repos directories
* path: id:integer! Environment id (from GET /api/environments)
* body: {name:string, host:string, port:integer, protocol:string, tlsCa:string, tlsCert:string, tlsKey:string, tlsSkipVerify:boolean, icon:string, socketPath:string, collectActivity:boolean, collectMetrics:boolean, highlightChanges:boolean, labels:string, connectionType:string, hawserToken:string, publicIp:string}
* body-desc: tlsKey/hawserToken are write-only - a blank value keeps the stored secret; a non-blank value replaces it.
* body-example: {"name":"hhdocker03","collectMetrics":true}
* resp-200: {id:integer!, name:string!, connectionType:string!, labels:array<string>, publicIp:string}
* resp-200: {id:integer!, name:string!, connectionType:string!, labels:array<string>, publicIp:string, hasTlsKey:boolean, hasHawserToken:boolean}
* resp-200-desc: The tlsKey/hawserToken secrets are NEVER returned; hasTlsKey / hasHawserToken indicate whether one is stored.
* resp-400: Invalid new name (rename validation)
* resp-403: Permission denied (RBAC 'environments:edit' missing)
* resp-404: Environment not found
@@ -155,6 +159,13 @@ export const PUT: RequestHandler = async (event) => {
? serializeLabels(Array.isArray(data.labels) ? data.labels.slice(0, MAX_LABELS) : [])
: undefined;
// The GET/list responses never return the tlsKey / hawserToken secrets, so the
// edit form can't round-trip them. A blank value therefore means "keep the stored
// secret" (pass undefined so updateEnvironment leaves the column untouched); a
// non-blank value replaces it. Same "leave blank to keep" pattern as registries /
// git credentials. (Removing a secret entirely is done by deleting the env.)
const cleanedTlsKey = cleanPem(data.tlsKey);
const trimmedToken = typeof data.hawserToken === 'string' ? data.hawserToken.trim() : data.hawserToken;
const env = await updateEnvironment(id, {
name: data.name,
host: data.host,
@@ -162,7 +173,7 @@ export const PUT: RequestHandler = async (event) => {
protocol: data.protocol,
tlsCa: cleanPem(data.tlsCa),
tlsCert: cleanPem(data.tlsCert),
tlsKey: cleanPem(data.tlsKey),
tlsKey: cleanedTlsKey || undefined,
tlsSkipVerify: data.tlsSkipVerify,
icon: data.icon,
socketPath: data.socketPath,
@@ -171,7 +182,7 @@ export const PUT: RequestHandler = async (event) => {
highlightChanges: data.highlightChanges,
labels: labels,
connectionType: data.connectionType,
hawserToken: data.hawserToken
hawserToken: trimmedToken || undefined
});
if (!env) {
@@ -207,7 +218,7 @@ export const PUT: RequestHandler = async (event) => {
// Parse labels from JSON string to array
return json({
...env,
...redactEnvironment(env),
labels: parseLabels(env.labels as string | null),
publicIp
});