Skip to content

Commit

Permalink
Merge pull request #3 from stephenc/jenkins-45467
Browse files Browse the repository at this point in the history
[FIXED JENKINS-45467] Add form validation for SSHCheckoutTrait
  • Loading branch information
stephenc committed Jul 12, 2017
2 parents da03e43 + bc64050 commit 40aec73
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/main/java/org/jenkinsci/plugin/gitea/SSHCheckoutTrait.java
Expand Up @@ -39,6 +39,7 @@
import hudson.plugins.git.GitSCM;
import hudson.scm.SCM;
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import jenkins.plugins.git.GitSCMBuilder;
Expand All @@ -47,6 +48,7 @@
import jenkins.scm.api.trait.SCMSourceContext;
import jenkins.scm.api.trait.SCMSourceTrait;
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.AncestorInPath;
Expand Down Expand Up @@ -79,7 +81,7 @@ public static class DescriptorImpl extends SCMSourceTraitDescriptor {
@NonNull
@Override
public String getDisplayName() {
return "Checkout over SSH";
return Messages.SSHCheckoutTrait_displayName();
}

@Override
Expand Down Expand Up @@ -122,7 +124,7 @@ public ListBoxModel doFillCredentialsIdItems(@CheckForNull @AncestorInPath Item
return result;
}
}
result.includeEmptyValue();
result.add(Messages.SSHCheckoutTrait_useAgentKey(), "");
result.includeMatchingAs(
context instanceof Queue.Task ?
Tasks.getDefaultAuthenticationOf((Queue.Task) context)
Expand All @@ -134,5 +136,45 @@ public ListBoxModel doFillCredentialsIdItems(@CheckForNull @AncestorInPath Item
);
return result;
}

/**
* Validation for checkout credentials.
*
* @param context the context.
* @param serverUrl the server url.
* @param value the current selection.
* @return the validation results
*/
@Restricted(NoExternalUse.class)
@SuppressWarnings("unused") // stapler form binding
public FormValidation doCheckCredentialsId(@CheckForNull @AncestorInPath Item context,
@QueryParameter String serverUrl,
@QueryParameter String value) {
if (context == null
? !Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER)
: !context.hasPermission(Item.EXTENDED_READ)) {
return FormValidation.ok();
}
if (StringUtils.isBlank(value)) {
// use agent key
return FormValidation.ok();
}
if (CredentialsMatchers.firstOrNull(CredentialsProvider
.lookupCredentials(SSHUserPrivateKey.class, context, context instanceof Queue.Task
? Tasks.getDefaultAuthenticationOf((Queue.Task) context)
: ACL.SYSTEM, URIRequirementBuilder.fromUri(serverUrl).build()),
CredentialsMatchers.withId(value)) != null) {
return FormValidation.ok();
}
if (CredentialsMatchers.firstOrNull(CredentialsProvider
.lookupCredentials(StandardUsernameCredentials.class, context, context instanceof Queue.Task
? Tasks.getDefaultAuthenticationOf((Queue.Task) context)
: ACL.SYSTEM, URIRequirementBuilder.fromUri(serverUrl).build()),
CredentialsMatchers.withId(value)) != null) {
return FormValidation.error(Messages.SSHCheckoutTrait_incompatibleCredentials());
}
return FormValidation.warning(Messages.SSHCheckoutTrait_missingCredentials());
}

}
}
Expand Up @@ -22,3 +22,7 @@ GiteaLink.displayName=Gitea
WebhookRegistrationTrait.disableHook=Disable hook management
WebhookRegistrationTrait.displayName=Override hook management
WebhookRegistrationTrait.useItemHook=Use item credentials for hook management
SSHCheckoutTrait.displayName=Checkout over SSH
SSHCheckoutTrait.incompatibleCredentials=The currently configured credentials are incompatible with this behaviour
SSHCheckoutTrait.missingCredentials=The currently configured credentials cannot be found
SSHCheckoutTrait.useAgentKey=- use build agent''s key -

0 comments on commit 40aec73

Please sign in to comment.