Skip to content

Commit 1fc4ec0

Browse files
authored
[core] RESTCatalog: update parse region from dlf uri (#5182)
1 parent ce0f6bf commit 1fc4ec0

File tree

4 files changed

+81
-8
lines changed

4 files changed

+81
-8
lines changed

paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalogOptions.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public class RESTCatalogOptions {
5050
.noDefaultValue()
5151
.withDescription("REST Catalog auth token provider.");
5252

53+
public static final ConfigOption<String> DLF_REGION =
54+
ConfigOptions.key("dlf.region")
55+
.stringType()
56+
.noDefaultValue()
57+
.withDescription("REST Catalog auth DLF region.");
58+
5359
public static final ConfigOption<String> DLF_TOKEN_PATH =
5460
ConfigOptions.key("dlf.token-path")
5561
.stringType()

paimon-core/src/main/java/org/apache/paimon/rest/auth/DLFAuthProviderFactory.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
import org.apache.paimon.options.Options;
2222
import org.apache.paimon.rest.RESTCatalogOptions;
2323

24+
import java.util.Optional;
25+
import java.util.regex.Matcher;
26+
import java.util.regex.Pattern;
27+
2428
import static org.apache.paimon.rest.RESTCatalogOptions.DLF_TOKEN_PATH;
2529
import static org.apache.paimon.rest.RESTCatalogOptions.TOKEN_REFRESH_TIME;
2630
import static org.apache.paimon.rest.RESTCatalogOptions.URI;
@@ -35,7 +39,8 @@ public String identifier() {
3539

3640
@Override
3741
public AuthProvider create(Options options) {
38-
String region = getRegion(options);
42+
String region =
43+
getRegion(options.getOptional(RESTCatalogOptions.DLF_REGION), options.get(URI));
3944
if (options.getOptional(RESTCatalogOptions.DLF_TOKEN_PATH).isPresent()) {
4045
String tokenFilePath = options.get(DLF_TOKEN_PATH);
4146
long tokenRefreshInMills = options.get(TOKEN_REFRESH_TIME).toMillis();
@@ -51,15 +56,22 @@ public AuthProvider create(Options options) {
5156
throw new IllegalArgumentException("DLF token path or AK must be set for DLF Auth.");
5257
}
5358

54-
private static String getRegion(Options options) {
55-
String region = "undefined";
59+
protected static String getRegion(Optional<String> region, String uri) {
60+
if (region.isPresent()) {
61+
return region.get();
62+
}
5663
try {
57-
String[] paths = options.get(URI).split("\\.");
58-
if (paths.length > 1) {
59-
region = paths[1];
64+
String regex = "dlf-(?:pre-)?([a-z]+-[a-z]+(?:-\\d+)?)(?:-internal)?";
65+
66+
Pattern pattern = Pattern.compile(regex);
67+
Matcher matcher = pattern.matcher(uri);
68+
69+
if (matcher.find()) {
70+
return matcher.group(1);
6071
}
6172
} catch (Exception ignore) {
6273
}
63-
return region;
74+
throw new IllegalArgumentException(
75+
"Could not get region from conf or uri, please check your config.");
6476
}
6577
}

paimon-core/src/test/java/org/apache/paimon/rest/auth/AuthSessionTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
import static org.apache.paimon.rest.RESTCatalogOptions.DLF_ACCESS_KEY_ID;
4545
import static org.apache.paimon.rest.RESTCatalogOptions.DLF_ACCESS_KEY_SECRET;
46+
import static org.apache.paimon.rest.RESTCatalogOptions.DLF_REGION;
4647
import static org.apache.paimon.rest.RESTCatalogOptions.DLF_SECURITY_TOKEN;
4748
import static org.apache.paimon.rest.RESTCatalogOptions.DLF_TOKEN_PATH;
4849
import static org.apache.paimon.rest.RESTCatalogOptions.TOKEN;
@@ -171,6 +172,7 @@ public void testCreateDLFAuthProviderByStsToken() throws IOException {
171172
options.set(DLF_ACCESS_KEY_ID.key(), token.getAccessKeyId());
172173
options.set(DLF_ACCESS_KEY_SECRET.key(), token.getAccessKeySecret());
173174
options.set(DLF_SECURITY_TOKEN.key(), token.getSecurityToken());
175+
options.set(DLF_REGION.key(), "cn-hangzhou");
174176
AuthProvider authProvider =
175177
AuthProviderFactory.createAuthProvider(AuthProviderEnum.DLF.identifier(), options);
176178
AuthSession session = AuthSession.fromRefreshAuthProvider(null, authProvider);
@@ -187,6 +189,7 @@ public void testCreateDLFAuthProviderByAk() throws IOException {
187189
DLFToken token = new DLFToken(akId, akSecret, null, null);
188190
options.set(DLF_ACCESS_KEY_ID.key(), token.getAccessKeyId());
189191
options.set(DLF_ACCESS_KEY_SECRET.key(), token.getAccessKeySecret());
192+
options.set(DLF_REGION.key(), "cn-hangzhou");
190193
AuthProvider authProvider =
191194
AuthProviderFactory.createAuthProvider(AuthProviderEnum.DLF.identifier(), options);
192195
AuthSession session = AuthSession.fromRefreshAuthProvider(null, authProvider);
@@ -224,7 +227,7 @@ public void testDLFAuthProviderAuthHeaderWhenDataIsNotEmpty() throws Exception {
224227
String fileName = UUID.randomUUID().toString();
225228
Pair<File, String> tokenFile2Token = generateTokenAndWriteToFile(fileName);
226229
String tokenStr = tokenFile2Token.getRight();
227-
String serverUrl = "https://dlf.cn-hangzhou.aliyuncs.com";
230+
String serverUrl = "https://dlf-cn-hangzhou.aliyuncs.com";
228231
AuthProvider authProvider = generateDLFAuthProvider(Optional.empty(), fileName, serverUrl);
229232
DLFToken token = OBJECT_MAPPER_INSTANCE.readValue(tokenStr, DLFToken.class);
230233
Map<String, String> parameters = new HashMap<>();
@@ -280,6 +283,7 @@ private AuthProvider generateDLFAuthProvider(
280283
Options options = new Options();
281284
options.set(DLF_TOKEN_PATH.key(), folder.getRoot().getPath() + "/" + fileName);
282285
options.set(RESTCatalogOptions.URI.key(), serverUrl);
286+
options.set(DLF_REGION.key(), "cn-hangzhou");
283287
tokenRefreshInMillsOpt.ifPresent(
284288
tokenRefreshInMills ->
285289
options.set(TOKEN_REFRESH_TIME.key(), tokenRefreshInMills + "ms"));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.paimon.rest.auth;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.util.Optional;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertThrows;
27+
28+
/** Test for {@link DLFAuthProviderFactory}. */
29+
class DLFAuthProviderFactoryTest {
30+
31+
@Test
32+
void getRegion() {
33+
String region = "cn-hangzhou";
34+
String ipPortUri = "http://127.0.0.1:8080";
35+
assertEquals(region, DLFAuthProviderFactory.getRegion(Optional.of(region), ipPortUri));
36+
String url = "https://dlf-" + region + "-internal.aliyuncs.com";
37+
assertEquals(region, DLFAuthProviderFactory.getRegion(Optional.empty(), url));
38+
url = "https://dlf-" + region + ".aliyuncs.com";
39+
assertEquals(region, DLFAuthProviderFactory.getRegion(Optional.empty(), url));
40+
url = "https://dlf-pre-" + region + ".aliyuncs.com";
41+
assertEquals(region, DLFAuthProviderFactory.getRegion(Optional.empty(), url));
42+
region = "us-east-1";
43+
url = "https://dlf-" + region + ".aliyuncs.com";
44+
assertEquals(region, DLFAuthProviderFactory.getRegion(Optional.empty(), url));
45+
url = "https://dlf-" + region + "-internal.aliyuncs.com";
46+
assertEquals(region, DLFAuthProviderFactory.getRegion(Optional.empty(), url));
47+
assertThrows(
48+
IllegalArgumentException.class,
49+
() -> DLFAuthProviderFactory.getRegion(Optional.empty(), ipPortUri));
50+
}
51+
}

0 commit comments

Comments
 (0)