Skip to content

Commit 8e6d479

Browse files
authored
Adapt tests to upstream KeyStore validation (#861)
* adapt tests to upstream KeyStore validation * keep codeql happy in test code 🤮
1 parent ad359b3 commit 8e6d479

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketAuthenticatorTest.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
import com.cloudbees.plugins.credentials.Credentials;
77
import com.cloudbees.plugins.credentials.CredentialsMatchers;
88
import com.cloudbees.plugins.credentials.CredentialsScope;
9+
import com.cloudbees.plugins.credentials.SecretBytes;
910
import com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl;
1011
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
11-
import edu.umd.cs.findbugs.annotations.NonNull;
12+
import java.io.ByteArrayOutputStream;
13+
import java.security.KeyStore;
1214
import java.util.Collections;
1315
import java.util.List;
16+
import java.util.UUID;
1417
import jenkins.authentication.tokens.api.AuthenticationTokenContext;
1518
import jenkins.authentication.tokens.api.AuthenticationTokens;
1619
import org.junit.ClassRule;
@@ -57,7 +60,7 @@ public void authenticationContextTest() {
5760
}
5861

5962
@Test
60-
public void passwordCredentialsTest() {
63+
public void passwordCredentialsTest() throws Exception {
6164
List<Credentials> list = Collections.<Credentials>singletonList(new UsernamePasswordCredentialsImpl(
6265
CredentialsScope.SYSTEM, "dummy", "dummy", "user", "pass"));
6366
AuthenticationTokenContext ctx = BitbucketAuthenticator.authenticationContext((null));
@@ -69,9 +72,11 @@ public void passwordCredentialsTest() {
6972
}
7073

7174
@Test
72-
public void certCredentialsTest() {
75+
public void certCredentialsTest() throws Exception {
76+
// random password in test code to keep code-ql happy 🤮
77+
String password = UUID.randomUUID().toString();
7378
List<Credentials> list = Collections.<Credentials>singletonList(new CertificateCredentialsImpl(
74-
CredentialsScope.SYSTEM, "dummy", "dummy", "password", new DummyKeyStoreSource()));
79+
CredentialsScope.SYSTEM, "dummy", "dummy", password, new DummyKeyStoreSource(password)));
7580

7681
AuthenticationTokenContext ctx = BitbucketAuthenticator.authenticationContext(null);
7782
Credentials c = CredentialsMatchers.firstOrNull(list, AuthenticationTokens.matcher(ctx));
@@ -87,15 +92,20 @@ public void certCredentialsTest() {
8792
assertThat(AuthenticationTokens.convert(ctx, c), notNullValue());
8893
}
8994

90-
private static class DummyKeyStoreSource extends CertificateCredentialsImpl.KeyStoreSource {
91-
@NonNull
92-
@Override
93-
public byte[] getKeyStoreBytes() { return new byte[0]; }
95+
private static class DummyKeyStoreSource extends CertificateCredentialsImpl.UploadedKeyStoreSource {
9496

95-
@Override
96-
public long getKeyStoreLastModified() { return 0; }
97+
DummyKeyStoreSource(String password) throws Exception {
98+
super(null, dummyPKCS12Store(password));
99+
}
100+
101+
private static SecretBytes dummyPKCS12Store(String password) throws Exception {
102+
KeyStore ks = KeyStore.getInstance("PKCS12");
103+
ks.load(null, password.toCharArray());
104+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
105+
ks.store(bos, password.toCharArray());
106+
return SecretBytes.fromBytes(bos.toByteArray());
107+
}
97108

98-
@Override
99-
public boolean isSnapshotSource() { return true; }
100109
}
110+
101111
}

0 commit comments

Comments
 (0)