Repository: GoogleContainerTools/jib
File: jib-core/src/main/java/com/google/cloud/tools/jib/http/Connection.java (Java)

Code before:
   * @throws GeneralSecurityException if unable to turn off TLS peer verification
   * @return {@link Connection} factory, a function that generates a {@link Connection} to a URL
   */
  public static Function<URL, Connection> getInsecureConnectionFactory()
      throws GeneralSecurityException {
    // Do not use {@link NetHttpTransport}. See {@link getConnectionFactory} for details.
    HttpTransport transport = new ApacheHttpTransport.Builder().doNotValidateCertificate().build();
    addProxyCredentials(transport);
    return url -> new Connection(url, transport);
  }

  private static void addProxyCredentials(HttpTransport transport) {
    DefaultHttpClient httpClient =
        (DefaultHttpClient) ((ApacheHttpTransport) transport).getHttpClient();

    boolean httpProxy = System.getProperty("http.proxyHost") != null;
    boolean httpCreds = System.getProperty("http.proxyUser") != null;
    boolean httpsProxy = System.getProperty("https.proxyHost") != null;
    boolean httpsCreds = System.getProperty("https.proxyUser") != null;
    if (httpProxy && httpCreds) {
      httpClient
          .getCredentialsProvider()
          .setCredentials(
              new AuthScope(
                  System.getProperty("http.proxyHost"),
                  Integer.parseInt(System.getProperty("http.proxyPort"))),
              new UsernamePasswordCredentials(
                  System.getProperty("http.proxyUser"), System.getProperty("http.proxyPassword")));
    }
    if (httpsProxy && (httpsCreds || httpCreds)) {
      httpClient
          .getCredentialsProvider()
          .setCredentials(
              new AuthScope(
                  System.getProperty("https.proxyHost"),
                  Integer.parseInt(System.getProperty("https.proxyPort"))),
              new UsernamePasswordCredentials(
                  httpsCreds
                      ? System.getProperty("https.proxyUser")
                      : System.getProperty("http.proxyUser"),
                  httpsCreds
                      ? System.getProperty("https.proxyPassword")
                      : System.getProperty("http.proxyPassword")));
    }
  }

  private HttpRequestFactory requestFactory;

  @Nullable private HttpResponse httpResponse;

  /** The URL to send the request to. */

Review comment (security):
Per https://github.com/GoogleContainerTools/jib/pull/1337#issuecomment-447107670, I think the logic should be

```java
    if (httpProxy && httpCreds) {
      httpClient.getCredentialsProvider().setCredentials(
          new AuthScope(http.proxyHost, http.proxyPort),
          new UsernamePasswordCredentials(http.proxyUser, http.proxyPassword));
    }
    if (httpsProxy && httpsCreds) {
      httpClient.getCredentialsProvider().setCredentials(
          new AuthScope(https.proxyHost, https.proxyPort),
          new UsernamePasswordCredentials(https.proxyUser, https.proxyPassword));
    }
```
`http.proxyHost` and `https.proxyHost` are independent, and I don't think it makes sense to set `http.proxyUser` for `https.proxyHost`. Agreed?

Code after:
   * @throws GeneralSecurityException if unable to turn off TLS peer verification
   * @return {@link Connection} factory, a function that generates a {@link Connection} to a URL
   */
  public static Function<URL, Connection> getInsecureConnectionFactory()
      throws GeneralSecurityException {
    // Do not use {@link NetHttpTransport}. See {@link getConnectionFactory} for details.
    HttpTransport transport = new ApacheHttpTransport.Builder().doNotValidateCertificate().build();
    addProxyCredentials(transport);
    return url -> new Connection(url, transport);
  }

  /**
   * Registers proxy credentials onto transport client, in order to deal with proxies that require
   * basic authentication.
   *
   * @param transport
   */
  private static void addProxyCredentials(HttpTransport transport) {
    DefaultHttpClient httpClient =
        (DefaultHttpClient) ((ApacheHttpTransport) transport).getHttpClient();

    boolean httpProxy = System.getProperty("http.proxyHost") != null;
    boolean httpCreds =
        System.getProperty("http.proxyUser") != null
            && System.getProperty("http.proxyPassword") != null;
    if (httpProxy && httpCreds) {
      httpClient
          .getCredentialsProvider()
          .setCredentials(
              new AuthScope(
                  System.getProperty("http.proxyHost"),
                  Integer.parseInt(System.getProperty("http.proxyPort", "8080"))),
              new UsernamePasswordCredentials(
                  System.getProperty("http.proxyUser"), System.getProperty("http.proxyPassword")));
    }
    boolean httpsProxy = System.getProperty("https.proxyHost") != null;
    boolean httpsCreds =
        System.getProperty("https.proxyUser") != null
            && System.getProperty("https.proxyPassword") != null;
    if (httpsProxy && httpsCreds) {
      httpClient
          .getCredentialsProvider()
          .setCredentials(
              new AuthScope(
                  System.getProperty("https.proxyHost"),
                  Integer.parseInt(System.getProperty("https.proxyPort", "443"))),
              new UsernamePasswordCredentials(
                  System.getProperty("https.proxyUser"),
                  System.getProperty("https.proxyPassword")));
    }
  }