mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-16 05:22:22 +00:00
[SECURITY-49] added a tool to re-key secrets
As an AdministrativeMonitor, it shows up in the manage Jenkins UI, and allows the administrator to run a re-keying operation.
This commit is contained in:
1 parent
a9aff088f3
commit
4895eaafca
11 files changed
+859
-22
No files matched your search
@@ -0,0 +1,33 @@
|
||||
package hudson.util;
|
||||
|
||||
import hudson.Util;
|
||||
import org.junit.rules.ExternalResource;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
/**
|
||||
* JUnit rule that cleans that sets a temporary {@link Secret#SECRET} value.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public class MockSecretRule extends ExternalResource {
|
||||
|
||||
private String value;
|
||||
|
||||
@Override
|
||||
protected void before() throws Throwable {
|
||||
byte[] random = new byte[32];
|
||||
sr.nextBytes(random);
|
||||
value = Util.toHexString(random);
|
||||
Secret.SECRET = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void after() {
|
||||
if (!Secret.SECRET.equals(value))
|
||||
throw new IllegalStateException("Someone tinkered with Secret.SECRET");
|
||||
Secret.SECRET = null;
|
||||
}
|
||||
|
||||
private static final SecureRandom sr = new SecureRandom();
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package hudson.util
|
||||
|
||||
import com.trilead.ssh2.crypto.Base64
|
||||
import hudson.FilePath
|
||||
import jenkins.security.ConfidentialStoreRule
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
|
||||
import javax.crypto.Cipher
|
||||
|
||||
import static hudson.Util.createTempDir
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
class SecretRewriterTest {
|
||||
@Rule
|
||||
public MockSecretRule mockSecretRule = new MockSecretRule()
|
||||
|
||||
@Rule
|
||||
public ConfidentialStoreRule confidentialStoreRule = new ConfidentialStoreRule();
|
||||
|
||||
@Test
|
||||
void singleFileRewrite() {
|
||||
def o = encryptOld('foobar') // old
|
||||
def n = encryptNew('foobar') // new
|
||||
roundtrip "<foo>${o}</foo>",
|
||||
"<foo>${n}</foo>"
|
||||
|
||||
roundtrip "<foo>${o}</foo><foo>${o}</foo>",
|
||||
"<foo>${n}</foo><foo>${n}</foo>"
|
||||
|
||||
roundtrip "<foo>${n}</foo>",
|
||||
"<foo>${n}</foo>"
|
||||
|
||||
roundtrip " <foo>thisIsLegalBase64AndLongEnoughThatItCouldLookLikeSecret</foo> ",
|
||||
" <foo>thisIsLegalBase64AndLongEnoughThatItCouldLookLikeSecret</foo> "
|
||||
|
||||
// to be rewritten, it needs to be between a tag
|
||||
roundtrip "<foo>$o", "<foo>$o"
|
||||
roundtrip "$o</foo>", "$o</foo>"
|
||||
|
||||
//
|
||||
roundtrip "<abc>\n<foo>$o</foo>\n</abc>", "<abc>\n<foo>$n</foo>\n</abc>"
|
||||
}
|
||||
|
||||
void roundtrip(String before, String after) {
|
||||
def sr = new SecretRewriter(null);
|
||||
def f = File.createTempFile("test","xml");
|
||||
try {
|
||||
f.text = before
|
||||
sr.rewrite(f,null)
|
||||
assert after.trim()==f.text.trim()
|
||||
} finally {
|
||||
f.delete()
|
||||
}
|
||||
}
|
||||
|
||||
String encryptOld(str) {
|
||||
def cipher = Secret.getCipher("AES");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, Secret.legacyKey);
|
||||
return new String(Base64.encode(cipher.doFinal((str + Secret.MAGIC).getBytes("UTF-8"))))
|
||||
}
|
||||
|
||||
String encryptNew(str) {
|
||||
return Secret.fromString(str).encryptedValue
|
||||
}
|
||||
|
||||
/**
|
||||
* Directory rewrite and recursion detection
|
||||
*/
|
||||
@Test
|
||||
void recursionDetection() {
|
||||
def backup = createTempDir()
|
||||
def sw = new SecretRewriter(backup);
|
||||
def st = StreamTaskListener.fromStdout()
|
||||
|
||||
def o = encryptOld("Hello world")
|
||||
def n = encryptNew("Hello world")
|
||||
def payload = "<msg>$o</msg>"
|
||||
def answer = "<msg>$n</msg>"
|
||||
|
||||
// set up some directories with stuff
|
||||
def t = createTempDir()
|
||||
def dirs = ["a", "b", "c", "c/d", "c/d/e"]
|
||||
dirs.each { p ->
|
||||
def d = new File(t, p)
|
||||
d.mkdir()
|
||||
new File(d,"foo.xml").text = payload
|
||||
}
|
||||
|
||||
// stuff outside
|
||||
def t2 = createTempDir()
|
||||
new File(t2,"foo.xml").text = payload
|
||||
|
||||
// some recursions as well as valid symlinks
|
||||
new FilePath(t).child("c/symlink").symlinkTo("..",st)
|
||||
new FilePath(t).child("b/symlink").symlinkTo(".",st)
|
||||
new FilePath(t).child("a/symlink").symlinkTo(t2.absolutePath,st)
|
||||
|
||||
assert 6==sw.rewriteRecursive(t, st)
|
||||
|
||||
dirs.each { p->
|
||||
assert new File(t,"$p/foo.xml").text.trim()==answer
|
||||
assert new File(backup,"$p/foo.xml").text.trim()==payload
|
||||
}
|
||||
|
||||
// t2 is only reachable by following a symlink. this should be covered, too
|
||||
assert new File(t2,"foo.xml").text.trim()==answer.trim();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -26,15 +26,10 @@ package hudson.util
|
||||
import com.trilead.ssh2.crypto.Base64;
|
||||
import jenkins.model.Jenkins
|
||||
import jenkins.security.ConfidentialStoreRule;
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import hudson.Util;
|
||||
|
||||
/**
|
||||
* @author Kohsuke Kawaguchi
|
||||
@@ -43,18 +38,8 @@ public class SecretTest {
|
||||
@Rule
|
||||
public ConfidentialStoreRule confidentialStore = new ConfidentialStoreRule()
|
||||
|
||||
@Test @Before
|
||||
void setUp() {
|
||||
def sr = new SecureRandom();
|
||||
byte[] random = new byte[32];
|
||||
sr.nextBytes(random);
|
||||
Secret.SECRET = Util.toHexString(random);
|
||||
}
|
||||
|
||||
@Test @After
|
||||
void tearDown() {
|
||||
Secret.SECRET = null;
|
||||
}
|
||||
@Rule
|
||||
public MockSecretRule mockSecretRule = new MockSecretRule()
|
||||
|
||||
@Test
|
||||
void testEncrypt() {
|
||||
|
||||
Reference in new issue
Block a user