Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into ekerwin_INTGEN-392
  • Loading branch information
Eric Kerwin committed May 24, 2016
2 parents 1fb8df6 + 1230619 commit 18345a8
Show file tree
Hide file tree
Showing 8 changed files with 443 additions and 258 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
public class HubSupportHelper implements Serializable {
private static final long serialVersionUID = 6440466357358359056L;

public static final String DEFAULT_CLI_DOWNLOAD = "scan.cli.zip";
public static final String WINDOWS_CLI_DOWNLOAD = "scan.cli-windows.zip";
public static final String MAC_CLI_DOWNLOAD = "scan.cli-macosx.zip";

private boolean hasBeenChecked = false;
private boolean hub3_0Support = false;

Expand Down Expand Up @@ -216,23 +220,23 @@ public static String getLinuxCLIWrapperLink(final String hubUrl) throws IllegalA
final String baseUrl = getCLIWrapperLink(hubUrl);
final StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(baseUrl);
urlBuilder.append("scan.cli.zip");
urlBuilder.append(DEFAULT_CLI_DOWNLOAD);
return urlBuilder.toString();
}

public static String getWindowsCLIWrapperLink(final String hubUrl) throws IllegalArgumentException {
final String baseUrl = getCLIWrapperLink(hubUrl);
final StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(baseUrl);
urlBuilder.append("scan.cli-windows.zip");
urlBuilder.append(WINDOWS_CLI_DOWNLOAD);
return urlBuilder.toString();
}

public static String getOSXCLIWrapperLink(final String hubUrl) throws IllegalArgumentException {
final String baseUrl = getCLIWrapperLink(hubUrl);
final StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(baseUrl);
urlBuilder.append("scan.cli-macosx.zip");
urlBuilder.append(MAC_CLI_DOWNLOAD);
return urlBuilder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -50,6 +48,7 @@
import com.blackducksoftware.integration.hub.exception.BDRestException;
import com.blackducksoftware.integration.hub.exception.HubIntegrationException;
import com.blackducksoftware.integration.hub.logging.IntLogger;
import com.blackducksoftware.integration.hub.util.AuthenticatorUtil;

public class CLIInstaller {
public static final String VERSION_FILE_NAME = "hubVersion.txt";
Expand Down Expand Up @@ -167,13 +166,16 @@ public String getCLIDownloadUrl(final IntLogger logger, final HubIntRestService
return HubSupportHelper.getLinuxCLIWrapperLink(restService.getBaseUrl());
}
} catch (final URISyntaxException e) {
logger.error(e.getMessage(), e);
if (logger != null) {
logger.error(e.getMessage(), e);
}
}
return null;

}

