Skip to content

Commit

Permalink
Merge pull request #14 from jglick/null-file-for-casc
Browse files Browse the repository at this point in the history
Allow file to be null for JCasC
  • Loading branch information
jglick authored Dec 11, 2018
2 parents c08a375 + 54daada commit f766ff3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
18 changes: 16 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.28</version>
<version>3.29</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -56,6 +56,7 @@
<properties>
<jenkins.version>2.60.3</jenkins.version>
<java.level>8</java.level>
<configuration-as-code-plugin.version>1.3</configuration-as-code-plugin.version>
</properties>

<repositories>
Expand All @@ -75,7 +76,20 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>2.1.5</version>
<version>2.1.16</version>
</dependency>

<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>${configuration-as-code-plugin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.configuration-as-code</groupId>
<artifactId>configuration-as-code-support</artifactId>
<version>${configuration-as-code-plugin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ public FileCredentialsImpl(@CheckForNull CredentialsScope scope, @CheckForNull S
*/
@DataBoundConstructor
public FileCredentialsImpl(@CheckForNull CredentialsScope scope, @CheckForNull String id,
@CheckForNull String description, @Nonnull FileItem file, @CheckForNull String fileName,
@CheckForNull String description, @CheckForNull FileItem file, @CheckForNull String fileName,
@CheckForNull SecretBytes secretBytes) throws IOException {
super(scope, id, description);
String name = file.getName();
String name = file != null ? file.getName() : "";
if (name.length() > 0) {
this.fileName = name.replaceFirst("^.+[/\\\\]", "");
this.secretBytes = SecretBytes.fromBytes(file.get());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.jenkinsci.plugins.plaincredentials;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import hudson.security.ACL;
import io.jenkins.plugins.casc.ConfigurationAsCode;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

/**
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
public class ConfigurationAsCodeTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void should_configure_file_credentials() throws Exception {
ConfigurationAsCode.get().configure(getClass().getResource("ConfigurationAsCode.yaml").toString());
final FileCredentials credentials = CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(FileCredentials.class, j.jenkins, ACL.SYSTEM, (DomainRequirement) null),
CredentialsMatchers.withId("secret-file"));
Assert.assertNotNull(credentials);
Assert.assertEquals("Some secret file", credentials.getDescription());
Assert.assertEquals("my-secret-file", credentials.getFileName());
Assert.assertEquals("FOO_BAR", IOUtils.toString(credentials.getContent()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
credentials:
system:
domainCredentials:
# global credentials
- credentials:
- file:
id: secret-file
scope: SYSTEM
description: "Some secret file"
fileName: my-secret-file
# FOO_BAR base64 encoded
secretBytes: Rk9PX0JBUg==

0 comments on commit f766ff3

Please sign in to comment.