支持为源码发布配置加密运行变量 #19

Merged
xhgit merged 1 commits from codex/source-publish-environment into main 2026-11-09 23:24:40 +08:00
4 changed files with 110 additions and 18 deletions

No files matched your search

@@ -43,7 +43,8 @@ public class HostingController {
public record Deploy(@NotBlank @Size(max = 200) String idempotencyKey, UUID rollbackId) {}
public record SourceDeploy(@NotBlank @Size(max = 200) String idempotencyKey) {}
public record SourceDeploy(
@NotBlank @Size(max = 200) String idempotencyKey, Map<String, String> environment) {}
public record ServiceSpec(
@NotBlank @Pattern(regexp = "[a-z][a-z0-9-]{0,62}") String name,
@@ -199,10 +200,15 @@ public class HostingController {
@Valid @RequestBody SourceDeploy request) {
var existing = hosting.beforeSourceSnapshot(who.id(), project, request.idempotencyKey());
if (existing != null) return existing;
HostingService.validateSourceEnvironment(request.environment());
if (!runtime.request("/runtime/source-ready", null).path("ready").asBoolean(false))
throw new ApiError.Failure(503, "源码发布运行环境尚未启用,请联系管理员");
return hosting.deploySource(
who.id(), project, request.idempotencyKey(), sandbox.releaseSnapshot(project));
who.id(),
project,
request.idempotencyKey(),
sandbox.releaseSnapshot(project),
request.environment());
}
@GetMapping("/projects/{project}/deployments/{id}/events")
@@ -163,15 +163,7 @@ public class HostingService {
id,
project)
!= 0)) throw new ApiError.Failure(409, "源码服务由平台管理,请使用「从源码发布」");
if (s.environment() != null
&& (s.environment().size() > 100
|| s.environment().entrySet().stream()
.anyMatch(
e ->
!e.getKey().matches("[A-Za-z_][A-Za-z0-9_]{0,100}")
|| e.getValue() == null
|| e.getValue().length() > 10000)))
throw new ApiError.Failure(400, "环境变量格式或数量无效");
validateEnvironment(s.environment());
if (s.command().size() > 30
|| s.command().stream().anyMatch(v -> v == null || v.length() > 5000))
throw new ApiError.Failure(400, "启动命令过长");
@@ -233,6 +225,26 @@ public class HostingService {
: ((Timestamp) value).toInstant();
}
private static void validateEnvironment(Map<String, String> environment) {
if (environment != null
&& (environment.size() > 100
|| environment.entrySet().stream()
.anyMatch(
entry ->
!entry.getKey().matches("[A-Za-z_][A-Za-z0-9_]{0,100}")
|| entry.getValue() == null
|| entry.getValue().length() > 10000
|| entry.getValue().indexOf(0) >= 0)))
throw new ApiError.Failure(400, "环境变量格式或数量无效");
}
static void validateSourceEnvironment(Map<String, String> environment) {
validateEnvironment(environment);
if (environment != null
&& !Collections.disjoint(environment.keySet(), Set.of("HOST", "PORT", "HOME", "DATA_DIR")))
throw new ApiError.Failure(400, "HOST、PORT、HOME、DATA_DIR 由平台配置");
}
@Transactional
public Map<String, Object> deploy(UUID user, UUID project, String key, UUID rollback) {
own(user, project);
@@ -280,7 +292,8 @@ public class HostingService {
}
@Transactional
public Map<String, Object> deploySource(UUID user, UUID project, String key, JsonNode snapshot) {
public Map<String, Object> deploySource(
UUID user, UUID project, String key, JsonNode snapshot, Map<String, String> environment) {
db.queryForList("SELECT id FROM projects WHERE id=? AND owner_id=? FOR UPDATE", project, user);
var replay = beforeSourceSnapshot(user, project, key);
if (replay != null) return replay;
@@ -307,7 +320,7 @@ public class HostingService {
UUID.randomUUID(),
project,
SOURCE_SERVICE,
vault.encrypt("{}"),
vault.encrypt(encode(environment == null ? Map.of() : environment)),
cpu,
memory);
} else {
@@ -318,6 +331,11 @@ public class HostingService {
cpu,
memory,
existing.getFirst().get("id"));
if (environment != null)
db.update(
"UPDATE project_services SET encrypted_environment=? WHERE id=?",
vault.encrypt(encode(environment)),
existing.getFirst().get("id"));
}
return enqueueDeployment(
user,
@@ -76,21 +76,64 @@ class HostingIntegrationTest {
.put("runtime", "NODE")
.put("install", false)
.put("build", false);
var first = hosting.deploySource(user, project, "source", snapshot);
assertThrows(
ApiError.Failure.class,
() -> HostingService.validateSourceEnvironment(Map.of("PORT", "9999")));
var first =
hosting.deploySource(
user, project, "source", snapshot, Map.of("ADMIN_PASSWORD", "fixture-secret"));
assertEquals("SOURCE", first.get("release_type"));
assertFalse(first.containsKey("source_json"));
assertEquals(first.get("id"), hosting.beforeSourceSnapshot(user, project, "source").get("id"));
assertEquals(true, hosting.services(user, project).getFirst().get("source_managed"));
assertFalse(hosting.services(user, project).getFirst().containsKey("encrypted_environment"));
assertFalse(
users
.db()
.queryForObject(
"SELECT spec_json FROM project_deployments WHERE id=?",
String.class,
first.get("id"))
.contains("fixture-secret"));
var claimed = deployments.claim();
var payload = deployments.payload(claimed);
assertEquals(snapshot, payload.path("source"));
assertTrue(payload.path("services").get(0).path("source_managed").asBoolean());
assertEquals(
"fixture-secret",
payload.path("services").get(0).path("environment").path("ADMIN_PASSWORD").asText());
deployments.finish(
(UUID) first.get("id"),
claimed.get("lease_token").toString(),
json.createObjectNode().put("ready", true));
var retained = hosting.deploySource(user, project, "retain-env", snapshot, null);
var next = deployments.claim();
assertEquals(
"fixture-secret",
deployments
.payload(next)
.path("services")
.get(0)
.path("environment")
.path("ADMIN_PASSWORD")
.asText());
deployments.finish(
(UUID) retained.get("id"),
next.get("lease_token").toString(),
json.createObjectNode().put("ready", true));
var cleared = hosting.deploySource(user, project, "clear-env", snapshot, Map.of());
var last = deployments.claim();
assertTrue(deployments.payload(last).path("services").get(0).path("environment").isEmpty());
deployments.finish(
(UUID) cleared.get("id"),
last.get("lease_token").toString(),
json.createObjectNode().put("ready", true));
hosting.deploy(user, project, "source-rollback", (UUID) first.get("id"));
assertEquals(snapshot, deployments.payload(deployments.claim()).path("source"));
var rollback = deployments.payload(deployments.claim());
assertEquals(snapshot, rollback.path("source"));
assertEquals(
"fixture-secret",
rollback.path("services").get(0).path("environment").path("ADMIN_PASSWORD").asText());
}
@Test
+28 -3
View File
@@ -26,7 +26,8 @@ const form = reactive({
enabled: true,
});
const command = ref("[]"),
environment = ref("");
environment = ref(""),
sourceEnvironment = ref("");
const publication = ref<{ reviewStatus: string; archived: boolean }>();
const reviewLabels: Record<string, string> = {
PENDING: "待管理员审核",
@@ -81,6 +82,7 @@ function selectProject() {
output.value = "";
error.value = "";
success.value = "";
sourceEnvironment.value = "";
void load();
}
function edit(s?: any) {
@@ -164,6 +166,19 @@ const sourceKeys = new Map<string, string>();
async function deploy(rollbackId?: string, fromSource = false) {
if (!canPublish.value || (!rollbackId && !fromSource && (!hasService.value || hasManagedSource.value)) || busy.value) return;
const id = project.value;
let variables: Record<string, string> | undefined;
if (fromSource && sourceEnvironment.value.trim()) {
try {
const value = JSON.parse(sourceEnvironment.value);
if (!value || Array.isArray(value) || typeof value !== "object" ||
Object.values(value).some(item => typeof item !== "string"))
throw new Error();
variables = value;
} catch {
error.value = "源码运行环境变量必须是 JSON 对象,所有值须为字符串。";
return;
}
}
if (!confirm(rollbackId ? "确认重新发布历史版本?" : fromSource ? "确认从当前 AI 工作区源码构建并发布?" : "确认发布当前服务配置?"))
return;
busy.value = true;
@@ -175,10 +190,13 @@ async function deploy(rollbackId?: string, fromSource = false) {
...(fromSource ? { signal: AbortSignal.timeout(170000) } : {}),
body: body({
idempotencyKey: fromSource ? sourceKeys.get(id) : crypto.randomUUID(),
...(fromSource ? {} : { rollbackId: rollbackId || null }),
...(fromSource ? { environment: variables } : { rollbackId: rollbackId || null }),
}),
});
if (fromSource) sourceKeys.delete(id);
if (fromSource) {
sourceKeys.delete(id);
sourceEnvironment.value = "";
}
success.value = fromSource ? "已保存源码快照,等待自动构建与发布" : "部署已入队";
await load();
} catch (e) {
@@ -272,6 +290,13 @@ onUnmounted(() => clearInterval(timer));
<p v-if="!hasService">可直接从 AI 工作区发布源码。使用已有镜像时,需要先添加服务。</p>
<p v-if="deploying">已有发布任务正在处理。</p>
</div>
<details v-if="project" class="panel form-panel">
<summary>源码运行环境变量</summary>
<label>
<span>JSON 对象。留空保留现有配置,输入 {} 清空。随下一次源码发布生效。</span>
<textarea v-model="sourceEnvironment" :disabled="busy" rows="3" autocomplete="off" spellcheck="false" placeholder='{"VARIABLE":"value"}'></textarea>
</label>
</details>
<section class="project-grid">
<article class="panel project-card" v-for="s in services" :key="s.id">
<div class="project-card-top">