Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-23468] Added UsernamePasswordMultiBinding.
- Loading branch information
Showing
10 changed files
with
249 additions
and
4 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
...main/java/org/jenkinsci/plugins/credentialsbinding/impl/UsernamePasswordMultiBinding.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/resources/org/jenkinsci/plugins/credentialsbinding/Binding/config-variables.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
3 changes: 2 additions & 1 deletion
3
src/main/resources/org/jenkinsci/plugins/credentialsbinding/impl/Messages.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
33 changes: 33 additions & 0 deletions
33
...insci/plugins/credentialsbinding/impl/UsernamePasswordMultiBinding/config-variables.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
3 changes: 3 additions & 0 deletions
3
...i/plugins/credentialsbinding/impl/UsernamePasswordMultiBinding/help-passwordVariable.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div> | ||
Name of an environment variable to be set to the username during the build. | ||
</div> |
3 changes: 3 additions & 0 deletions
3
...i/plugins/credentialsbinding/impl/UsernamePasswordMultiBinding/help-usernameVariable.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div> | ||
Name of an environment variable to be set to the username during the build. | ||
</div> |
8 changes: 8 additions & 0 deletions
8
...rces/org/jenkinsci/plugins/credentialsbinding/impl/UsernamePasswordMultiBinding/help.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
71 changes: 71 additions & 0 deletions
71
.../java/org/jenkinsci/plugins/credentialsbinding/impl/UsernamePasswordMultiBindingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |