forked from zstackio/zstack
-
Notifications
You must be signed in to change notification settings - Fork 0
<feature>[identity]: add generic external tenant resource isolation framework #3496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MatheMatrix
wants to merge
1
commit into
feature-zcf-v0.1
Choose a base branch
from
sync/hanyu.liang/zcf-1147@@2
base: feature-zcf-v0.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| CREATE TABLE IF NOT EXISTS `zstack`.`ExternalTenantResourceRefVO` ( | ||
| `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, | ||
| `source` VARCHAR(64) NOT NULL COMMENT 'source service identifier (zcf, svcX, ...)', | ||
| `tenantId` VARCHAR(128) NOT NULL COMMENT 'external tenant identifier', | ||
| `userId` VARCHAR(128) DEFAULT NULL COMMENT 'external user identifier (optional)', | ||
| `resourceUuid` VARCHAR(32) NOT NULL COMMENT 'resource UUID', | ||
| `resourceType` VARCHAR(256) NOT NULL COMMENT 'resource type (VO SimpleName)', | ||
| `accountUuid` VARCHAR(32) NOT NULL COMMENT 'associated ZStack Account', | ||
| `lastOpDate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, | ||
| `createDate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', | ||
| INDEX idx_source_tenant (`source`, `tenantId`), | ||
| INDEX idx_source_tenant_user (`source`, `tenantId`, `userId`), | ||
| INDEX idx_resource (`resourceUuid`), | ||
| UNIQUE KEY uk_resource_source_tenant (`resourceUuid`, `source`, `tenantId`), | ||
| CONSTRAINT fk_ext_tenant_resource FOREIGN KEY (`resourceUuid`) | ||
| REFERENCES `ResourceVO`(`uuid`) ON DELETE CASCADE, | ||
| CONSTRAINT fk_ext_tenant_account FOREIGN KEY (`accountUuid`) | ||
| REFERENCES `AccountVO`(`uuid`) ON DELETE CASCADE | ||
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
header/src/main/java/org/zstack/header/core/ThreadLocalPropagation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package org.zstack.header.core; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
|
|
||
| /** | ||
| * Registry and utility for cross-thread ThreadLocal propagation. | ||
| * | ||
| * Usage pattern: | ||
| * <pre> | ||
| * // At submission time (in caller thread): | ||
| * Object[] snapshot = ThreadLocalPropagation.capture(); | ||
| * | ||
| * // In worker thread, before task execution: | ||
| * ThreadLocalPropagation.restore(snapshot); | ||
| * | ||
| * // In worker thread, after task execution (finally): | ||
| * ThreadLocalPropagation.clear(); | ||
| * </pre> | ||
| * | ||
| * Propagators are registered once during component startup via | ||
| * {@link #register(ThreadLocalPropagator)}. | ||
| */ | ||
| public class ThreadLocalPropagation { | ||
| private static final List<ThreadLocalPropagator> propagators = new CopyOnWriteArrayList<>(); | ||
|
|
||
| /** | ||
| * Register a propagator. Thread-safe, typically called during component start(). | ||
| */ | ||
| public static void register(ThreadLocalPropagator propagator) { | ||
| if (propagator != null && !propagators.contains(propagator)) { | ||
| propagators.add(propagator); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Unregister a propagator. Thread-safe, typically called during component stop(). | ||
| */ | ||
| public static void unregister(ThreadLocalPropagator propagator) { | ||
| propagators.remove(propagator); | ||
| } | ||
|
|
||
| /** | ||
| * Capture state from all registered propagators. | ||
| * Called in the submitting thread. | ||
| * | ||
| * @return snapshot array (one element per propagator), never null | ||
| */ | ||
| public static Object[] capture() { | ||
| List<ThreadLocalPropagator> current = propagators; | ||
| Object[] snapshot = new Object[current.size()]; | ||
| for (int i = 0; i < current.size(); i++) { | ||
| snapshot[i] = current.get(i).capture(); | ||
| } | ||
| return snapshot; | ||
| } | ||
|
|
||
| /** | ||
| * Restore captured state in the worker thread. | ||
| * Must be called before task execution. | ||
| * | ||
| * @param snapshot the array returned by {@link #capture()} | ||
| */ | ||
| public static void restore(Object[] snapshot) { | ||
| if (snapshot == null) { | ||
| return; | ||
| } | ||
| List<ThreadLocalPropagator> current = propagators; | ||
| int len = Math.min(snapshot.length, current.size()); | ||
| for (int i = 0; i < len; i++) { | ||
| current.get(i).restore(snapshot[i]); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Clear all propagated state in the worker thread. | ||
| * Must be called in a finally block after task execution. | ||
| */ | ||
| public static void clear() { | ||
| for (ThreadLocalPropagator p : propagators) { | ||
| p.clear(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @return true if any propagators are registered | ||
| */ | ||
| public static boolean hasPropagators() { | ||
| return !propagators.isEmpty(); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
header/src/main/java/org/zstack/header/core/ThreadLocalPropagator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package org.zstack.header.core; | ||
|
|
||
| /** | ||
| * SPI for propagating ThreadLocal state across thread boundaries. | ||
| * | ||
| * Implementations capture the current thread's state at submission time, | ||
| * restore it in the worker thread before task execution, and clear it after. | ||
| * | ||
| * Register via {@link ThreadLocalPropagation#register(ThreadLocalPropagator)}. | ||
| */ | ||
| public interface ThreadLocalPropagator { | ||
| /** | ||
| * Capture current thread's state. Called in the submitting thread. | ||
| * @return opaque state object, or null if nothing to propagate | ||
| */ | ||
| Object capture(); | ||
|
|
||
| /** | ||
| * Restore captured state in the worker thread. Called before task execution. | ||
| * @param state the object returned by {@link #capture()}, may be null | ||
| */ | ||
| void restore(Object state); | ||
|
|
||
| /** | ||
| * Clear state in the worker thread. Called after task execution (in finally block). | ||
| */ | ||
| void clear(); | ||
| } |
70 changes: 70 additions & 0 deletions
70
header/src/main/java/org/zstack/header/identity/ExternalTenantContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package org.zstack.header.identity; | ||
|
|
||
| import java.io.Serializable; | ||
|
|
||
| /** | ||
| * External tenant context DTO. | ||
| * Passed by external services (like ZCF, AIOS, etc.) through HTTP Headers, | ||
| * attached to SessionInventory throughout the entire request chain. | ||
| */ | ||
| public class ExternalTenantContext implements Serializable { | ||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| // ThreadLocal used to pass current request's external tenant context at AOP level | ||
| // Set by RestServer after Header parsing, cleaned up after request completion | ||
| private static final ThreadLocal<ExternalTenantContext> current = new ThreadLocal<>(); | ||
|
|
||
| public static void setCurrent(ExternalTenantContext ctx) { | ||
| current.set(ctx); | ||
| } | ||
|
|
||
| public static ExternalTenantContext getCurrent() { | ||
| return current.get(); | ||
| } | ||
|
|
||
| public static void clearCurrent() { | ||
| current.remove(); | ||
| } | ||
|
|
||
| private String source; // Source service identifier, such as "zcf", "svcX" | ||
| private String tenantId; // External tenant identifier | ||
| private String userId; // External user identifier (optional) | ||
|
|
||
| public ExternalTenantContext() { | ||
| } | ||
|
|
||
| public ExternalTenantContext(String source, String tenantId, String userId) { | ||
| this.source = source; | ||
| this.tenantId = tenantId; | ||
| this.userId = userId; | ||
| } | ||
|
|
||
| public String getSource() { | ||
| return source; | ||
| } | ||
|
|
||
| public void setSource(String source) { | ||
| this.source = source; | ||
| } | ||
|
|
||
| public String getTenantId() { | ||
| return tenantId; | ||
| } | ||
|
|
||
| public void setTenantId(String tenantId) { | ||
| this.tenantId = tenantId; | ||
| } | ||
|
|
||
| public String getUserId() { | ||
| return userId; | ||
| } | ||
|
|
||
| public void setUserId(String userId) { | ||
| this.userId = userId; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format("ExternalTenantContext{source='%s', tenantId='%s', userId='%s'}", source, tenantId, userId); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
升级脚本中的外键引用建议显式带上
zstackschema。当前 FK 引用未带 schema,和本目录既有升级脚本约定不一致,建议统一写成
zstack.\ResourceVO`/zstack.`AccountVO``。🛠️ 建议修复
Based on learnings: In ZStack upgrade scripts under
conf/db/upgrade, schema is fixed aszstackand table references should stay schema-qualified.📝 Committable suggestion
🤖 Prompt for AI Agents