Skip to content

Commit

Permalink
Merge pull request #156 from coatrack/Proxy-path-variable-need-to-be-…
Browse files Browse the repository at this point in the history
…split-up-in-two-different-use-cases

update the way we retrieve the proxy template in order to offer the p…
  • Loading branch information
Francisco Pérez committed Oct 12, 2021
2 parents b37db24 + d6ab492 commit b0fe292
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 35 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
# - a CoatRack build requires some mail configuration parameters to be set
# - the configuration values in this file are just dummy/fallback values for development builds

mvn clean package -Dygg.mail.sender.user="" -Dygg.mail.sender.password="" -Dygg.mail.server.url="" -Dygg.mail.server.port=0 -Dygg.mail.sender.from=""
mvn clean package -Dygg.mail.sender.user="" -Dygg.mail.sender.password="" -Dygg.mail.server.url="" -Dygg.mail.server.port=0 -Dygg.mail.sender.from="" -Dygg.proxy.executable-jar.template.url="" -Dygg.proxy.executable-jar.generated.path=""
2 changes: 1 addition & 1 deletion run-admin-web-application.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ if [ ! -z $1 ]; then
VM_ARG_SPRING_PROFILE="-Dspring.profiles.active=$1"
fi

java -jar $VM_ARG_SPRING_PROFILE -Dygg.mail.sender.user=$2 -Dygg.mail.sender.password=$3 $COATRACK_JAR_FILE
java -jar $VM_ARG_SPRING_PROFILE -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y -Dygg.mail.sender.user=$2 -Dygg.mail.sender.password=$3 $COATRACK_JAR_FILE

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import eu.coatrack.admin.model.repository.ProxyRepository;
import eu.coatrack.admin.model.repository.ServiceApiRepository;
import eu.coatrack.admin.service.GitService;
import eu.coatrack.admin.util.CustomProxyFileGenerator;
import eu.coatrack.api.ApiKey;
import eu.coatrack.api.Proxy;
import org.eclipse.jgit.api.errors.GitAPIException;
Expand All @@ -49,12 +48,12 @@
import java.util.UUID;
import eu.coatrack.admin.logic.CreateProxyAction;
import eu.coatrack.admin.model.repository.UserRepository;
import eu.coatrack.admin.service.CustomProxyFileGeneratorService;
import eu.coatrack.api.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import org.springframework.web.client.HttpClientErrorException;

/**
* @author Timon Veenstra <tveenstra@bebr.nl>
Expand Down Expand Up @@ -86,7 +85,7 @@ public class AdminProxiesController {
private CreateProxyAction createProxyAction;

@Autowired
private CustomProxyFileGenerator customProxyFileGenerator;
private CustomProxyFileGeneratorService customProxyFileGenerator;

@Autowired
private GitService gitService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.coatrack.admin.util;
package eu.coatrack.admin.service;

/*-
* #%L
Expand All @@ -19,8 +19,9 @@
* limitations under the License.
* #L%
*/

import eu.coatrack.api.Proxy;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -30,32 +31,39 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import org.apache.commons.io.FileUtils;

