Merge pull request '支持为源码发布配置加密运行变量' (#19) from codex/source-publish-environment into main
Verify control plane / verify (push) Canceled after 0s
Verify control plane / verify (push) Canceled after 0s
Reviewed-on: http://git.xhmcl.com/xhgit/webup-cloud/pulls/19
This commit was merged in pull request #19.
This commit is contained in:
commit
494511987e
4 files changed
+110
-18
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 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(
|
public record ServiceSpec(
|
||||||
@NotBlank @Pattern(regexp = "[a-z][a-z0-9-]{0,62}") String name,
|
@NotBlank @Pattern(regexp = "[a-z][a-z0-9-]{0,62}") String name,
|
||||||
@@ -199,10 +200,15 @@ public class HostingController {
|
|||||||
@Valid @RequestBody SourceDeploy request) {
|
@Valid @RequestBody SourceDeploy request) {
|
||||||
var existing = hosting.beforeSourceSnapshot(who.id(), project, request.idempotencyKey());
|
var existing = hosting.beforeSourceSnapshot(who.id(), project, request.idempotencyKey());
|
||||||
if (existing != null) return existing;
|
if (existing != null) return existing;
|
||||||
|
HostingService.validateSourceEnvironment(request.environment());
|
||||||
if (!runtime.request("/runtime/source-ready", null).path("ready").asBoolean(false))
|
if (!runtime.request("/runtime/source-ready", null).path("ready").asBoolean(false))
|
||||||
throw new ApiError.Failure(503, "源码发布运行环境尚未启用,请联系管理员");
|
throw new ApiError.Failure(503, "源码发布运行环境尚未启用,请联系管理员");
|
||||||
return hosting.deploySource(
|
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")
|
@GetMapping("/projects/{project}/deployments/{id}/events")
|
||||||
|
|||||||
@@ -163,15 +163,7 @@ public class HostingService {
|
|||||||
id,
|
id,
|
||||||
project)
|
project)
|
||||||
!= 0)) throw new ApiError.Failure(409, "源码服务由平台管理,请使用「从源码发布」");
|
!= 0)) throw new ApiError.Failure(409, "源码服务由平台管理,请使用「从源码发布」");
|
||||||
if (s.environment() != null
|
validateEnvironment(s.environment());
|
||||||
&& (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, "环境变量格式或数量无效");
|
|
||||||
if (s.command().size() > 30
|
if (s.command().size() > 30
|
||||||
|| s.command().stream().anyMatch(v -> v == null || v.length() > 5000))
|
|| s.command().stream().anyMatch(v -> v == null || v.length() > 5000))
|
||||||
throw new ApiError.Failure(400, "启动命令过长");
|
throw new ApiError.Failure(400, "启动命令过长");
|
||||||
@@ -233,6 +225,26 @@ public class HostingService {
|
|||||||
: ((Timestamp) value).toInstant();
|
: ((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
|
@Transactional
|
||||||
public Map<String, Object> deploy(UUID user, UUID project, String key, UUID rollback) {
|
public Map<String, Object> deploy(UUID user, UUID project, String key, UUID rollback) {
|
||||||
own(user, project);
|
own(user, project);
|
||||||
@@ -280,7 +292,8 @@ public class HostingService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@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);
|
db.queryForList("SELECT id FROM projects WHERE id=? AND owner_id=? FOR UPDATE", project, user);
|
||||||
var replay = beforeSourceSnapshot(user, project, key);
|
var replay = beforeSourceSnapshot(user, project, key);
|
||||||
if (replay != null) return replay;
|
if (replay != null) return replay;
|
||||||
@@ -307,7 +320,7 @@ public class HostingService {
|
|||||||
UUID.randomUUID(),
|
UUID.randomUUID(),
|
||||||
project,
|
project,
|
||||||
SOURCE_SERVICE,
|
SOURCE_SERVICE,
|
||||||
vault.encrypt("{}"),
|
vault.encrypt(encode(environment == null ? Map.of() : environment)),
|
||||||
cpu,
|
cpu,
|
||||||
memory);
|
memory);
|
||||||
} else {
|
} else {
|
||||||
@@ -318,6 +331,11 @@ public class HostingService {
|
|||||||
cpu,
|
cpu,
|
||||||
memory,
|
memory,
|
||||||
existing.getFirst().get("id"));
|
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(
|
return enqueueDeployment(
|
||||||
user,
|
user,
|
||||||
|
|||||||
@@ -76,21 +76,64 @@ class HostingIntegrationTest {
|
|||||||
.put("runtime", "NODE")
|
.put("runtime", "NODE")
|
||||||
.put("install", false)
|
.put("install", false)
|
||||||
.put("build", 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"));
|
assertEquals("SOURCE", first.get("release_type"));
|
||||||
assertFalse(first.containsKey("source_json"));
|
assertFalse(first.containsKey("source_json"));
|
||||||
assertEquals(first.get("id"), hosting.beforeSourceSnapshot(user, project, "source").get("id"));
|
assertEquals(first.get("id"), hosting.beforeSourceSnapshot(user, project, "source").get("id"));
|
||||||
assertEquals(true, hosting.services(user, project).getFirst().get("source_managed"));
|
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 claimed = deployments.claim();
|
||||||
var payload = deployments.payload(claimed);
|
var payload = deployments.payload(claimed);
|
||||||
assertEquals(snapshot, payload.path("source"));
|
assertEquals(snapshot, payload.path("source"));
|
||||||
assertTrue(payload.path("services").get(0).path("source_managed").asBoolean());
|
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(
|
deployments.finish(
|
||||||
(UUID) first.get("id"),
|
(UUID) first.get("id"),
|
||||||
claimed.get("lease_token").toString(),
|
claimed.get("lease_token").toString(),
|
||||||
json.createObjectNode().put("ready", true));
|
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"));
|
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
|
@Test
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ const form = reactive({
|
|||||||
enabled: true,
|
enabled: true,
|
||||||
});
|
});
|
||||||
const command = ref("[]"),
|
const command = ref("[]"),
|
||||||
environment = ref("");
|
environment = ref(""),
|
||||||
|
sourceEnvironment = ref("");
|
||||||
const publication = ref<{ reviewStatus: string; archived: boolean }>();
|
const publication = ref<{ reviewStatus: string; archived: boolean }>();
|
||||||
const reviewLabels: Record<string, string> = {
|
const reviewLabels: Record<string, string> = {
|
||||||
PENDING: "待管理员审核",
|
PENDING: "待管理员审核",
|
||||||
@@ -81,6 +82,7 @@ function selectProject() {
|
|||||||
output.value = "";
|
output.value = "";
|
||||||
error.value = "";
|
error.value = "";
|
||||||
success.value = "";
|
success.value = "";
|
||||||
|
sourceEnvironment.value = "";
|
||||||
void load();
|
void load();
|
||||||
}
|
}
|
||||||
function edit(s?: any) {
|
function edit(s?: any) {
|
||||||
@@ -164,6 +166,19 @@ const sourceKeys = new Map<string, string>();
|
|||||||
async function deploy(rollbackId?: string, fromSource = false) {
|
async function deploy(rollbackId?: string, fromSource = false) {
|
||||||
if (!canPublish.value || (!rollbackId && !fromSource && (!hasService.value || hasManagedSource.value)) || busy.value) return;
|
if (!canPublish.value || (!rollbackId && !fromSource && (!hasService.value || hasManagedSource.value)) || busy.value) return;
|
||||||
const id = project.value;
|
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 工作区源码构建并发布?" : "确认发布当前服务配置?"))
|
if (!confirm(rollbackId ? "确认重新发布历史版本?" : fromSource ? "确认从当前 AI 工作区源码构建并发布?" : "确认发布当前服务配置?"))
|
||||||
return;
|
return;
|
||||||
busy.value = true;
|
busy.value = true;
|
||||||
@@ -175,10 +190,13 @@ async function deploy(rollbackId?: string, fromSource = false) {
|
|||||||
...(fromSource ? { signal: AbortSignal.timeout(170000) } : {}),
|
...(fromSource ? { signal: AbortSignal.timeout(170000) } : {}),
|
||||||
body: body({
|
body: body({
|
||||||
idempotencyKey: fromSource ? sourceKeys.get(id) : crypto.randomUUID(),
|
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 ? "已保存源码快照,等待自动构建与发布" : "部署已入队";
|
success.value = fromSource ? "已保存源码快照,等待自动构建与发布" : "部署已入队";
|
||||||
await load();
|
await load();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -272,6 +290,13 @@ onUnmounted(() => clearInterval(timer));
|
|||||||
<p v-if="!hasService">可直接从 AI 工作区发布源码。使用已有镜像时,需要先添加服务。</p>
|
<p v-if="!hasService">可直接从 AI 工作区发布源码。使用已有镜像时,需要先添加服务。</p>
|
||||||
<p v-if="deploying">已有发布任务正在处理。</p>
|
<p v-if="deploying">已有发布任务正在处理。</p>
|
||||||
</div>
|
</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">
|
<section class="project-grid">
|
||||||
<article class="panel project-card" v-for="s in services" :key="s.id">
|
<article class="panel project-card" v-for="s in services" :key="s.id">
|
||||||
<div class="project-card-top">
|
<div class="project-card-top">
|
||||||
|
|||||||
Reference in new issue
Block a user