Skip to content

Commit

Permalink
SECURITY-1761
Browse files Browse the repository at this point in the history
  • Loading branch information
mat1e committed Feb 13, 2020
1 parent 9f7e1db commit 86aebd3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/main/java/fr/edf/jenkins/plugins/mac/MacHost.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.edf.jenkins.plugins.mac

import org.acegisecurity.AccessDeniedException
import org.apache.commons.lang.StringUtils
import org.kohsuke.stapler.AncestorInPath
import org.kohsuke.stapler.DataBoundConstructor
Expand Down Expand Up @@ -224,12 +225,7 @@ class MacHost implements Describable<MacHost> {
*/
@POST
public FormValidation doCheckKey(@QueryParameter String key) {
try {
MacHostKeyVerifier.parseKey(key)
return FormValidation.ok()
} catch (MacHostKeyVerifierException|IllegalArgumentException ex) {
return FormValidation.error(ex.getMessage())
}
return FormUtils.verifyHostKey(key)
}
}
}
19 changes: 19 additions & 0 deletions src/main/java/fr/edf/jenkins/plugins/mac/util/FormUtils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import static com.cloudbees.plugins.credentials.CredentialsMatchers.anyOf
import static com.cloudbees.plugins.credentials.CredentialsMatchers.instanceOf
import static com.cloudbees.plugins.credentials.domains.URIRequirementBuilder.fromUri

import org.acegisecurity.AccessDeniedException
import org.antlr.v4.runtime.misc.NotNull
import org.jenkinsci.plugins.plaincredentials.FileCredentials
import org.kohsuke.accmod.Restricted
Expand All @@ -18,6 +19,7 @@ import fr.edf.jenkins.plugins.mac.Messages
import fr.edf.jenkins.plugins.mac.ssh.SSHCommand
import fr.edf.jenkins.plugins.mac.ssh.connection.SSHGlobalConnectionConfiguration
import fr.edf.jenkins.plugins.mac.ssh.key.verifiers.MacHostKeyVerifier
import fr.edf.jenkins.plugins.mac.ssh.key.verifiers.MacHostKeyVerifierException
import hudson.model.Item
import hudson.model.ModelObject
import hudson.security.ACL
Expand Down Expand Up @@ -94,6 +96,7 @@ class FormUtils {
static FormValidation verifyConnection(final String host, final Integer port,
final String credentialsId, final String key, final ModelObject context) {
try {
Jenkins.get().checkPermission(Jenkins.ADMINISTER)
MacHostKeyVerifier verifier = new MacHostKeyVerifier(key)
String result = SSHCommand.checkConnection(new SSHGlobalConnectionConfiguration(credentialsId: credentialsId, port: port,
context: context, host: host, connectionTimeout: 30,
Expand Down Expand Up @@ -160,4 +163,20 @@ class FormUtils {
fromUri(getUri(Jenkins.get().getRootUrl()).toString()).build(),
anyOf(instanceOf(FileCredentials)))
}

/**
* Check the validity of the given key
* @param key
* @return ok if valid, error with exception message if not
*/
@Restricted(NoExternalUse)
static FormValidation verifyHostKey(String key) {
try {
Jenkins.get().checkPermission(Jenkins.ADMINISTER)
MacHostKeyVerifier.parseKey(key)
return FormValidation.ok()
} catch (MacHostKeyVerifierException|IllegalArgumentException|AccessDeniedException ex) {
return FormValidation.error(ex.getMessage())
}
}
}

0 comments on commit 86aebd3

Please # to comment.