-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TO BE REMOVED WHEN WE RELY ON plugin-tools final
- Loading branch information
Showing
3 changed files
with
249 additions
and
1 deletion.
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
103 changes: 103 additions & 0 deletions
103
plugin/src/main/java/org/wildfly/plugins/bootablejar/maven/goals/ClientCallbackHandler.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,103 @@ | ||
/* | ||
* Copyright The WildFly Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.wildfly.plugins.bootablejar.maven.goals; | ||
|
||
import java.io.Console; | ||
import java.io.IOException; | ||
|
||
import javax.security.auth.callback.Callback; | ||
import javax.security.auth.callback.CallbackHandler; | ||
import javax.security.auth.callback.NameCallback; | ||
import javax.security.auth.callback.PasswordCallback; | ||
import javax.security.auth.callback.UnsupportedCallbackException; | ||
import javax.security.sasl.RealmCallback; | ||
import javax.security.sasl.RealmChoiceCallback; | ||
|
||
/** | ||
* A CallbackHandler implementation to supply the username and password if required when | ||
* connecting to the server - if these are not available the user will be prompted to | ||
* supply them. | ||
* | ||
* @author <a href="mailto:darran.lofthouse@jboss.com">Darran Lofthouse</a> | ||
*/ | ||
public class ClientCallbackHandler implements CallbackHandler { | ||
|
||
private final Console console; | ||
private boolean promptShown = false; | ||
private String username; | ||
private char[] password; | ||
|
||
public ClientCallbackHandler(final String username, final String password) { | ||
console = System.console(); | ||
this.username = username; | ||
if (password != null) { | ||
this.password = password.toCharArray(); | ||
} | ||
} | ||
|
||
@Override | ||
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { | ||
// Special case for anonymous authentication to avoid prompting user for their name. | ||
if (callbacks.length == 1 && callbacks[0] instanceof NameCallback) { | ||
((NameCallback) callbacks[0]).setName("anonymous demo user"); | ||
return; | ||
} | ||
|
||
for (Callback current : callbacks) { | ||
if (current instanceof RealmCallback) { | ||
final RealmCallback rcb = (RealmCallback) current; | ||
final String defaultText = rcb.getDefaultText(); | ||
rcb.setText(defaultText); // For now just use the realm suggested. | ||
|
||
prompt(defaultText); | ||
} else if (current instanceof RealmChoiceCallback) { | ||
throw new UnsupportedCallbackException(current, "Realm choice not currently supported."); | ||
} else if (current instanceof NameCallback) { | ||
final NameCallback ncb = (NameCallback) current; | ||
final String userName = obtainUsername("Username:"); | ||
|
||
ncb.setName(userName); | ||
} else if (current instanceof PasswordCallback) { | ||
PasswordCallback pcb = (PasswordCallback) current; | ||
char[] password = obtainPassword("Password:"); | ||
|
||
pcb.setPassword(password); | ||
} else { | ||
throw new UnsupportedCallbackException(current); | ||
} | ||
} | ||
} | ||
|
||
private void prompt(final String realm) { | ||
if (!promptShown) { | ||
promptShown = true; | ||
} | ||
} | ||
|
||
private String obtainUsername(final String prompt) { | ||
if (username == null) { | ||
checkConsole(); | ||
username = console.readLine(prompt); | ||
} | ||
return username; | ||
} | ||
|
||
private char[] obtainPassword(final String prompt) { | ||
if (password == null) { | ||
checkConsole(); | ||
password = console.readPassword(prompt); | ||
} | ||
|
||
return password; | ||
} | ||
|
||
private void checkConsole() { | ||
if (console == null) { | ||
throw new IllegalStateException( | ||
"The environment does not have a usable console. Cannot prompt for user name and password"); | ||
} | ||
} | ||
|
||
} |
146 changes: 146 additions & 0 deletions
146
.../org/wildfly/plugins/bootablejar/maven/goals/ToolsModelControllerClientConfiguration.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,146 @@ | ||
/* | ||
* Copyright The WildFly Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.wildfly.plugins.bootablejar.maven.goals; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutorService; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import javax.security.auth.callback.CallbackHandler; | ||
|
||
import org.jboss.as.controller.client.ModelControllerClientConfiguration; | ||
import org.wildfly.security.SecurityFactory; | ||
|
||
/** | ||
* A configuration used to connect a {@link org.jboss.as.controller.client.ModelControllerClient} or used to connect a | ||
* CLI {@code CommandContext} | ||
* | ||
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a> | ||
*/ | ||
public class ToolsModelControllerClientConfiguration implements ModelControllerClientConfiguration { | ||
|
||
private final ModelControllerClientConfiguration delegate; | ||
private final String username; | ||
private final String password; | ||
private final CallbackHandler callbackHandler; | ||
|
||
public ToolsModelControllerClientConfiguration(final ModelControllerClientConfiguration delegate, final String username, | ||
final String password) { | ||
this.delegate = delegate; | ||
this.username = username; | ||
this.password = password; | ||
if (delegate.getAuthenticationConfigUri() == null) { | ||
callbackHandler = new ClientCallbackHandler(username, password); | ||
} else { | ||
callbackHandler = delegate.getCallbackHandler(); | ||
} | ||
} | ||
|
||
@Override | ||
public String getHost() { | ||
return delegate.getHost(); | ||
} | ||
|
||
@Override | ||
public int getPort() { | ||
return delegate.getPort(); | ||
} | ||
|
||
@Override | ||
public String getProtocol() { | ||
return delegate.getProtocol(); | ||
} | ||
|
||
@Override | ||
public int getConnectionTimeout() { | ||
return delegate.getConnectionTimeout(); | ||
} | ||
|
||
@Override | ||
public CallbackHandler getCallbackHandler() { | ||
return callbackHandler; | ||
} | ||
|
||
@Override | ||
public Map<String, String> getSaslOptions() { | ||
return delegate.getSaslOptions(); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("deprecation") | ||
public SSLContext getSSLContext() { | ||
return delegate.getSSLContext(); | ||
} | ||
|
||
@Override | ||
public SecurityFactory<SSLContext> getSslContextFactory() { | ||
return delegate.getSslContextFactory(); | ||
} | ||
|
||
@Override | ||
public ExecutorService getExecutor() { | ||
return delegate.getExecutor(); | ||
} | ||
|
||
@Override | ||
public String getClientBindAddress() { | ||
return delegate.getClientBindAddress(); | ||
} | ||
|
||
@Override | ||
public URI getAuthenticationConfigUri() { | ||
return delegate.getAuthenticationConfigUri(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
delegate.close(); | ||
} | ||
|
||
/** | ||
* The username provided or {@code null} if one was not provided. | ||
* | ||
* @return the username or {@code null} | ||
*/ | ||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
/** | ||
* The password providedor {@code null} if one was not provided. | ||
* | ||
* @return the password or {@code null} | ||
*/ | ||
public char[] getPassword() { | ||
if (password == null) { | ||
return null; | ||
} | ||
return password.toCharArray(); | ||
} | ||
|
||
/** | ||
* Formats a connection string for CLI to use as it's controller connection. | ||
* | ||
* @return the controller string to connect CLI | ||
*/ | ||
public String getController() { | ||
final StringBuilder controller = new StringBuilder(); | ||
if (getProtocol() != null) { | ||
controller.append(getProtocol()).append("://"); | ||
} | ||
if (getHost() != null) { | ||
controller.append(getHost()); | ||
} else { | ||
controller.append("localhost"); | ||
} | ||
if (getPort() > 0) { | ||
controller.append(':').append(getPort()); | ||
} | ||
return controller.toString(); | ||
} | ||
} |