Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ lazy val server = (project in file("server"))
// Server and control models are added as provided to avoid them being added as maven dependencies
// This is because the server and control models are included in the server jar
.dependsOn(serverModels % "provided", controlModels % "provided")
.dependsOn(controlApi % "test->compile")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed? which class?

@yili-db yili-db Nov 7, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The access control test base class SdkAccessControlBaseCRUDTest.java needs the following imports in order to create test users:

io.unitycatalog.control.api.UsersApi;
io.unitycatalog.control.ApiClient
io.unitycatalog.control.ApiException

.enablePlugins(CheckstylePlugin)
.settings (
name := s"$artifactNamePrefix-server",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
package io.unitycatalog.server.sdk.access;

import static io.unitycatalog.server.security.SecurityContext.Issuers.INTERNAL;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import io.unitycatalog.client.ApiException;
import io.unitycatalog.client.api.GrantsApi;
import io.unitycatalog.client.model.CreateCatalog;
import io.unitycatalog.client.model.CreateSchema;
import io.unitycatalog.client.model.PermissionsChange;
import io.unitycatalog.client.model.Privilege;
import io.unitycatalog.client.model.SecurableType;
import io.unitycatalog.client.model.UpdatePermissions;
import io.unitycatalog.control.api.UsersApi;
import io.unitycatalog.control.model.Email;
import io.unitycatalog.control.model.UserResource;
import io.unitycatalog.server.base.ServerConfig;
import io.unitycatalog.server.base.access.BaseAccessControlCRUDTest;
import io.unitycatalog.server.base.catalog.CatalogOperations;
import io.unitycatalog.server.base.schema.SchemaOperations;
import io.unitycatalog.server.persist.model.Privileges;
import io.unitycatalog.server.sdk.catalog.SdkCatalogOperations;
import io.unitycatalog.server.sdk.schema.SdkSchemaOperations;
import io.unitycatalog.server.utils.TestUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Base class for SDK-based access control CRUD tests that provides common support functionality.
*
* <p>This class extends {@link BaseAccessControlCRUDTest} and provides additional utilities for
* SDK-based tests, including:
*
* <ul>
* <li>Schema and table operations setup
* <li>User management (creating test users)
* <li>Permission management (granting/revoking permissions)
* <li>JWT token generation for test users
* <li>Common resource creation (catalogs, schemas)
* <li>Automatic cleanup of created users and granted permissions
* </ul>
*/
public abstract class SdkAccessControlBaseCRUDTest extends BaseAccessControlCRUDTest {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously the access control tests are only done via CLI. They are based on CliAccessControlBaseCrudTest which uses CLI to setup users and permissions. That won't allow us to do fine grain test between staging table and actual table because CLI can only do a table creation as a whole.
This new base class is just based on SDK and supports the user/permission setup.

private static final Logger LOGGER = LoggerFactory.getLogger(SdkAccessControlBaseCRUDTest.class);

public static final String METASTORE_NAME = "metastore"; // The name of metastore is this

protected ServerConfig adminConfig;
protected UsersApi usersApi;
protected GrantsApi grantsApi;

// Track created users and granted permissions for cleanup
private final List<String> createdUserIds = new ArrayList<>();
private final List<PermissionGrant> grantedPermissions = new ArrayList<>();

/** Helper class to track granted permissions for cleanup. */
private record PermissionGrant(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it's JDK 16+ language syntax , right ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked. It's from Java 14. We are using Java 17. So it should be fine?

String userEmail,
SecurableType resourceSecurableType,
String resourceFullName,
List<Privilege> privileges) {}

@Override
protected CatalogOperations createCatalogOperations(ServerConfig config) {
return new SdkCatalogOperations(TestUtils.createApiClient(config));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please file an issue to track that we need to reuse the ApiClient construction that we've already used in ApiClientFactory . Also @timothyw553 is helping to build the retry policy for it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #1181

}

/**
* Creates schema operations for the given server configuration.
*
* @param config Server configuration including URL and auth token
* @return A SchemaOperations instance
*/
protected SchemaOperations createSchemaOperations(ServerConfig config) {
return new SdkSchemaOperations(TestUtils.createApiClient(config));
}

@BeforeEach
@Override
public void setUp() {
super.setUp();
// Use admin token for creating shared resources
String adminToken = securityContext.createServiceToken();
adminConfig = new ServerConfig(serverConfig.getServerUrl(), adminToken);
usersApi = new UsersApi(createControlApiClient(adminConfig));
grantsApi = new GrantsApi(TestUtils.createApiClient(adminConfig));
createCommonResources();
}

@AfterEach
public void tearDown() {
// Revoke granted permissions
for (PermissionGrant grant : grantedPermissions) {
try {
LOGGER.debug(
"Revoking permissions {} from {} on {}",
grant.privileges,
grant.userEmail,
grant.resourceFullName);
PermissionsChange permissionsChange =
new PermissionsChange()
.principal(grant.userEmail)
.add(List.of())
.remove(grant.privileges);
UpdatePermissions updatePermissions =
new UpdatePermissions().changes(java.util.List.of(permissionsChange));
grantsApi.update(grant.resourceSecurableType, grant.resourceFullName, updatePermissions);
} catch (Exception e) {
LOGGER.warn(
"Failed to revoke permissions for {} on {}: {}",
grant.userEmail,
grant.resourceFullName,
e.getMessage());
}
}

// Delete created users
for (String userId : createdUserIds) {
try {
LOGGER.debug("Deleting test user: {}", userId);
usersApi.deleteUser(userId);
} catch (Exception e) {
LOGGER.warn("Failed to delete test user {}: {}", userId, e.getMessage());
}
}

// Clear tracking lists for each test
createdUserIds.clear();
grantedPermissions.clear();
}

/** Creates common resources (catalog and schema) using an admin token for authorization. */
private void createCommonResources() {
CatalogOperations adminCatalogOps =
new SdkCatalogOperations(TestUtils.createApiClient(adminConfig));
SchemaOperations adminSchemaOps =
new SdkSchemaOperations(TestUtils.createApiClient(adminConfig));

CreateCatalog createCatalog =
new CreateCatalog().name(TestUtils.CATALOG_NAME).comment(TestUtils.COMMENT);

@yili-db yili-db Nov 6, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The catalog, along with everything under it, is cleaned up by BaseCRUDTest

try {
adminCatalogOps.createCatalog(createCatalog);
adminSchemaOps.createSchema(
new CreateSchema().name(TestUtils.SCHEMA_NAME).catalogName(TestUtils.CATALOG_NAME));
} catch (ApiException e) {
throw new RuntimeException(e);
}
}

/**
* Creates a control API client for user management operations.
*
* @param config Server configuration including URL and auth token
* @return A configured control API client
*/
protected io.unitycatalog.control.ApiClient createControlApiClient(ServerConfig config) {
io.unitycatalog.control.ApiClient controlApiClient = new io.unitycatalog.control.ApiClient();
java.net.URI uri = java.net.URI.create(config.getServerUrl());
int port = uri.getPort();
controlApiClient.setHost(uri.getHost());
controlApiClient.setPort(port);
controlApiClient.setScheme(uri.getScheme());
if (config.getAuthToken() != null && !config.getAuthToken().isEmpty()) {
controlApiClient.setRequestInterceptor(
request -> request.header("Authorization", "Bearer " + config.getAuthToken()));
}
Comment on lines +167 to +175

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen lots of places are trying to constrcut the api client by themselves. Actually we need to unify them into one place, because:

  1. Retry policy.
  2. need to care about this issue: Allow providing base path for UC API Client #1130

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the control api client. It looks similar to io.unitycatalog.client.ApiClient but it's actually a different class generated by openapi with no common base class.

return controlApiClient;
}

/**
* Creates a test user in the database. This is required before the user can be authenticated. The
* created user is tracked and will be automatically deleted during tearDown.
*
* @param email The user's email
* @param name The user's display name
* @return The created User object
* @throws io.unitycatalog.control.ApiException if user creation fails
*/
protected UserResource createTestUser(String email, String name)
throws io.unitycatalog.control.ApiException {
UserResource userResource =
new UserResource()
.displayName(name)
.emails(java.util.List.of(new Email().value(email).primary(true)));
UserResource createdUser = usersApi.createUser(userResource);
// Track the created user for cleanup
createdUserIds.add(createdUser.getId());
LOGGER.debug("Created test user: {} (ID: {})", email, createdUser.getId());
return createdUser;
}

/**
* Grants permissions to a user on a resource. The granted permissions are tracked and will be
* automatically revoked during tearDown.
*
* @param userEmail The user's email
* @param resourceSecurableType The resource securable type
* @param resourceFullName The resource full name
* @param privileges The privileges to grant
* @throws Exception if permission grant fails
*/
protected void grantPermissions(
String userEmail,
SecurableType resourceSecurableType,
String resourceFullName,
Privileges... privileges)
throws Exception {
LOGGER.debug("grantPermissions {} {} {}", userEmail, resourceFullName, privileges);
// Build list of Privilege enums from Privileges
List<Privilege> privilegeList =
Arrays.stream(privileges)
.map(p -> Privilege.fromValue(p.getValue()))
.collect(Collectors.toList());
if (!privilegeList.isEmpty()) {
PermissionsChange permissionsChange =
new PermissionsChange().principal(userEmail).add(privilegeList).remove(List.of());
UpdatePermissions updatePermissions =
new UpdatePermissions().changes(java.util.List.of(permissionsChange));
grantsApi.update(resourceSecurableType, resourceFullName, updatePermissions);
// Track the granted permissions for cleanup
grantedPermissions.add(
new PermissionGrant(userEmail, resourceSecurableType, resourceFullName, privilegeList));
}
}

/**
* Helper method to create a JWT token for testing purposes using the actual RSA keys from the
* configuration.
*
* <p>This method creates tokens the same way the production system does, using RSA512 algorithm
* with the keys from etc/conf directory.
*
* @param subject The subject (email) for the JWT token
* @return A JWT token string signed with the actual RSA private key
*/
protected String createTestJwtToken(String subject) {
try {
// Use the securityConfiguration that was set up by BaseAccessControlCRUDTest
Algorithm algorithm = securityConfiguration.algorithmRSA();
String keyId = securityConfiguration.getKeyId();

// Create token following the same pattern as SecurityContext.createAccessToken
return JWT.create()
.withSubject(subject)
.withIssuer(INTERNAL)
.withIssuedAt(new Date())
.withKeyId(keyId)
.withJWTId(UUID.randomUUID().toString())
.withClaim("email", subject)
.sign(algorithm);
} catch (Exception e) {
throw new RuntimeException("Failed to create test JWT token: " + e.getMessage(), e);
}
}

/**
* Helper method to create a ServerConfig for the user.
*
* @param subject The subject (email) for the user
* @return A ServerConfig with the user token
*/
protected ServerConfig createTestUserServerConfig(String subject) {
return new ServerConfig(serverConfig.getServerUrl(), createTestJwtToken(subject));
}
}
Loading