private boolean customInstall(final URL archive, String hubVersion, final String localHostName, final IntLogger logger) throws IOException, InterruptedException,
public void customInstall(final URL archive, String hubVersion, final String localHostName,
final IntLogger logger) throws IOException, InterruptedException,
HubIntegrationException {

try {
Expand All @@ -198,6 +200,7 @@ private boolean customInstall(final URL archive, String hubVersion, final String
final FileWriter writer = new FileWriter(hubVersionFile);
writer.write(hubVersion);
writer.close();
hubVersionFile.setLastModified(0L);
}
final long cliTimestamp = hubVersionFile.lastModified();

Expand All @@ -211,18 +214,9 @@ private boolean customInstall(final URL archive, String hubVersion, final String
if (proxy != null) {

if (StringUtils.isNotBlank(proxyUserName) && StringUtils.isNotBlank(proxyPassword)) {
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
proxyUserName,
proxyPassword.toCharArray());
}
}
);
AuthenticatorUtil.setAuthenticator(proxyUserName, proxyPassword);
} else {
Authenticator.setDefault(null);
AuthenticatorUtil.resetAuthenticator();
}
}
if (proxy != null) {
Expand All @@ -234,12 +228,12 @@ public PasswordAuthentication getPasswordAuthentication() {
connection.connect();
} catch (final IOException ioe) {
logger.error("Skipping installation of " + archive + " to " + directoryToInstallTo.getCanonicalPath() + ": " + ioe.toString());
return false;
return;
}

if (connection instanceof HttpURLConnection
&& ((HttpURLConnection) connection).getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
return false; // CLI has not been modified
return; // CLI has not been modified
}

final long sourceTimestamp = connection.getLastModified();
Expand All @@ -248,7 +242,7 @@ public PasswordAuthentication getPasswordAuthentication() {
if (cliInstallDirectory.exists() && cliInstallDirectory.listFiles().length > 0) {
if (!cliMismatch && sourceTimestamp == cliTimestamp)
{
return false; // already up to date
return; // already up to date
}
// delete directory contents
deleteFilesRecursive(cliInstallDirectory.listFiles());
Expand All @@ -268,7 +262,7 @@ public PasswordAuthentication getPasswordAuthentication() {
throw new IOException(String.format("Failed to unpack %s (%d bytes read of total %d)",
archive, cis.getByteCount(), connection.getContentLength()), e);
}
return true;
return;
} catch (final IOException e) {
throw new IOException("Failed to install " + archive + " to " + directoryToInstallTo.getCanonicalPath(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

import java.io.IOException;
import java.io.Serializable;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
Expand All @@ -39,6 +37,7 @@
import com.blackducksoftware.integration.hub.encryption.PasswordDecrypter;
import com.blackducksoftware.integration.hub.exception.EncryptionException;
import com.blackducksoftware.integration.hub.exception.HubIntegrationException;
import com.blackducksoftware.integration.hub.util.AuthenticatorUtil;

public class HubProxyInfo implements Serializable {

Expand Down Expand Up @@ -95,19 +94,13 @@ public boolean shouldUseProxyForUrl(final URL url) {

public void setDefaultAuthenticator() {
if (getUsername() != null && getEncryptedPassword() != null) {
Authenticator.setDefault(new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
try {
return new PasswordAuthentication(getProxyCredentials().getUsername(),
PasswordDecrypter.decrypt(getProxyCredentials().getEncryptedPassword()).toCharArray());
} catch (final Exception e) {
}
return null;
}
});
try {
AuthenticatorUtil.setAuthenticator(getProxyCredentials().getUsername(),
PasswordDecrypter.decrypt(getProxyCredentials().getEncryptedPassword()));
} catch (final Exception e) {
}
} else {
Authenticator.setDefault(null);
AuthenticatorUtil.resetAuthenticator();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*******************************************************************************
* Copyright (C) 2016 Black Duck Software, Inc.
* http://www.blackducksoftware.com/
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*******************************************************************************/
package com.blackducksoftware.integration.hub.util;

import java.lang.reflect.Constructor;
import java.net.Authenticator;
import java.net.PasswordAuthentication;

import org.apache.commons.lang3.StringUtils;

public class AuthenticatorUtil {

public static void resetAuthenticator() {
Authenticator.setDefault(null);

attemptResetProxyCache();
}

public static void setAuthenticator(final String proxyUser, final String proxyPassword) {
if (!StringUtils.isBlank(proxyUser) && !StringUtils.isBlank(proxyPassword)) {
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType() == RequestorType.PROXY) {
return new PasswordAuthentication(proxyUser, proxyPassword.toCharArray());
}
return null;
}
});
}
}

private static void attemptResetProxyCache() {
try {
Class<?> sunAuthCacheValue;
Class<?> sunAuthCache;
Class<?> sunAuthCacheImpl;
try {
sunAuthCacheValue = Class.forName("sun.net.www.protocol.http.AuthCacheValue");
sunAuthCache = Class.forName("sun.net.www.protocol.http.AuthCache");
sunAuthCacheImpl = Class.forName("sun.net.www.protocol.http.AuthCacheImpl");
} catch (final Exception e) {
// Must not be using a JDK with sun classes so we abandon this
// reset since it is sun specific
return;
}

final java.lang.reflect.Method m = sunAuthCacheValue.getDeclaredMethod("setAuthCache", sunAuthCache);

final Constructor<?> authCacheImplConstr = sunAuthCacheImpl.getConstructor();
final Object authCachImp = authCacheImplConstr.newInstance();

m.invoke(null, authCachImp);
} catch (final Exception e) {
System.err.println(e.toString());
}
}
}

0 comments on commit 18345a8

Please sign in to comment.