|
| 1 | +package hudson.model; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.containsString; |
| 5 | +import static org.hamcrest.Matchers.equalTo; |
| 6 | +import static org.hamcrest.Matchers.not; |
| 7 | +import static org.hamcrest.Matchers.notNullValue; |
| 8 | + |
| 9 | +import hudson.util.Secret; |
| 10 | +import jakarta.servlet.ServletRequest; |
| 11 | +import java.net.URL; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import jenkins.model.Jenkins; |
| 14 | +import org.apache.commons.io.IOUtils; |
| 15 | +import org.htmlunit.HttpMethod; |
| 16 | +import org.htmlunit.Page; |
| 17 | +import org.htmlunit.WebRequest; |
| 18 | +import org.htmlunit.html.HtmlForm; |
| 19 | +import org.htmlunit.html.HtmlPage; |
| 20 | +import org.htmlunit.xml.XmlPage; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.jvnet.hudson.test.Issue; |
| 24 | +import org.jvnet.hudson.test.JenkinsRule; |
| 25 | +import org.jvnet.hudson.test.MockAuthorizationStrategy; |
| 26 | +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; |
| 27 | +import org.jvnet.hudson.test.recipes.LocalData; |
| 28 | + |
| 29 | +@SuppressWarnings("deprecation") |
| 30 | +@Issue("SECURITY-783") |
| 31 | +@WithJenkins |
| 32 | +public class BuildAuthorizationTokenMigrationTest { |
| 33 | + private static final String OLDTOKEN = "oldtoken"; |
| 34 | + private static final String ADMIN_USERNAME = "alice"; |
| 35 | + private static final String VERSION_NUMBER = "2.528.3"; // TODO Finalize version number |
| 36 | + private static final String JOB_NAME = "my_freestyle_job"; |
| 37 | + private static final String OLD_DATA_PAGE_URL = "administrativeMonitor/OldData/manage"; |
| 38 | + private static final String JOB_WITHOUT_TOKEN_NAME = "job_without_token"; |
| 39 | + public static final String EXTENDED_READER_USERNAME = "bob"; |
| 40 | + |
| 41 | + // This should be a JenkinsSessionRule / RealJenkinsRule, to ensure no monitor after startup, |
| 42 | + // but it looks like neither works (anymore) with @LocalData/@WithLocalData. |
| 43 | + private JenkinsRule j; |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + public void setUp(JenkinsRule j) { |
| 47 | + this.j = j; |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + @LocalData |
| 52 | + void basicMigration() throws Throwable { |
| 53 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 54 | + j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy() |
| 55 | + .grant(Jenkins.READ).everywhere().toEveryone() |
| 56 | + .grant(Item.READ, Item.EXTENDED_READ).everywhere().to(EXTENDED_READER_USERNAME) |
| 57 | + .grant(Jenkins.ADMINISTER).everywhere().to(ADMIN_USERNAME)); |
| 58 | + j.jenkins.save(); |
| 59 | + |
| 60 | + final FreeStyleProject fs = j.jenkins.getItemByFullName(JOB_NAME, FreeStyleProject.class); |
| 61 | + |
| 62 | + { |
| 63 | + // inspect original migrated token |
| 64 | + assertThat(fs.getConfigFile().asString(), containsString("<authToken>" + OLDTOKEN + "</authToken>")); |
| 65 | + |
| 66 | + final BuildAuthorizationToken authToken = fs.getAuthToken(); |
| 67 | + assertThat(authToken, notNullValue()); |
| 68 | + assertThat(authToken.getToken(), equalTo(OLDTOKEN)); |
| 69 | + assertThat(authToken.getEncryptedToken().getPlainText(), equalTo(OLDTOKEN)); |
| 70 | + |
| 71 | + // Not redacted yet |
| 72 | + try (JenkinsRule.WebClient wc = j.createWebClient().login(EXTENDED_READER_USERNAME)) { |
| 73 | + final XmlPage xmlPage = wc.goToXml(fs.getUrl() + "config.xml"); |
| 74 | + assertThat(xmlPage.getWebResponse().getContentAsString(), containsString("<authToken>" + OLDTOKEN + "</authToken>")); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + assertThat(j.jenkins.getAdministrativeMonitor("OldData").isActivated(), equalTo(true)); |
| 79 | + |
| 80 | + try (JenkinsRule.WebClient wc = j.createWebClient().login(ADMIN_USERNAME)) { |
| 81 | + { |
| 82 | + final HtmlPage oldDataPage = wc.goTo(OLD_DATA_PAGE_URL); |
| 83 | + final String oldDataContent = oldDataPage.getWebResponse().getContentAsString(); |
| 84 | + assertThat(oldDataContent, containsString(JOB_NAME)); |
| 85 | + assertThat(oldDataContent, not(containsString(JOB_WITHOUT_TOKEN_NAME))); |
| 86 | + |
| 87 | + assertThat(oldDataContent, containsString(VERSION_NUMBER)); |
| 88 | + assertThat(oldDataContent, not(containsString("No old data"))); |
| 89 | + |
| 90 | + final HtmlForm form = oldDataPage.getFormByName("oldDataUpgrade"); |
| 91 | + form.submit(form.getButtonByName("Submit")); |
| 92 | + } |
| 93 | + { |
| 94 | + final HtmlPage oldDataPage = wc.goTo(OLD_DATA_PAGE_URL); |
| 95 | + final String oldDataContent = oldDataPage.getWebResponse().getContentAsString(); |
| 96 | + assertThat(oldDataContent, not(containsString(JOB_NAME))); |
| 97 | + // TODO Add the following assertion back when we change the version string to something not appearing everywhere on the page: |
| 98 | + //assertThat(oldDataContent, not(containsString(VERSION_NUMBER))); |
| 99 | + assertThat(oldDataContent, containsString("No old data")); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + assertThat(j.jenkins.getAdministrativeMonitor("OldData").isActivated(), equalTo(false)); |
| 104 | + |
| 105 | + { |
| 106 | + // New auth token |
| 107 | + final BuildAuthorizationToken authToken = fs.getAuthToken(); |
| 108 | + assertThat(authToken, notNullValue()); |
| 109 | + assertThat(authToken.getToken(), equalTo(OLDTOKEN)); |
| 110 | + final Secret newEncryptedToken = authToken.getEncryptedToken(); |
| 111 | + assertThat(newEncryptedToken.getPlainText(), equalTo(OLDTOKEN)); |
| 112 | + |
| 113 | + assertThat(fs.getConfigFile().asString(), not(containsString("<authToken>" + OLDTOKEN + "</authToken>"))); |
| 114 | + assertThat(fs.getConfigFile().asString(), containsString("<authToken>" + newEncryptedToken.getEncryptedValue() + "</authToken>")); |
| 115 | + |
| 116 | + // Redacted after save |
| 117 | + try (JenkinsRule.WebClient wc = j.createWebClient().login(EXTENDED_READER_USERNAME)) { |
| 118 | + final XmlPage xmlPage = wc.goToXml(fs.getUrl() + "config.xml"); |
| 119 | + assertThat(xmlPage.getWebResponse().getContentAsString(), containsString("<authToken>********</authToken>")); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void testPostConfigXmlAndClearing() throws Exception { |
| 126 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 127 | + j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().grant(Jenkins.READ).everywhere().toEveryone().grant(Jenkins.ADMINISTER).everywhere().to(ADMIN_USERNAME)); |
| 128 | + |
| 129 | + assertThat(j.jenkins.getAdministrativeMonitor("OldData").isActivated(), equalTo(false)); |
| 130 | + |
| 131 | + final FreeStyleProject freeStyleProject = j.createFreeStyleProject(); |
| 132 | + |
| 133 | + try (JenkinsRule.WebClient wc = j.createWebClient().login(ADMIN_USERNAME)) { |
| 134 | + { |
| 135 | + // POST config.xml with plain token |
| 136 | + final WebRequest webRequest = new WebRequest(new URL(j.getURL(), freeStyleProject.getUrl() + "config.xml?" + |
| 137 | + j.jenkins.getCrumbIssuer().getDescriptor().getCrumbRequestField() + "=" + j.jenkins.getCrumbIssuer().getCrumb((ServletRequest) null)), |
| 138 | + HttpMethod.POST); |
| 139 | + webRequest.setAdditionalHeader("Content-Type", "application/xml"); |
| 140 | + webRequest.setRequestBody(IOUtils.resourceToString("/" + getClass().getName().replace(".", "/") + "/job-config-with-plain-token.xml", StandardCharsets.UTF_8)); |
| 141 | + final Page apiPage = wc.getPage(webRequest); |
| 142 | + assertThat(apiPage.getWebResponse().getStatusCode(), equalTo(200)); |
| 143 | + } |
| 144 | + |
| 145 | + assertThat(j.jenkins.getAdministrativeMonitor("OldData").isActivated(), equalTo(true)); |
| 146 | + |
| 147 | + { |
| 148 | + // Testing that GET config.xml does not clear the in-memory flag via ConverterImpl |
| 149 | + final XmlPage xmlPage = wc.goToXml(freeStyleProject.getUrl() + "config.xml"); |
| 150 | + assertThat(xmlPage.getWebResponse().getContentAsString(), containsString("plain_token")); |
| 151 | + |
| 152 | + assertThat(freeStyleProject.getConfigFile().asString(), containsString("plain_token")); |
| 153 | + } |
| 154 | + |
| 155 | + // Saving clears the admin monitor |
| 156 | + freeStyleProject.save(); |
| 157 | + assertThat(j.jenkins.getAdministrativeMonitor("OldData").isActivated(), equalTo(false)); |
| 158 | + |
| 159 | + { |
| 160 | + final XmlPage xmlPage = wc.goToXml(freeStyleProject.getUrl() + "config.xml"); |
| 161 | + assertThat(xmlPage.getWebResponse().getContentAsString(), not(containsString("plain_token"))); |
| 162 | + assertThat(freeStyleProject.getConfigFile().asString(), not(containsString("plain_token"))); |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments