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

adapt tests to upstream KeyStore validation #861

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SecretBytes;
import com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.ByteArrayOutputStream;
import java.security.KeyStore;
import java.util.Collections;
import java.util.List;
import jenkins.authentication.tokens.api.AuthenticationTokenContext;
Expand Down Expand Up @@ -57,7 +59,7 @@
}

@Test
public void passwordCredentialsTest() {
public void passwordCredentialsTest() throws Exception {
List<Credentials> list = Collections.<Credentials>singletonList(new UsernamePasswordCredentialsImpl(
CredentialsScope.SYSTEM, "dummy", "dummy", "user", "pass"));
AuthenticationTokenContext ctx = BitbucketAuthenticator.authenticationContext((null));
Expand All @@ -69,9 +71,9 @@
}

@Test
public void certCredentialsTest() {
public void certCredentialsTest() throws Exception {
List<Credentials> list = Collections.<Credentials>singletonList(new CertificateCredentialsImpl(
CredentialsScope.SYSTEM, "dummy", "dummy", "password", new DummyKeyStoreSource()));
CredentialsScope.SYSTEM, "dummy", "dummy", "password", new DummyKeyStoreSource("password")));
jtnord marked this conversation as resolved.
Show resolved Hide resolved

AuthenticationTokenContext ctx = BitbucketAuthenticator.authenticationContext(null);
Credentials c = CredentialsMatchers.firstOrNull(list, AuthenticationTokens.matcher(ctx));
Expand All @@ -87,15 +89,20 @@
assertThat(AuthenticationTokens.convert(ctx, c), notNullValue());
}

private static class DummyKeyStoreSource extends CertificateCredentialsImpl.KeyStoreSource {
@NonNull
@Override
public byte[] getKeyStoreBytes() { return new byte[0]; }
private static class DummyKeyStoreSource extends CertificateCredentialsImpl.UploadedKeyStoreSource {

@Override
public long getKeyStoreLastModified() { return 0; }
DummyKeyStoreSource(String password) throws Exception {
super(null, dummyPKCS12Store(password));
}

private static SecretBytes dummyPKCS12Store(String password) throws Exception {
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(null, password.toCharArray());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ks.store(bos, password.toCharArray());
return SecretBytes.fromBytes(bos.toByteArray());
}

@Override
public boolean isSnapshotSource() { return true; }
}

}