Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
[JENKINS-23468] Permit multiple variables per binding (not yet used).
- Loading branch information
Showing
with
192 additions
and 111 deletions.
- +2 −17 pom.xml
- +40 −41 src/main/java/org/jenkinsci/plugins/credentialsbinding/Binding.java
- +2 −2 src/main/java/org/jenkinsci/plugins/credentialsbinding/BindingDescriptor.java
- +98 −0 src/main/java/org/jenkinsci/plugins/credentialsbinding/MultiBinding.java
- +14 −5 src/main/java/org/jenkinsci/plugins/credentialsbinding/impl/FileBinding.java
- +15 −14 src/main/java/org/jenkinsci/plugins/credentialsbinding/impl/SecretBuildWrapper.java
- +4 −3 src/main/java/org/jenkinsci/plugins/credentialsbinding/impl/StringBinding.java
- +4 −3 src/main/java/org/jenkinsci/plugins/credentialsbinding/impl/UsernamePasswordBinding.java
- +2 −26 src/main/java/org/jenkinsci/plugins/credentialsbinding/impl/ZipFileBinding.java
- +11 −0 src/test/java/org/jenkinsci/plugins/credentialsbinding/impl/UsernamePasswordBindingTest.java
@@ -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; | ||
|
||
import com.cloudbees.plugins.credentials.CredentialsProvider; | ||
import com.cloudbees.plugins.credentials.common.StandardCredentials; | ||
import hudson.ExtensionPoint; | ||
import hudson.FilePath; | ||
import hudson.Launcher; | ||
import hudson.model.AbstractDescribableImpl; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import javax.annotation.Nonnull; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
/** | ||
* A way of binding a kind of credentials to an environment variable during a build. | ||
* @param <C> a kind of credentials | ||
*/ | ||
public abstract class MultiBinding<C extends StandardCredentials> extends AbstractDescribableImpl<MultiBinding<C>> implements ExtensionPoint { | ||
|
||
private final String credentialsId; | ||
|
||
/** For use with {@link DataBoundConstructor}. */ | ||
protected MultiBinding(String credentialsId) { | ||
this.credentialsId = credentialsId; | ||
} | ||
|
||
/** Type token. */ | ||
protected abstract Class<C> type(); | ||
|
||
/** Identifier of the credentials to be bound. */ | ||
public final String getCredentialsId() { | ||
return credentialsId; | ||
} | ||
|
||
/** Callback for processing during a build. */ | ||
public interface MultiEnvironment { | ||
|
||
/** Produces the value of the environment variables. */ | ||
Map<String,String> values(); | ||
|
||
/** Performs any needed cleanup. */ | ||
void unbind() throws IOException, InterruptedException; | ||
|
||
} | ||
|
||
/** Sets up bindings for a build. */ | ||
public abstract MultiEnvironment bind(@Nonnull Run<?,?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws IOException, InterruptedException; | ||
|
||
/** Defines keys expected to be set in {@link MultiEnvironment#values}, particularly any that might be sensitive. */ | ||
public abstract Set<String> variables(); | ||
|
||
/** | ||
* Looks up the actual credentials. | ||
* @param build the build. | ||
* @return the credentials | ||
* @throws FileNotFoundException if the credentials could not be found (for convenience, rather than returning null) | ||
*/ | ||
protected final @Nonnull C getCredentials(@Nonnull Run<?,?> build) throws IOException { | ||
C c = CredentialsProvider.findCredentialById(credentialsId, type(), build); | ||
if (c != null) { | ||
return c; | ||
} | ||
throw new FileNotFoundException(credentialsId); | ||
} | ||
|
||
@Override public BindingDescriptor<C> getDescriptor() { | ||
return (BindingDescriptor<C>) super.getDescriptor(); | ||
} | ||
|
||
} |
Oops, something went wrong.