Skip to content

Commit

Permalink
[JENKINS-37778] credentials should not be a step
Browse files Browse the repository at this point in the history
Now "just a method" in environment
  • Loading branch information
rsandell committed Nov 7, 2016
1 parent 47f5b4c commit 8287a8a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 205 deletions.

This file was deleted.

Expand Up @@ -25,6 +25,7 @@ package org.jenkinsci.plugins.pipeline.modeldefinition

import org.jenkinsci.plugins.pipeline.modeldefinition.model.AbstractBuildConditionResponder
import org.jenkinsci.plugins.pipeline.modeldefinition.model.ClosureContentsChecker
import org.jenkinsci.plugins.pipeline.modeldefinition.model.Environment
import org.jenkinsci.plugins.pipeline.modeldefinition.model.MappedClosure
import org.jenkinsci.plugins.pipeline.modeldefinition.model.MethodMissingWrapper
import org.jenkinsci.plugins.pipeline.modeldefinition.model.MethodsToList
Expand Down Expand Up @@ -131,7 +132,7 @@ public class ClosureModelTranslator implements MethodMissingWrapper, Serializabl
}
// if it's a PropertiesToMap, we use PropertiesToMapTranslator to translate it into the right form.
else if (Utils.assignableFromWrapper(PropertiesToMap.class, actualType)) {
def ptm = new PropertiesToMapTranslator(script)
def ptm = new PropertiesToMapTranslator(script, Utils.assignableFromWrapper(Environment.class, actualType))
resolveClosure(argValue, ptm)
resultValue = ptm.toNestedModel(actualType)
}
Expand Down
Expand Up @@ -23,9 +23,12 @@
*/
package org.jenkinsci.plugins.pipeline.modeldefinition

import org.jenkinsci.plugins.pipeline.modeldefinition.model.CredentialsBindingHandler
import org.jenkinsci.plugins.pipeline.modeldefinition.model.MethodMissingWrapper
import org.jenkinsci.plugins.pipeline.modeldefinition.model.NestedModel
import org.jenkinsci.plugins.pipeline.modeldefinition.steps.CredentialWrapper
import org.jenkinsci.plugins.workflow.cps.CpsScript
import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper

