Prevent null pointer when exporting file credentials (#1282)

* Prevent nullpointer when exporting file credentials

* Use incremental version

* Reformat

* Fix enforcer

* Fix encoding

* bump credentials

Co-authored-by: Joseph Petersen <josephp90@gmail.com>
This commit is contained in:
Tim Jacomb 2020-02-27 22:07:35 +00:00 committed by GitHub
parent 95a195df96
commit 31595ec63e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 14 deletions

View File

@ -158,6 +158,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ssh-credentials</artifactId>
@ -344,6 +350,11 @@
<version>1.23</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.7</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>jackson2-api</artifactId>

View File

@ -23,11 +23,11 @@ import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import static java.util.Objects.requireNonNull;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class CredentialsTest {
@ -67,6 +67,31 @@ public class CredentialsTest {
assertEquals("secret", creds.get(0).getPassword().getPlainText());
}
@Test
@ConfiguredWithCode("GlobalCredentials.yml")
public void testExportFileCredentials() throws Exception {
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
ConfigurationContext context = new ConfigurationContext(registry);
CredentialsRootConfigurator root = ExtensionList.lookupSingleton(CredentialsRootConfigurator.class);
CNode node = root.describe(root.getTargetComponent(context), context);
assertNotNull(node);
final Mapping mapping = node.asMapping();
Mapping fileCredential = mapping.get("system")
.asMapping()
.get("domainCredentials")
.asSequence().get(0)
.asMapping().get("credentials")
.asSequence().get(2)
.asMapping().get("file").asMapping();
assertThat(fileCredential.getScalarValue("scope"), is("GLOBAL"));
assertThat(fileCredential.getScalarValue("id"), is("secret-file"));
assertThat(fileCredential.getScalarValue("fileName"), is("mysecretfile.txt"));
assertThat(fileCredential.getScalarValue("secretBytes"), not("WJjZAo="));
}
@ConfiguredWithCode("GlobalCredentials.yml")
@Test
public void testExportSSHCredentials() throws Exception {

View File

@ -19,3 +19,8 @@ credentials:
privateKeySource:
directEntry:
privateKey: sp0ds9d+skkfjf
- file:
scope: GLOBAL
id: "secret-file"
fileName: "mysecretfile.txt"
secretBytes: "YWJjZGVmZwo="

View File

@ -279,21 +279,24 @@ public class DataBoundConfigurator<T> extends BaseConfigurator<T> {
for (int i = 0; i < parameters.length; i++) {
final Parameter p = parameters[i];
final Attribute a = createAttribute(names[i], TypePair.of(p));
Object value = a.getValue(instance);
if (value != null) {
Object converted = Stapler.CONVERT_UTILS.convert(value, a.getType());
if (converted instanceof Collection || p.getType().isArray() || !a.isMultiple()) {
args[i] = converted;
} else if (Set.class.isAssignableFrom(p.getType())) {
args[i] = Collections.singleton(converted);
} else {
args[i] = Collections.singletonList(converted);
if (a != null) {
Object value = a.getValue(instance);
if (value != null) {
Object converted = Stapler.CONVERT_UTILS.convert(value, a.getType());
if (converted instanceof Collection ||
p.getType().isArray() || !a.isMultiple()) {
args[i] = converted;
} else if (Set.class.isAssignableFrom(p.getType())) {
args[i] = Collections.singleton(converted);
} else {
args[i] = Collections.singletonList(converted);
}
}
if (args[i] == null && p.getType().isPrimitive()) {
args[i] = defaultValue(p.getType());
}
attributes[i] = a;
}
if (args[i] == null && p.getType().isPrimitive()) {
args[i] = defaultValue(p.getType());
}
attributes[i] = a;
}
T ref = (T) constructor.newInstance(args);