forked from apache/gravitino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[apache#5973] feat(hadoop-catalog): Support credential when using fil…
…eset catalog with cloud storage (apache#5974) ### What changes were proposed in this pull request? Support dynamic credential in obtaining cloud storage fileset. ### Why are the changes needed? Static key are not very safe, we need to optimize it. Fix: apache#5973 ### Does this PR introduce _any_ user-facing change? N/A ### How was this patch tested? ITs
- Loading branch information
1 parent
3b24ee2
commit 2f81bdc
Showing
26 changed files
with
1,765 additions
and
136 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
bundles/aliyun/src/main/java/org/apache/gravitino/oss/fs/OSSCredentialsProvider.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,88 @@ | ||
/* | ||
* 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.oss.fs; | ||
|
||
import com.aliyun.oss.common.auth.BasicCredentials; | ||
import com.aliyun.oss.common.auth.Credentials; | ||
import com.aliyun.oss.common.auth.CredentialsProvider; | ||
import com.aliyun.oss.common.auth.DefaultCredentials; | ||
import java.net.URI; | ||
import org.apache.gravitino.catalog.hadoop.fs.FileSystemUtils; | ||
import org.apache.gravitino.catalog.hadoop.fs.GravitinoFileSystemCredentialsProvider; | ||
import org.apache.gravitino.credential.Credential; | ||
import org.apache.gravitino.credential.OSSSecretKeyCredential; | ||
import org.apache.gravitino.credential.OSSTokenCredential; | ||
import org.apache.hadoop.conf.Configuration; | ||
|
||
public class OSSCredentialsProvider implements CredentialsProvider { | ||
|
||
private GravitinoFileSystemCredentialsProvider gravitinoFileSystemCredentialsProvider; | ||
private Credentials basicCredentials; | ||
private long expirationTime = Long.MAX_VALUE; | ||
private static final double EXPIRATION_TIME_FACTOR = 0.5D; | ||
|
||
public OSSCredentialsProvider(URI uri, Configuration conf) { | ||
this.gravitinoFileSystemCredentialsProvider = FileSystemUtils.getGvfsCredentialProvider(conf); | ||
} | ||
|
||
@Override | ||
public void setCredentials(Credentials credentials) {} | ||
|
||
@Override | ||
public Credentials getCredentials() { | ||
if (basicCredentials == null || System.currentTimeMillis() >= expirationTime) { | ||
synchronized (this) { | ||
refresh(); | ||
} | ||
} | ||
|
||
return basicCredentials; | ||
} | ||
|
||
private void refresh() { | ||
Credential[] gravitinoCredentials = gravitinoFileSystemCredentialsProvider.getCredentials(); | ||
Credential credential = OSSUtils.getSuitableCredential(gravitinoCredentials); | ||
if (credential == null) { | ||
throw new RuntimeException("No suitable credential for OSS found..."); | ||
} | ||
|
||
if (credential instanceof OSSSecretKeyCredential) { | ||
OSSSecretKeyCredential ossSecretKeyCredential = (OSSSecretKeyCredential) credential; | ||
basicCredentials = | ||
new DefaultCredentials( | ||
ossSecretKeyCredential.accessKeyId(), ossSecretKeyCredential.secretAccessKey()); | ||
} else if (credential instanceof OSSTokenCredential) { | ||
OSSTokenCredential ossTokenCredential = (OSSTokenCredential) credential; | ||
basicCredentials = | ||
new BasicCredentials( | ||
ossTokenCredential.accessKeyId(), | ||
ossTokenCredential.secretAccessKey(), | ||
ossTokenCredential.securityToken()); | ||
} | ||
|
||
if (credential.expireTimeInMs() > 0) { | ||
expirationTime = | ||
System.currentTimeMillis() | ||
+ (long) | ||
((credential.expireTimeInMs() - System.currentTimeMillis()) | ||
* EXPIRATION_TIME_FACTOR); | ||
} | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
bundles/aliyun/src/main/java/org/apache/gravitino/oss/fs/OSSUtils.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,52 @@ | ||
/* | ||
* 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.oss.fs; | ||
|
||
import org.apache.gravitino.credential.Credential; | ||
import org.apache.gravitino.credential.OSSSecretKeyCredential; | ||
import org.apache.gravitino.credential.OSSTokenCredential; | ||
|
||
public class OSSUtils { | ||
|
||
/** | ||
* Get the credential from the credential array. Using dynamic credential first, if not found, | ||
* uses static credential. | ||
* | ||
* @param credentials The credential array. | ||
* @return A credential. Null if not found. | ||
*/ | ||
static Credential getSuitableCredential(Credential[] credentials) { | ||
// Use dynamic credential if found. | ||
for (Credential credential : credentials) { | ||
if (credential instanceof OSSTokenCredential) { | ||
return credential; | ||
} | ||
} | ||
|
||
// If dynamic credential not found, use the static one | ||
for (Credential credential : credentials) { | ||
if (credential instanceof OSSSecretKeyCredential) { | ||
return credential; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
bundles/aws/src/main/java/org/apache/gravitino/s3/fs/S3CredentialsProvider.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,88 @@ | ||
/* | ||
* 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.s3.fs; | ||
|
||
import com.amazonaws.auth.AWSCredentials; | ||
import com.amazonaws.auth.AWSCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.auth.BasicSessionCredentials; | ||
import java.net.URI; | ||
import org.apache.gravitino.catalog.hadoop.fs.FileSystemUtils; | ||
import org.apache.gravitino.catalog.hadoop.fs.GravitinoFileSystemCredentialsProvider; | ||
import org.apache.gravitino.credential.Credential; | ||
import org.apache.gravitino.credential.S3SecretKeyCredential; | ||
import org.apache.gravitino.credential.S3TokenCredential; | ||
import org.apache.hadoop.conf.Configuration; | ||
|
||
public class S3CredentialsProvider implements AWSCredentialsProvider { | ||
private GravitinoFileSystemCredentialsProvider gravitinoFileSystemCredentialsProvider; | ||
|
||
private AWSCredentials basicSessionCredentials; | ||
private long expirationTime = Long.MAX_VALUE; | ||
private static final double EXPIRATION_TIME_FACTOR = 0.5D; | ||
|
||
public S3CredentialsProvider(final URI uri, final Configuration conf) { | ||
this.gravitinoFileSystemCredentialsProvider = FileSystemUtils.getGvfsCredentialProvider(conf); | ||
} | ||
|
||
@Override | ||
public AWSCredentials getCredentials() { | ||
// Refresh credentials if they are null or about to expire. | ||
if (basicSessionCredentials == null || System.currentTimeMillis() >= expirationTime) { | ||
synchronized (this) { | ||
refresh(); | ||
} | ||
} | ||
|
||
return basicSessionCredentials; | ||
} | ||
|
||
@Override | ||
public void refresh() { | ||
Credential[] gravitinoCredentials = gravitinoFileSystemCredentialsProvider.getCredentials(); | ||
Credential credential = S3Utils.getSuitableCredential(gravitinoCredentials); | ||
|
||
if (credential == null) { | ||
throw new RuntimeException("No suitable credential for S3 found..."); | ||
} | ||
|
||
if (credential instanceof S3SecretKeyCredential) { | ||
S3SecretKeyCredential s3SecretKeyCredential = (S3SecretKeyCredential) credential; | ||
basicSessionCredentials = | ||
new BasicAWSCredentials( | ||
s3SecretKeyCredential.accessKeyId(), s3SecretKeyCredential.secretAccessKey()); | ||
} else if (credential instanceof S3TokenCredential) { | ||
S3TokenCredential s3TokenCredential = (S3TokenCredential) credential; | ||
basicSessionCredentials = | ||
new BasicSessionCredentials( | ||
s3TokenCredential.accessKeyId(), | ||
s3TokenCredential.secretAccessKey(), | ||
s3TokenCredential.sessionToken()); | ||
} | ||
|
||
if (credential.expireTimeInMs() > 0) { | ||
expirationTime = | ||
System.currentTimeMillis() | ||
+ (long) | ||
((credential.expireTimeInMs() - System.currentTimeMillis()) | ||
* EXPIRATION_TIME_FACTOR); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.