Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
Signed-off-by: Smith Cruise <chendingchao1@126.com>
  • Loading branch information
Smith-Cruise committed Dec 11, 2024
1 parent 312f8a0 commit 54f97c6
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class IcebergRESTCatalog implements IcebergCatalog {
private static final Logger LOG = LogManager.getLogger(IcebergRESTCatalog.class);

public static final String KEY_CREDENTIAL_WITH_PREFIX = ICEBERG_CUSTOM_PROPERTIES_PREFIX + "credential";
public static final String KEY_DISABLE_VENDED_CREDENTIAL = "disable_vended_credential";
public static final String KEY_VENDED_CREDENTIALS_ENABLED = "vended-credentials-enabled";

private final Configuration conf;
private final RESTCatalog delegate;
Expand All @@ -86,12 +86,12 @@ public IcebergRESTCatalog(String name, Configuration conf, Map<String, String> p
copiedProperties.put(CatalogProperties.FILE_IO_IMPL, IcebergCachingFileIO.class.getName());
copiedProperties.put(CatalogProperties.METRICS_REPORTER_IMPL, IcebergMetricsReporter.class.getName());

boolean disableVendedCredential =
Boolean.parseBoolean(copiedProperties.getOrDefault(KEY_DISABLE_VENDED_CREDENTIAL, "false"));
if (disableVendedCredential) {
copiedProperties.put(AwsProperties.CLIENT_FACTORY, IcebergAwsClientFactory.class.getName());
} else {
boolean enableVendedCredentials =
Boolean.parseBoolean(copiedProperties.getOrDefault(KEY_VENDED_CREDENTIALS_ENABLED, "true"));
if (enableVendedCredentials) {
copiedProperties.put("header.X-Iceberg-Access-Delegation", "vended-credentials");
} else {
copiedProperties.put(AwsProperties.CLIENT_FACTORY, IcebergAwsClientFactory.class.getName());
}

// setup oauth2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class OAuth2SecurityConfig {
// The Bearer token which will be used for interactions with the server
public static String OAUTH2_TOKEN = "oauth2.token";
// The endpoint to retrieve access token from OAuth2 Server
public static String SERVER_URI = "server_uri";
public static String SERVER_URI = "oauth2.server-uri";

public SecurityEnum getSecurity() {
return security;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.iceberg.FileFormat;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.Schema;
import org.apache.iceberg.catalog.Namespace;
import org.apache.iceberg.types.Types;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -241,4 +242,11 @@ public void testTime() {
Type resType = fromIcebergType(icebergType);
Assert.assertEquals(resType, timeType);
}

@Test
public void testConvertDbNameToNamespace() {
Assert.assertEquals(Namespace.of(""), IcebergApiConverter.convertDbNameToNamespace(""));
Assert.assertEquals(Namespace.of("a"), IcebergApiConverter.convertDbNameToNamespace("a"));
Assert.assertEquals(Namespace.of("a", "b", "c"), IcebergApiConverter.convertDbNameToNamespace("a.b.c"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed 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
//
// https://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 com.starrocks.connector.iceberg.rest;

import org.apache.iceberg.rest.auth.OAuth2Properties;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class OAuth2SecurityConfigTest {

@Test
public void testBuild() {
Map<String, String> properties = new HashMap<>();
properties.put("security", "none");
OAuth2SecurityConfig config = OAuth2SecurityConfigBuilder.build(properties);
Assert.assertEquals(SecurityEnum.NONE, config.getSecurity());

properties = new HashMap<>();
properties.put("security", "oaUth2");
properties.put("oauth2.credential", "smith:cruise");
properties.put("oauth2.scope", "PRINCIPAL");
config = OAuth2SecurityConfigBuilder.build(properties);
Assert.assertEquals(SecurityEnum.OAUTH2, config.getSecurity());
Assert.assertEquals("smith:cruise", config.getCredential().get());
Assert.assertEquals("PRINCIPAL", config.getScope().get());

properties = new HashMap<>();
properties.put("security", "oaUth2");
properties.put("oauth2.credential", "smith:cruise");
properties.put("oauth2.token", "123456");
properties.put("oauth2.scope", "PRINCIPAL");
config = OAuth2SecurityConfigBuilder.build(properties);
Assert.assertEquals(SecurityEnum.NONE, config.getSecurity());
}

@Test
public void testProperties() {
Map<String, String> properties = new HashMap<>();
properties.put("security", "oaUth2");
properties.put("oauth2.credential", "smith:cruise");
properties.put("oauth2.scope", "PRINCIPAL");
properties.put("oauth2.server-uri", "http://localhost:8080");
OAuth2SecurityConfig config = OAuth2SecurityConfigBuilder.build(properties);
OAuth2SecurityProperties oauth2Properties = new OAuth2SecurityProperties(config);
Assert.assertEquals("smith:cruise", oauth2Properties.get().get(OAuth2Properties.CREDENTIAL));
Assert.assertEquals("PRINCIPAL", oauth2Properties.get().get(OAuth2Properties.SCOPE));
Assert.assertEquals("http://localhost:8080", oauth2Properties.get().get(OAuth2Properties.OAUTH2_SERVER_URI));

properties = new HashMap<>();
config = OAuth2SecurityConfigBuilder.build(properties);
oauth2Properties = new OAuth2SecurityProperties(config);
Assert.assertEquals(0, oauth2Properties.get().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void testBuildCloudConfigurationForVendedCredentials() {
map.put(S3FileIOProperties.ACCESS_KEY_ID, "ak");
map.put(S3FileIOProperties.SECRET_ACCESS_KEY, "sk");
map.put(S3FileIOProperties.SESSION_TOKEN, "token");
map.put(S3FileIOProperties.PATH_STYLE_ACCESS, "true");
map.put(AwsClientProperties.CLIENT_REGION, "region");
CloudConfiguration cloudConfiguration = CloudConfigurationFactory.buildCloudConfigurationForVendedCredentials(map);
Assert.assertNotNull(cloudConfiguration);
Expand All @@ -44,7 +45,7 @@ public void testBuildCloudConfigurationForVendedCredentials() {
"cred=AWSCloudCredential{useAWSSDKDefaultBehavior=false, " +
"useInstanceProfile=false, accessKey='ak', secretKey='sk', " +
"sessionToken='token', iamRoleArn='', stsRegion='', stsEndpoint='', externalId='', " +
"region='region', endpoint=''}, enablePathStyleAccess=false, enableSSL=true}",
"region='region', endpoint=''}, enablePathStyleAccess=true, enableSSL=true}",
cloudConfiguration.toConfString());
}

Expand Down

0 comments on commit 54f97c6

Please sign in to comment.