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.
feat: backend ssh public key validation
* replaced regexp in SSH key validators with call to backend validation method * this unifies the process and offers a more strict validation
- Loading branch information
Showing
8 changed files
with
333 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,29 @@ | ||
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; | ||
|
||
@Override | ||
public boolean validateLocal(ListBox listBox) { | ||
|
@@ -26,34 +32,95 @@ 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(); | ||
|
||
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(",")) { | ||
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 (ValidationState.ERROR == state) { | ||
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); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(PerunException error) { | ||
validatingCounter--; | ||
checkVals.put(sshKey, ValidationState.ERROR); | ||
// set error immediately | ||
setResult(Result.INVALID_FORMAT); | ||
listBox.setStatus(getTransl().sshKeySeparatorNotAllowed(), ValidationState.ERROR); | ||
return false; | ||
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); | ||
} | ||
} | ||
|
||
MatchResult matcher = regExp.exec(sshKey); | ||
if (matcher == null) { | ||
wrongValues += "<br>" + index + ". " + (sshKey.length() > 25 ? sshKey.substring(0, 23) + "..." : sshKey); | ||
@Override | ||
public void onLoadingStart() { | ||
if (validatingCounter == 0) { | ||
listBox.unsetStatus(); | ||
events.onLoadingStart(); | ||
setResult(Result.CHECKING_SSH); | ||
listBox.setStatus(ValidationState.WARNING); | ||
} | ||
validatingCounter++; | ||
} | ||
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.