Skip to content

[JENKINS-59109] Bind triggering user's credentials #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -970,8 +970,9 @@ public static <C extends IdCredentials> C findCredentialById(@NonNull String id,
* @param run the {@link Run} to find the trigger of.
* @return the trigger of the supplied run or {@code null} if this could not be determined.
*/
@Restricted(NoExternalUse.class)
@CheckForNull
private static Map.Entry<User, Run<?, ?>> triggeredBy(Run<?, ?> run) {
public static Map.Entry<User, Run<?, ?>> triggeredBy(Run<?, ?> run) {
Cause.UserIdCause cause = run.getCause(Cause.UserIdCause.class);
if (cause != null) {
User u = User.get(cause.getUserId(), false, Collections.emptyMap());
Expand Down Expand Up @@ -1784,4 +1785,3 @@ public int compare(ListBoxModel.Option o1, ListBoxModel.Option o2) {
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
package com.cloudbees.plugins.credentials.builds;

import com.cloudbees.plugins.credentials.CredentialsParameterValue;
import hudson.model.Cause;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import hudson.model.InvisibleAction;
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.Run;
import hudson.model.User;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

Expand Down Expand Up @@ -60,8 +61,8 @@ public static CredentialsParameterBinder getOrCreate(@Nonnull final Run<?, ?> ru
resolver = new CredentialsParameterBinder();
final ParametersAction action = run.getAction(ParametersAction.class);
if (action != null) {
final Cause.UserIdCause cause = run.getCause(Cause.UserIdCause.class);
final String userId = cause == null ? null : cause.getUserId();
Map.Entry<User, Run<?, ?>> triggeredBy = CredentialsProvider.triggeredBy(run);
final String userId = triggeredBy == null ? null : triggeredBy.getKey().getId();
for (final ParameterValue parameterValue : action) {
if (parameterValue instanceof CredentialsParameterValue) {
resolver.bindCredentialsParameter(userId, (CredentialsParameterValue) parameterValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import hudson.model.FreeStyleProject;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Run;
import hudson.model.User;
import hudson.security.ACL;
import hudson.security.ACLContext;
Expand Down Expand Up @@ -125,11 +126,30 @@ public void forParameterNameReturnsTriggeringUser() throws Exception {
assertNotNull(CredentialsProvider.findCredentialById(cred.getParameterName(), IdCredentials.class, build));
}

@Test
public void forParameterNameReturnsTriggeringUserForUpstream() throws Exception {
FreeStyleProject upstreamProject = j.createFreeStyleProject();
upstreamProject.addProperty(new ParametersDefinitionProperty(new CredentialsParameterDefinition(PARAMETER_NAME, null, null, IdCredentials.class.getName(), true)));
final Run<?, ?> upstreamBuild = j.assertBuildStatusSuccess(upstreamProject.scheduleBuild2(0,
new Cause.UserIdCause(USER_ID), selectCredentialsById(USER_CREDENTIALS_ID)));

addCredentialsParameterDefinition();
final FreeStyleBuild build = j.assertBuildStatusSuccess(project.scheduleBuild2(0,
new Cause.UpstreamCause(upstreamBuild), selectCredentialsById(USER_CREDENTIALS_ID)));
final CredentialsParameterBinder binder = CredentialsParameterBinder.getOrCreate(build);
final CredentialsParameterBinding cred = binder.forParameterName(PARAMETER_NAME);
assertNotNull(cred);
assertEquals(USER_ID, cred.getUserId());
assertEquals(USER_CREDENTIALS_ID, cred.getCredentialsId());
// as a result:
assertNotNull(CredentialsProvider.findCredentialById(cred.getParameterName(), IdCredentials.class, build));
}

private void addCredentialsParameterDefinition() throws IOException {
project.addProperty(new ParametersDefinitionProperty(new CredentialsParameterDefinition(PARAMETER_NAME, null, null, IdCredentials.class.getName(), true)));
}

private static ParametersAction selectCredentialsById(String credentialsId) {
return new ParametersAction(new CredentialsParameterValue(PARAMETER_NAME, credentialsId, null, false));
}
}
}