-
Notifications
You must be signed in to change notification settings - Fork 251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/custom auth provider plugpoint #160
Open
nilaysundarkar
wants to merge
2
commits into
mongodb:master
Choose a base branch
from
Evernorth:feature/custom-auth-provider-plugpoint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
13 changes: 13 additions & 0 deletions
13
...main/java/com/mongodb/kafka/connect/util/custom/credentials/CustomCredentialProvider.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,13 @@ | ||
package com.mongodb.kafka.connect.util.custom.credentials; | ||
|
||
import java.util.Map; | ||
|
||
import com.mongodb.MongoCredential; | ||
|
||
public interface CustomCredentialProvider { | ||
MongoCredential getCustomCredential(Map<?, ?> configs); | ||
|
||
void validate(Map<?, ?> configs); | ||
|
||
void init(Map<?, ?> configs); | ||
} |
10 changes: 10 additions & 0 deletions
10
.../com/mongodb/kafka/connect/util/custom/credentials/CustomCredentialProviderConstants.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,10 @@ | ||
package com.mongodb.kafka.connect.util.custom.credentials; | ||
|
||
public final class CustomCredentialProviderConstants { | ||
private CustomCredentialProviderConstants() {} | ||
|
||
public static final String CUSTOM_AUTH_ENABLE_CONFIG = "mongo.custom.auth.mechanism.enable"; | ||
|
||
public static final String CUSTOM_AUTH_PROVIDER_CLASS = | ||
"mongo.custom.auth.mechanism.providerClass"; | ||
} |
76 changes: 76 additions & 0 deletions
76
...odb/kafka/connect/util/custom/credentials/CustomCredentialProviderGenericInitializer.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,76 @@ | ||
package com.mongodb.kafka.connect.util.custom.credentials; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Map; | ||
|
||
import org.apache.kafka.common.config.ConfigException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class CustomCredentialProviderGenericInitializer { | ||
|
||
static final Logger LOGGER = | ||
LoggerFactory.getLogger(CustomCredentialProviderGenericInitializer.class); | ||
|
||
public static CustomCredentialProvider initializeCustomProvider(Map<?, ?> originals) | ||
throws ConfigException { | ||
// Validate if CUSTOM_AUTH_ENABLE_CONFIG is set to true | ||
String customAuthMechanismEnabled = | ||
String.valueOf(originals.get(CustomCredentialProviderConstants.CUSTOM_AUTH_ENABLE_CONFIG)); | ||
if (customAuthMechanismEnabled == null | ||
|| customAuthMechanismEnabled.equals("null") | ||
|| customAuthMechanismEnabled.isEmpty()) { | ||
throw new ConfigException( | ||
CustomCredentialProviderConstants.CUSTOM_AUTH_ENABLE_CONFIG | ||
+ " is not set to true. " | ||
+ "CustomCredentialProvider should not be used."); | ||
} | ||
// Validate if CUSTOM_AUTH_PROVIDER_CLASS is provided | ||
String qualifiedAuthProviderClassName = | ||
String.valueOf(originals.get(CustomCredentialProviderConstants.CUSTOM_AUTH_PROVIDER_CLASS)); | ||
if (qualifiedAuthProviderClassName == null | ||
|| qualifiedAuthProviderClassName.equals("null") | ||
|| qualifiedAuthProviderClassName.isEmpty()) { | ||
throw new ConfigException( | ||
CustomCredentialProviderConstants.CUSTOM_AUTH_PROVIDER_CLASS | ||
+ " is required when " | ||
+ CustomCredentialProviderConstants.CUSTOM_AUTH_ENABLE_CONFIG | ||
+ " is set to true."); | ||
} | ||
try { | ||
// Validate if qualifiedAuthProviderClassName is on the class path. | ||
Class<?> authProviderClass = | ||
Class.forName( | ||
qualifiedAuthProviderClassName, | ||
false, | ||
CustomCredentialProviderGenericInitializer.class.getClassLoader()); | ||
// Validate if qualifiedAuthProviderClassName implements CustomCredentialProvider interface. | ||
if (!CustomCredentialProvider.class.isAssignableFrom(authProviderClass)) { | ||
throw new ConfigException( | ||
"Provided Class does not implement CustomCredentialProvider interface."); | ||
} | ||
CustomCredentialProvider customCredentialProvider = | ||
initializeCustomProvider(authProviderClass); | ||
// Perform config validations specific to CustomCredentialProvider impl provided | ||
customCredentialProvider.validate(originals); | ||
// Initialize custom variables required by implementation of CustomCredentialProvider | ||
customCredentialProvider.init(originals); | ||
return customCredentialProvider; | ||
} catch (ClassNotFoundException e) { | ||
throw new ConfigException( | ||
"Unable to find " + qualifiedAuthProviderClassName + " on the classpath."); | ||
} | ||
} | ||
|
||
private static CustomCredentialProvider initializeCustomProvider(Class<?> authProviderClass) { | ||
try { | ||
return (CustomCredentialProvider) authProviderClass.getDeclaredConstructor().newInstance(); | ||
} catch (InstantiationException | ||
| IllegalAccessException | ||
| InvocationTargetException | ||
| NoSuchMethodException e) { | ||
LOGGER.error("Error while instantiating " + authProviderClass + " class"); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
...kafka/connect/util/custom/credentials/CustomCredentialProviderGenericInitializerTest.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,113 @@ | ||
package com.mongodb.kafka.connect.util.custom.credentials; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.kafka.common.config.ConfigException; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class CustomCredentialProviderGenericInitializerTest { | ||
|
||
@Test | ||
@DisplayName("Test Exception scenarios") | ||
void testExceptions() { | ||
Map<String, Object> props = new HashMap<>(); | ||
ConfigException configException = | ||
assertThrows( | ||
ConfigException.class, | ||
() -> CustomCredentialProviderGenericInitializer.initializeCustomProvider(props), | ||
"Expected initializeCustomProvider() to throw, but it didn't"); | ||
assertEquals( | ||
CustomCredentialProviderConstants.CUSTOM_AUTH_ENABLE_CONFIG | ||
+ " is not set to true. " | ||
+ "CustomCredentialProvider should not be used.", | ||
configException.getMessage()); | ||
props.put(CustomCredentialProviderConstants.CUSTOM_AUTH_ENABLE_CONFIG, true); | ||
configException = | ||
assertThrows( | ||
ConfigException.class, | ||
() -> CustomCredentialProviderGenericInitializer.initializeCustomProvider(props), | ||
"Expected initializeCustomProvider() to throw, but it didn't"); | ||
assertEquals( | ||
CustomCredentialProviderConstants.CUSTOM_AUTH_PROVIDER_CLASS | ||
+ " is required when " | ||
+ CustomCredentialProviderConstants.CUSTOM_AUTH_ENABLE_CONFIG | ||
+ " is set to true.", | ||
configException.getMessage()); | ||
String qualifiedAuthProviderClassName = "com.nonexistant.package.Test"; | ||
props.put( | ||
CustomCredentialProviderConstants.CUSTOM_AUTH_PROVIDER_CLASS, | ||
qualifiedAuthProviderClassName); | ||
configException = | ||
assertThrows( | ||
ConfigException.class, | ||
() -> CustomCredentialProviderGenericInitializer.initializeCustomProvider(props), | ||
"Expected initializeCustomProvider() to throw, but it didn't"); | ||
assertEquals( | ||
"Unable to find " + qualifiedAuthProviderClassName + " on the classpath.", | ||
configException.getMessage()); | ||
qualifiedAuthProviderClassName = | ||
"com.mongodb.kafka.connect.util.custom.credentials.TestInvalidCustomCredentialProvider"; | ||
props.put( | ||
CustomCredentialProviderConstants.CUSTOM_AUTH_PROVIDER_CLASS, | ||
qualifiedAuthProviderClassName); | ||
configException = | ||
assertThrows( | ||
ConfigException.class, | ||
() -> CustomCredentialProviderGenericInitializer.initializeCustomProvider(props), | ||
"Expected initializeCustomProvider() to throw, but it didn't"); | ||
assertEquals( | ||
"Provided Class does not implement CustomCredentialProvider interface.", | ||
configException.getMessage()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test CustomCredentialProvider initialization") | ||
void testInitializeCustomCredentialProvider() { | ||
Map<String, Object> props = new HashMap<>(); | ||
props.put(CustomCredentialProviderConstants.CUSTOM_AUTH_ENABLE_CONFIG, true); | ||
props.put( | ||
CustomCredentialProviderConstants.CUSTOM_AUTH_PROVIDER_CLASS, | ||
"com.mongodb.kafka.connect.util.custom.credentials.TestCustomCredentialProvider"); | ||
props.put("customProperty", "customValue"); | ||
CustomCredentialProvider customCredentialProvider = | ||
CustomCredentialProviderGenericInitializer.initializeCustomProvider(props); | ||
assertEquals(TestCustomCredentialProvider.class, customCredentialProvider.getClass()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test CustomCredentialProvider custom props initialization") | ||
void testCustomPropsInit() { | ||
Map<String, Object> props = new HashMap<>(); | ||
props.put(CustomCredentialProviderConstants.CUSTOM_AUTH_ENABLE_CONFIG, true); | ||
props.put( | ||
CustomCredentialProviderConstants.CUSTOM_AUTH_PROVIDER_CLASS, | ||
"com.mongodb.kafka.connect.util.custom.credentials.TestCustomCredentialProvider"); | ||
props.put("customProperty", "customValue"); | ||
TestCustomCredentialProvider customCredentialProvider = | ||
(TestCustomCredentialProvider) | ||
CustomCredentialProviderGenericInitializer.initializeCustomProvider(props); | ||
assertEquals("customValue", customCredentialProvider.getCustomProperty()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test CustomCredentialProvider custom props validation") | ||
void testCustomPropsValidate() { | ||
Map<String, Object> props = new HashMap<>(); | ||
props.put(CustomCredentialProviderConstants.CUSTOM_AUTH_ENABLE_CONFIG, true); | ||
props.put( | ||
CustomCredentialProviderConstants.CUSTOM_AUTH_PROVIDER_CLASS, | ||
"com.mongodb.kafka.connect.util.custom.credentials.TestCustomCredentialProvider"); | ||
props.put("customProperty", "invalidValue"); | ||
ConfigException configException = | ||
assertThrows( | ||
ConfigException.class, | ||
() -> CustomCredentialProviderGenericInitializer.initializeCustomProvider(props), | ||
"Expected initializeCustomProvider() to throw, but it didn't"); | ||
assertEquals("Invalid value set for customProperty", configException.getMessage()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any specific reason for this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jagadishmdb ,
I made that change thinking that it is needed for MONGODB-AWS auth mechanism. We have been using 4.9.1 drivers so far.
After checking for compatibility at - https://www.mongodb.com/docs/drivers/java/sync/v4.7/fundamentals/auth/#std-label-mongodb-aws-auth-mechanism, I see that 4.7 supports it as well. I am testing with 4.7 to confirm. I think we can keep 'mongodbDriverVersion' to the same value as before, if that is desirable.
I will let you know how the tests go.
Thanks,
Nilay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests successful with the existing mongo driver versions. Please let me know if I should change the PR for restoring mongodbDriverVersion. I see that it has been already changed at #161.
Thanks,
Nilay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for confirming Nilay! There are some additional tests we run which were getting different error codes than what we were expecting with 4.9.1 driver. I will take care of fixing those later. Thanks for the contribution!