forked from xhgit/webup-cloud
Merge pull request '支持为源码发布配置加密运行变量' (#19) from codex/source-publish-environment into main
Reviewed-on: http://git.xhmcl.com/xhgit/webup-cloud/pulls/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 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
|
||||
|
||||
Reference in new issue
Block a user