Skip to content

Commit

Permalink
[JENKINS-23468] Added UsernamePasswordMultiBinding.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Jan 8, 2015
1 parent 595d5b9 commit f35be17
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 4 deletions.
@@ -0,0 +1,98 @@
/*
* The MIT License
*
* Copyright 2015 Jesse Glick.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.credentialsbinding.impl;

import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Run;
import hudson.model.TaskListener;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.jenkinsci.plugins.credentialsbinding.BindingDescriptor;
import org.jenkinsci.plugins.credentialsbinding.MultiBinding;
import org.kohsuke.stapler.DataBoundConstructor;

public class UsernamePasswordMultiBinding extends MultiBinding<StandardUsernamePasswordCredentials> {

private final String usernameVariable;
private final String passwordVariable;

@DataBoundConstructor public UsernamePasswordMultiBinding(String usernameVariable, String passwordVariable, String credentialsId) {
super(credentialsId);
this.usernameVariable = usernameVariable;
this.passwordVariable = passwordVariable;
}

public String getUsernameVariable() {
return usernameVariable;
}

public String getPasswordVariable() {
return passwordVariable;
}

@Override protected Class<StandardUsernamePasswordCredentials> type() {
return StandardUsernamePasswordCredentials.class;
}

@Override public MultiEnvironment bind(Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
StandardUsernamePasswordCredentials credentials = getCredentials(build);
final String username = credentials.getUsername();
final String password = credentials.getPassword().getPlainText();
return new MultiEnvironment() {
@Override public Map<String,String> values() {
Map<String,String> m = new HashMap<String,String>();
m.put(usernameVariable, username);
m.put(passwordVariable, password);
return m;
}
@Override public void unbind() throws IOException, InterruptedException {}
};
}

@Override public Set<String> variables() {
return new HashSet<String>(Arrays.asList(usernameVariable, passwordVariable));
}

@Extension public static class DescriptorImpl extends BindingDescriptor<StandardUsernamePasswordCredentials> {

@Override protected Class<StandardUsernamePasswordCredentials> type() {
return StandardUsernamePasswordCredentials.class;
}

@Override public String getDisplayName() {
return Messages.UsernamePasswordMultiBinding_username_and_password();
}

}

}
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright 2013 jglick.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler" xmlns:c="/lib/credentials">
<f:entry title="${%Variable}" field="variable">
<f:textbox/>
</f:entry>
</j:jelly>
Expand Up @@ -24,9 +24,7 @@ THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler" xmlns:c="/lib/credentials">
<f:entry title="${%Variable}" field="variable">
<f:textbox/>
</f:entry>
<st:include page="config-variables.jelly"/>
<f:entry title="${%Credentials}" field="credentialsId">
<c:select expressionAllowed="true"/>
</f:entry>
Expand Down
@@ -1,5 +1,6 @@
FileBinding.secret_file=Secret file
SecretBuildWrapper.use_secret_text_s_or_file_s_=Use secret text(s) or file(s)
StringBinding.secret_text=Secret text
UsernamePasswordBinding.username_and_password=Username and password
UsernamePasswordBinding.username_and_password=Username and password (conjoined)
UsernamePasswordMultiBinding.username_and_password=Username and password (separated)
ZipFileBinding.secret_zip_file=Secret ZIP file
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright 2013 jglick.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler" xmlns:c="/lib/credentials">
<f:entry title="${%Username Variable}" field="usernameVariable">
<f:textbox/>
</f:entry>
<f:entry title="${%Password Variable}" field="passwordVariable">
<f:textbox/>
</f:entry>
</j:jelly>
@@ -0,0 +1,3 @@
<div>
Name of an environment variable to be set to the username during the build.
</div>
@@ -0,0 +1,3 @@
<div>
Name of an environment variable to be set to the username during the build.
</div>
@@ -0,0 +1,8 @@
<div>
Sets one variable to the username and one variable to the password given in the credentials.
</div>
<div>
<strong>Warning</strong>: if the master or slave node has multiple executors,
any other build running concurrently on the same node will be able to read
the text of the secret, for example on Linux using <code>ps e</code>.
</div>
@@ -0,0 +1,71 @@
/*
* The MIT License
*
* Copyright 2015 Jesse Glick.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.credentialsbinding.impl;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.tasks.Shell;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;
import org.jenkinsci.plugins.credentialsbinding.MultiBinding;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.jvnet.hudson.test.JenkinsRule;

public class UsernamePasswordMultiBindingTest {

@Rule public JenkinsRule r = new JenkinsRule();

@Test public void basics() throws Exception {
String username = "bob";
String password = "s3cr3t";
UsernamePasswordCredentialsImpl c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, null, "sample", username, password);
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c);
FreeStyleProject p = r.createFreeStyleProject();
p.getBuildWrappersList().add(new SecretBuildWrapper(Collections.<MultiBinding<?>>singletonList(new UsernamePasswordMultiBinding("user", "pass", c.getId()))));
p.getBuildersList().add(new Shell("set +x\necho $user/$pass > auth.txt"));
r.configRoundtrip(p);
SecretBuildWrapper wrapper = p.getBuildWrappersList().get(SecretBuildWrapper.class);
assertNotNull(wrapper);
List<? extends MultiBinding<?>> bindings = wrapper.getBindings();
assertEquals(1, bindings.size());
MultiBinding<?> binding = bindings.get(0);
assertEquals(c.getId(), binding.getCredentialsId());
assertEquals(UsernamePasswordMultiBinding.class, binding.getClass());
assertEquals("user", ((UsernamePasswordMultiBinding) binding).getUsernameVariable());
assertEquals("pass", ((UsernamePasswordMultiBinding) binding).getPasswordVariable());
FreeStyleBuild b = r.buildAndAssertSuccess(p);
r.assertLogNotContains(password, b);
assertEquals(username + '/' + password, b.getWorkspace().child("auth.txt").readToString().trim());
assertEquals("[pass, user]", new TreeSet<String>(b.getSensitiveBuildVariables()).toString());
}

}

0 comments on commit f35be17

Please sign in to comment.