This commit is contained in:
commit
db82325ed7
216 files changed
+33718
No files matched your search
@@ -0,0 +1,376 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@SpringBootTest(properties = "app.encryption-key=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
|
||||
@ActiveProfiles("test")
|
||||
@AutoConfigureMockMvc
|
||||
class DomainIntegrationTest {
|
||||
@Autowired MockMvc mvc;
|
||||
@Autowired IdentityService users;
|
||||
@Autowired WalletService wallets;
|
||||
@Autowired HostingService hosting;
|
||||
@Autowired DeploymentCoordinator releases;
|
||||
@Autowired DomainService domains;
|
||||
@Autowired DomainCoordinator coordinator;
|
||||
@Autowired IntegrationService integrations;
|
||||
@Autowired ObjectMapper json;
|
||||
@MockitoBean EphemeralStore ephemeral;
|
||||
@MockitoBean DomainResolver resolver;
|
||||
@MockitoBean RuntimeGateway runtime;
|
||||
@MockitoBean DomainDnsGateway dns;
|
||||
UUID user, project, service, plan;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
TestDatabase.clear(users.db());
|
||||
reset(resolver, runtime, dns);
|
||||
user = users.create("domains@test.com", "Domains", "StrongPassword123", "test");
|
||||
project = UUID.randomUUID();
|
||||
plan = UUID.randomUUID();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,'Domains','','HTML','')",
|
||||
project,
|
||||
user);
|
||||
users
|
||||
.db()
|
||||
.update("INSERT INTO project_moderation(project_id,status) VALUES (?,'APPROVED')", project);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " billing_plans(id,name,monthly_price,cpu_millis,memory_mb,disk_gb,traffic_gb,max_services,max_replicas)"
|
||||
+ " VALUES (?,'Fixture',10,1000,1024,5,5,3,2)",
|
||||
plan);
|
||||
service =
|
||||
(UUID)
|
||||
hosting
|
||||
.service(
|
||||
user,
|
||||
project,
|
||||
null,
|
||||
new HostingController.ServiceSpec(
|
||||
"web",
|
||||
"example.invalid/image:v1",
|
||||
List.of(),
|
||||
Map.of(),
|
||||
3000,
|
||||
250,
|
||||
256,
|
||||
1,
|
||||
"/",
|
||||
true))
|
||||
.get("id");
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE system_settings SET setting_value='webup.run' WHERE setting_key IN"
|
||||
+ " ('deploymentDomain','mainDomain')");
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE system_settings SET setting_value='ingress.example.test' WHERE"
|
||||
+ " setting_key='domainDeliveryTarget'");
|
||||
integrations.save(
|
||||
"ALIYUN_DNS",
|
||||
user,
|
||||
Map.of(
|
||||
"accessKeyId",
|
||||
"fixture-dns-key",
|
||||
"accessKeySecret",
|
||||
"fixture-secret",
|
||||
"regionId",
|
||||
"cn-hangzhou",
|
||||
"endpoint",
|
||||
"alidns.aliyuncs.com",
|
||||
"rootDomain",
|
||||
"webup.run",
|
||||
"recordTtl",
|
||||
"600"),
|
||||
true,
|
||||
"test");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> platform(String slug) {
|
||||
return (Map<String, Object>) domains.platform(user, project, service, slug);
|
||||
}
|
||||
|
||||
void publish() {
|
||||
wallets.adjust(user, new BigDecimal("100"), "credit", "test", user, "test");
|
||||
hosting.subscribe(user, project, plan, "plan");
|
||||
UUID release = (UUID) hosting.deploy(user, project, "release", null).get("id");
|
||||
var claimed = releases.claim();
|
||||
releases.finish(
|
||||
release, claimed.get("lease_token").toString(), json.createObjectNode().put("ready", true));
|
||||
}
|
||||
|
||||
String state(UUID id) {
|
||||
return users
|
||||
.db()
|
||||
.queryForObject("SELECT status FROM project_domains WHERE id=?", String.class, id);
|
||||
}
|
||||
|
||||
void poll() {
|
||||
var row = coordinator.claim();
|
||||
assertNotNull(row);
|
||||
coordinator.execute(row);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stablePlatformBindingIsIdempotentAndNeverReassignsRemovedHost() {
|
||||
var a = platform("stable-project");
|
||||
assertEquals(a.get("id"), platform("stable-project").get("id"));
|
||||
UUID id = (UUID) a.get("id");
|
||||
domains.queue(user, project, id, true);
|
||||
assertEquals("REMOVED", state(id));
|
||||
UUID other = UUID.randomUUID();
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,'Other','','HTML','')",
|
||||
other,
|
||||
user);
|
||||
UUID otherService =
|
||||
(UUID)
|
||||
hosting
|
||||
.service(
|
||||
user,
|
||||
other,
|
||||
null,
|
||||
new HostingController.ServiceSpec(
|
||||
"web",
|
||||
"example.invalid/image:v1",
|
||||
List.of(),
|
||||
Map.of(),
|
||||
3000,
|
||||
250,
|
||||
256,
|
||||
1,
|
||||
"/",
|
||||
true))
|
||||
.get("id");
|
||||
assertThrows(
|
||||
ApiError.Failure.class,
|
||||
() -> domains.platform(user, other, otherService, "stable-project"));
|
||||
assertFalse(a.containsKey("lease_token"));
|
||||
assertFalse(a.containsKey("dns_identity_digest"));
|
||||
verifyNoInteractions(runtime);
|
||||
}
|
||||
|
||||
@Test
|
||||
void customDomainRequiresMatchingUnexpiredProofAndOwnService() {
|
||||
@SuppressWarnings("unchecked")
|
||||
var claim = (Map<String, Object>) domains.challenge(user, project, "WWW.Example.com.");
|
||||
UUID id = (UUID) claim.get("id");
|
||||
assertEquals("www.example.com", claim.get("hostname"));
|
||||
assertThrows(ApiError.Failure.class, () -> domains.verify(user, project, id, service));
|
||||
when(resolver.proof(eq("www.example.com"), eq(claim.get("proof_token").toString())))
|
||||
.thenReturn(true);
|
||||
assertThrows(
|
||||
ApiError.Failure.class, () -> domains.verify(user, project, id, UUID.randomUUID()));
|
||||
domains.verify(user, project, id, service);
|
||||
assertEquals(
|
||||
1,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM project_domains WHERE kind='CUSTOM'", Integer.class));
|
||||
assertThrows(ApiError.Failure.class, () -> domains.challenge(user, project, "admin.webup.run"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expiredProofCannotCreateBindingAndOtherOwnerCannotOperate() {
|
||||
@SuppressWarnings("unchecked")
|
||||
var claim = (Map<String, Object>) domains.challenge(user, project, "example.com");
|
||||
UUID id = (UUID) claim.get("id");
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE domain_claims SET expires_at=? WHERE id=?",
|
||||
Timestamp.from(Instant.now().minusSeconds(1)),
|
||||
id);
|
||||
assertThrows(ApiError.Failure.class, () -> domains.verify(user, project, id, service));
|
||||
verifyNoInteractions(resolver);
|
||||
UUID stranger = users.create("stranger@test.com", "Stranger", "StrongPassword123", "test");
|
||||
UUID binding = (UUID) platform("owner-site").get("id");
|
||||
assertThrows(ApiError.Failure.class, () -> domains.queue(stranger, project, binding, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void primaryIsUniqueAndNoUnpaidProvisioning() {
|
||||
UUID first = (UUID) platform("first-site").get("id"),
|
||||
second = (UUID) platform("second-site").get("id");
|
||||
domains.primary(user, project, second);
|
||||
assertEquals(
|
||||
1,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM project_domains WHERE is_primary=TRUE", Integer.class));
|
||||
assertThrows(ApiError.Failure.class, () -> domains.queue(user, project, first, false));
|
||||
assertEquals("VERIFIED", state(first));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dnsCertificateAndRouteAreIndependentReadinessGates() throws Exception {
|
||||
publish();
|
||||
UUID id = (UUID) platform("ready-site").get("id");
|
||||
domains.queue(user, project, id, false);
|
||||
when(dns.ensure(anyMap(), eq("ready-site.webup.run"), eq("ingress.example.test")))
|
||||
.thenReturn("owned-record");
|
||||
when(runtime.request(anyString(), any()))
|
||||
.thenReturn(json.readTree("{\"configured\":true,\"tlsReady\":false,\"routeReady\":true}"));
|
||||
poll();
|
||||
assertEquals("WAITING_DNS", state(id));
|
||||
when(resolver.pointsTo(anyString(), anyString())).thenReturn(true);
|
||||
force(id);
|
||||
poll();
|
||||
assertEquals("WAITING_TLS", state(id));
|
||||
when(runtime.request(anyString(), any()))
|
||||
.thenReturn(json.readTree("{\"configured\":true,\"tlsReady\":true,\"routeReady\":false}"));
|
||||
force(id);
|
||||
poll();
|
||||
assertEquals("WAITING_ROUTE", state(id));
|
||||
when(runtime.request(anyString(), any()))
|
||||
.thenReturn(json.readTree("{\"configured\":true,\"tlsReady\":true,\"routeReady\":true}"));
|
||||
force(id);
|
||||
poll();
|
||||
assertEquals("ACTIVE", state(id));
|
||||
assertEquals(
|
||||
"owned-record",
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT dns_record_id FROM project_domains WHERE id=?", String.class, id));
|
||||
}
|
||||
|
||||
void force(UUID id) {
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE project_domains SET not_before=? WHERE id=?",
|
||||
Timestamp.from(Instant.now().minusSeconds(1)),
|
||||
id);
|
||||
}
|
||||
|
||||
@Test
|
||||
void staleLeaseCannotMarkNewGenerationActive() {
|
||||
publish();
|
||||
UUID id = (UUID) platform("fenced-site").get("id");
|
||||
domains.queue(user, project, id, false);
|
||||
var claimed = coordinator.claim();
|
||||
users.db().update("UPDATE project_domains SET generation=generation+1 WHERE id=?", id);
|
||||
assertThrows(ApiError.Failure.class, () -> coordinator.finish(claimed, "ACTIVE", ""));
|
||||
assertNotEquals("ACTIVE", state(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
void bannedWebsiteCannotProvisionAnIngress() {
|
||||
publish();
|
||||
UUID id = (UUID) platform("banned-site").get("id");
|
||||
domains.queue(user, project, id, false);
|
||||
users.db().update("UPDATE project_moderation SET status='BANNED' WHERE project_id=?", project);
|
||||
var claimed = coordinator.claim();
|
||||
assertThrows(ApiError.Failure.class, () -> coordinator.execute(claimed));
|
||||
verifyNoInteractions(runtime, dns);
|
||||
}
|
||||
|
||||
@Test
|
||||
void removingBorrowedDnsDoesNotDeleteExternalRecords() throws Exception {
|
||||
publish();
|
||||
UUID id = (UUID) platform("borrowed-site").get("id");
|
||||
domains.queue(user, project, id, false);
|
||||
when(runtime.request(anyString(), any()))
|
||||
.thenReturn(json.readTree("{\"configured\":true,\"tlsReady\":true,\"routeReady\":true}"));
|
||||
when(resolver.pointsTo(anyString(), anyString())).thenReturn(true);
|
||||
poll();
|
||||
domains.queue(user, project, id, true);
|
||||
when(runtime.request(anyString(), any())).thenReturn(json.readTree("{\"removed\":true}"));
|
||||
poll();
|
||||
assertEquals("REMOVED", state(id));
|
||||
verify(dns, never()).remove(anyMap(), anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminDomainMutationRequiresPermissionAndAuditsActualActor() throws Exception {
|
||||
UUID id = (UUID) platform("admin-test").get("id");
|
||||
String userToken =
|
||||
users
|
||||
.login("domains@test.com", "StrongPassword123", "test", "test", 1)
|
||||
.get("token")
|
||||
.toString();
|
||||
String payload = "{\"action\":\"REMOVE\",\"reason\":\"测试解绑请求\"}";
|
||||
mvc.perform(
|
||||
post("/api/admin/domains/" + id + "/actions")
|
||||
.header("Authorization", "Bearer " + userToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(payload))
|
||||
.andExpect(status().isForbidden());
|
||||
UUID admin = users.create("domain-admin@test.com", "Admin", "StrongPassword123", "test");
|
||||
users.db().update("INSERT INTO user_roles VALUES (?,'OPS')", admin);
|
||||
String token =
|
||||
users
|
||||
.login("domain-admin@test.com", "StrongPassword123", "test", "test", 1)
|
||||
.get("token")
|
||||
.toString();
|
||||
mvc.perform(
|
||||
post("/api/admin/domains/" + id + "/actions")
|
||||
.header("Authorization", "Bearer " + token)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(payload))
|
||||
.andExpect(status().isOk());
|
||||
assertEquals("REMOVED", state(id));
|
||||
assertEquals(
|
||||
admin,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT actor_id FROM admin_operation_logs WHERE action='DOMAIN_REMOVE'",
|
||||
UUID.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void changedDnsAccountNeverDeletesOldProviderRecord() throws Exception {
|
||||
publish();
|
||||
UUID id = (UUID) platform("rotated-key").get("id");
|
||||
domains.queue(user, project, id, false);
|
||||
when(runtime.request(anyString(), any()))
|
||||
.thenReturn(json.readTree("{\"configured\":true,\"tlsReady\":true,\"routeReady\":true}"));
|
||||
when(resolver.pointsTo(anyString(), anyString())).thenReturn(true);
|
||||
when(dns.ensure(anyMap(), anyString(), anyString())).thenReturn("owned");
|
||||
poll();
|
||||
domains.queue(user, project, id, true);
|
||||
users
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE project_domains SET dns_identity_digest=? WHERE id=?",
|
||||
SecurityConfig.hash("different-key"),
|
||||
id);
|
||||
when(runtime.request(anyString(), any())).thenReturn(json.readTree("{\"removed\":true}"));
|
||||
var row = coordinator.claim();
|
||||
assertThrows(ApiError.Failure.class, () -> coordinator.execute(row));
|
||||
verify(dns, never()).remove(anyMap(), anyString(), anyString(), anyString());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user