Skip to content

Commit

Permalink
Fixes #711 - Fix some cli stuff, dont prompt for secure storage until…
Browse files Browse the repository at this point in the history
… necessary

Signed-off-by: Rob Stryker <[email protected]>
  • Loading branch information
robstryker committed Sep 1, 2023
1 parent db1a329 commit 725ceaf
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public abstract static class PromptStringHandler implements InputHandler {
private String prompt;
private boolean isSecret;
private boolean done = false;
private boolean shown = false;
public PromptStringHandler(String prompt) {
this(prompt, false);
}
Expand All @@ -209,6 +210,15 @@ public PromptStringHandler(String prompt, boolean secret) {
this.isSecret = secret;
}

@Override
public void setPromptShown() {
this.shown = true;
}

@Override
public boolean isPromptShown() {
return this.shown;
}
@Override
public String getPrompt() {
return prompt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

public interface InputHandler {
public String getPrompt();
public void setPromptShown();
public boolean isPromptShown();
public void handleInput(String line) throws Exception;
public boolean isSecret();
public boolean isDone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ private InputHandler getInputHandler() {

private void printUserPrompt(InputHandler handler) {
String prompt = handler.getPrompt();
if (prompt != null && !prompt.isEmpty()) {
if (prompt != null && !prompt.isEmpty() && !handler.isPromptShown()) {
handler.setPromptShown();
System.out.println(prompt);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ private static void handleSinglePrompt(WorkflowResponseItem item, HashMap<String
private InputProvider provider;
private PromptAssistant assistant;
private boolean done = false;
private boolean shown = false;

public StandardCommandHandler(ServerManagementClientLauncher launcher, InputProvider provider) {
this.launcher = launcher;
Expand Down Expand Up @@ -993,4 +994,14 @@ private void showCommands() {
public String getPrompt() {
return "Please enter a command.\n";
}

@Override
public void setPromptShown() {
this.shown = true;
}

@Override
public boolean isPromptShown() {
return this.shown;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ public File getLaunchingSupportFile() {
ClassLoader classLoader = getClass().getClassLoader();
InputStream is = classLoader.getResourceAsStream("launchingsupport.jar");

// Debugging only. For some reason when running as standalone java app, cant find the jar
// if( is == null ) {
// String url = "https://raw.githubusercontent.com/redhat-developer/rsp-server/v0_26_2/framework/bundles/org.jboss.tools.rsp.launching.java/src/main/resources/launchingsupport.jar";
// try {
// is = new URL(url).openStream();
// } catch( IOException err) {
// log(err);
// }
// }
//Debugging only. For some reason when running as standalone java app, cant find the jar
if( is == null ) {
String url = "https://raw.githubusercontent.com/redhat-developer/rsp-server/v0_26_2/framework/bundles/org.jboss.tools.rsp.launching.java/src/main/resources/launchingsupport.jar";
try {
is = new URL(url).openStream();
} catch( IOException err) {
log(err);
}
}

if( is != null ) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public interface IServerModel {
Attributes getRequiredAttributes(IServerType serverType);

Attributes getOptionalAttributes(IServerType serverType);

boolean hasSecureAttributes(IServerType type);

List<ServerLaunchMode> getLaunchModes(IServerType serverType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,19 +561,20 @@ public ServerType[] getAccessibleServerTypes() {
all.add(st);
}

if (all.size() > free.size()
&& !hasPermissions()) {
return free.toArray(new ServerType[free.size()]);
}

// if (all.size() > free.size()
// && !hasPermissions()) {
// return free.toArray(new ServerType[free.size()]);
// }
//
return all.toArray(new ServerType[all.size()]);
}

private boolean hasPermissions() {
return managementModel.getSecureStorageProvider().getSecureStorage(true) != null;
}
//
// private boolean hasPermissions() {
// return managementModel.getSecureStorageProvider().getSecureStorage(true) != null;
// }

private boolean hasSecureAttributes(IServerType type) {
@Override
public boolean hasSecureAttributes(IServerType type) {
Attributes a = type.getRequiredAttributes();
Attributes b = type.getOptionalAttributes();
Set<String> all = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public class SecureStorageGuardian implements ISecureStorageProvider {

private File file;
private Map<RSPClient, byte[]> permissions;
private List<RSPClient> maxTriesReached;
private Map<RSPClient, Integer> maxTriesReached;
private ISecureStorage storage = null;
private ICapabilityManagement capabilities;
public SecureStorageGuardian(File file, ICapabilityManagement capabilities) {
this.file = file;
this.capabilities = capabilities;
this.permissions = new HashMap<>();
this.maxTriesReached = new ArrayList<>();
this.maxTriesReached = new HashMap<>();
}

public void addClient(RSPClient client, byte[] key) throws CryptoException {
Expand Down Expand Up @@ -80,20 +80,25 @@ private RSPSecureStorage checkKey(byte[] key) throws CryptoException {
private void authenticateClient(RSPClient client, int maxTries) throws InterruptedException, ExecutionException {
if( canPromptClient(client, capabilities)) {
String msg = "Please provide a secure-storage password to either create a new, or load an existing, secure storage.";
StringPrompt prompt = new StringPrompt(100, msg, true);
int tries = 0;
String failedMsg = "The provided password did not decrypt the secure storage. ";
int tries = maxTriesReached.get(client) == null ? 0 : maxTriesReached.get(client);
while(tries < maxTries) {
String msgToUse = (tries == 0 ? msg : failedMsg + msg);
StringPrompt prompt = new StringPrompt(100, msgToUse, true);
String secureKey = client.promptString(prompt).get();
if( secureKey != null && secureKey.length() != 0 && secureKey.trim().length() != 0) {
try {
addClient(client, secureKey.getBytes());
// success at decrypting the file, or, file didn't exist yet
return;
} catch(CryptoException ce) {
LOG.error(ce.getMessage(), ce);
}
tries = tries + 1;
maxTriesReached.put(client, tries);
boolean empty = secureKey == null || secureKey.length() == 0 || secureKey.trim().length() == 0;
if( empty ) {
return;
}
try {
addClient(client, secureKey.getBytes());
// success at decrypting the file, or, file didn't exist yet
return;
} catch(CryptoException ce) {
LOG.error(ce.getMessage(), ce);
}
tries += 1;
}
}
}
Expand Down Expand Up @@ -131,9 +136,11 @@ public ISecureStorage getSecureStorage() {
public ISecureStorage getSecureStorage(boolean prompt) {
ISecureStorage storage = getSecureStorage();
RSPClient rspc = ClientThreadLocal.getActiveClient();
if( rspc != null && storage == null && prompt && !maxTriesReached.contains(rspc)) {
int maxTries = 4;
int tries = maxTriesReached.get(rspc) == null ? 0 : maxTriesReached.get(rspc);
if( rspc != null && storage == null && prompt && tries <= maxTries) {
try {
authenticateClient(rspc, 4);
authenticateClient(rspc, maxTries);
if( getSecureStorage() != null ) {
return getSecureStorage();
}
Expand All @@ -143,9 +150,6 @@ public ISecureStorage getSecureStorage(boolean prompt) {
}
LOG.error(ie.getMessage(), ie);
}
if( storage == null ) {
maxTriesReached.add(rspc);
}
}
return storage;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
<booleanAttribute key="org.eclipse.debug.core.ATTR_FORCE_SYSTEM_CONSOLE_ENCODING" value="false"/>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/org.jboss.tools.rsp.server.minishift/src/main/java/org/jboss/tools/rsp/server/minishift/impl/MinishiftServerMain.java"/>
</listAttribute>
Expand All @@ -14,7 +15,8 @@
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.jboss.tools.rsp.server.minishift.impl.MinishiftServerMain"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="27511"/>
<stringAttribute key="org.eclipse.jdt.launching.MODULE_NAME" value="org.jboss.tools.rsp.server.minishift"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="9000"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.jboss.tools.rsp.server.minishift"/>
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Dorg.jboss.tools.rsp.id=redhat-server-connector"/>
Expand Down

0 comments on commit 725ceaf

Please sign in to comment.