Skip to content

Commit

Permalink
Replace MD5 digest with SHA256 while logging payload in Mock class ca…
Browse files Browse the repository at this point in the history
…lled from tests (#8319)
  • Loading branch information
SujathaH committed Aug 11, 2023
1 parent 7f7f865 commit 4a262f5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
25 changes: 25 additions & 0 deletions core/src/main/java/hudson/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -1927,4 +1927,29 @@ public static long daysElapsedSince(@NonNull Date date) {
private static PathRemover newPathRemover(@NonNull PathRemover.PathChecker pathChecker) {
return PathRemover.newFilteredRobustRemover(pathChecker, DELETION_RETRIES, GC_AFTER_FAILED_DELETE, WAIT_BETWEEN_DELETION_RETRIES);
}

/**
* Returns SHA-256 Digest of input bytes
*/
@Restricted(NoExternalUse.class)
public static byte[] getSHA256DigestOf(@NonNull byte[] input) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(input);
return messageDigest.digest();
} catch (NoSuchAlgorithmException noSuchAlgorithmException) {

Check warning on line 1940 in core/src/main/java/hudson/Util.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 1940 is not covered by tests
throw new IllegalStateException("SHA-256 could not be instantiated, but is required to" +

Check warning on line 1941 in core/src/main/java/hudson/Util.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 1941 is not covered by tests
" be implemented by the language specification", noSuchAlgorithmException);
}
}

/**
* Returns Hex string of SHA-256 Digest of passed input
*/
@Restricted(NoExternalUse.class)
public static String getHexOfSHA256DigestOf(byte[] input) throws IOException {
//get hex string of sha 256 of payload
byte[] payloadDigest = Util.getSHA256DigestOf(input);
return (payloadDigest != null) ? Util.toHexString(payloadDigest) : null;

Check warning on line 1953 in core/src/main/java/hudson/Util.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1953 is only partially covered, one branch is missing
}
}
6 changes: 4 additions & 2 deletions core/src/main/java/jenkins/security/ConfidentialStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,16 @@ void clear() {

@Override
protected void store(ConfidentialKey key, byte[] payload) throws IOException {
LOGGER.fine(() -> "storing " + key.getId() + " " + Util.getDigestOf(Util.toHexString(payload)));
//called only from tests, get hex string of sha 256 for logging payload
LOGGER.fine("storing " + key.getId() + " " + Util.getHexOfSHA256DigestOf(payload));
data.put(key.getId(), payload);
}

@Override
protected byte[] load(ConfidentialKey key) throws IOException {
byte[] payload = data.get(key.getId());
LOGGER.fine(() -> "loading " + key.getId() + " " + (payload != null ? Util.getDigestOf(Util.toHexString(payload)) : "null"));
//called only from tests, get hex string of sha 256 for logging payload
LOGGER.fine("loading " + key.getId() + " " + (payload != null ? Util.getHexOfSHA256DigestOf(payload) : "null"));
return payload;
}

Expand Down
19 changes: 19 additions & 0 deletions core/src/test/java/hudson/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -726,6 +727,24 @@ public void ifOverriddenFailure() {
assertEquals("The class " + DerivedClassFailure.class.getName() + " must override at least one of the BaseClass.method methods", error.getMessage());
}

@Test
public void testGetHexOfSHA256DigestOf() throws IOException {
byte[] input = new byte[] {12, 34, 16};
String str = Util.getHexOfSHA256DigestOf(input);
assertEquals(str, "134fefbd329986726407a5208107ef07c9e33da779f5068bff191733268fe997");
}

@Test
public void testGetSHA256DigestOf() {
byte[] input = new byte[] {12, 34, 16};
byte[] sha256DigestActual = Util.getSHA256DigestOf(input);

byte[] expected = new byte[]
{ 19, 79, -17, -67, 50, -103, -122, 114, 100, 7, -91, 32, -127, 7, -17, 7, -55, -29, 61, -89, 121, -11,
6, -117, -1, 25, 23, 51, 38, -113, -23, -105};
assertArrayEquals(expected, sha256DigestActual);
}

public static class BaseClass {
protected String method() {
return "base";
Expand Down

0 comments on commit 4a262f5

Please sign in to comment.