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
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); | ||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
54
header/src/main/java/org/zstack/header/identity/ExternalTenantProvider.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,54 @@ | ||
| package org.zstack.header.identity; | ||
|
|
||
| /** | ||
| * External tenant Provider SPI. | ||
| * Each external service (ZCF, AIOS, etc.) implements this interface to integrate with the universal tenant resource isolation framework. | ||
| * | ||
| * The framework automatically collects all implementations through {@link org.zstack.core.componentloader.PluginRegistry}. | ||
| * Each Provider returns a unique source identifier (such as "zcf") through {@link #getSource()}, | ||
| * corresponding to the HTTP Header X-Tenant-Source value. | ||
| */ | ||
| public interface ExternalTenantProvider { | ||
| /** | ||
| * Source identifier, such as "zcf", "svcX". | ||
| * Corresponds to X-Tenant-Source header value. | ||
| * Must be globally unique. | ||
| */ | ||
| String getSource(); | ||
|
|
||
| /** | ||
| * Validate tenant context validity. | ||
| * Called after RestServer parses Header and before injecting into Session. | ||
| * Throwing an exception indicates validation failure, and the request will be rejected with HTTP 400. | ||
| * Any exception type is acceptable; non-RestException will be wrapped as 400 Bad Request by RestServer. | ||
| * | ||
| * @param ctx External tenant context (already parsed from Header) | ||
| * @throws RuntimeException if validation fails (e.g. invalid tenantId format) | ||
| */ | ||
| void validateTenant(ExternalTenantContext ctx); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Whether to track this type of resource. | ||
| * After resource creation, the framework calls this method to decide whether to write to ExternalTenantResourceRefVO. | ||
| * Returning false indicates that this resource type does not need to be associated with tenant. | ||
| * Default is true (track all resources). | ||
| * | ||
| * @param resourceType Resource type (VO SimpleName, such as "VmInstanceVO") | ||
| */ | ||
| default boolean shouldTrackResource(String resourceType) { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Resource binding callback (optional). | ||
| * Called after ExternalTenantResourceRefVO is written, | ||
| * Provider can use this for custom logic such as sending notifications or writing audit logs. | ||
| * | ||
| * @param ctx External tenant context | ||
| * @param resourceUuid Resource UUID | ||
| * @param resourceType Resource type (VO SimpleName) | ||
| */ | ||
| default void onResourceBound(ExternalTenantContext ctx, | ||
| String resourceUuid, String resourceType) { | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
125 changes: 125 additions & 0 deletions
125
header/src/main/java/org/zstack/header/identity/ExternalTenantResourceRefInventory.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,125 @@ | ||
| package org.zstack.header.identity; | ||
|
|
||
| import org.zstack.header.configuration.PythonClassInventory; | ||
| import org.zstack.header.search.Inventory; | ||
|
|
||
| import java.sql.Timestamp; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Inventory for ExternalTenantResourceRefVO | ||
| */ | ||
| @PythonClassInventory | ||
| @Inventory(mappingVOClass = ExternalTenantResourceRefVO.class) | ||
| public class ExternalTenantResourceRefInventory { | ||
| private long id; | ||
| private String source; | ||
| private String tenantId; | ||
| private String userId; | ||
| private String resourceUuid; | ||
| private String accountUuid; | ||
| private String resourceType; | ||
| private Timestamp createDate; | ||
| private Timestamp lastOpDate; | ||
|
|
||
| public ExternalTenantResourceRefInventory() { | ||
| } | ||
|
|
||
| public static List<ExternalTenantResourceRefInventory> valueOf(Collection<ExternalTenantResourceRefVO> vos) { | ||
| List<ExternalTenantResourceRefInventory> invs = new ArrayList<>(); | ||
| for (ExternalTenantResourceRefVO vo : vos) { | ||
| invs.add(valueOf(vo)); | ||
| } | ||
| return invs; | ||
| } | ||
|
|
||
| public static ExternalTenantResourceRefInventory valueOf(ExternalTenantResourceRefVO vo) { | ||
| return new ExternalTenantResourceRefInventory(vo); | ||
| } | ||
|
|
||
| public ExternalTenantResourceRefInventory(ExternalTenantResourceRefVO vo) { | ||
| this.id = vo.getId(); | ||
| this.source = vo.getSource(); | ||
| this.tenantId = vo.getTenantId(); | ||
| this.userId = vo.getUserId(); | ||
| this.resourceUuid = vo.getResourceUuid(); | ||
| this.accountUuid = vo.getAccountUuid(); | ||
| this.resourceType = vo.getResourceType(); | ||
| this.createDate = vo.getCreateDate(); | ||
| this.lastOpDate = vo.getLastOpDate(); | ||
| } | ||
|
|
||
| public long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| public String getResourceUuid() { | ||
| return resourceUuid; | ||
| } | ||
|
|
||
| public void setResourceUuid(String resourceUuid) { | ||
| this.resourceUuid = resourceUuid; | ||
| } | ||
|
|
||
| public String getAccountUuid() { | ||
| return accountUuid; | ||
| } | ||
|
|
||
| public void setAccountUuid(String accountUuid) { | ||
| this.accountUuid = accountUuid; | ||
| } | ||
|
|
||
| public String getResourceType() { | ||
| return resourceType; | ||
| } | ||
|
|
||
| public void setResourceType(String resourceType) { | ||
| this.resourceType = resourceType; | ||
| } | ||
|
|
||
| public Timestamp getCreateDate() { | ||
| return createDate; | ||
| } | ||
|
|
||
| public void setCreateDate(Timestamp createDate) { | ||
| this.createDate = createDate; | ||
| } | ||
|
|
||
| public Timestamp getLastOpDate() { | ||
| return lastOpDate; | ||
| } | ||
|
|
||
| public void setLastOpDate(Timestamp lastOpDate) { | ||
| this.lastOpDate = lastOpDate; | ||
| } | ||
| } |
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