-
Notifications
You must be signed in to change notification settings - Fork 319
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
[#4992] support credential vending framework #4995
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bdf04fa
credential framework
FANNG1 22a4709
polish
FANNG1 8397303
polish
FANNG1 b963158
use millisecons
FANNG1 0ca5e98
xx
FANNG1 f66d189
xx
FANNG1 0666c87
xx
FANNG1 a2f76d3
fix api doc
FANNG1 7a7a629
fix comment
FANNG1 dd0d578
fix comment
FANNG1 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
67 changes: 67 additions & 0 deletions
67
api/src/main/java/org/apache/gravitino/credential/Credential.java
This file contains 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,67 @@ | ||
/* | ||
* 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 { | ||
/** 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 as the credential type of the | ||
* credential provider. | ||
* | ||
* @return the credential type as a String. | ||
*/ | ||
String credentialType(); | ||
|
||
/** | ||
* Returns the expiration time of the credential in milliseconds since the epoch, 0 means not | ||
* expire. | ||
* | ||
* @return the expiration time as a long. | ||
*/ | ||
long expireTimeInMs(); | ||
|
||
/** | ||
* Returns credential information. | ||
* | ||
* @return a map of credential information. | ||
*/ | ||
Map<String, String> credentialInfo(); | ||
|
||
/** | ||
* Converts the credential to properties to transfer the credential though API. | ||
* | ||
* @return a map containing credential properties. | ||
*/ | ||
default Map<String, String> toProperties() { | ||
return new ImmutableMap.Builder<String, String>() | ||
.putAll(credentialInfo()) | ||
.put(CREDENTIAL_TYPE, credentialType()) | ||
.put(EXPIRE_TIME_IN_MS, String.valueOf(expireTimeInMs())) | ||
.build(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
core/src/main/java/org/apache/gravitino/credential/CatalogCredentialContext.java
This file contains 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,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; | ||
|
||
/** CatalogCredentialContext is generated when user requesting catalog credentials. */ | ||
public class CatalogCredentialContext implements CredentialContext { | ||
@NotNull private final String userName; | ||
|
||
public CatalogCredentialContext(String userName) { | ||
Preconditions.checkNotNull(userName, "User name should not be null"); | ||
this.userName = userName; | ||
} | ||
|
||
@Override | ||
public String getUserName() { | ||
return userName; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
core/src/main/java/org/apache/gravitino/credential/CredentialContext.java
This file contains 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,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 credential context information to get credential from a credential provider. */ | ||
public interface CredentialContext { | ||
/** | ||
* Providing the username. | ||
* | ||
* @return A string identifying user name. | ||
*/ | ||
String getUserName(); | ||
} |
56 changes: 56 additions & 0 deletions
56
core/src/main/java/org/apache/gravitino/credential/CredentialProvider.java
This file contains 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,56 @@ | ||
/* | ||
* 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.io.Closeable; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Interface for credential providers. | ||
* | ||
* <p>A credential provider is responsible for managing and retrieving credentials. | ||
*/ | ||
public interface CredentialProvider extends Closeable { | ||
/** | ||
* 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<String, String> properties); | ||
|
||
/** | ||
* 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(CredentialContext context); | ||
} |
69 changes: 69 additions & 0 deletions
69
core/src/main/java/org/apache/gravitino/credential/CredentialProviderFactory.java
This file contains 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,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<String, String> catalogProperties) { | ||
Class<? extends CredentialProvider> 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<? extends CredentialProvider> lookupCredentialProvider( | ||
String credentialType) { | ||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | ||
ServiceLoader<CredentialProvider> serviceLoader = | ||
ServiceLoader.load(CredentialProvider.class, classLoader); | ||
List<Class<? extends CredentialProvider>> 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); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
core/src/main/java/org/apache/gravitino/credential/PathBasedCredentialContext.java
This file contains 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,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 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 PathBasedCredentialContext implements CredentialContext { | ||
|
||
@NotNull private final Set<String> writePaths; | ||
@NotNull private final Set<String> readPaths; | ||
@NotNull private final String userName; | ||
|
||
public PathBasedCredentialContext( | ||
String userName, Set<String> writePaths, Set<String> readPaths) { | ||
Preconditions.checkNotNull(userName, "User name 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.writePaths = writePaths; | ||
this.readPaths = readPaths; | ||
} | ||
|
||
@Override | ||
public String getUserName() { | ||
return userName; | ||
} | ||
|
||
public Set<String> getWritePaths() { | ||
return writePaths; | ||
} | ||
|
||
public Set<String> getReadPaths() { | ||
return readPaths; | ||
} | ||
} |
Oops, something went wrong.
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.
What is the consideration of using service loader?
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.
Different catalogs may use different credential providers so that users could place the s3 provider in catalogA, place gcs provider in catalogB, so we using service loader to load corresponding jar in catalog classpaths.