Skip to content

allow file to be null for use in CasC #11

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
15 changes: 14 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,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>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.configuration-as-code</groupId>
<artifactId>configuration-as-code-support</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public final class FileCredentialsImpl extends BaseStandardCredentials implement
*/
@Nonnull
private final SecretBytes secretBytes;

/**
* The legacy encrypted version of the secret bytes.
*/
Expand Down Expand Up @@ -137,10 +138,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,35 @@
package org.jenkinsci.plugins.plaincredentials;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.SecretBytes;
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 {
SecretBytes.decrypt(null); // force class loading to get Stapler converter registered
Copy link
Member

Choose a reason for hiding this comment

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

This does not look right. You are referring to SecretBytes.StaplerConverterImpl? I am not sure what normally causes it to be registered, but if it is not being registered implicitly by the rest of the test case, something is broken and that needs to be fixed first.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have no idea why it doesn't get loaded, static initializer isn't executed as demonstrated running with a debugger. No idea what could go wrong here.

Copy link
Member

Choose a reason for hiding this comment

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

According to this code, the converter is used by virtue of its class name. That would mean that the abovementioned line could be deleted and the test would still pass.

Copy link
Member

Choose a reason for hiding this comment

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

And jenkinsci/configuration-as-code-plugin#317 does not have such a trick, so…?

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==