Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Util;
import hudson.remoting.Channel;
import hudson.util.Secret;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.security.GeneralSecurityException;
import java.util.Arrays;
Expand Down Expand Up @@ -78,7 +80,8 @@
*/
private static final Logger LOGGER = Logger.getLogger(SecretBytes.class.getName());
/**
* The encrypted bytes.
* The (usually) encrypted bytes.
* Will be in plain text during remoting calls.
*/
@NonNull
private final byte[] value;
Expand Down Expand Up @@ -119,6 +122,14 @@
}
}

/**
* Construct a SecretBytes with the specified value (that may or may not be encrypted) that is used raw without any transformation.
* @param value explicit value to use for the data. No encryption or dencryption is performed on this value and it is used as is.
*/
private SecretBytes(@NonNull byte[] value) {
this.value = value;
}

/**
* Returns the raw unencrypted data. The caller is responsible for zeroing out the returned {@code byte[]} after
* use.
Expand Down Expand Up @@ -343,6 +354,23 @@
return s == null ? "" : s.toString();
}

protected Object writeReplace() throws ObjectStreamException {
if (Channel.current() == null) {

Check warning on line 358 in src/main/java/com/cloudbees/plugins/credentials/SecretBytes.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 358 is only partially covered, one branch is missing
return this;

Check warning on line 359 in src/main/java/com/cloudbees/plugins/credentials/SecretBytes.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 359 is not covered by tests
}
// we need plain text during remoting serialisation as the remote side will not have the confidential key
return new SecretBytes(this.getPlainData());
}

protected Object readResolve() throws ObjectStreamException {
if (Channel.current() == null) {
return this;
}
// re-encrypt the byte array after serialisation.
// The remote side may have its own confidential key (for Jenkins to Jenkins remoting, or will use the mock key for an agent)
return SecretBytes.fromRawBytes(value);

Check warning on line 371 in src/main/java/com/cloudbees/plugins/credentials/SecretBytes.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 366-371 are not covered by tests
}

/**
* Our XStream converter.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
*/
package com.cloudbees.plugins.credentials;

import hudson.slaves.DumbSlave;
import java.nio.charset.StandardCharsets;
import java.util.Random;
import jenkins.model.Jenkins;
import jenkins.security.MasterToSlaveCallable;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -163,4 +167,29 @@ void largeRawString__chunking__urlSafe() {
}


@WithJenkins
@Test
public void serialisationOverRemoting(JenkinsRule r) throws Exception {
final byte[] data = new byte[] {0x01,0x02,0x03,0x04,0x05};
final SecretBytes localSecretBytes = SecretBytes.fromRawBytes(data);
DumbSlave onlineSlave = r.createOnlineSlave();
onlineSlave.getChannel().call(new CheckSecretBytesCallable(localSecretBytes,data));
}

private static class CheckSecretBytesCallable extends MasterToSlaveCallable<Void, AssertionError> {

private SecretBytes sb;
private byte[] expectedUnencryptedValue;

CheckSecretBytesCallable(SecretBytes sb, byte[] expectedUnencryptedValue) {
this.sb = sb;
this.expectedUnencryptedValue = expectedUnencryptedValue;
}

@Override
public Void call() throws AssertionError {
assertThat(sb.getPlainData(), is(expectedUnencryptedValue));
Copy link
Member Author

Choose a reason for hiding this comment

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

prior to the fix this errored with

java.io.IOException: Remote call on slave0 failed
	at hudson.remoting.Channel.call(Channel.java:1112)
	at com.cloudbees.plugins.credentials.SecretBytesTest.serialisationOverRemoting(SecretBytesTest.java:176)
Caused by: java.lang.Error: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
	at app//com.cloudbees.plugins.credentials.SecretBytes.getPlainData(SecretBytes.java:141)
	at app//com.cloudbees.plugins.credentials.SecretBytesTest$CheckSecretBytesCallable.call(SecretBytesTest.java:191)
	at app//com.cloudbees.plugins.credentials.SecretBytesTest$CheckSecretBytesCallable.call(SecretBytesTest.java:179)
	at hudson.remoting.UserRequest.perform(UserRequest.java:225)
	at hudson.remoting.UserRequest.perform(UserRequest.java:50)
	at hudson.remoting.Request$2.run(Request.java:391)
	at hudson.remoting.InterceptingExecutorService.lambda$wrap$0(InterceptingExecutorService.java:81)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
	at java.base/java.lang.Thread.run(Thread.java:1583)
	Suppressed: hudson.remoting.Channel$CallSiteStackTrace: Remote call to slave0
		at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1916)
		at hudson.remoting.UserRequest$ExceptionResponse.retrieve(UserRequest.java:384)
		at hudson.remoting.Channel.call(Channel.java:1108)
		at com.cloudbees.plugins.credentials.SecretBytesTest.serialisationOverRemoting(SecretBytesTest.java:176)
Caused by: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
	at java.base/com.sun.crypto.provider.CipherCore.unpad(CipherCore.java:861)
	at java.base/com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:941)
	at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:734)
	at java.base/com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
	at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2244)
	at app//com.cloudbees.plugins.credentials.SecretBytes.getPlainData(SecretBytes.java:139)
	... 10 more

return null;
}
}
}
Loading