Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic Auth support for the remote GAV retrieval during uber jar creation in Payara Micro / FISH-1304 #5164

Merged
merged 1 commit into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
*/
package fish.payara.micro.impl;

import fish.payara.deployment.util.URIUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -247,7 +249,7 @@ public void buildUberJar() {
if ("file".equalsIgnoreCase(deploymentURI.getScheme())) {
Files.copy(Paths.get(deploymentURI), jos);
} else {
try (InputStream is = deploymentURI.toURL().openStream()) {
try (InputStream is = URIUtils.openHttpConnection(deploymentURI).getInputStream()) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
//Portions Copyright [2016-2018] [Payara Foundation]
//Portions Copyright [2016-2021] [Payara Foundation]

package com.sun.enterprise.admin.cli.embeddable;

import com.sun.enterprise.util.io.FileUtils;
import fish.payara.deployment.util.URIUtils;
import org.glassfish.admin.payload.PayloadFilesManager;
import org.glassfish.admin.payload.PayloadImpl;
import org.glassfish.api.ActionReport;
Expand All @@ -50,11 +51,11 @@
import org.glassfish.api.admin.Payload;
import org.glassfish.embeddable.Deployer;
import org.glassfish.embeddable.GlassFishException;
import org.jvnet.hk2.annotations.ContractsProvided;

import org.jvnet.hk2.annotations.Service;
import org.glassfish.hk2.api.PerLookup;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.internal.api.InternalSystemAdministrator;
import org.jvnet.hk2.annotations.ContractsProvided;
import org.jvnet.hk2.annotations.Service;

import javax.inject.Inject;
import java.io.ByteArrayInputStream;
Expand All @@ -65,16 +66,12 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.internal.api.InternalSystemAdministrator;

/**
* This is an implementation of {@link Deployer}.
Expand Down Expand Up @@ -177,7 +174,7 @@ public Collection<String> getDeployedApplications() throws GlassFishException {
CommandExecutorImpl executer = habitat.getService(CommandExecutorImpl.class);
ActionReport report = executer.executeCommand("list-components");
Properties props = report.getTopMessagePart().getProps();
return new ArrayList<String>(props.stringPropertyNames());
return new ArrayList<>(props.stringPropertyNames());
} catch (Exception e) {
throw new GlassFishException(e);
}
Expand All @@ -188,15 +185,7 @@ private File convertToFile(URI archive) throws IOException {
if ("file".equalsIgnoreCase(archive.getScheme())) {
file = new File(archive);
} else {
URL urlArchive = archive.toURL();
String auth = urlArchive.getUserInfo();
HttpURLConnection httpConnection = (HttpURLConnection) urlArchive.openConnection();
if (auth != null) {
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());
httpConnection.setRequestProperty("Authorization", "Basic " + encodedAuth);
}

file = createFile(httpConnection.getInputStream());
file = createFile(URIUtils.openHttpConnection(archive).getInputStream());
}
return file;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2021] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.deployment.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.Base64;

/**
* General URI manipulation utilities.
*
* @author avpinchuk
*/
public final class URIUtils {
// Suppress default constructor
private URIUtils() {
throw new AssertionError();
}

/**
* Returns a {@link HttpURLConnection} instance that represents a connection to the
* remote object referred by the {@code uri}.
* <p/>
* The {@code uri} must represent a HTTP(S) resource.
* <p/>
* If {@code uri} contains the user info part, set the <em>Authorization</em> header
* with Basic authentication credentials.
*
* @param uri the {@code URI} to open connection from.
* @return A {@code HttpURLConnection} to the {@code uri}.
* @throws IOException if an I/O exception occurs.
* @throws ClassCastException if the connection is not instance of {@code HttpURLConnection}.
*/
public static HttpURLConnection openHttpConnection(URI uri) throws IOException {
URL url = uri.toURL();
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
String userInfo = url.getUserInfo();
if (userInfo != null) {
String encodedUserInfo = Base64.getEncoder().encodeToString(userInfo.getBytes());
httpConnection.setRequestProperty("Authorization", "Basic " + encodedUserInfo);
}
return httpConnection;
}
}