Skip to content

Commit

Permalink
Merge pull request #3093 from dwnusbaum/JENKINS-47448
Browse files Browse the repository at this point in the history
[JENKINS-47448] Fix JDKInstaller for downloads requiring an Oracle login
  • Loading branch information
oleg-nenashev committed Oct 21, 2017
2 parents 987b604 + 36ef5d5 commit 136fe82
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 79 deletions.
31 changes: 28 additions & 3 deletions core/src/main/java/hudson/tools/JDKInstaller.java
Expand Up @@ -48,6 +48,7 @@
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
Expand Down Expand Up @@ -476,17 +477,41 @@ public URL locate(TaskListener log, Platform platform, CPU cpu) throws IOExcepti
throw new IOException("Failed to request " + m.getURI() +" exit code="+r);

if (m.getURI().getHost().equals("login.oracle.com")) {
/* Login flow:
* 1. /oaam_server/oamLoginPage.jsp: Form for username + password. Submit action is:
* 2. /oaam_server/login.do: Returns a 302 to:
* 3. /oaam_server/loginAuth.do: After 2 seconds, JS sets window.location to:
* 4. /oaam_server/authJump.do: Contains a single form with hidden inputs and JS that submits the form to:
* 5. /oam/server/dap/cred_submit: Returns a 302 to:
* 6. https://edelivery.oracle.com/osso_login_success: Returns a 302 to the download.
*/
if (m.getURI().getPath().contains("/loginAuth.do")) { // You are redirected to this page immediately after logging in.
try {
Thread.sleep(2000); // Oracle website waits 2 seconds after logging in before redirecting.
m.releaseConnection();
m = new GetMethod(new URI(m.getURI(), "/oaam_server/authJump.do?jump=false", true).toString());
continue;
} catch (InterruptedException x) {
throw new IOException("Interrupted while logging in", x);
}
}

LOGGER.fine("Appears to be a login page");
String resp = IOUtils.toString(m.getResponseBodyAsStream(), m.getResponseCharSet());
m.releaseConnection();
Matcher pm = Pattern.compile("<form .*?action=\"([^\"]*)\" .*?</form>", Pattern.DOTALL).matcher(resp);
Matcher pm = Pattern.compile("<form .*?action=\"([^\"]*)\".*?</form>", Pattern.DOTALL).matcher(resp);
if (!pm.find())
throw new IllegalStateException("Unable to find a form in the response:\n"+resp);

String form = pm.group();
PostMethod post = new PostMethod(
new URL(new URL(m.getURI().getURI()),pm.group(1)).toExternalForm());

if (m.getURI().getPath().contains("/authJump.do")) {
m = post;
continue;
}

String u = getDescriptor().getUsername();
Secret p = getDescriptor().getPassword();
if (u==null || p==null) {
Expand All @@ -498,9 +523,9 @@ public URL locate(TaskListener log, Platform platform, CPU cpu) throws IOExcepti
String n = extractAttribute(fragment,"name");
String v = extractAttribute(fragment,"value");
if (n==null || v==null) continue;
if (n.equals("ssousername"))
if (n.equals("userid"))
v = u;
if (n.equals("password")) {
if (n.equals("pass")) {
v = p.getPlainText();
if (authCount++ > 3) {
log.hyperlink(getCredentialPageUrl(),"Your Oracle account doesn't appear valid. Please specify a valid username/password\n");
Expand Down
76 changes: 0 additions & 76 deletions test/src/test/java/hudson/tools/JDKInstallerTest.java
Expand Up @@ -15,28 +15,20 @@
import org.junit.Rule;
import org.junit.Test;
import hudson.model.JDK;
import hudson.model.FreeStyleProject;
import hudson.model.FreeStyleBuild;
import hudson.model.TaskListener;
import hudson.tasks.Shell;
import hudson.util.StreamTaskListener;
import hudson.tools.JDKInstaller.Platform;
import hudson.tools.JDKInstaller.CPU;
import hudson.FilePath;
import hudson.Functions;
import hudson.Launcher.LocalLauncher;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;
import java.util.logging.Logger;

import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.xml.sax.SAXException;

/**
* @author Kohsuke Kawaguchi
Expand Down Expand Up @@ -101,74 +93,6 @@ public void configRoundtrip() throws Exception {
j.assertEqualBeans(installer, isp.installers.get(JDKInstaller.class), "id,acceptLicense");
}

/**
* Can we locate the bundles?
*/
@Test
public void locate() throws Exception {
Assume.assumeTrue("this is a really time consuming test, so only run it when we really want", Boolean.getBoolean("jenkins.testJDKInstaller"));

retrieveUpdateCenterData();

JDKInstaller i = new JDKInstaller("jdk-7u3-oth-JPR", true);
StreamTaskListener listener = StreamTaskListener.fromStdout();
i.locate(listener, Platform.LINUX, CPU.i386);
i.locate(listener, Platform.WINDOWS, CPU.amd64);
i.locate(listener, Platform.SOLARIS, CPU.Sparc);
}

private void retrieveUpdateCenterData() throws IOException, SAXException {
j.createWebClient().goTo("/"); // make sure data is loaded
}

/**
* Tests the auto installation.
*/
@Test
public void autoInstallation6u13() throws Exception {
doTestAutoInstallation("jdk-6u13-oth-JPR@CDS-CDS_Developer", "1.6.0_13-b03");
}

/**
* JDK7 is distributed as a gzip file
*/
@Test
public void autoInstallation7() throws Exception {
doTestAutoInstallation("jdk-7-oth-JPR", "1.7.0-b147");
}

@Test
@Issue("JENKINS-3989")
public void autoInstallation142_17() throws Exception {
doTestAutoInstallation("j2sdk-1.4.2_17-oth-JPR@CDS-CDS_Developer", "1.4.2_17-b06");
}

/**
* End-to-end installation test.
*/
private void doTestAutoInstallation(String id, String fullversion) throws Exception {
Assume.assumeTrue("this is a really time consuming test, so only run it when we really want", Boolean.getBoolean("jenkins.testJDKInstaller"));

retrieveUpdateCenterData();

JDKInstaller installer = new JDKInstaller(id, true);

JDK jdk = new JDK("test", tmp.getRoot().getAbsolutePath(), Arrays.asList(
new InstallSourceProperty(Arrays.<ToolInstaller>asList(installer))));

j.jenkins.getJDKs().add(jdk);

FreeStyleProject p = j.createFreeStyleProject();
p.setJDK(jdk);
p.getBuildersList().add(new Shell("java -fullversion\necho $JAVA_HOME"));
FreeStyleBuild b = j.buildAndAssertSuccess(p);
@SuppressWarnings("deprecation") String log = b.getLog();
System.out.println(log);
// make sure it runs with the JDK that just got installed
assertTrue(log.contains(fullversion));
assertTrue(log.contains(tmp.getRoot().getAbsolutePath()));
}

/**
* Fake installation on Unix.
*/
Expand Down

0 comments on commit 136fe82

Please sign in to comment.