安全与效率优化,引入Redis,包名修改

This commit is contained in:
lxp
2026-10-22 14:01:10 +08:00
parent cdc1e4d261
commit 961a61afb4
231 changed files with 5782 additions and 2049 deletions
@@ -0,0 +1,261 @@
package com.linkerbot.vibemake.adminapi;
import com.linkerbot.vibemake.bean.Contracts.*;
import com.linkerbot.vibemake.common.PageResponse;
import com.linkerbot.vibemake.service.gitea.*;
import java.security.MessageDigest;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
final class FakeGiteaClient implements GiteaClient {
private static final class RepoData {
Repository repository;
String head;
Map<String, Map<String, byte[]>> snapshots = new HashMap<>();
List<HistoryEntry> history = new ArrayList<>();
}
private final Map<Long, RepoData> repositories = new HashMap<>();
private final AtomicLong sequence = new AtomicLong();
String lastAuthorCode;
int migrations;
boolean migrationTimeout;
boolean migrationRejected;
boolean timeoutNextDelete;
boolean deletionRejected;
int deletions;
boolean mainConflict;
private final Map<String, RemoteRepository> migrated = new HashMap<>();
final Map<String, String> migrationDescriptions = new HashMap<>();
private final Set<String> mainReady = new HashSet<>();
boolean timeoutNextCommit;
boolean timeoutNextCreate;
boolean failNextCreate;
void reset() {
repositories.clear();
migrated.clear();
mainReady.clear();
migrations = 0;
deletions = 0;
migrationDescriptions.clear();
migrationTimeout = false;
migrationRejected = false;
timeoutNextDelete = false;
deletionRejected = false;
mainConflict = false;
timeoutNextCommit = false;
timeoutNextCreate = false;
failNextCreate = false;
}
private String sha() {
return String.format("%040x", sequence.incrementAndGet());
}
private String blob(byte[] data) {
try {
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-1").digest(data));
} catch (Exception error) {
throw new IllegalStateException(error);
}
}
private RepoData repo(Repository repository) {
return repositories.get(repository.id());
}
synchronized Map<String, byte[]> snapshot(Repository repository, String sha) {
RepoData data = repo(repository);
Map<String, byte[]> snapshot = data == null ? null : data.snapshots.get(sha);
if (snapshot == null) throw new GiteaFailure(404, "NOT_FOUND", "发布版本不存在", false);
Map<String, byte[]> copy = new TreeMap<>();
snapshot.forEach((name, bytes) -> copy.put(name, bytes.clone()));
return copy;
}
@Override
public synchronized Repository createRepository(String name, boolean initialize) {
if (failNextCreate) {
failNextCreate = false;
throw new GiteaFailure(502, "GITEA_ERROR", "服务拒绝请求", false);
}
RepoData data = new RepoData();
data.repository = new Repository(sequence.incrementAndGet(), "vibemake", name);
repositories.put(data.repository.id(), data);
if (initialize)
commit(
data.repository,
List.of(new Change("create", "README.md", null, "initial".getBytes())),
"Initial commit",
"system",
"system");
if (timeoutNextCreate) {
timeoutNextCreate = false;
throw new GiteaFailure(504, "REMOTE_RESULT_UNKNOWN", "创建结果未知", true);
}
return data.repository;
}
@Override
public synchronized String head(Repository repository) {
return repo(repository).head;
}
@Override
public synchronized List<TreeEntry> tree(Repository repository, String sha, String path) {
Map<String, byte[]> snapshot = repo(repository).snapshots.get(sha);
if (snapshot == null) throw new GiteaFailure(404, "NOT_FOUND", "版本不存在", false);
String prefix = path.isEmpty() ? "" : path + "/";
Map<String, TreeEntry> entries = new TreeMap<>();
for (Map.Entry<String, byte[]> file : snapshot.entrySet()) {
if (!file.getKey().startsWith(prefix)) continue;
String remainder = file.getKey().substring(prefix.length());
int separator = remainder.indexOf('/');
String name = separator < 0 ? remainder : remainder.substring(0, separator);
String entryPath = prefix + name;
entries.put(
entryPath,
separator < 0
? new TreeEntry(
entryPath,
name,
"file",
file.getValue().length,
blob(file.getValue()))
: new TreeEntry(entryPath, name, "dir", 0, ""));
}
return new ArrayList<>(entries.values());
}
@Override
public synchronized StoredFile file(Repository repository, String sha, String path) {
Map<String, byte[]> snapshot = repo(repository).snapshots.get(sha);
byte[] bytes = snapshot == null ? null : snapshot.get(path);
if (bytes == null) throw new GiteaFailure(404, "NOT_FOUND", "文件不存在", false);
return new StoredFile(path, blob(bytes), bytes.clone());
}
@Override
public synchronized CommitView commit(
Repository repository,
List<Change> changes,
String message,
String authorName,
String authorCode) {
lastAuthorCode = authorCode;
RepoData repo = repo(repository);
Map<String, byte[]> snapshot =
repo.head == null ? new TreeMap<>() : new TreeMap<>(repo.snapshots.get(repo.head));
for (Change change : changes) {
byte[] old = snapshot.get(change.path());
if (change.operation().equals("create") && old != null)
throw new GiteaFailure(409, "GITEA_CONFLICT", "文件已存在", false);
if (!change.operation().equals("create")
&& (old == null || !blob(old).equals(change.blobSha())))
throw new GiteaFailure(409, "GITEA_CONFLICT", "文件版本冲突", false);
if (change.operation().equals("delete")) snapshot.remove(change.path());
else snapshot.put(change.path(), change.bytes().clone());
}
repo.head = sha();
repo.snapshots.put(repo.head, snapshot);
repo.history.addFirst(
new HistoryEntry(repo.head, message, authorName, "2026-10-13T00:00:00Z"));
if (timeoutNextCommit) {
timeoutNextCommit = false;
throw new GiteaFailure(504, "REMOTE_RESULT_UNKNOWN", "提交结果未知", true);
}
return new CommitView(repo.head, message);
}
@Override
public synchronized PageResponse<HistoryEntry> history(
Repository repository, long page, long size) {
List<HistoryEntry> history = repo(repository).history;
int from = (int) Math.min(history.size(), (page - 1) * size);
int to = (int) Math.min(history.size(), from + size);
return new PageResponse<>(history.subList(from, to), page, size, history.size());
}
@Override
public void checkMigrationSettings() {}
@Override
public synchronized RemoteRepository migrate(
String source, String name, java.time.Duration timeout) {
return migrate(source, name, timeout, null);
}
@Override
public synchronized RemoteRepository migrate(
String source, String name, java.time.Duration timeout, String description) {
migrations++;
Repository repository = createRepository(name, true);
RemoteRepository remote =
new RemoteRepository(
repository.id(), "vibemake", name, true, false, false, source, "master");
migrated.put(name, remote);
migrationDescriptions.put(name, description);
if (migrationTimeout) throw new GiteaFailure(504, "REMOTE_RESULT_UNKNOWN", "迁移结果不明", true);
if (migrationRejected) throw new GiteaFailure(422, "GITEA_MIGRATION_REJECTED", "迁移失败", false);
return remote;
}
@Override
public synchronized MigrationTarget inspectMigrationTarget(String name) {
return new MigrationTarget(inspect(name), migrationDescriptions.get(name));
}
@Override
public synchronized void deleteRepository(Repository repository) {
if (inspect(repository.name()).id() != repository.id())
throw new GiteaFailure(409, "GITEA_CONFLICT", "仓库身份已变化", false);
if (deletionRejected) throw new GiteaFailure(502, "GITEA_ERROR", "清理失败", false);
deletions++;
migrated.remove(repository.name());
migrationDescriptions.remove(repository.name());
mainReady.remove(repository.name());
repositories.remove(repository.id());
if (timeoutNextDelete) {
timeoutNextDelete = false;
throw new GiteaFailure(504, "REMOTE_RESULT_UNKNOWN", "删除结果不明", true);
}
}
@Override
public synchronized RemoteRepository inspect(String name) {
RemoteRepository remote = migrated.get(name);
if (remote == null) throw new GiteaFailure(404, "NOT_FOUND", "仓库不存在", false);
return remote;
}
@Override
public synchronized String branch(Repository repository, String branch) {
if (branch.equals("main") && mainConflict) return "f".repeat(40);
if (branch.equals("main") && !mainReady.contains(repository.name()))
throw new GiteaFailure(404, "NOT_FOUND", "分支不存在", false);
return head(repository);
}
@Override
public synchronized void createBranch(Repository repository, String name, String sha) {
mainReady.add(repository.name());
}
@Override
public synchronized void defaultBranch(Repository repository, String branch) {
RemoteRepository remote = migrated.get(repository.name());
migrated.put(
repository.name(),
new RemoteRepository(
remote.id(),
remote.owner(),
remote.name(),
true,
false,
false,
remote.originalUrl(),
branch));
}
}
@@ -0,0 +1,26 @@
package com.linkerbot.vibemake.adminapi;
import com.linkerbot.vibemake.common.BusinessException;
import com.linkerbot.vibemake.service.oss.OssObjectStore;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
final class FakeOssObjectStore implements OssObjectStore {
final Map<String, Metadata> objects = new ConcurrentHashMap<>();
@Override
public Metadata head(String objectKey) {
Metadata metadata = objects.get(objectKey);
if (metadata == null)
throw new BusinessException(400, "URDF_FILE_MISSING", "URDF 文件未上传完整,请重新上传");
return metadata;
}
@Override
public void copy(String sourceKey, String targetKey, String sourceEtag) {
Metadata source = head(sourceKey);
if (!source.etag().equals(sourceEtag)) throw BusinessException.conflict("源文件发生变化");
objects.put(targetKey, source);
}
}
@@ -0,0 +1,52 @@
package com.linkerbot.vibemake.adminapi;
import static org.assertj.core.api.Assertions.*;
import com.linkerbot.vibemake.adminapi.config.ProductionConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.DefaultApplicationArguments;
import org.springframework.mock.env.MockEnvironment;
class ProductionConfigurationTest {
private MockEnvironment configuredBusiness() {
return new MockEnvironment()
.withProperty("spring.datasource.url", "jdbc:mysql://localhost/test")
.withProperty("spring.datasource.username", "fixture")
.withProperty("spring.datasource.password", "do-not-log-this")
.withProperty("vibemake.gitea.base-url", "https://example.invalid")
.withProperty("vibemake.gitea.token", "do-not-log-this")
.withProperty("vibemake.gitea.owner", "fixture");
}
@Test
void bootstrapDoesNotRequireRedisConfiguration() {
assertThatCode(
() ->
new ProductionConfiguration(
configuredBusiness(),
new DefaultApplicationArguments(
"--bootstrap-admin-user-code=111111111111")))
.doesNotThrowAnyException();
}
@Test
void normalProductionRequiresExplicitCredentialsWithoutLeakingValues() {
MockEnvironment environment = configuredBusiness();
assertThatThrownBy(
() ->
new ProductionConfiguration(
environment, new DefaultApplicationArguments()))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("REDIS_HOST")
.hasMessageNotContaining("do-not-log-this");
environment
.withProperty("REDIS_HOST", "localhost")
.withProperty("REDIS_PASSWORD", "fixture");
assertThatCode(
() ->
new ProductionConfiguration(
environment, new DefaultApplicationArguments()))
.doesNotThrowAnyException();
}
}
@@ -0,0 +1,202 @@
package com.linkerbot.vibemake.adminapi;
import static org.assertj.core.api.Assertions.*;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.linkerbot.vibemake.common.BusinessException;
import com.linkerbot.vibemake.service.mapper.ProjectMapper;
import com.linkerbot.vibemake.service.support.ProjectLocks;
import com.zaxxer.hikari.*;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import java.nio.file.*;
import java.sql.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
/** 只在新建的随机临时数据库验证,绝不对连接所指向的现有业务库执行DDL或写入。 */
@EnabledIfEnvironmentVariable(named = "ADMIN_MYSQL_TEST_URL", matches = "jdbc:mysql:.*")
class ProjectLocksMySqlTest {
@Test
void independentApplicationsShareLocksAndPersistUncertainWrites() throws Exception {
String url = System.getenv("ADMIN_MYSQL_TEST_URL");
String username = System.getenv("ADMIN_MYSQL_TEST_USERNAME");
String password = System.getenv("ADMIN_MYSQL_TEST_PASSWORD");
String database = "vibemake_admin_verify_" + UUID.randomUUID().toString().replace("-", "");
try (Connection bootstrap =
DriverManager.getConnection(url, username, password == null ? "" : password)) {
assertThat(bootstrap.getMetaData().getDatabaseProductName()).isEqualTo("MySQL");
try (Statement statement = bootstrap.createStatement()) {
statement.execute(
"CREATE DATABASE "
+ database
+ " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
}
try {
String plain = url.split("\\?", 2)[0];
String isolated =
plain.substring(0, plain.lastIndexOf('/') + 1)
+ database
+ (url.contains("?") ? "?" + url.split("\\?", 2)[1] : "");
HikariConfig config = new HikariConfig();
config.setJdbcUrl(isolated);
config.setUsername(username);
config.setPassword(password);
config.setMaximumPoolSize(4);
try (HikariDataSource source = new HikariDataSource(config)) {
JdbcTemplate jdbc = new JdbcTemplate(source);
for (String doc : List.of("database.md", "admin-database.md")) {
Matcher tables =
Pattern.compile("CREATE TABLE [\\s\\S]*?;")
.matcher(Files.readString(Path.of("../docs/" + doc)));
while (tables.find()) jdbc.execute(tables.group());
}
jdbc.update(
"INSERT INTO"
+ " sys_user(user_code,email,nickname,status,created_at,updated_at)"
+ " VALUES"
+ " ('111111111111','verify@example.invalid','verify','ACTIVE',UTC_TIMESTAMP(3),UTC_TIMESTAMP(3))");
jdbc.update(
"INSERT INTO"
+ " project(project_code,owner_user_id,name,description,project_type,gitea_owner,gitea_repo_name,created_at,updated_at)"
+ " SELECT"
+ " '222222222222',id,'verify','','GENERAL','vibemake','p_222222222222',UTC_TIMESTAMP(3),UTC_TIMESTAMP(3)"
+ " FROM sys_user WHERE user_code='111111111111'");
MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
factory.setDataSource(source);
SqlSessionFactory sessions = factory.getObject();
sessions.getConfiguration().addMapper(ProjectMapper.class);
ProjectMapper mapper =
new SqlSessionTemplate(sessions).getMapper(ProjectMapper.class);
ProjectLocks first = new ProjectLocks(source, mapper);
ProjectLocks second = new ProjectLocks(source, mapper);
ExecutorService executor = Executors.newFixedThreadPool(2);
try {
CountDownLatch entered = new CountDownLatch(1);
CountDownLatch release = new CountDownLatch(1);
Future<String> writer =
executor.submit(
() ->
first.write(
"222222222222",
() -> {
entered.countDown();
await(release);
return "done";
}));
assertThat(entered.await(5, TimeUnit.SECONDS)).isTrue();
assertThat(
jdbc.queryForObject(
"SELECT repo_write_status FROM project WHERE"
+ " project_code='222222222222'",
String.class))
.isEqualTo("WRITING");
Future<String> other =
executor.submit(() -> second.serial("222222222222", () -> "other"));
Thread.sleep(100);
assertThat(other.isDone()).isFalse();
release.countDown();
assertThat(writer.get(5, TimeUnit.SECONDS)).isEqualTo("done");
assertThat(other.get(5, TimeUnit.SECONDS)).isEqualTo("other");
assertThat(
first.serial(
"222222222222",
() ->
first.serial(
"222222222222", () -> "reentrant")))
.isEqualTo("reentrant");
assertThatThrownBy(
() ->
first.write(
"222222222222",
() -> {
throw new IllegalStateException(
"simulated remote failure");
}))
.isInstanceOf(IllegalStateException.class);
assertThat(
jdbc.queryForObject(
"SELECT repo_write_status FROM project WHERE"
+ " project_code='222222222222'",
String.class))
.isEqualTo("REVIEW");
first.close();
assertThatThrownBy(() -> second.write("222222222222", () -> "unsafe retry"))
.isInstanceOf(BusinessException.class);
// 模拟进程在写入中退出:只有持久化WRITING,新的进程仍必须阻断。
jdbc.update(
"UPDATE project SET repo_write_status='WRITING' WHERE"
+ " project_code='222222222222'");
ProjectLocks restarted = new ProjectLocks(source, mapper);
try {
assertThatThrownBy(
() ->
restarted.write(
"222222222222", () -> "unsafe restart"))
.isInstanceOf(BusinessException.class);
} finally {
restarted.close();
}
// 断开真正持锁连接,检查不会把未知状态清为空闲。
second.serial(
"222222222222",
() -> {
second.confirmKnownResult("222222222222");
return null;
});
String namespace = Integer.toHexString(database.hashCode());
assertThatThrownBy(
() ->
second.write(
"222222222222",
() -> {
Long connection =
jdbc.queryForObject(
"SELECT"
+ " IS_USED_LOCK(?)",
Long.class,
"vibemake:"
+ namespace
+ ":222222222222");
jdbc.execute(
"KILL CONNECTION "
+ connection);
return "unconfirmed";
}))
.isInstanceOf(BusinessException.class);
assertThat(
jdbc.queryForObject(
"SELECT repo_write_status FROM project WHERE"
+ " project_code='222222222222'",
String.class))
.isEqualTo("REVIEW");
System.out.println("MySQL共享锁、重入、持久化阻断及连接断开验证完成,临时库=" + database);
} finally {
executor.shutdownNow();
first.close();
second.close();
}
}
} finally {
try (Statement statement = bootstrap.createStatement()) {
statement.execute("DROP DATABASE " + database);
}
}
}
}
private void await(CountDownLatch latch) {
try {
if (!latch.await(10, TimeUnit.SECONDS)) throw new IllegalStateException("锁测试超时");
} catch (InterruptedException error) {
Thread.currentThread().interrupt();
throw new IllegalStateException(error);
}
}
}