forked from CESNET/perun-wui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request CESNET#255 from xflord/sshValidation
feat: backend ssh public key validation
- Loading branch information
Showing
10 changed files
with
185 additions
and
240 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,30 @@ | ||
package cz.metacentrum.perun.wui.registrar.widgets.items.validators; | ||
|
||
import com.google.gwt.regexp.shared.MatchResult; | ||
import com.google.gwt.regexp.shared.RegExp; | ||
import com.google.gwt.core.client.JavaScriptObject; | ||
import cz.metacentrum.perun.wui.json.Events; | ||
import cz.metacentrum.perun.wui.json.JsonEvents; | ||
import cz.metacentrum.perun.wui.json.managers.UsersManager; | ||
import cz.metacentrum.perun.wui.model.PerunException; | ||
import cz.metacentrum.perun.wui.registrar.widgets.items.ListBox; | ||
import cz.metacentrum.perun.wui.widgets.boxes.ExtendedTextBox; | ||
import org.gwtbootstrap3.client.ui.constants.ValidationState; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Validator for ListBox | ||
* | ||
* @author Jakub Hejda <[email protected]> | ||
*/ | ||
public class SshKeysListBoxValidator extends ListBoxValidator { | ||
|
||
RegExp regExp = RegExp.compile("^(" + | ||
"(ssh-(rsa|dss|ed25519)([email protected])?)|" + | ||
"(sk-(ssh-ed25519|ecdsa-sha2-nistp256)(-cert-v01)[email protected])|" + | ||
"(ecdsa-sha2-nistp(256|384|521)([email protected])?))" + | ||
" (([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?)( [^,\n]+)?$"); | ||
final Map<String, ValidationState> checkVals = new TreeMap<>(); | ||
final Map<String, Integer> indexMap = new HashMap<>(); | ||
int validatingCounter = 0; | ||
boolean callFailed = false; | ||
|
||
@Override | ||
public boolean validateLocal(ListBox listBox) { | ||
|
@@ -26,34 +33,106 @@ public boolean validateLocal(ListBox listBox) { | |
listBox.setRawStatus(getTransl().cantBeEmpty(), ValidationState.ERROR); | ||
return false; | ||
} | ||
// FIXME - but it is probably not necessary as API should check it | ||
for (ExtendedTextBox extendedTextBox : listBox.getListValue()) { | ||
String sshKey = extendedTextBox.getValue(); | ||
if (sshKey.contains(",")) { | ||
setResult(Result.INVALID_FORMAT); | ||
listBox.setStatus(getTransl().sshKeySeparatorNotAllowed(), ValidationState.ERROR); | ||
return false; | ||
} | ||
} | ||
listBox.setStatus(ValidationState.SUCCESS); | ||
return true; | ||
} | ||
|
||
@Override | ||
public void validate(ListBox listBox, Events<Boolean> events) { | ||
|
||
if (!validateLocal(listBox)) { | ||
events.onFinished(false); | ||
return; | ||
} | ||
|
||
if (listBox.getValue() == null || listBox.getValue().isEmpty()) { | ||
events.onFinished(true); | ||
return; | ||
} | ||
|
||
checkVals.clear(); | ||
indexMap.clear(); | ||
callFailed = false; | ||
|
||
if (listBox.getValue() != null && !listBox.getValue().isEmpty()) { | ||
int counter = 1; | ||
for (ExtendedTextBox extendedTextBox : listBox.getListValue()) { | ||
String sshKey = extendedTextBox.getValue(); | ||
indexMap.put(sshKey, counter); | ||
checkVals.put(sshKey, ValidationState.NONE); | ||
counter++; | ||
} | ||
|
||
String wrongValues = ""; | ||
int index = 1; | ||
for (ExtendedTextBox extendedTextBox : listBox.getListValue()) { | ||
String sshKey = extendedTextBox.getValue(); | ||
for (String sshKey : checkVals.keySet()) { | ||
|
||
if (sshKey.contains(",")) { | ||
setResult(Result.INVALID_FORMAT); | ||
listBox.setStatus(getTransl().sshKeySeparatorNotAllowed(), ValidationState.ERROR); | ||
return false; | ||
UsersManager.validateSSHKey(sshKey, new JsonEvents() { | ||
@Override | ||
public void onFinished(JavaScriptObject result) { | ||
validatingCounter--; | ||
checkVals.put(sshKey, ValidationState.SUCCESS); | ||
if (validatingCounter == 0) { | ||
// last check trigger events | ||
for (ValidationState state : checkVals.values()) { | ||
// at least one key is invalid -> switch to error if API call didn't fail | ||
if (ValidationState.ERROR == state) { | ||
if (!callFailed) { | ||
setResult(Result.INVALID_FORMAT); | ||
listBox.setRawStatus(getTransl().incorrectFormatItemList() + " <b> <br>" + checkVals.entrySet().stream().filter(entry -> (entry.getValue() == ValidationState.ERROR)).map((entry) -> indexMap.get(entry.getKey()) + ". " + | ||
(entry.getKey().length() > 25 ? entry.getKey().substring(0, 23) + "..." : entry.getKey())).collect(Collectors.joining("<br>")) + "</b>", ValidationState.ERROR); | ||
} | ||
// pass to outer event as fail | ||
events.onFinished(false); | ||
return; | ||
} | ||
} | ||
// all values were OK -> trigger success | ||
listBox.setStatus(ValidationState.SUCCESS); | ||
events.onFinished(true); | ||
} | ||
} | ||
|
||
MatchResult matcher = regExp.exec(sshKey); | ||
if (matcher == null) { | ||
wrongValues += "<br>" + index + ". " + (sshKey.length() > 25 ? sshKey.substring(0, 23) + "..." : sshKey); | ||
@Override | ||
public void onError(PerunException error) { | ||
if (!"SSHKeyNotValidException".equalsIgnoreCase(error.getName())) { | ||
callFailed = true; | ||
} | ||
validatingCounter--; | ||
checkVals.put(sshKey, ValidationState.ERROR); | ||
// set error immediately | ||
if (callFailed) { | ||
setResult(Result.CANT_CHECK_SSH); | ||
listBox.setStatus(getTransl().checkingSSHFailed(), ValidationState.ERROR); | ||
} else { | ||
setResult(Result.INVALID_FORMAT); | ||
listBox.setRawStatus(getTransl().incorrectFormatItemList() + " <b> <br>" + checkVals.entrySet().stream().filter(entry -> (entry.getValue() == ValidationState.ERROR)).map((entry) -> indexMap.get(entry.getKey()) + ". " + | ||
(entry.getKey().length() > 25 ? entry.getKey().substring(0, 23) + "..." : entry.getKey())).collect(Collectors.joining("<br>")) + "</b>", ValidationState.ERROR); | ||
} | ||
if (validatingCounter == 0) { | ||
// if last pass to outer event as fail | ||
events.onFinished(false); | ||
} | ||
} | ||
index++; | ||
} | ||
if (!wrongValues.isEmpty()) { | ||
setResult(Result.INVALID_FORMAT); | ||
listBox.setRawStatus(getTransl().incorrectFormatItemList() + " <b>" + wrongValues + "</b>", ValidationState.ERROR); | ||
return false; | ||
} | ||
} | ||
|
||
listBox.setStatus(ValidationState.SUCCESS); | ||
return true; | ||
@Override | ||
public void onLoadingStart() { | ||
if (validatingCounter == 0) { | ||
listBox.unsetStatus(); | ||
events.onLoadingStart(); | ||
setResult(Result.CHECKING_SSH); | ||
listBox.setStatus(ValidationState.WARNING); | ||
} | ||
validatingCounter++; | ||
} | ||
}); | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.