use of hudson.EnvVars in project blueocean-plugin by jenkinsci.
the class GitCloneReadSaveRequest method cloneRepo.
GitClient cloneRepo() throws InterruptedException, IOException {
EnvVars environment = new EnvVars();
TaskListener taskListener = new LogTaskListener(Logger.getAnonymousLogger(), Level.ALL);
String gitExe = gitTool.getGitExe();
GitClient git = Git.with(taskListener, environment).in(repositoryPath).using(gitExe).getClient();
git.addCredentials(gitSource.getRemote(), getCredential());
try {
git.clone(gitSource.getRemote(), "origin", true, null);
log.fine("Repository " + gitSource.getRemote() + " cloned to: " + repositoryPath.getCanonicalPath());
} catch (GitException e) {
// check if this is an empty repository
boolean isEmptyRepo = false;
try {
if (git.getRemoteReferences(gitSource.getRemote(), null, true, false).isEmpty()) {
isEmptyRepo = true;
}
} catch (GitException ge) {
// *sigh* @ this necessary hack; {@link org.jenkinsci.plugins.gitclient.CliGitAPIImpl#getRemoteReferences}
if ("unexpected ls-remote output ".equals(ge.getMessage())) {
// blank line, command succeeded
isEmptyRepo = true;
}
// ignore other reasons
}
if (isEmptyRepo) {
git.init();
git.addRemoteUrl("origin", gitSource.getRemote());
log.fine("Repository " + gitSource.getRemote() + " not found, created new to: " + repositoryPath.getCanonicalPath());
} else {
throw e;
}
}
return git;
}
use of hudson.EnvVars in project workflow-cps-plugin by jenkinsci.
the class DSL method invokeStep.
/**
* When {@link #invokeMethod(String, Object)} is calling a {@link StepDescriptor}
*/
protected Object invokeStep(StepDescriptor d, Object args) {
final NamedArgsAndClosure ps = parseArgs(args, d);
CpsThread thread = CpsThread.current();
FlowNode an;
// TODO: generalize the notion of Step taking over the FlowNode creation.
boolean hack = d instanceof ParallelStep.DescriptorImpl || d instanceof LoadStep.DescriptorImpl;
if (ps.body == null && !hack) {
an = new StepAtomNode(exec, d, thread.head.get());
// TODO: use CPS call stack to obtain the current call site source location. See JENKINS-23013
thread.head.setNewHead(an);
} else {
an = new StepStartNode(exec, d, thread.head.get());
thread.head.setNewHead(an);
}
final CpsStepContext context = new CpsStepContext(d, thread, handle, an, ps.body);
Step s;
boolean sync;
ClassLoader originalLoader = Thread.currentThread().getContextClassLoader();
try {
d.checkContextAvailability(context);
Thread.currentThread().setContextClassLoader(CpsVmExecutorService.ORIGINAL_CONTEXT_CLASS_LOADER.get());
s = d.newInstance(ps.namedArgs);
try {
// No point storing empty arguments, and ParallelStep is a special case where we can't store its closure arguments
if (ps.namedArgs != null && !(ps.namedArgs.isEmpty()) && isKeepStepArguments() && !(s instanceof ParallelStep)) {
// Get the environment variables to find ones that might be credentials bindings
Computer comp = context.get(Computer.class);
EnvVars allEnv = new EnvVars(context.get(EnvVars.class));
if (comp != null && allEnv != null) {
allEnv.entrySet().removeAll(comp.getEnvironment().entrySet());
}
an.addAction(new ArgumentsActionImpl(ps.namedArgs, allEnv));
}
} catch (Exception e) {
// Avoid breaking execution because we can't store some sort of crazy Step argument
LOGGER.log(Level.WARNING, "Error storing the arguments for step: " + d.getFunctionName(), e);
}
// Persist the node - block start and end nodes do their own persistence.
CpsFlowExecution.maybeAutoPersistNode(an);
StepExecution e = s.start(context);
thread.setStep(e);
sync = e.start();
} catch (Exception e) {
if (e instanceof MissingContextVariableException)
reportMissingContextVariableException(context, (MissingContextVariableException) e);
context.onFailure(e);
s = null;
sync = true;
} finally {
Thread.currentThread().setContextClassLoader(originalLoader);
}
if (sync) {
assert context.bodyInvokers.isEmpty() : "If a step claims synchronous completion, it shouldn't invoke body";
if (context.getOutcome() == null) {
context.onFailure(new AssertionError("Step " + s + " claimed to have ended synchronously, but didn't set the result via StepContext.onSuccess/onFailure"));
}
thread.setStep(null);
// we just move on accordingly
if (an instanceof StepStartNode) {
// no body invoked, so EndNode follows StartNode immediately.
thread.head.setNewHead(new StepEndNode(exec, (StepStartNode) an, an));
}
thread.head.markIfFail(context.getOutcome());
return context.replay();
} else {
// if it's in progress, suspend it until we get invoked later.
// when it resumes, the CPS caller behaves as if this method returned with the resume value
Continuable.suspend(new ThreadTaskImpl(context));
// so the execution will never reach here.
throw new AssertionError();
}
}
use of hudson.EnvVars in project workflow-cps-plugin by jenkinsci.
the class EnvActionImpl method getEnvironment.
@Override
public EnvVars getEnvironment() throws IOException, InterruptedException {
TaskListener listener;
if (owner instanceof FlowExecutionOwner.Executable) {
FlowExecutionOwner executionOwner = ((FlowExecutionOwner.Executable) owner).asFlowExecutionOwner();
if (executionOwner != null) {
listener = executionOwner.getListener();
} else {
listener = new LogTaskListener(LOGGER, Level.INFO);
}
} else {
listener = new LogTaskListener(LOGGER, Level.INFO);
}
EnvVars e = owner.getEnvironment(listener);
e.putAll(env);
return e;
}
use of hudson.EnvVars in project workflow-cps-plugin by jenkinsci.
the class ArgumentsActionImplTest method testBasicCreateAndMask.
@Test
public void testBasicCreateAndMask() throws Exception {
HashMap<String, String> passwordBinding = new HashMap<String, String>();
passwordBinding.put("mypass", "p4ssw0rd");
Map<String, Object> arguments = new HashMap<String, Object>();
arguments.put("message", "I have a secret p4ssw0rd");
Field maxSizeF = ArgumentsAction.class.getDeclaredField("MAX_RETAINED_LENGTH");
maxSizeF.setAccessible(true);
int maxSize = maxSizeF.getInt(null);
// Same string, unsanitized
ArgumentsActionImpl argumentsActionImpl = new ArgumentsActionImpl(arguments, new EnvVars());
Assert.assertEquals(true, argumentsActionImpl.isUnmodifiedArguments());
Assert.assertEquals(arguments.get("message"), argumentsActionImpl.getArgumentValueOrReason("message"));
Assert.assertEquals(1, argumentsActionImpl.getArguments().size());
Assert.assertEquals("I have a secret p4ssw0rd", argumentsActionImpl.getArguments().get("message"));
// Test sanitizing arguments now
argumentsActionImpl = new ArgumentsActionImpl(arguments, new EnvVars(passwordBinding));
Assert.assertEquals(false, argumentsActionImpl.isUnmodifiedArguments());
Assert.assertEquals(ArgumentsActionImpl.NotStoredReason.MASKED_VALUE, argumentsActionImpl.getArgumentValueOrReason("message"));
Assert.assertEquals(1, argumentsActionImpl.getArguments().size());
Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, argumentsActionImpl.getArguments().get("message"));
// Mask oversized values
arguments.clear();
arguments.put("text", RandomStringUtils.random(maxSize + 1));
argumentsActionImpl = new ArgumentsActionImpl(arguments);
Assert.assertEquals(ArgumentsAction.NotStoredReason.OVERSIZE_VALUE, argumentsActionImpl.getArgumentValueOrReason("text"));
}
use of hudson.EnvVars in project workflow-cps-plugin by jenkinsci.
the class ArgumentsActionImplTest method testRecursiveSanitizationOfContent.
@Test
public void testRecursiveSanitizationOfContent() {
int maxLen = ArgumentsActionImpl.getMaxRetainedLength();
ArgumentsActionImpl impl = new ArgumentsActionImpl();
EnvVars env = new EnvVars();
String secretUsername = "secretuser";
// assume secretuser is a bound credential
env.put("USERVARIABLE", secretUsername);
char[] oversized = new char[maxLen + 10];
Arrays.fill(oversized, 'a');
String oversizedString = new String(oversized);
// Simplest masking of secret and oversized value
Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, impl.sanitizeObjectAndRecordMutation(secretUsername, env));
Assert.assertFalse(impl.isUnmodifiedArguments());
impl.isUnmodifiedBySanitization = true;
Assert.assertEquals(ArgumentsAction.NotStoredReason.OVERSIZE_VALUE, impl.sanitizeObjectAndRecordMutation(oversizedString, env));
Assert.assertFalse(impl.isUnmodifiedArguments());
impl.isUnmodifiedBySanitization = true;
// Test explosion of Step & UninstantiatedDescribable objects
Step mystep = new EchoStep("I have a " + secretUsername);
Map<String, ?> singleSanitization = (Map<String, Object>) (impl.sanitizeObjectAndRecordMutation(mystep, env));
Assert.assertEquals(1, singleSanitization.size());
Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, singleSanitization.get("message"));
Assert.assertFalse(impl.isUnmodifiedArguments());
impl.isUnmodifiedBySanitization = true;
singleSanitization = ((UninstantiatedDescribable) (impl.sanitizeObjectAndRecordMutation(mystep.getDescriptor().uninstantiate(mystep), env))).getArguments();
Assert.assertEquals(1, singleSanitization.size());
Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, singleSanitization.get("message"));
Assert.assertFalse(impl.isUnmodifiedArguments());
impl.isUnmodifiedBySanitization = true;
// Maps
HashMap<String, Object> dangerous = new HashMap<>();
dangerous.put("name", secretUsername);
Map<String, Object> sanitizedMap = impl.sanitizeMapAndRecordMutation(dangerous, env);
Assert.assertNotEquals(sanitizedMap, dangerous);
Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, sanitizedMap.get("name"));
Assert.assertFalse(impl.isUnmodifiedArguments());
impl.isUnmodifiedBySanitization = true;
// String is no longer dangerous
Map<String, Object> identicalMap = impl.sanitizeMapAndRecordMutation(dangerous, new EnvVars());
Assert.assertEquals(identicalMap, dangerous);
Assert.assertTrue(impl.isUnmodifiedArguments());
// Lists
List unsanitizedList = Arrays.asList("cheese", null, secretUsername);
List sanitized = (List) impl.sanitizeListAndRecordMutation(unsanitizedList, env);
Assert.assertEquals(3, sanitized.size());
Assert.assertFalse(impl.isUnmodifiedArguments());
Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, sanitized.get(2));
impl.isUnmodifiedBySanitization = true;
Assert.assertEquals(unsanitizedList, impl.sanitizeObjectAndRecordMutation(unsanitizedList, new EnvVars()));
Assert.assertEquals(unsanitizedList, impl.sanitizeListAndRecordMutation(unsanitizedList, new EnvVars()));
}
Aggregations