-
Notifications
You must be signed in to change notification settings - Fork 379
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
Changes from 2 commits
bdf04fa
22a4709
8397303
b963158
0ca5e98
f66d189
0666c87
a2f76d3
7a7a629
dd0d578
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of put it to common package? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It maybe used in the client side. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please give me an example about how to use it in client side? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Take Spark read Iceberg table for example, // Fetch credential from Gravitino
Credential credentail = gravitinoClient.fetchCredential(NameIdentifier identifer, String credentialType)
// Transform credential properties to engine specific properties
Map credentailProperties = CredentialUtils.toIcebergProperties(credential)
// using credential properties to construct FileIO
FileIO file = new FileIO(credentailProperties) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If so, I would suggest you move to API module, not in common module. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, this seems reasonable |
||
|
||
/** | ||
* Returns the type of the credential. It should same with the credential type of the credential | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "It should be the same with..." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "It should be the same as..." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
* provider. | ||
* | ||
* @return the credential type as a String. | ||
*/ | ||
String getCredentialType(); | ||
|
||
/** | ||
* 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 credential information. | ||
* | ||
* @return a map of credential information. | ||
*/ | ||
Map<String, String> getCredentialInfo(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest change the style of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to define an interface for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest that you can refer to Hadoop's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The concrete Credential has the clear definition , take S3TokenCredential for example public class S3TokenCredential implements Credential {
private String accessKeyId;
private String secretAccessKey;
private String sessionToken;
private long expireMs;
public S3TokenCredential(
String accessKeyId, String secretAccessKey, String sessionToken, long expireMs) {
this.accessKeyId = accessKeyId;
this.secretAccessKey = secretAccessKey;
this.sessionToken = sessionToken;
this.expireMs = expireMs;
}
@Override
public String getCredentialType() {
return CredentialConstants.S3_TOKEN_CREDENTIAL_TYPE;
}
@Override
public long getExpireTime() {
return expireMs;
}
@Override
public Map<String, String> getCredentialInfo() {
return (new ImmutableMap.Builder<String, String>())
.put(S3Properties.GRAVITINO_S3_ACCESS_KEY_ID, accessKeyId)
.put(S3Properties.GRAVITINO_S3_SECRET_ACCESS_KEY, secretAccessKey)
.put(S3Properties.GRAVITINO_S3_TOKEN, sessionToken)
.build();
}
} |
||
|
||
/** | ||
* 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(getCredentialInfo()) | ||
.put(CredentialConstants.CREDENTIAL_TYPE, getCredentialType()) | ||
.put(CredentialConstants.EXPIRE_TIME_SECS, String.valueOf(getExpireTimeSecs())) | ||
.build(); | ||
} | ||
} |
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; | ||
|
||
/** 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; | ||
} | ||
} |
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 context information to get credential from credential provider. */ | ||
public interface Context { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
/** | ||
* Providing the username. | ||
* | ||
* @return A string identifying user name. | ||
*/ | ||
String getUserName(); | ||
} |
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 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can change to like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
/** | ||
* 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); | ||
|
||
/** 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); | ||
} |
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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. |
||
.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); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can rename this class name to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
@NotNull private final Set<String> writeLocations; | ||
@NotNull private final Set<String> readLocations; | ||
@NotNull private final String userName; | ||
|
||
public LocationContext(String userName, Set<String> writeLocations, Set<String> 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<String> getWriteLocations() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "writePaths" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
return writeLocations; | ||
} | ||
|
||
public Set<String> getReadLocations() { | ||
return readLocations; | ||
} | ||
} |
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.
The dependency here looks strange if you only want to define two properties in catalog-common, it is easy to introduce the cyclic dependency.
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.
catalog-common is mainly used to define some properties used by catalog, client, connectors, I think it's ok to add the dependence here.
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.
No, it's weird to make common module rely on catalog-common module, which have different purposes and easy to introduce cyclic dependency, please change it.
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.
ok