|
| 1 | +package io.jenkins.plugins.gitlabserverconfig.servers; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.containsString; |
| 5 | +import static org.hamcrest.Matchers.equalTo; |
| 6 | +import static org.hamcrest.Matchers.hasItem; |
| 7 | +import static org.hamcrest.Matchers.hasSize; |
| 8 | +import static org.hamcrest.Matchers.not; |
| 9 | + |
| 10 | +import com.cloudbees.plugins.credentials.Credentials; |
| 11 | +import com.cloudbees.plugins.credentials.CredentialsMatchers; |
| 12 | +import com.cloudbees.plugins.credentials.CredentialsProvider; |
| 13 | +import com.cloudbees.plugins.credentials.domains.DomainRequirement; |
| 14 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 15 | +import edu.umd.cs.findbugs.annotations.Nullable; |
| 16 | +import hudson.diagnosis.OldDataMonitor; |
| 17 | +import hudson.model.ItemGroup; |
| 18 | +import hudson.security.ACL; |
| 19 | +import java.util.List; |
| 20 | +import java.util.logging.Level; |
| 21 | +import jenkins.model.Jenkins; |
| 22 | +import org.acegisecurity.Authentication; |
| 23 | +import org.jenkinsci.plugins.plaincredentials.StringCredentials; |
| 24 | +import org.junit.Rule; |
| 25 | +import org.junit.Test; |
| 26 | +import org.jvnet.hudson.test.JenkinsRule; |
| 27 | +import org.jvnet.hudson.test.LoggerRule; |
| 28 | +import org.jvnet.hudson.test.TestExtension; |
| 29 | +import org.jvnet.hudson.test.recipes.LocalData; |
| 30 | + |
| 31 | +public class GitLabServersTest { |
| 32 | + @Rule |
| 33 | + public LoggerRule logger = |
| 34 | + new LoggerRule().record(OldDataMonitor.class, Level.FINE).capture(50); |
| 35 | + |
| 36 | + @Rule |
| 37 | + public JenkinsRule j = new JenkinsRule(); |
| 38 | + |
| 39 | + @LocalData |
| 40 | + @Test |
| 41 | + public void migrationToCredentials() throws Throwable { |
| 42 | + // LocalData creating using the following: |
| 43 | + /* |
| 44 | + GitLabServer server = new GitLabServer("http://localhost", "my-server", null); |
| 45 | + server.setSecretToken(Secret.fromString("s3cr3t!")); |
| 46 | + GitLabServers.get().addServer(server); |
| 47 | + */ |
| 48 | + var server = GitLabServers.get().getServers().stream() |
| 49 | + .filter(s -> s.getName().equals("my-server")) |
| 50 | + .findFirst() |
| 51 | + .orElseThrow(); |
| 52 | + var credentialsId = server.getWebhookSecretCredentialsId(); |
| 53 | + var credentials = CredentialsMatchers.filter( |
| 54 | + CredentialsProvider.lookupCredentialsInItemGroup(StringCredentials.class, Jenkins.get(), ACL.SYSTEM2), |
| 55 | + CredentialsMatchers.withId(credentialsId)); |
| 56 | + assertThat(credentials, hasSize(1)); |
| 57 | + assertThat(credentials.get(0).getSecret().getPlainText(), equalTo("s3cr3t!")); |
| 58 | + assertThat( |
| 59 | + logger.getMessages(), not(hasItem(containsString("Trouble loading " + GitLabServers.class.getName())))); |
| 60 | + } |
| 61 | + |
| 62 | + @TestExtension("migrationToCredentials") |
| 63 | + public static class CredentialsProviderThatRequiresDescriptorLookup extends CredentialsProvider { |
| 64 | + @Override |
| 65 | + public <C extends Credentials> List<C> getCredentials( |
| 66 | + @NonNull Class<C> type, |
| 67 | + @Nullable ItemGroup itemGroup, |
| 68 | + @Nullable Authentication authentication, |
| 69 | + @NonNull List<DomainRequirement> domainRequirements) { |
| 70 | + // Prior to fix, this caused the GitLabServer migration code to recurse infinitely, causing problems when |
| 71 | + // starting Jenkins. |
| 72 | + // In practice this was caused by a lookup of another descriptor type, but I am using GitLabServers for |
| 73 | + // clarity. |
| 74 | + Jenkins.get().getDescriptor(GitLabServers.class); |
| 75 | + return List.of(); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments