From bdf04faeb60abcc77a8a4922f4b2473ee09484d7 Mon Sep 17 00:00:00 2001 From: fanng Date: Thu, 12 Sep 2024 11:56:26 +0800 Subject: [PATCH 01/10] credential framework --- .../credential/CredentialConstants.java | 27 ++++++ common/build.gradle.kts | 1 + .../gravitino/credential/Credential.java | 62 ++++++++++++++ .../gravitino/credential/CatalogContext.java | 38 +++++++++ .../apache/gravitino/credential/Context.java | 30 +++++++ .../credential/CredentialProvider.java | 58 +++++++++++++ .../credential/CredentialProviderFactory.java | 69 ++++++++++++++++ .../gravitino/credential/LocationContext.java | 57 +++++++++++++ .../credential/DummyCredentialProvider.java | 82 +++++++++++++++++++ .../credential/TestCredentialProvider.java | 53 ++++++++++++ ...he.gravitino.credential.CredentialProvider | 19 +++++ 11 files changed, 496 insertions(+) create mode 100644 catalogs/catalog-common/src/main/java/org/apache/gravitino/credential/CredentialConstants.java create mode 100644 common/src/main/java/org/apache/gravitino/credential/Credential.java create mode 100644 core/src/main/java/org/apache/gravitino/credential/CatalogContext.java create mode 100644 core/src/main/java/org/apache/gravitino/credential/Context.java create mode 100644 core/src/main/java/org/apache/gravitino/credential/CredentialProvider.java create mode 100644 core/src/main/java/org/apache/gravitino/credential/CredentialProviderFactory.java create mode 100644 core/src/main/java/org/apache/gravitino/credential/LocationContext.java create mode 100644 core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java create mode 100644 core/src/test/java/org/apache/gravitino/credential/TestCredentialProvider.java create mode 100644 core/src/test/resources/META-INF/services/org.apache.gravitino.credential.CredentialProvider diff --git a/catalogs/catalog-common/src/main/java/org/apache/gravitino/credential/CredentialConstants.java b/catalogs/catalog-common/src/main/java/org/apache/gravitino/credential/CredentialConstants.java new file mode 100644 index 00000000000..9ed3c4a2d96 --- /dev/null +++ b/catalogs/catalog-common/src/main/java/org/apache/gravitino/credential/CredentialConstants.java @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.credential; + +public class CredentialConstants { + public static final String CREDENTIAL_TYPE = "credential-type"; + public static final String EXPIRE_TIME_SECS = "expire-time-secs"; + + private CredentialConstants() {} +} diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 91e2d137f25..4e830b6a735 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -28,6 +28,7 @@ plugins { dependencies { implementation(project(":api")) + implementation(project(":catalogs:catalog-common")) implementation(libs.commons.collections4) implementation(libs.commons.lang3) diff --git a/common/src/main/java/org/apache/gravitino/credential/Credential.java b/common/src/main/java/org/apache/gravitino/credential/Credential.java new file mode 100644 index 00000000000..b9919621fd8 --- /dev/null +++ b/common/src/main/java/org/apache/gravitino/credential/Credential.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.credential; + +import com.google.common.collect.ImmutableMap; +import java.util.Map; + +/** Interface representing a credential with type, expiration time, and additional information. */ +public interface Credential { + + /** + * Returns the type of the credential. It should same with the credential type of the credential + * provider. + * + * @return the credential type as a String. + */ + String getCredentialType(); + + /** + * Returns the expiration time of the credential in seconds since the epoch. + * + * @return the expiration time as a long. + */ + long getExpireTimeSecs(); + + /** + * Returns additional information related to the credential. + * + * @return a map of credential information. + */ + Map getCredentialInfo(); + + /** + * Converts the credential to properties to transfer the credential. + * + * @return a map containing credential properties. + */ + default Map toProperties() { + return new ImmutableMap.Builder() + .putAll(getCredentialInfo()) + .put(CredentialConstants.CREDENTIAL_TYPE, getCredentialType()) + .put(CredentialConstants.EXPIRE_TIME_SECS, String.valueOf(getExpireTimeSecs())) + .build(); + } +} diff --git a/core/src/main/java/org/apache/gravitino/credential/CatalogContext.java b/core/src/main/java/org/apache/gravitino/credential/CatalogContext.java new file mode 100644 index 00000000000..563464b0e6f --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/credential/CatalogContext.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.credential; + +import com.google.common.base.Preconditions; +import javax.validation.constraints.NotNull; + +/** CatalogContext is generated when user requesting catalog credentials. */ +public class CatalogContext implements Context { + @NotNull private final String userName; + + public CatalogContext(String userName) { + Preconditions.checkNotNull(userName, "User name should not be null"); + this.userName = userName; + } + + @Override + public String getUserName() { + return userName; + } +} diff --git a/core/src/main/java/org/apache/gravitino/credential/Context.java b/core/src/main/java/org/apache/gravitino/credential/Context.java new file mode 100644 index 00000000000..0a796baf39b --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/credential/Context.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.credential; + +/** Contains context information to get credential from credential provider. */ +public interface Context { + /** + * Providing the username. + * + * @return A string identifying user name. + */ + String getUserName(); +} diff --git a/core/src/main/java/org/apache/gravitino/credential/CredentialProvider.java b/core/src/main/java/org/apache/gravitino/credential/CredentialProvider.java new file mode 100644 index 00000000000..d5005ae1643 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/credential/CredentialProvider.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.credential; + +import java.util.Map; +import javax.annotation.Nullable; + +/** + * Interface for credential providers. + * + *

A credential provider is responsible for managing and retrieving credentials. + */ +public interface CredentialProvider { + /** + * Initializes the credential provider with catalog properties. + * + * @param properties catalog properties that can be used to configure the provider. The specific + * properties required vary by implementation. + */ + void initialize(Map properties); + + /** Stops the credential provider, performing any necessary cleanup. */ + void stop(); + + /** + * Returns the type of credential, it should be identical in Gravitino. + * + * @return A string identifying the type of credentials. + */ + String credentialType(); + + /** + * Obtains a credential based on the provided context information. + * + * @param context A context object providing necessary information for retrieving credentials. + * @return A Credential object containing the authentication information needed to access a system + * or resource. Null will be returned if no credential is available. + */ + @Nullable + Credential getCredential(Context context); +} diff --git a/core/src/main/java/org/apache/gravitino/credential/CredentialProviderFactory.java b/core/src/main/java/org/apache/gravitino/credential/CredentialProviderFactory.java new file mode 100644 index 00000000000..3833eeda9bf --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/credential/CredentialProviderFactory.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.credential; + +import com.google.common.collect.Iterables; +import com.google.common.collect.Streams; +import java.util.List; +import java.util.Map; +import java.util.ServiceLoader; +import java.util.stream.Collectors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CredentialProviderFactory { + private static final Logger LOG = LoggerFactory.getLogger(CredentialProviderFactory.class); + + public static CredentialProvider create( + String credentialType, Map catalogProperties) { + Class providerClz = lookupCredentialProvider(credentialType); + try { + CredentialProvider provider = providerClz.getDeclaredConstructor().newInstance(); + provider.initialize(catalogProperties); + return provider; + } catch (Exception e) { + LOG.warn("Create credential provider failed, {}", credentialType, e); + throw new RuntimeException(e); + } + } + + private static Class lookupCredentialProvider( + String credentialType) { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + ServiceLoader serviceLoader = + ServiceLoader.load(CredentialProvider.class, classLoader); + List> providers = + Streams.stream(serviceLoader.iterator()) + .filter( + credentialProvider -> + credentialType.equalsIgnoreCase(credentialProvider.credentialType())) + .map(CredentialProvider::getClass) + .collect(Collectors.toList()); + + if (providers.isEmpty()) { + throw new IllegalArgumentException("No credential provider found for: " + credentialType); + } else if (providers.size() > 1) { + throw new IllegalArgumentException( + "Multiple credential providers found for: " + credentialType); + } else { + return Iterables.getOnlyElement(providers); + } + } +} diff --git a/core/src/main/java/org/apache/gravitino/credential/LocationContext.java b/core/src/main/java/org/apache/gravitino/credential/LocationContext.java new file mode 100644 index 00000000000..07d192d6dbd --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/credential/LocationContext.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.credential; + +import com.google.common.base.Preconditions; +import java.util.Set; +import javax.validation.constraints.NotNull; + +/** + * LocationContext is generated when user requesting resources associated with storage location like + * table, fileset, etc. + */ +public class LocationContext implements Context { + + @NotNull private final Set writeLocations; + @NotNull private final Set readLocations; + @NotNull private final String userName; + + public LocationContext(String userName, Set writeLocations, Set readLocations) { + Preconditions.checkNotNull(userName, "User name should not be null"); + Preconditions.checkNotNull(writeLocations, "Write locations should not be null"); + Preconditions.checkNotNull(readLocations, "Read locations should not be null"); + this.userName = userName; + this.writeLocations = writeLocations; + this.readLocations = readLocations; + } + + @Override + public String getUserName() { + return userName; + } + + public Set getWriteLocations() { + return writeLocations; + } + + public Set getReadLocations() { + return readLocations; + } +} diff --git a/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java b/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java new file mode 100644 index 00000000000..d6bd9328f99 --- /dev/null +++ b/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.credential; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableMap; +import java.util.Map; +import java.util.Set; +import lombok.Getter; + +public class DummyCredentialProvider implements CredentialProvider { + Map properties; + static final String CREDENTIAL_TYPE = "dummy"; + + @Override + public void initialize(Map properties) { + this.properties = properties; + } + + @Override + public void stop() {} + + @Override + public String credentialType() { + return CREDENTIAL_TYPE; + } + + @Override + public Credential getCredential(Context context) { + Preconditions.checkArgument( + context instanceof LocationContext || context instanceof CatalogContext, + "Doesn't support context: " + context.getClass().getSimpleName()); + if (context instanceof LocationContext) { + return new DummyCredential((LocationContext) context); + } + return null; + } + + public static class DummyCredential implements Credential { + + @Getter private Set writeLocations; + @Getter private Set readLocations; + + public DummyCredential(LocationContext locationContext) { + this.writeLocations = locationContext.getWriteLocations(); + this.readLocations = locationContext.getReadLocations(); + } + + @Override + public String getCredentialType() { + return DummyCredentialProvider.CREDENTIAL_TYPE; + } + + @Override + public long getExpireTimeSecs() { + return 0; + } + + @Override + public Map getCredentialInfo() { + return ImmutableMap.of( + "writeLocation", writeLocations.toString(), "readLocation", readLocations.toString()); + } + } +} diff --git a/core/src/test/java/org/apache/gravitino/credential/TestCredentialProvider.java b/core/src/test/java/org/apache/gravitino/credential/TestCredentialProvider.java new file mode 100644 index 00000000000..81019c86102 --- /dev/null +++ b/core/src/test/java/org/apache/gravitino/credential/TestCredentialProvider.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.credential; + +import java.util.Map; +import org.apache.gravitino.credential.DummyCredentialProvider.DummyCredential; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.testcontainers.shaded.com.google.common.collect.ImmutableMap; +import org.testcontainers.shaded.com.google.common.collect.ImmutableSet; + +public class TestCredentialProvider { + @Test + void testCredentialProvider() { + Map catalogProperties = ImmutableMap.of("a", "b"); + CredentialProvider credentialProvider = + CredentialProviderFactory.create( + DummyCredentialProvider.CREDENTIAL_TYPE, catalogProperties); + Assertions.assertEquals( + DummyCredentialProvider.CREDENTIAL_TYPE, credentialProvider.credentialType()); + Assertions.assertTrue(credentialProvider instanceof DummyCredentialProvider); + DummyCredentialProvider dummyCredentialProvider = (DummyCredentialProvider) credentialProvider; + Assertions.assertEquals(catalogProperties, dummyCredentialProvider.properties); + + ImmutableSet writeLocations = ImmutableSet.of("location1"); + ImmutableSet readLocations = ImmutableSet.of("location2"); + + LocationContext locationContext = new LocationContext("user", writeLocations, readLocations); + Credential credential = dummyCredentialProvider.getCredential(locationContext); + Assertions.assertTrue(credential instanceof DummyCredential); + DummyCredential dummyCredential = (DummyCredential) credential; + + Assertions.assertEquals(writeLocations, dummyCredential.getWriteLocations()); + Assertions.assertEquals(readLocations, dummyCredential.getReadLocations()); + } +} diff --git a/core/src/test/resources/META-INF/services/org.apache.gravitino.credential.CredentialProvider b/core/src/test/resources/META-INF/services/org.apache.gravitino.credential.CredentialProvider new file mode 100644 index 00000000000..cbdbff0bee9 --- /dev/null +++ b/core/src/test/resources/META-INF/services/org.apache.gravitino.credential.CredentialProvider @@ -0,0 +1,19 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +org.apache.gravitino.credential.DummyCredentialProvider \ No newline at end of file From 22a470974de263fd6753e838e411e7de14b5a4bf Mon Sep 17 00:00:00 2001 From: fanng Date: Tue, 8 Oct 2024 15:25:51 +0800 Subject: [PATCH 02/10] polish --- .../java/org/apache/gravitino/credential/Credential.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/org/apache/gravitino/credential/Credential.java b/common/src/main/java/org/apache/gravitino/credential/Credential.java index b9919621fd8..c96e81776b1 100644 --- a/common/src/main/java/org/apache/gravitino/credential/Credential.java +++ b/common/src/main/java/org/apache/gravitino/credential/Credential.java @@ -34,21 +34,21 @@ public interface Credential { String getCredentialType(); /** - * Returns the expiration time of the credential in seconds since the epoch. + * Returns the expiration time of the credential in seconds since the epoch, 0 means not expire. * * @return the expiration time as a long. */ long getExpireTimeSecs(); /** - * Returns additional information related to the credential. + * Returns credential information. * * @return a map of credential information. */ Map getCredentialInfo(); /** - * Converts the credential to properties to transfer the credential. + * Converts the credential to properties to transfer the credential though API. * * @return a map containing credential properties. */ From 8397303f2a3c95ca11079326c7dbd674e5cb6575 Mon Sep 17 00:00:00 2001 From: fanng Date: Wed, 9 Oct 2024 15:51:43 +0800 Subject: [PATCH 03/10] polish --- .../org/apache/gravitino/credential/Credential.java | 12 ++++++------ .../gravitino/credential/CredentialProvider.java | 6 ++---- .../credential/DummyCredentialProvider.java | 8 ++++---- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/common/src/main/java/org/apache/gravitino/credential/Credential.java b/common/src/main/java/org/apache/gravitino/credential/Credential.java index c96e81776b1..91fd231cc22 100644 --- a/common/src/main/java/org/apache/gravitino/credential/Credential.java +++ b/common/src/main/java/org/apache/gravitino/credential/Credential.java @@ -31,21 +31,21 @@ public interface Credential { * * @return the credential type as a String. */ - String getCredentialType(); + String credentialType(); /** * Returns the expiration time of the credential in seconds since the epoch, 0 means not expire. * * @return the expiration time as a long. */ - long getExpireTimeSecs(); + long expireTimeSecs(); /** * Returns credential information. * * @return a map of credential information. */ - Map getCredentialInfo(); + Map credentialInfo(); /** * Converts the credential to properties to transfer the credential though API. @@ -54,9 +54,9 @@ public interface Credential { */ default Map toProperties() { return new ImmutableMap.Builder() - .putAll(getCredentialInfo()) - .put(CredentialConstants.CREDENTIAL_TYPE, getCredentialType()) - .put(CredentialConstants.EXPIRE_TIME_SECS, String.valueOf(getExpireTimeSecs())) + .putAll(credentialInfo()) + .put(CredentialConstants.CREDENTIAL_TYPE, credentialType()) + .put(CredentialConstants.EXPIRE_TIME_SECS, String.valueOf(expireTimeSecs())) .build(); } } diff --git a/core/src/main/java/org/apache/gravitino/credential/CredentialProvider.java b/core/src/main/java/org/apache/gravitino/credential/CredentialProvider.java index d5005ae1643..fa3dce7df20 100644 --- a/core/src/main/java/org/apache/gravitino/credential/CredentialProvider.java +++ b/core/src/main/java/org/apache/gravitino/credential/CredentialProvider.java @@ -19,6 +19,7 @@ package org.apache.gravitino.credential; +import java.io.Closeable; import java.util.Map; import javax.annotation.Nullable; @@ -27,7 +28,7 @@ * *

A credential provider is responsible for managing and retrieving credentials. */ -public interface CredentialProvider { +public interface CredentialProvider extends Closeable { /** * Initializes the credential provider with catalog properties. * @@ -36,9 +37,6 @@ public interface CredentialProvider { */ void initialize(Map properties); - /** Stops the credential provider, performing any necessary cleanup. */ - void stop(); - /** * Returns the type of credential, it should be identical in Gravitino. * diff --git a/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java b/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java index d6bd9328f99..24853a483cb 100644 --- a/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java +++ b/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java @@ -35,7 +35,7 @@ public void initialize(Map properties) { } @Override - public void stop() {} + public void close() {} @Override public String credentialType() { @@ -64,17 +64,17 @@ public DummyCredential(LocationContext locationContext) { } @Override - public String getCredentialType() { + public String credentialType() { return DummyCredentialProvider.CREDENTIAL_TYPE; } @Override - public long getExpireTimeSecs() { + public long expireTimeSecs() { return 0; } @Override - public Map getCredentialInfo() { + public Map credentialInfo() { return ImmutableMap.of( "writeLocation", writeLocations.toString(), "readLocation", readLocations.toString()); } From b96315873c18290778f9b3186acd8a98fd927ded Mon Sep 17 00:00:00 2001 From: fanng Date: Thu, 10 Oct 2024 18:04:35 +0800 Subject: [PATCH 04/10] use millisecons --- .../apache/gravitino/credential/CredentialConstants.java | 2 +- .../java/org/apache/gravitino/credential/Credential.java | 7 ++++--- .../gravitino/credential/DummyCredentialProvider.java | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/catalogs/catalog-common/src/main/java/org/apache/gravitino/credential/CredentialConstants.java b/catalogs/catalog-common/src/main/java/org/apache/gravitino/credential/CredentialConstants.java index 9ed3c4a2d96..6aa04690bc4 100644 --- a/catalogs/catalog-common/src/main/java/org/apache/gravitino/credential/CredentialConstants.java +++ b/catalogs/catalog-common/src/main/java/org/apache/gravitino/credential/CredentialConstants.java @@ -21,7 +21,7 @@ public class CredentialConstants { public static final String CREDENTIAL_TYPE = "credential-type"; - public static final String EXPIRE_TIME_SECS = "expire-time-secs"; + public static final String EXPIRE_TIME_MS = "expire-time-ms"; private CredentialConstants() {} } diff --git a/common/src/main/java/org/apache/gravitino/credential/Credential.java b/common/src/main/java/org/apache/gravitino/credential/Credential.java index 91fd231cc22..f35a9ee3a44 100644 --- a/common/src/main/java/org/apache/gravitino/credential/Credential.java +++ b/common/src/main/java/org/apache/gravitino/credential/Credential.java @@ -34,11 +34,12 @@ public interface Credential { String credentialType(); /** - * Returns the expiration time of the credential in seconds since the epoch, 0 means not expire. + * Returns the expiration time of the credential in milliseconds since the epoch, 0 means not + * expire. * * @return the expiration time as a long. */ - long expireTimeSecs(); + long expireTimeMs(); /** * Returns credential information. @@ -56,7 +57,7 @@ default Map toProperties() { return new ImmutableMap.Builder() .putAll(credentialInfo()) .put(CredentialConstants.CREDENTIAL_TYPE, credentialType()) - .put(CredentialConstants.EXPIRE_TIME_SECS, String.valueOf(expireTimeSecs())) + .put(CredentialConstants.EXPIRE_TIME_MS, String.valueOf(expireTimeMs())) .build(); } } diff --git a/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java b/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java index 24853a483cb..47d38492898 100644 --- a/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java +++ b/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java @@ -69,7 +69,7 @@ public String credentialType() { } @Override - public long expireTimeSecs() { + public long expireTimeMs() { return 0; } From 0ca5e98594aa87b4d92885b2cc29c2973d595787 Mon Sep 17 00:00:00 2001 From: fanng Date: Mon, 14 Oct 2024 18:20:28 +0800 Subject: [PATCH 05/10] xx --- .../gravitino/credential/Credential.java | 9 +++---- .../credential/CredentialConstants.java | 0 common/build.gradle.kts | 1 - ...ext.java => CatalogCredentialContext.java} | 4 +-- .../{Context.java => CredentialContext.java} | 2 +- .../credential/CredentialProvider.java | 2 +- ...t.java => PathBasedCredentialContext.java} | 25 ++++++++++--------- .../credential/DummyCredentialProvider.java | 17 +++++++------ .../credential/TestCredentialProvider.java | 3 ++- 9 files changed, 32 insertions(+), 31 deletions(-) rename {common => api}/src/main/java/org/apache/gravitino/credential/Credential.java (92%) rename {catalogs/catalog-common => api}/src/main/java/org/apache/gravitino/credential/CredentialConstants.java (100%) rename core/src/main/java/org/apache/gravitino/credential/{CatalogContext.java => CatalogCredentialContext.java} (91%) rename core/src/main/java/org/apache/gravitino/credential/{Context.java => CredentialContext.java} (96%) rename core/src/main/java/org/apache/gravitino/credential/{LocationContext.java => PathBasedCredentialContext.java} (67%) diff --git a/common/src/main/java/org/apache/gravitino/credential/Credential.java b/api/src/main/java/org/apache/gravitino/credential/Credential.java similarity index 92% rename from common/src/main/java/org/apache/gravitino/credential/Credential.java rename to api/src/main/java/org/apache/gravitino/credential/Credential.java index f35a9ee3a44..2cccc65c220 100644 --- a/common/src/main/java/org/apache/gravitino/credential/Credential.java +++ b/api/src/main/java/org/apache/gravitino/credential/Credential.java @@ -24,10 +24,9 @@ /** Interface representing a credential with type, expiration time, and additional information. */ public interface Credential { - /** - * Returns the type of the credential. It should same with the credential type of the credential - * provider. + * Returns the type of the credential. It should be the same with the credential type of the + * credential provider. * * @return the credential type as a String. */ @@ -39,7 +38,7 @@ public interface Credential { * * @return the expiration time as a long. */ - long expireTimeMs(); + long expireTimeInMs(); /** * Returns credential information. @@ -57,7 +56,7 @@ default Map toProperties() { return new ImmutableMap.Builder() .putAll(credentialInfo()) .put(CredentialConstants.CREDENTIAL_TYPE, credentialType()) - .put(CredentialConstants.EXPIRE_TIME_MS, String.valueOf(expireTimeMs())) + .put(CredentialConstants.EXPIRE_TIME_MS, String.valueOf(expireTimeInMs())) .build(); } } diff --git a/catalogs/catalog-common/src/main/java/org/apache/gravitino/credential/CredentialConstants.java b/api/src/main/java/org/apache/gravitino/credential/CredentialConstants.java similarity index 100% rename from catalogs/catalog-common/src/main/java/org/apache/gravitino/credential/CredentialConstants.java rename to api/src/main/java/org/apache/gravitino/credential/CredentialConstants.java diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 4e830b6a735..91e2d137f25 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -28,7 +28,6 @@ plugins { dependencies { implementation(project(":api")) - implementation(project(":catalogs:catalog-common")) implementation(libs.commons.collections4) implementation(libs.commons.lang3) diff --git a/core/src/main/java/org/apache/gravitino/credential/CatalogContext.java b/core/src/main/java/org/apache/gravitino/credential/CatalogCredentialContext.java similarity index 91% rename from core/src/main/java/org/apache/gravitino/credential/CatalogContext.java rename to core/src/main/java/org/apache/gravitino/credential/CatalogCredentialContext.java index 563464b0e6f..784bef92b6f 100644 --- a/core/src/main/java/org/apache/gravitino/credential/CatalogContext.java +++ b/core/src/main/java/org/apache/gravitino/credential/CatalogCredentialContext.java @@ -23,10 +23,10 @@ import javax.validation.constraints.NotNull; /** CatalogContext is generated when user requesting catalog credentials. */ -public class CatalogContext implements Context { +public class CatalogCredentialContext implements CredentialContext { @NotNull private final String userName; - public CatalogContext(String userName) { + public CatalogCredentialContext(String userName) { Preconditions.checkNotNull(userName, "User name should not be null"); this.userName = userName; } diff --git a/core/src/main/java/org/apache/gravitino/credential/Context.java b/core/src/main/java/org/apache/gravitino/credential/CredentialContext.java similarity index 96% rename from core/src/main/java/org/apache/gravitino/credential/Context.java rename to core/src/main/java/org/apache/gravitino/credential/CredentialContext.java index 0a796baf39b..1fe3eb63c54 100644 --- a/core/src/main/java/org/apache/gravitino/credential/Context.java +++ b/core/src/main/java/org/apache/gravitino/credential/CredentialContext.java @@ -20,7 +20,7 @@ package org.apache.gravitino.credential; /** Contains context information to get credential from credential provider. */ -public interface Context { +public interface CredentialContext { /** * Providing the username. * diff --git a/core/src/main/java/org/apache/gravitino/credential/CredentialProvider.java b/core/src/main/java/org/apache/gravitino/credential/CredentialProvider.java index fa3dce7df20..4056cd00b1b 100644 --- a/core/src/main/java/org/apache/gravitino/credential/CredentialProvider.java +++ b/core/src/main/java/org/apache/gravitino/credential/CredentialProvider.java @@ -52,5 +52,5 @@ public interface CredentialProvider extends Closeable { * or resource. Null will be returned if no credential is available. */ @Nullable - Credential getCredential(Context context); + Credential getCredential(CredentialContext context); } diff --git a/core/src/main/java/org/apache/gravitino/credential/LocationContext.java b/core/src/main/java/org/apache/gravitino/credential/PathBasedCredentialContext.java similarity index 67% rename from core/src/main/java/org/apache/gravitino/credential/LocationContext.java rename to core/src/main/java/org/apache/gravitino/credential/PathBasedCredentialContext.java index 07d192d6dbd..03e7bbe0e31 100644 --- a/core/src/main/java/org/apache/gravitino/credential/LocationContext.java +++ b/core/src/main/java/org/apache/gravitino/credential/PathBasedCredentialContext.java @@ -27,19 +27,20 @@ * LocationContext is generated when user requesting resources associated with storage location like * table, fileset, etc. */ -public class LocationContext implements Context { +public class PathBasedCredentialContext implements CredentialContext { - @NotNull private final Set writeLocations; - @NotNull private final Set readLocations; + @NotNull private final Set writePaths; + @NotNull private final Set readPaths; @NotNull private final String userName; - public LocationContext(String userName, Set writeLocations, Set readLocations) { + public PathBasedCredentialContext( + String userName, Set writePaths, Set readPaths) { Preconditions.checkNotNull(userName, "User name should not be null"); - Preconditions.checkNotNull(writeLocations, "Write locations should not be null"); - Preconditions.checkNotNull(readLocations, "Read locations should not be null"); + Preconditions.checkNotNull(writePaths, "Write paths should not be null"); + Preconditions.checkNotNull(readPaths, "Read paths should not be null"); this.userName = userName; - this.writeLocations = writeLocations; - this.readLocations = readLocations; + this.writePaths = writePaths; + this.readPaths = readPaths; } @Override @@ -47,11 +48,11 @@ public String getUserName() { return userName; } - public Set getWriteLocations() { - return writeLocations; + public Set getWritePaths() { + return writePaths; } - public Set getReadLocations() { - return readLocations; + public Set getReadPaths() { + return readPaths; } } diff --git a/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java b/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java index 47d38492898..864635e96c9 100644 --- a/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java +++ b/core/src/test/java/org/apache/gravitino/credential/DummyCredentialProvider.java @@ -43,12 +43,13 @@ public String credentialType() { } @Override - public Credential getCredential(Context context) { + public Credential getCredential(CredentialContext context) { Preconditions.checkArgument( - context instanceof LocationContext || context instanceof CatalogContext, + context instanceof PathBasedCredentialContext + || context instanceof CatalogCredentialContext, "Doesn't support context: " + context.getClass().getSimpleName()); - if (context instanceof LocationContext) { - return new DummyCredential((LocationContext) context); + if (context instanceof PathBasedCredentialContext) { + return new DummyCredential((PathBasedCredentialContext) context); } return null; } @@ -58,9 +59,9 @@ public static class DummyCredential implements Credential { @Getter private Set writeLocations; @Getter private Set readLocations; - public DummyCredential(LocationContext locationContext) { - this.writeLocations = locationContext.getWriteLocations(); - this.readLocations = locationContext.getReadLocations(); + public DummyCredential(PathBasedCredentialContext locationContext) { + this.writeLocations = locationContext.getWritePaths(); + this.readLocations = locationContext.getReadPaths(); } @Override @@ -69,7 +70,7 @@ public String credentialType() { } @Override - public long expireTimeMs() { + public long expireTimeInMs() { return 0; } diff --git a/core/src/test/java/org/apache/gravitino/credential/TestCredentialProvider.java b/core/src/test/java/org/apache/gravitino/credential/TestCredentialProvider.java index 81019c86102..b419375b136 100644 --- a/core/src/test/java/org/apache/gravitino/credential/TestCredentialProvider.java +++ b/core/src/test/java/org/apache/gravitino/credential/TestCredentialProvider.java @@ -42,7 +42,8 @@ void testCredentialProvider() { ImmutableSet writeLocations = ImmutableSet.of("location1"); ImmutableSet readLocations = ImmutableSet.of("location2"); - LocationContext locationContext = new LocationContext("user", writeLocations, readLocations); + PathBasedCredentialContext locationContext = + new PathBasedCredentialContext("user", writeLocations, readLocations); Credential credential = dummyCredentialProvider.getCredential(locationContext); Assertions.assertTrue(credential instanceof DummyCredential); DummyCredential dummyCredential = (DummyCredential) credential; From f66d1897710fe352346c0ba17c8f62cf1124bc95 Mon Sep 17 00:00:00 2001 From: fanng Date: Mon, 14 Oct 2024 18:24:47 +0800 Subject: [PATCH 06/10] xx --- .../apache/gravitino/credential/CatalogCredentialContext.java | 2 +- .../java/org/apache/gravitino/credential/CredentialContext.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/apache/gravitino/credential/CatalogCredentialContext.java b/core/src/main/java/org/apache/gravitino/credential/CatalogCredentialContext.java index 784bef92b6f..a39dbba01bd 100644 --- a/core/src/main/java/org/apache/gravitino/credential/CatalogCredentialContext.java +++ b/core/src/main/java/org/apache/gravitino/credential/CatalogCredentialContext.java @@ -22,7 +22,7 @@ import com.google.common.base.Preconditions; import javax.validation.constraints.NotNull; -/** CatalogContext is generated when user requesting catalog credentials. */ +/** CatalogCredentialContext is generated when user requesting catalog credentials. */ public class CatalogCredentialContext implements CredentialContext { @NotNull private final String userName; diff --git a/core/src/main/java/org/apache/gravitino/credential/CredentialContext.java b/core/src/main/java/org/apache/gravitino/credential/CredentialContext.java index 1fe3eb63c54..6e82efea0f1 100644 --- a/core/src/main/java/org/apache/gravitino/credential/CredentialContext.java +++ b/core/src/main/java/org/apache/gravitino/credential/CredentialContext.java @@ -19,7 +19,7 @@ package org.apache.gravitino.credential; -/** Contains context information to get credential from credential provider. */ +/** Contains credential context information to get credential from a credential provider. */ public interface CredentialContext { /** * Providing the username. From 0666c871cdffc35154092e52bd0ee2ff578f7216 Mon Sep 17 00:00:00 2001 From: fanng Date: Mon, 14 Oct 2024 19:57:53 +0800 Subject: [PATCH 07/10] xx --- .../main/java/org/apache/gravitino/credential/Credential.java | 2 +- .../org/apache/gravitino/credential/CredentialConstants.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/org/apache/gravitino/credential/Credential.java b/api/src/main/java/org/apache/gravitino/credential/Credential.java index 2cccc65c220..f24d1d2c182 100644 --- a/api/src/main/java/org/apache/gravitino/credential/Credential.java +++ b/api/src/main/java/org/apache/gravitino/credential/Credential.java @@ -56,7 +56,7 @@ default Map toProperties() { return new ImmutableMap.Builder() .putAll(credentialInfo()) .put(CredentialConstants.CREDENTIAL_TYPE, credentialType()) - .put(CredentialConstants.EXPIRE_TIME_MS, String.valueOf(expireTimeInMs())) + .put(CredentialConstants.EXPIRE_TIME_AT_MS, String.valueOf(expireTimeInMs())) .build(); } } diff --git a/api/src/main/java/org/apache/gravitino/credential/CredentialConstants.java b/api/src/main/java/org/apache/gravitino/credential/CredentialConstants.java index 6aa04690bc4..2a9b22087e5 100644 --- a/api/src/main/java/org/apache/gravitino/credential/CredentialConstants.java +++ b/api/src/main/java/org/apache/gravitino/credential/CredentialConstants.java @@ -20,8 +20,10 @@ package org.apache.gravitino.credential; public class CredentialConstants { + // Credential type in the credential public static final String CREDENTIAL_TYPE = "credential-type"; - public static final String EXPIRE_TIME_MS = "expire-time-ms"; + // Credential expire time at ms since the epoch. + public static final String EXPIRE_TIME_AT_MS = "expire-time-at-ms"; private CredentialConstants() {} } From a2f76d33374d0440a293181b6a68cded28a19a09 Mon Sep 17 00:00:00 2001 From: fanng Date: Mon, 14 Oct 2024 21:08:02 +0800 Subject: [PATCH 08/10] fix api doc --- .../org/apache/gravitino/credential/CredentialConstants.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/org/apache/gravitino/credential/CredentialConstants.java b/api/src/main/java/org/apache/gravitino/credential/CredentialConstants.java index 2a9b22087e5..fce578bca01 100644 --- a/api/src/main/java/org/apache/gravitino/credential/CredentialConstants.java +++ b/api/src/main/java/org/apache/gravitino/credential/CredentialConstants.java @@ -19,10 +19,11 @@ package org.apache.gravitino.credential; +/** Contains credential constants used by Gravitino server and client. */ public class CredentialConstants { - // Credential type in the credential + /** Credential type in the credential. */ public static final String CREDENTIAL_TYPE = "credential-type"; - // Credential expire time at ms since the epoch. + /** Credential expire time at ms since the epoch. */ public static final String EXPIRE_TIME_AT_MS = "expire-time-at-ms"; private CredentialConstants() {} From 7a7a62935deee439911770aa023c8373dc7025e8 Mon Sep 17 00:00:00 2001 From: fanng Date: Tue, 15 Oct 2024 09:32:43 +0800 Subject: [PATCH 09/10] fix comment --- .../gravitino/credential/Credential.java | 9 ++++-- .../credential/CredentialConstants.java | 30 ------------------- 2 files changed, 7 insertions(+), 32 deletions(-) delete mode 100644 api/src/main/java/org/apache/gravitino/credential/CredentialConstants.java diff --git a/api/src/main/java/org/apache/gravitino/credential/Credential.java b/api/src/main/java/org/apache/gravitino/credential/Credential.java index f24d1d2c182..1836998edfb 100644 --- a/api/src/main/java/org/apache/gravitino/credential/Credential.java +++ b/api/src/main/java/org/apache/gravitino/credential/Credential.java @@ -24,6 +24,11 @@ /** Interface representing a credential with type, expiration time, and additional information. */ public interface Credential { + /** Credential type in the credential. */ + String CREDENTIAL_TYPE = "credential-type"; + /** Credential expire time in ms since the epoch. */ + String EXPIRE_TIME_IN_MS = "expire-time-in-ms"; + /** * Returns the type of the credential. It should be the same with the credential type of the * credential provider. @@ -55,8 +60,8 @@ public interface Credential { default Map toProperties() { return new ImmutableMap.Builder() .putAll(credentialInfo()) - .put(CredentialConstants.CREDENTIAL_TYPE, credentialType()) - .put(CredentialConstants.EXPIRE_TIME_AT_MS, String.valueOf(expireTimeInMs())) + .put(CREDENTIAL_TYPE, credentialType()) + .put(EXPIRE_TIME_IN_MS, String.valueOf(expireTimeInMs())) .build(); } } diff --git a/api/src/main/java/org/apache/gravitino/credential/CredentialConstants.java b/api/src/main/java/org/apache/gravitino/credential/CredentialConstants.java deleted file mode 100644 index fce578bca01..00000000000 --- a/api/src/main/java/org/apache/gravitino/credential/CredentialConstants.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.gravitino.credential; - -/** Contains credential constants used by Gravitino server and client. */ -public class CredentialConstants { - /** Credential type in the credential. */ - public static final String CREDENTIAL_TYPE = "credential-type"; - /** Credential expire time at ms since the epoch. */ - public static final String EXPIRE_TIME_AT_MS = "expire-time-at-ms"; - - private CredentialConstants() {} -} From dd0d578023ffe7d869981cf0dabd94b5d33b5eac Mon Sep 17 00:00:00 2001 From: fanng Date: Tue, 15 Oct 2024 09:42:43 +0800 Subject: [PATCH 10/10] fix comment --- .../main/java/org/apache/gravitino/credential/Credential.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/org/apache/gravitino/credential/Credential.java b/api/src/main/java/org/apache/gravitino/credential/Credential.java index 1836998edfb..b2fdb1971e6 100644 --- a/api/src/main/java/org/apache/gravitino/credential/Credential.java +++ b/api/src/main/java/org/apache/gravitino/credential/Credential.java @@ -30,7 +30,7 @@ public interface Credential { String EXPIRE_TIME_IN_MS = "expire-time-in-ms"; /** - * Returns the type of the credential. It should be the same with the credential type of the + * Returns the type of the credential. It should be the same as the credential type of the * credential provider. * * @return the credential type as a String.