Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent null pointer when exporting file credentials #1282

Merged
merged 7 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions integrations/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>2.3.2-rc745.235a70dd17df</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ssh-credentials</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
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 {
Expand Down Expand Up @@ -67,6 +67,31 @@ public void testDomainScopedCredentials() {
assertEquals("secret", creds.get(0).getPassword().getPlainText());
}

@Test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add

@Issue("JENKINS-60467")

IIUC

@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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ credentials:
privateKeySource:
directEntry:
privateKey: sp0ds9d+skkfjf
- file:
scope: GLOBAL
id: "secret-file"
fileName: "mysecretfile.txt"
secretBytes: WJjZAo=
Original file line number Diff line number Diff line change
Expand Up @@ -279,21 +279,24 @@ public CNode describe(T instance, ConfigurationContext context) throws Exception
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be formatted differently it looks odd.

.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);
Expand Down