/**
* Translates a closure containing one or more "foo = 'bar'" statements into a map.
Expand All @@ -34,8 +37,10 @@ import org.jenkinsci.plugins.workflow.cps.CpsScript
public class PropertiesToMapTranslator implements MethodMissingWrapper, Serializable {
Map<String,Object> actualMap = [:]
CpsScript script
private final boolean resolveCredentials

PropertiesToMapTranslator(CpsScript script) {
PropertiesToMapTranslator(CpsScript script, boolean resolveCredentials = false) {
this.resolveCredentials = resolveCredentials
this.script = script
}

Expand All @@ -47,6 +52,19 @@ public class PropertiesToMapTranslator implements MethodMissingWrapper, Serializ
argValue = args[0]
}

if (resolveCredentials && s == "credentials") {
if (args.length == 1) {
String id = "${args[0]}"

RunWrapper currentBuild = script.getProperty("currentBuild")

CredentialsBindingHandler handler = CredentialsBindingHandler.forId(id, currentBuild.rawBuild);
return new CredentialWrapper(id, handler.getWithCredentialsParameters(id));
} else {
throw new IllegalArgumentException("credentials is expecting one parameter for the credentials id")
}
}

return script."${s}"(argValue)
}

Expand All @@ -66,5 +84,4 @@ public class PropertiesToMapTranslator implements MethodMissingWrapper, Serializ
m.modelFromMap(actualMap)
return m
}

}

This file was deleted.

This file was deleted.

Expand Up @@ -26,12 +26,17 @@
package org.jenkinsci.plugins.pipeline.modeldefinition.steps;

import com.cloudbees.hudson.plugins.folder.Folder;
import com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider;
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey;
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.domains.DomainCredentials;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import hudson.model.ModelObject;
import hudson.model.Result;
import hudson.util.Secret;
import org.jenkinsci.plugins.pipeline.modeldefinition.AbstractModelDefTest;
Expand All @@ -40,12 +45,14 @@
import org.junit.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;

import static org.hamcrest.core.AllOf.allOf;
import static org.hamcrest.core.StringContains.containsString;

/**
* Tests {@link CredentialWrapperStep}.
* Tests the "fake" {@code credentials} step in {@code environment}.
*/
public class CredentialWrapperStepTest extends AbstractModelDefTest {

Expand All @@ -62,27 +69,30 @@ public class CredentialWrapperStepTest extends AbstractModelDefTest {
private static final String mixedEnvInFolderCred2P = "folder-supersecretpassword+mydogsname";

@BeforeClass
public static void setup() throws IOException {
public static void setup() throws Exception {
CredentialsStore store = CredentialsProvider.lookupStores(j.jenkins).iterator().next();

String usernamePasswordCredentialsId = "FOOcredentials";
UsernamePasswordCredentialsImpl usernamePassword = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, usernamePasswordCredentialsId, "sample", usernamePasswordUsername, usernamePasswordPassword);
CredentialsProvider.lookupStores(j.jenkins).iterator().next().addCredentials(Domain.global(), usernamePassword);
store.addCredentials(Domain.global(), usernamePassword);

StringCredentialsImpl mixedEnvCred1 = new StringCredentialsImpl(CredentialsScope.GLOBAL, mixedEnvCred1Id, "test", Secret.fromString(mixedEnvCred1Secret));
CredentialsProvider.lookupStores(j.jenkins).iterator().next().addCredentials(Domain.global(), mixedEnvCred1);
store.addCredentials(Domain.global(), mixedEnvCred1);
UsernamePasswordCredentialsImpl mixedEnvCred2 = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, mixedEnvCred2Id, "sample", mixedEnvCred2U, mixedEnvCred2P);
CredentialsProvider.lookupStores(j.jenkins).iterator().next().addCredentials(Domain.global(), mixedEnvCred2);
store.addCredentials(Domain.global(), mixedEnvCred2);

folder = j.jenkins.createProject(Folder.class, "testFolder");
j.configRoundtrip(folder);
CredentialsStore folderStore = folder.getProperties().get(FolderCredentialsProvider.FolderCredentialsProperty.class).getStore();
StringCredentialsImpl sc = new StringCredentialsImpl(CredentialsScope.GLOBAL, mixedEnvCred1Id, "test", Secret.fromString(mixedEnvInFolderCred1Secret));
CredentialsProvider.lookupStores(folder).iterator().next().addCredentials(Domain.global(), sc);
folderStore.addCredentials(Domain.global(), sc);
UsernamePasswordCredentialsImpl c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, mixedEnvCred2Id, "sample", mixedEnvInFoldercred2U, mixedEnvInFolderCred2P);
CredentialsProvider.lookupStores(folder).iterator().next().addCredentials(Domain.global(), c);
folderStore.addCredentials(Domain.global(), c);

SSHUserPrivateKey k = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, "sshCred1", "bobby", new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource("abc123"), null, "sample");
CredentialsProvider.lookupStores(j.jenkins).iterator().next().addCredentials(Domain.global(), k);
store.addCredentials(Domain.global(), k);
}


@Test
public void usernamePassword() throws Exception {
expect("credentials", "usernamePassword").runFromRepo(false)
Expand All @@ -94,7 +104,7 @@ public void usernamePassword() throws Exception {

@Test
public void mixedEnv() throws Exception {
expect("credentials", "mixedEnv").runFromRepo(false)
expect("credentials", "mixedEnv")
.logContains("SOME_VAR is SOME VALUE",
"INBETWEEN is Something in between",
"OTHER_VAR is OTHER VALUE")
Expand Down

0 comments on commit 8287a8a

Please sign in to comment.