Skip to content

Commit d93f13b

Browse files
committed
WIP cred vend to storage-creds-cache - step 1
1 parent 606c0d8 commit d93f13b

File tree

15 files changed

+198
-338
lines changed

15 files changed

+198
-338
lines changed

polaris-core/src/main/java/org/apache/polaris/core/persistence/BaseMetaStoreManager.java

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818
*/
1919
package org.apache.polaris.core.persistence;
2020

21+
import static java.util.Objects.requireNonNull;
22+
2123
import com.fasterxml.jackson.core.JsonProcessingException;
2224
import com.fasterxml.jackson.core.type.TypeReference;
2325
import com.fasterxml.jackson.databind.ObjectMapper;
2426
import jakarta.annotation.Nonnull;
2527
import java.util.Map;
2628
import java.util.Set;
2729
import org.apache.polaris.core.PolarisCallContext;
28-
import org.apache.polaris.core.PolarisDiagnostics;
2930
import org.apache.polaris.core.entity.PolarisBaseEntity;
3031
import org.apache.polaris.core.entity.PolarisEntityConstants;
3132
import org.apache.polaris.core.entity.PolarisEntitySubType;
3233
import org.apache.polaris.core.entity.PolarisEntityType;
3334
import org.apache.polaris.core.persistence.dao.entity.BaseResult;
34-
import org.apache.polaris.core.persistence.dao.entity.EntityResult;
3535
import org.apache.polaris.core.persistence.dao.entity.GenerateEntityIdResult;
3636
import org.apache.polaris.core.persistence.dao.entity.ScopedCredentialsResult;
3737
import org.apache.polaris.core.storage.AccessConfig;
@@ -54,9 +54,7 @@ public BaseMetaStoreManager(PolarisStorageIntegrationProvider storageIntegration
5454
@Override
5555
public @Nonnull ScopedCredentialsResult getSubscopedCredsForEntity(
5656
@Nonnull PolarisCallContext callCtx,
57-
long catalogId,
58-
long entityId,
59-
PolarisEntityType entityType,
57+
@Nonnull PolarisStorageConfigurationInfo storageConfigurationInfo,
6058
boolean allowListOperation,
6159
@Nonnull Set<String> allowedReadLocations,
6260
@Nonnull Set<String> allowedWriteLocations) {
@@ -68,28 +66,12 @@ public BaseMetaStoreManager(PolarisStorageIntegrationProvider storageIntegration
6866
!allowedReadLocations.isEmpty() || !allowedWriteLocations.isEmpty(),
6967
"allowed_locations_to_subscope_is_required");
7068

71-
// reload the entity, error out if not found
72-
EntityResult reloadedEntity = loadEntity(callCtx, catalogId, entityId, entityType);
73-
if (reloadedEntity.getReturnStatus() != BaseResult.ReturnStatus.SUCCESS) {
74-
return new ScopedCredentialsResult(
75-
reloadedEntity.getReturnStatus(), reloadedEntity.getExtraInformation());
76-
}
77-
7869
// get storage integration
79-
PolarisBaseEntity entity = reloadedEntity.getEntity();
8070
PolarisStorageIntegration<PolarisStorageConfigurationInfo> storageIntegration =
81-
storageIntegrationProvider.getStorageIntegrationForConfig(
82-
BaseMetaStoreManager.extractStorageConfiguration(callCtx.getDiagServices(), entity));
71+
storageIntegrationProvider.getStorageIntegrationForConfig(storageConfigurationInfo);
8372

8473
// cannot be null
85-
callCtx
86-
.getDiagServices()
87-
.checkNotNull(
88-
storageIntegration,
89-
"storage_integration_not_exists",
90-
"catalogId={}, entityId={}",
91-
catalogId,
92-
entityId);
74+
requireNonNull(storageIntegration);
9375

9476
try {
9577
AccessConfig accessConfig =
@@ -105,22 +87,6 @@ public BaseMetaStoreManager(PolarisStorageIntegrationProvider storageIntegration
10587
}
10688
}
10789

108-
public static PolarisStorageConfigurationInfo extractStorageConfiguration(
109-
@Nonnull PolarisDiagnostics diagnostics, PolarisBaseEntity reloadedEntity) {
110-
Map<String, String> propMap =
111-
PolarisObjectMapperUtil.deserializeProperties(reloadedEntity.getInternalProperties());
112-
String storageConfigInfoStr =
113-
propMap.get(PolarisEntityConstants.getStorageConfigInfoPropertyName());
114-
115-
diagnostics.check(
116-
storageConfigInfoStr != null,
117-
"missing_storage_configuration_info",
118-
"catalogId={}, entityId={}",
119-
reloadedEntity.getCatalogId(),
120-
reloadedEntity.getId());
121-
return PolarisStorageConfigurationInfo.deserialize(storageConfigInfoStr);
122-
}
123-
12490
/**
12591
* Given the internal property as a map of key/value pairs, serialize it to a String
12692
*

polaris-core/src/main/java/org/apache/polaris/core/persistence/TransactionWorkspaceMetaStoreManager.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.apache.polaris.core.persistence.pagination.PageToken;
5656
import org.apache.polaris.core.policy.PolicyEntity;
5757
import org.apache.polaris.core.policy.PolicyType;
58+
import org.apache.polaris.core.storage.PolarisStorageConfigurationInfo;
5859

5960
/**
6061
* Wraps an existing impl of PolarisMetaStoreManager and delegates expected "read" operations
@@ -332,17 +333,13 @@ public EntitiesResult loadTasks(
332333
@Override
333334
public ScopedCredentialsResult getSubscopedCredsForEntity(
334335
@Nonnull PolarisCallContext callCtx,
335-
long catalogId,
336-
long entityId,
337-
PolarisEntityType entityType,
336+
@Nonnull PolarisStorageConfigurationInfo storageConfigurationInfo,
338337
boolean allowListOperation,
339338
@Nonnull Set<String> allowedReadLocations,
340339
@Nonnull Set<String> allowedWriteLocations) {
341340
return delegate.getSubscopedCredsForEntity(
342341
callCtx,
343-
catalogId,
344-
entityId,
345-
entityType,
342+
storageConfigurationInfo,
346343
allowListOperation,
347344
allowedReadLocations,
348345
allowedWriteLocations);

polaris-core/src/main/java/org/apache/polaris/core/storage/PolarisCredentialVendor.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import jakarta.annotation.Nonnull;
2222
import java.util.Set;
2323
import org.apache.polaris.core.PolarisCallContext;
24-
import org.apache.polaris.core.entity.PolarisEntityType;
2524
import org.apache.polaris.core.persistence.dao.entity.ScopedCredentialsResult;
2625

2726
/** Manage credentials for storage locations. */
@@ -31,20 +30,16 @@ public interface PolarisCredentialVendor {
3130
* locations.
3231
*
3332
* @param callCtx the polaris call context
34-
* @param catalogId the catalog id
35-
* @param entityId the entity id
33+
* @param storageConfigurationInfo storage configuration object
3634
* @param allowListOperation whether to allow LIST operation on the allowedReadLocations and
3735
* allowedWriteLocations
3836
* @param allowedReadLocations a set of allowed to read locations
3937
* @param allowedWriteLocations a set of allowed to write locations
4038
* @return an enum map containing the scoped credentials
4139
*/
42-
@Nonnull
4340
ScopedCredentialsResult getSubscopedCredsForEntity(
4441
@Nonnull PolarisCallContext callCtx,
45-
long catalogId,
46-
long entityId,
47-
PolarisEntityType entityType,
42+
@Nonnull PolarisStorageConfigurationInfo storageConfigurationInfo,
4843
boolean allowListOperation,
4944
@Nonnull Set<String> allowedReadLocations,
5045
@Nonnull Set<String> allowedWriteLocations);

polaris-core/src/main/java/org/apache/polaris/core/storage/cache/StorageCredentialCache.java

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,24 @@
1818
*/
1919
package org.apache.polaris.core.storage.cache;
2020

21+
import static java.util.Objects.requireNonNull;
22+
2123
import com.github.benmanes.caffeine.cache.Caffeine;
2224
import com.github.benmanes.caffeine.cache.Expiry;
2325
import com.github.benmanes.caffeine.cache.LoadingCache;
2426
import com.google.common.annotations.VisibleForTesting;
2527
import jakarta.annotation.Nonnull;
26-
import jakarta.annotation.Nullable;
2728
import java.time.Duration;
28-
import java.util.Map;
29-
import java.util.Optional;
3029
import java.util.Set;
3130
import java.util.function.Function;
3231
import org.apache.iceberg.exceptions.UnprocessableEntityException;
3332
import org.apache.polaris.core.PolarisCallContext;
3433
import org.apache.polaris.core.config.FeatureConfiguration;
3534
import org.apache.polaris.core.config.RealmConfig;
36-
import org.apache.polaris.core.entity.PolarisEntity;
37-
import org.apache.polaris.core.entity.PolarisEntityType;
3835
import org.apache.polaris.core.persistence.dao.entity.ScopedCredentialsResult;
3936
import org.apache.polaris.core.storage.AccessConfig;
4037
import org.apache.polaris.core.storage.PolarisCredentialVendor;
38+
import org.apache.polaris.core.storage.PolarisStorageConfigurationInfo;
4139
import org.slf4j.Logger;
4240
import org.slf4j.LoggerFactory;
4341

@@ -93,7 +91,7 @@ private long maxCacheDurationMs(RealmConfig realmConfig) {
9391
*
9492
* @param credentialVendor the credential vendor used to generate a new scoped creds if needed
9593
* @param callCtx the call context
96-
* @param polarisEntity the polaris entity that is going to scoped creds
94+
* @param storageConfigurationInfo storage configuration
9795
* @param allowListOperation whether allow list action on the provided read and write locations
9896
* @param allowedReadLocations a set of allowed to read locations
9997
* @param allowedWriteLocations a set of allowed to write locations.
@@ -102,19 +100,14 @@ private long maxCacheDurationMs(RealmConfig realmConfig) {
102100
public AccessConfig getOrGenerateSubScopeCreds(
103101
@Nonnull PolarisCredentialVendor credentialVendor,
104102
@Nonnull PolarisCallContext callCtx,
105-
@Nonnull PolarisEntity polarisEntity,
103+
@Nonnull PolarisStorageConfigurationInfo storageConfigurationInfo,
106104
boolean allowListOperation,
107105
@Nonnull Set<String> allowedReadLocations,
108106
@Nonnull Set<String> allowedWriteLocations) {
109-
if (!isTypeSupported(polarisEntity.getType())) {
110-
callCtx
111-
.getDiagServices()
112-
.fail("entity_type_not_suppported_to_scope_creds", "type={}", polarisEntity.getType());
113-
}
114107
StorageCredentialCacheKey key =
115108
StorageCredentialCacheKey.of(
116109
callCtx.getRealmContext().getRealmIdentifier(),
117-
polarisEntity,
110+
storageConfigurationInfo,
118111
allowListOperation,
119112
allowedReadLocations,
120113
allowedWriteLocations);
@@ -125,9 +118,7 @@ public AccessConfig getOrGenerateSubScopeCreds(
125118
ScopedCredentialsResult scopedCredentialsResult =
126119
credentialVendor.getSubscopedCredsForEntity(
127120
callCtx,
128-
k.catalogId(),
129-
polarisEntity.getId(),
130-
polarisEntity.getType(),
121+
storageConfigurationInfo,
131122
k.allowedListAction(),
132123
k.allowedReadLocations(),
133124
k.allowedWriteLocations());
@@ -144,30 +135,16 @@ public AccessConfig getOrGenerateSubScopeCreds(
144135
"Failed to get subscoped credentials: %s",
145136
scopedCredentialsResult.getExtraInformation());
146137
};
147-
return cache.get(key, loader).toAccessConfig();
138+
return requireNonNull(cache.get(key, loader)).toAccessConfig();
148139
}
149140

150141
@VisibleForTesting
151-
@Nullable
152-
Map<String, String> getIfPresent(StorageCredentialCacheKey key) {
153-
return getAccessConfig(key).map(AccessConfig::credentials).orElse(null);
154-
}
155-
156-
@VisibleForTesting
157-
Optional<AccessConfig> getAccessConfig(StorageCredentialCacheKey key) {
158-
return Optional.ofNullable(cache.getIfPresent(key))
159-
.map(StorageCredentialCacheEntry::toAccessConfig);
160-
}
161-
162-
private boolean isTypeSupported(PolarisEntityType type) {
163-
return type == PolarisEntityType.CATALOG
164-
|| type == PolarisEntityType.NAMESPACE
165-
|| type == PolarisEntityType.TABLE_LIKE
166-
|| type == PolarisEntityType.TASK;
142+
boolean isPresent(StorageCredentialCacheKey key) {
143+
return cache.getIfPresent(key) != null;
167144
}
168145

169146
@VisibleForTesting
170-
public long getEstimatedSize() {
147+
long getEstimatedSize() {
171148
return this.cache.estimatedSize();
172149
}
173150

polaris-core/src/main/java/org/apache/polaris/core/storage/cache/StorageCredentialCacheKey.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
*/
1919
package org.apache.polaris.core.storage.cache;
2020

21-
import jakarta.annotation.Nullable;
2221
import java.util.Set;
23-
import org.apache.polaris.core.entity.PolarisEntity;
24-
import org.apache.polaris.core.entity.PolarisEntityConstants;
22+
import org.apache.polaris.core.storage.PolarisStorageConfigurationInfo;
2523
import org.apache.polaris.immutables.PolarisImmutable;
2624
import org.immutables.value.Value;
2725

@@ -32,35 +30,26 @@ public interface StorageCredentialCacheKey {
3230
String realmId();
3331

3432
@Value.Parameter(order = 2)
35-
long catalogId();
33+
PolarisStorageConfigurationInfo storageConfigurationInfo();
3634

3735
@Value.Parameter(order = 3)
38-
@Nullable
39-
String storageConfigSerializedStr();
40-
41-
@Value.Parameter(order = 4)
4236
boolean allowedListAction();
4337

44-
@Value.Parameter(order = 5)
38+
@Value.Parameter(order = 4)
4539
Set<String> allowedReadLocations();
4640

47-
@Value.Parameter(order = 6)
41+
@Value.Parameter(order = 5)
4842
Set<String> allowedWriteLocations();
4943

5044
static StorageCredentialCacheKey of(
5145
String realmId,
52-
PolarisEntity entity,
46+
PolarisStorageConfigurationInfo storageConfigurationInfo,
5347
boolean allowedListAction,
5448
Set<String> allowedReadLocations,
5549
Set<String> allowedWriteLocations) {
56-
String storageConfigSerializedStr =
57-
entity
58-
.getInternalPropertiesAsMap()
59-
.get(PolarisEntityConstants.getStorageConfigInfoPropertyName());
6050
return ImmutableStorageCredentialCacheKey.of(
6151
realmId,
62-
entity.getCatalogId(),
63-
storageConfigSerializedStr,
52+
storageConfigurationInfo,
6453
allowedListAction,
6554
allowedReadLocations,
6655
allowedWriteLocations);

0 commit comments

Comments
 (0)