|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.fluss.fs.s3; |
| 19 | + |
| 20 | +import org.apache.fluss.fs.s3.token.DynamicTemporaryAWSCredentialsProvider; |
| 21 | +import org.apache.fluss.fs.s3.token.S3DelegationTokenReceiver; |
| 22 | +import org.apache.fluss.fs.token.Credentials; |
| 23 | +import org.apache.fluss.fs.token.CredentialsJsonSerde; |
| 24 | +import org.apache.fluss.fs.token.ObtainedSecurityToken; |
| 25 | + |
| 26 | +import org.apache.hadoop.conf.Configuration; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import java.util.Collections; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +/** Tests for server/client detection in {@link S3FileSystemPlugin}. */ |
| 34 | +class S3FileSystemPluginTest { |
| 35 | + |
| 36 | + private static final String PROVIDER_CONFIG = "fs.s3a.aws.credentials.provider"; |
| 37 | + |
| 38 | + @Test |
| 39 | + void testServerModeWithStaticKeys() { |
| 40 | + Configuration hadoopConfig = new Configuration(); |
| 41 | + hadoopConfig.set("fs.s3a.access.key", "testAccessKey"); |
| 42 | + hadoopConfig.set("fs.s3a.secret.key", "testSecretKey"); |
| 43 | + |
| 44 | + S3FileSystemPlugin plugin = new S3FileSystemPlugin(); |
| 45 | + plugin.setCredentialProvider(hadoopConfig); |
| 46 | + |
| 47 | + String providers = hadoopConfig.get(PROVIDER_CONFIG, ""); |
| 48 | + assertThat(providers).doesNotContain(DynamicTemporaryAWSCredentialsProvider.NAME); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testServerModeWithRoleArnOnly() { |
| 53 | + Configuration hadoopConfig = new Configuration(); |
| 54 | + hadoopConfig.set("fs.s3a.assumed.role.arn", "arn:aws:iam::123456789012:role/test-role"); |
| 55 | + |
| 56 | + S3FileSystemPlugin plugin = new S3FileSystemPlugin(); |
| 57 | + plugin.setCredentialProvider(hadoopConfig); |
| 58 | + |
| 59 | + String providers = hadoopConfig.get(PROVIDER_CONFIG, ""); |
| 60 | + assertThat(providers).doesNotContain(DynamicTemporaryAWSCredentialsProvider.NAME); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testClientModeWithDelegatedCredentials() { |
| 65 | + // Pre-populate receiver so updateHadoopConfig does not throw. |
| 66 | + Credentials creds = new Credentials("testKey", "testSecret", "testToken"); |
| 67 | + ObtainedSecurityToken token = |
| 68 | + new ObtainedSecurityToken( |
| 69 | + "s3", |
| 70 | + CredentialsJsonSerde.toJson(creds), |
| 71 | + System.currentTimeMillis() + 3600000, |
| 72 | + Collections.singletonMap("fs.s3a.region", "us-east-1")); |
| 73 | + S3DelegationTokenReceiver receiver = new S3DelegationTokenReceiver(); |
| 74 | + receiver.onNewTokensObtained(token); |
| 75 | + |
| 76 | + Configuration hadoopConfig = new Configuration(); |
| 77 | + |
| 78 | + S3FileSystemPlugin plugin = new S3FileSystemPlugin(); |
| 79 | + plugin.setCredentialProvider(hadoopConfig); |
| 80 | + |
| 81 | + String providers = hadoopConfig.get(PROVIDER_CONFIG, ""); |
| 82 | + assertThat(providers).contains(DynamicTemporaryAWSCredentialsProvider.NAME); |
| 83 | + } |
| 84 | +} |
0 commit comments