/**
* Generates custom CoatRack proxy jars, which can then be downloaded and installed
* on API provider side
* Generates custom CoatRack proxy jars, which can then be downloaded and
* installed on API provider side
*
* @author gr-hovest(at)atb-bremen.de
*/
@Service
public class CustomProxyFileGenerator {

private static final Logger log = LoggerFactory.getLogger(CustomProxyFileGenerator.class);
public class CustomProxyFileGeneratorService {

@Value("${ygg.proxy.executable-jar.path}")
private String PATH_TO_YGG_PROXY_JAR;
private static final Logger log = LoggerFactory.getLogger(CustomProxyFileGeneratorService.class);

@Value("${ygg.proxy.executable-jar.filename-original-prefix}")
// **************** Config Properties ****************
@Value("${ygg.proxy.executable-jar.template.url}")
private String YGG_PROXY_FILENAME_ORIGINAL_PREFIX;

@Value("${ygg.proxy.executable-jar.generated.path}")
private String PATH_TO_GENERATED_YGG_PROXY_JAR;

@Value("${mvn.pom.project.version}")
private String MVN_POM_PROJECT_VERSION;

Expand All @@ -65,6 +73,13 @@ public class CustomProxyFileGenerator {
@Value("${ygg.proxy.executable-jar.filename-custom.suffix}")
private String YGG_PROXY_FILENAME_CUSTOM_SUFFIX;

@Value("${ygg.proxy.generate-bootstrap-properties.spring.application.name.prefix}")
private String CUSTOM_PROPERTIES_APPLICATION_NAME_PREFIX;

@Value("${ygg.proxy.generate-bootstrap-properties.spring.cloud.config.uri}")
private String CUSTOM_PROPERTIES_CLOUD_CONFIG_URI_VALUE;

// **************** Config Properties ****************
// Please note: the following also works on Windows because the JAR file API seems to use "/" as separator on every system
private static final String CUSTOM_CONFIG_FILE = "BOOT-INF/classes/bootstrap.properties";

Expand All @@ -75,37 +90,47 @@ public class CustomProxyFileGenerator {

private static final String CUSTOM_PROPERTIES_CREDENTIAL_PASSWORD = "spring.cloud.config.password";

@Value("${ygg.proxy.generate-bootstrap-properties.spring.application.name.prefix}")
private String CUSTOM_PROPERTIES_APPLICATION_NAME_PREFIX;

@Value("${ygg.proxy.generate-bootstrap-properties.spring.cloud.config.uri}")
private String CUSTOM_PROPERTIES_CLOUD_CONFIG_URI_VALUE;
/**
* This method generates the proxy file from an Proxy Entity using a jar
* template and overwriting specific properties
*
* @param proxy
* @return
* @throws java.net.MalformedURLException
*/
public File getCustomJarForDownload(Proxy proxy) throws MalformedURLException, IOException {

// ***
// Retrieve proxy template
// ***
URL urlProxyOriginalExecutableFile = new URL(
YGG_PROXY_FILENAME_ORIGINAL_PREFIX
+ MVN_POM_PROJECT_VERSION
+ ".jar");

Path localTemplateProxyJar = Files.createTempFile("proxy-template", ".jar");

public File getCustomJarForDownload(Proxy proxy) {
FileUtils.copyURLToFile(urlProxyOriginalExecutableFile, localTemplateProxyJar.toFile());

// ***
// Create generate proxy file
// ***
String newCustomProxyFilename = YGG_PROXY_FILENAME_CUSTOM_PREFIX + MVN_POM_PROJECT_VERSION + "-" + proxy.getId() + YGG_PROXY_FILENAME_CUSTOM_SUFFIX;

String proxyOriginalExecutableFilePath = PATH_TO_YGG_PROXY_JAR + File.separator
+ YGG_PROXY_FILENAME_ORIGINAL_PREFIX
+ MVN_POM_PROJECT_VERSION
+ ".jar";

log.debug("obtaining the original path from '{}'", proxyOriginalExecutableFilePath);

File proxyOriginalExecutableFile = new File(proxyOriginalExecutableFilePath);

File newCustomProxyFile = new File(
PATH_TO_YGG_PROXY_JAR + File.separator
PATH_TO_GENERATED_YGG_PROXY_JAR + File.separator
+ newCustomProxyFilename);

log.debug("generating new file '{}' based on original file '{}'", newCustomProxyFile, proxyOriginalExecutableFile);
log.debug("generating new file '{}' based on original file '{}'", newCustomProxyFile, localTemplateProxyJar.toFile());

// ***
// Prepare jar
// ***
try {
JarFile jarToCopy = new JarFile(proxyOriginalExecutableFile);
JarFile jarToCopy = new JarFile(localTemplateProxyJar.toFile());
Enumeration<JarEntry> entriesInsideJarToCopy = jarToCopy.entries();

JarInputStream originalJarInputStream = new JarInputStream(new FileInputStream(proxyOriginalExecutableFile));
JarInputStream originalJarInputStream = new JarInputStream(new FileInputStream(localTemplateProxyJar.toFile()));
JarOutputStream customJarOutputStream = new JarOutputStream(new FileOutputStream(newCustomProxyFile));

// copy all elements from the original CoatRack proxy jar, except for custom config file
Expand Down Expand Up @@ -151,6 +176,14 @@ public File getCustomJarForDownload(Proxy proxy) {
return newCustomProxyFile;
}

/**
* This method aims to fill the necessary bootstrap properties for the
* generated proxy, enabling the proxy to get its config from the CoatRack
* config server
*
* @param proxy
* @return the bootstrap properties for the generated proxy
*/
private Properties generateCustomProperties(Proxy proxy) {

Properties bootstrapPropsForGeneratedFile = new Properties();
Expand All @@ -168,7 +201,6 @@ private Properties generateCustomProperties(Proxy proxy) {
CUSTOM_PROPERTIES_CREDENTIAL_PASSWORD,
proxy.getConfigServerPassword());

// TODO clean up
bootstrapPropsForGeneratedFile.setProperty("management.security.enabled", "false");

return bootstrapPropsForGeneratedFile;
Expand Down

0 comments on commit b0fe292

Please sign in to comment.