-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[WIP] Proof of concept for a generic password provider #82
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
Open
adenes
wants to merge
2
commits into
apache:trunk
Choose a base branch
from
adenes:password-provider
base: trunk
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
89 changes: 89 additions & 0 deletions
89
flume-ng-core/src/main/java/org/apache/flume/conf/ExternalProcessPasswordProvider.java
This file contains hidden or 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,89 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* http://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 org.apache.flume.conf; | ||
|
||
import com.google.common.base.Optional; | ||
import com.google.common.io.ByteStreams; | ||
import org.apache.flume.Context; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.UnsupportedCharsetException; | ||
|
||
public class ExternalProcessPasswordProvider implements PasswordProvider { | ||
|
||
private static final String COMMAND_KEY = "command"; | ||
private static final String CHARSET_KEY = "charset"; | ||
|
||
private Optional<String> password = Optional.absent(); | ||
|
||
@Override | ||
public String getPassword(Context context, String key) { | ||
if (!password.isPresent()) { | ||
password = Optional.of(loadPassword(context, key)); | ||
} | ||
return password.get(); | ||
} | ||
|
||
private String loadPassword(Context context, String key) { | ||
String charsetKey = key + "." + CHARSET_KEY; | ||
String charsetName = context.getString(charsetKey, "iso-8859-1"); | ||
|
||
Charset charset; | ||
try { | ||
charset = Charset.forName(charsetName); | ||
} catch (UnsupportedCharsetException ex) { | ||
throw new RuntimeException("Unsupported charset: " + charsetName, ex); | ||
} | ||
|
||
String commandKey = key + "." + COMMAND_KEY; | ||
String command = context.getString(commandKey); | ||
if (command == null) { | ||
throw new IllegalArgumentException(commandKey + " must be set for " + | ||
"ExternalProcessPasswordProvider"); | ||
} | ||
|
||
String commandOutput; | ||
try { | ||
commandOutput = execCommand(command, charset); | ||
} catch (InterruptedException | IOException ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
|
||
return commandOutput; | ||
} | ||
|
||
private String execCommand(String command, Charset charset) | ||
throws IOException, InterruptedException { | ||
Process p = Runtime.getRuntime().exec(command); | ||
p.waitFor(); | ||
if (p.exitValue() != 0) { | ||
String stderr; | ||
try { | ||
stderr = new String(ByteStreams.toByteArray(p.getErrorStream()), charset); | ||
} catch (Throwable t) { | ||
stderr = null; | ||
} | ||
throw new RuntimeException( | ||
String.format("Process (%s) exited with non-zero (%s) status code. Sterr: %s", | ||
command, p.exitValue(), stderr)); | ||
} | ||
return new String(ByteStreams.toByteArray(p.getInputStream()), charset); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
flume-ng-core/src/main/java/org/apache/flume/conf/PasswordConfigurator.java
This file contains hidden or 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,54 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* http://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 org.apache.flume.conf; | ||
|
||
import org.apache.flume.Context; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
|
||
public final class PasswordConfigurator { | ||
|
||
private static final String PASSWORD_PROVIDER_CLASS_KEY = "passwordProviderClass"; | ||
|
||
private PasswordConfigurator() { | ||
} | ||
|
||
public static String getPassword(Context context, String passwordKey) { | ||
PasswordProvider passwordProvider; | ||
try { | ||
passwordProvider = getPasswordProvider(context, passwordKey); | ||
} catch (ReflectiveOperationException ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
|
||
return passwordProvider.getPassword(context, passwordKey); | ||
} | ||
|
||
private static PasswordProvider getPasswordProvider(Context context, String passwordKey) throws | ||
ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, | ||
InvocationTargetException { | ||
|
||
String fullKey = passwordKey + "." + PASSWORD_PROVIDER_CLASS_KEY; | ||
String className = context.getString(fullKey, PlainTextPasswordProvider.class.getName()); | ||
|
||
Class<? extends PasswordProvider> passwordProviderClass = | ||
(Class<? extends PasswordProvider>) Class.forName(className); | ||
return passwordProviderClass.newInstance(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
flume-ng-core/src/main/java/org/apache/flume/conf/PasswordProvider.java
This file contains hidden or 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,26 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* http://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 org.apache.flume.conf; | ||
|
||
import org.apache.flume.Context; | ||
|
||
public interface PasswordProvider { | ||
|
||
String getPassword(Context context, String key); | ||
} |
32 changes: 32 additions & 0 deletions
32
flume-ng-core/src/main/java/org/apache/flume/conf/PlainTextPasswordProvider.java
This file contains hidden or 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,32 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* http://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 org.apache.flume.conf; | ||
|
||
import org.apache.flume.Context; | ||
|
||
public class PlainTextPasswordProvider implements PasswordProvider { | ||
|
||
public PlainTextPasswordProvider() { | ||
} | ||
|
||
@Override | ||
public String getPassword(Context context, String key) { | ||
return context.getString(key); | ||
} | ||
} |
This file contains hidden or 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
52 changes: 52 additions & 0 deletions
52
flume-ng-core/src/test/java/org/apache/flume/conf/TestExternalProcessPasswordProvider.java
This file contains hidden or 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,52 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* http://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 org.apache.flume.conf; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.apache.flume.Context; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class TestExternalProcessPasswordProvider { | ||
|
||
@Test | ||
public void testValidCommand() { | ||
Context context = new Context(ImmutableMap.of( | ||
"password.command", "echo -n applepie")); | ||
ExternalProcessPasswordProvider passwordProvider = new ExternalProcessPasswordProvider(); | ||
Assert.assertEquals("applepie", passwordProvider.getPassword(context, "password")); | ||
} | ||
|
||
@Test | ||
public void testUTF8() { | ||
Context context = new Context(ImmutableMap.of( | ||
"password.command", "echo -n applepieéáű", | ||
"password.charset", "utf-8")); | ||
ExternalProcessPasswordProvider passwordProvider = new ExternalProcessPasswordProvider(); | ||
Assert.assertEquals("applepieéáű", passwordProvider.getPassword(context, "password")); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testNoCommand() { | ||
Context context = new Context(); | ||
ExternalProcessPasswordProvider passwordProvider = new ExternalProcessPasswordProvider(); | ||
passwordProvider.getPassword(context, "password"); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
flume-ng-core/src/test/java/org/apache/flume/conf/TestPasswordConfigurator.java
This file contains hidden or 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,53 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* http://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 org.apache.flume.conf; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.apache.flume.Context; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class TestPasswordConfigurator { | ||
|
||
@Test | ||
public void testBackwardCompatibility() { | ||
Context ctx = new Context(ImmutableMap.of("password", "applepie")); | ||
Assert.assertEquals("applepie", PasswordConfigurator.getPassword(ctx, "password")); | ||
} | ||
|
||
@Test | ||
public void testNullPassword() { | ||
Context ctx = new Context(); | ||
Assert.assertNull(PasswordConfigurator.getPassword(ctx, "password")); | ||
} | ||
|
||
@Test | ||
public void testPlainTextPasswordProvider() { | ||
Context ctx = new Context(ImmutableMap.of("password", "applepie", | ||
"password.passwordProviderClass", PlainTextPasswordProvider.class.getName())); | ||
Assert.assertEquals("applepie", PasswordConfigurator.getPassword(ctx, "password")); | ||
} | ||
|
||
@Test | ||
public void testExternalProcessPasswordProvider() { | ||
Context ctx = new Context(ImmutableMap.of("password.command", "echo -n applepie", | ||
"password.passwordProviderClass", ExternalProcessPasswordProvider.class.getName())); | ||
Assert.assertEquals("applepie", PasswordConfigurator.getPassword(ctx, "password")); | ||
} | ||
} |
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.
Why iso-8859-1?
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.
As it's about the output of a shell script I don't expect to have non-ascii characters, that's why I decided to use iso-8859-1 by default. I might be wrong so I'm open to reconsider this.
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.
+1 for Unicode standard and UTF-8 implementation even for defaults.