Skip to content

Commit

Permalink
create credentials provider by conf about auth
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-024 committed Dec 9, 2024
1 parent 858f13b commit 162af5e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public class RESTCatalogInternalOptions {
.stringType()
.noDefaultValue()
.withDescription("REST Catalog uri's prefix.");
public static final ConfigOption<String> CREDENTIALS_PROVIDER =
ConfigOptions.key("credentials-provider")
.stringType()
.noDefaultValue()
.withDescription("REST Catalog auth credentials provider.");
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,4 @@ public class RESTCatalogOptions {
.stringType()
.noDefaultValue()
.withDescription("REST Catalog auth token file path.");
public static final ConfigOption<String> CREDENTIALS_PROVIDER =
ConfigOptions.key("credentials-provider")
.stringType()
.noDefaultValue()
.withDescription("REST Catalog auth credentials provider.");
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.paimon.options.Options;
import org.apache.paimon.rest.RESTCatalogOptions;

import static org.apache.paimon.rest.RESTCatalogOptions.CREDENTIALS_PROVIDER;
import static org.apache.paimon.rest.RESTCatalogInternalOptions.CREDENTIALS_PROVIDER;

/** Factory for creating {@link CredentialsProvider}. */
public interface CredentialsProviderFactory extends Factory {
Expand All @@ -34,17 +34,21 @@ default CredentialsProvider create(Options options) {
}

static CredentialsProvider createCredentialsProvider(Options options, ClassLoader classLoader) {
String credentialsProviderIdentifier = options.get(CREDENTIALS_PROVIDER);
String credentialsProviderIdentifier = getCredentialsProviderTypeByConf(options).name();
CredentialsProviderFactory credentialsProviderFactory =
FactoryUtil.discoverFactory(
classLoader,
CredentialsProviderFactory.class,
credentialsProviderIdentifier);
return credentialsProviderFactory.create(options);
}

try {
return credentialsProviderFactory.create(options);
} catch (UnsupportedOperationException ignore) {
static CredentialsProviderType getCredentialsProviderTypeByConf(Options options) {
if (options.getOptional(CREDENTIALS_PROVIDER).isPresent()) {
return CredentialsProviderType.valueOf(options.get(CREDENTIALS_PROVIDER));
} else if (options.getOptional(RESTCatalogOptions.TOKEN_PROVIDER_PATH).isPresent()) {
return CredentialsProviderType.BEAR_TOKEN_FILE;
}
return new BearTokenCredentialsProvider(options.get(RESTCatalogOptions.TOKEN));
return CredentialsProviderType.BEAR_TOKEN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.apache.paimon.options.CatalogOptions;
import org.apache.paimon.options.Options;
import org.apache.paimon.rest.auth.CredentialsProviderType;

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
Expand Down Expand Up @@ -51,8 +50,6 @@ public void setUp() throws IOException {
options.set(RESTCatalogOptions.TOKEN, initToken);
options.set(RESTCatalogOptions.THREAD_POOL_SIZE, 1);
mockOptions(RESTCatalogInternalOptions.PREFIX.key(), "prefix");
options.set(
RESTCatalogOptions.CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN.name());
restCatalog = new RESTCatalog(options);
}

Expand All @@ -65,8 +62,6 @@ public void tearDown() throws IOException {
public void testInitFailWhenDefineWarehouse() {
Options options = new Options();
options.set(CatalogOptions.WAREHOUSE, "/a/b/c");
options.set(
RESTCatalogOptions.CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN.name());
assertThrows(IllegalArgumentException.class, () -> new RESTCatalog(options));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.time.Duration;
import java.util.UUID;

import static org.apache.paimon.rest.RESTCatalogInternalOptions.CREDENTIALS_PROVIDER;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

Expand All @@ -43,8 +44,6 @@ public void testCreateBearTokenCredentialsProviderSuccess() {
Options options = new Options();
String token = UUID.randomUUID().toString();
options.set(RESTCatalogOptions.TOKEN, token);
options.set(
RESTCatalogOptions.CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN.name());
BearTokenCredentialsProvider credentialsProvider =
(BearTokenCredentialsProvider)
CredentialsProviderFactory.createCredentialsProvider(
Expand All @@ -55,8 +54,6 @@ public void testCreateBearTokenCredentialsProviderSuccess() {
@Test
public void testCreateBearTokenCredentialsProviderFail() {
Options options = new Options();
options.set(
RESTCatalogOptions.CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN.name());
assertThrows(
IllegalArgumentException.class,
() ->
Expand All @@ -72,9 +69,7 @@ public void testCreateBearTokenFileCredentialsProviderSuccess() throws Exception
String token = UUID.randomUUID().toString();
FileUtils.writeStringToFile(tokenFile, token);
options.set(RESTCatalogOptions.TOKEN_PROVIDER_PATH, tokenFile.getPath());
options.set(
RESTCatalogOptions.CREDENTIALS_PROVIDER,
CredentialsProviderType.BEAR_TOKEN_FILE.name());
options.set(CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN_FILE.name());
BearTokenFileCredentialsProvider credentialsProvider =
(BearTokenFileCredentialsProvider)
CredentialsProviderFactory.createCredentialsProvider(
Expand All @@ -85,9 +80,7 @@ public void testCreateBearTokenFileCredentialsProviderSuccess() throws Exception
@Test
public void testCreateBearTokenFileCredentialsProviderFail() throws Exception {
Options options = new Options();
options.set(
RESTCatalogOptions.CREDENTIALS_PROVIDER,
CredentialsProviderType.BEAR_TOKEN_FILE.name());
options.set(CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN_FILE.name());
assertThrows(
IllegalArgumentException.class,
() ->
Expand All @@ -102,9 +95,7 @@ public void testCreateRefreshBearTokenFileCredentialsProviderSuccess() throws Ex
File tokenFile = folder.newFile(fileName);
String token = UUID.randomUUID().toString();
FileUtils.writeStringToFile(tokenFile, token);
options.set(
RESTCatalogOptions.CREDENTIALS_PROVIDER,
CredentialsProviderType.BEAR_TOKEN_FILE.name());
options.set(CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN_FILE.name());
options.set(RESTCatalogOptions.TOKEN_PROVIDER_PATH, tokenFile.getPath());
options.set(RESTCatalogOptions.TOKEN_REFRESH_ENABLED, true);
options.set(RESTCatalogOptions.TOKEN_EXPIRATION_TIME, Duration.ofSeconds(10L));
Expand All @@ -122,18 +113,40 @@ public void testCreateRefreshBearTokenFileCredentialsProviderFail() throws Excep
File tokenFile = folder.newFile(fileName);
String token = UUID.randomUUID().toString();
FileUtils.writeStringToFile(tokenFile, token);
options.set(
RESTCatalogOptions.CREDENTIALS_PROVIDER,
CredentialsProviderType.BEAR_TOKEN_FILE.name());
options.set(CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN_FILE.name());
options.set(RESTCatalogOptions.TOKEN_PROVIDER_PATH, tokenFile.getPath());
options.set(RESTCatalogOptions.TOKEN_REFRESH_ENABLED, true);
options.set(
RESTCatalogOptions.CREDENTIALS_PROVIDER,
CredentialsProviderType.BEAR_TOKEN_FILE.name());
options.set(CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN_FILE.name());
assertThrows(
IllegalArgumentException.class,
() ->
CredentialsProviderFactory.createCredentialsProvider(
options, this.getClass().getClassLoader()));
}

@Test
public void getCredentialsProviderTypeByConfWhenDefineTokenPath() {
Options options = new Options();
options.set(RESTCatalogOptions.TOKEN_PROVIDER_PATH, "/a/b/c");
assertEquals(
CredentialsProviderType.BEAR_TOKEN_FILE,
CredentialsProviderFactory.getCredentialsProviderTypeByConf(options));
}

@Test
public void getCredentialsProviderTypeByConfWhenConfNotDefined() {
Options options = new Options();
assertEquals(
CredentialsProviderType.BEAR_TOKEN,
CredentialsProviderFactory.getCredentialsProviderTypeByConf(options));
}

@Test
public void getCredentialsProviderTypeByConfWhenDefineProviderType() {
Options options = new Options();
options.set(CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN_FILE.name());
assertEquals(
CredentialsProviderType.BEAR_TOKEN_FILE,
CredentialsProviderFactory.getCredentialsProviderTypeByConf(options));
}
}

0 comments on commit 162af5e

Please sign in to comment.