| 
									
										
										
										
											2023-11-29 18:14:03 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * MIT License
 | 
					
						
							|  |  |  |  *
 | 
					
						
							|  |  |  |  * Copyright (c) 2020-present Cloudogu GmbH and Contributors
 | 
					
						
							|  |  |  |  *
 | 
					
						
							|  |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
					
						
							|  |  |  |  * of this software and associated documentation files (the "Software"), to deal
 | 
					
						
							|  |  |  |  * in the Software without restriction, including without limitation the rights
 | 
					
						
							|  |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
					
						
							|  |  |  |  * copies of the Software, and to permit persons to whom the Software is
 | 
					
						
							|  |  |  |  * furnished to do so, subject to the following conditions:
 | 
					
						
							|  |  |  |  *
 | 
					
						
							|  |  |  |  * The above copyright notice and this permission notice shall be included in all
 | 
					
						
							|  |  |  |  * copies or substantial portions of the Software.
 | 
					
						
							|  |  |  |  *
 | 
					
						
							|  |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
					
						
							|  |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
					
						
							|  |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
					
						
							|  |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
					
						
							|  |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 | 
					
						
							|  |  |  |  * SOFTWARE.
 | 
					
						
							|  |  |  |  */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package sonia.scm.server;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public class ServerConfigYaml {
 | 
					
						
							| 
									
										
										
										
											2024-02-02 15:10:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 18:14:03 +01:00
										 |  |  |   private static final String SCM_SERVER_PREFIX = "SCM_";
 | 
					
						
							| 
									
										
										
										
											2024-02-02 15:10:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 18:14:03 +01:00
										 |  |  |   // ### Server
 | 
					
						
							|  |  |  |   private String addressBinding = "0.0.0.0";
 | 
					
						
							|  |  |  |   private int port = 8080;
 | 
					
						
							|  |  |  |   private String contextPath = "/scm";
 | 
					
						
							|  |  |  |   private int httpHeaderSize = 16384;
 | 
					
						
							|  |  |  |   // The default temp dir path depends on the platform
 | 
					
						
							|  |  |  |   private String tempDir = "work/scm";
 | 
					
						
							|  |  |  |   // Resolves the client ip instead of the reverse proxy ip if the X-Forwarded-For header is present
 | 
					
						
							|  |  |  |   private boolean forwardHeadersEnabled = false;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // ### SSL-related config
 | 
					
						
							|  |  |  |   // Only configure SSL if the key store path is set
 | 
					
						
							|  |  |  |   private SSLConfig https = new SSLConfig();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public static class SSLConfig {
 | 
					
						
							|  |  |  |     private String keyStorePath = "";
 | 
					
						
							|  |  |  |     private String keyStorePassword = "";
 | 
					
						
							|  |  |  |     private String keyStoreType = "PKCS12";
 | 
					
						
							|  |  |  |     // If the ssl port is set, the http port will automatically redirect to this
 | 
					
						
							|  |  |  |     private int sslPort = 8443;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private boolean redirectHttpToHttps = false;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public String getKeyStorePath() {
 | 
					
						
							|  |  |  |       return getEnvWithDefault("HTTPS_KEY_STORE_PATH", keyStorePath);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public String getKeyStorePassword() {
 | 
					
						
							|  |  |  |       return getEnvWithDefault("HTTPS_KEY_STORE_PASSWORD", keyStorePassword);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public String getKeyStoreType() {
 | 
					
						
							|  |  |  |       return getEnvWithDefault("HTTPS_KEY_STORE_TYPE", keyStoreType);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public int getSslPort() {
 | 
					
						
							|  |  |  |       return getEnvWithDefault("HTTPS_SSL_PORT", sslPort);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public boolean isRedirectHttpToHttps() {
 | 
					
						
							|  |  |  |       return getEnvWithDefault("HTTPS_REDIRECT_HTTP_TO_HTTPS", redirectHttpToHttps);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public void setKeyStorePath(String keyStorePath) {
 | 
					
						
							|  |  |  |       this.keyStorePath = keyStorePath;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public void setKeyStorePassword(String keyStorePassword) {
 | 
					
						
							|  |  |  |       this.keyStorePassword = keyStorePassword;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public void setKeyStoreType(String keyStoreType) {
 | 
					
						
							|  |  |  |       this.keyStoreType = keyStoreType;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public void setSslPort(int sslPort) {
 | 
					
						
							|  |  |  |       this.sslPort = sslPort;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public void setRedirectHttpToHttps(boolean redirectHttpToHttps) {
 | 
					
						
							|  |  |  |       this.redirectHttpToHttps = redirectHttpToHttps;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public String getAddressBinding() {
 | 
					
						
							|  |  |  |     return getEnvWithDefault("ADDRESS_BINDING", addressBinding);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public void setAddressBinding(String addressBinding) {
 | 
					
						
							|  |  |  |     this.addressBinding = addressBinding;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public int getPort() {
 | 
					
						
							|  |  |  |     return getEnvWithDefault("PORT", port);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public void setPort(int port) {
 | 
					
						
							|  |  |  |     this.port = port;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public String getContextPath() {
 | 
					
						
							|  |  |  |     return getEnvWithDefault("CONTEXT_PATH", contextPath);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public void setContextPath(String contextPath) {
 | 
					
						
							|  |  |  |     this.contextPath = contextPath;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public int getHttpHeaderSize() {
 | 
					
						
							|  |  |  |     return getEnvWithDefault("HTTP_HEADER_SIZE", httpHeaderSize);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public void setHttpHeaderSize(int httpHeaderSize) {
 | 
					
						
							|  |  |  |     this.httpHeaderSize = httpHeaderSize;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public SSLConfig getHttps() {
 | 
					
						
							|  |  |  |     return https;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public void setHttps(SSLConfig https) {
 | 
					
						
							|  |  |  |     this.https = https;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public String getTempDir() {
 | 
					
						
							|  |  |  |     return getEnvWithDefault("TEMP_DIR", tempDir);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public void setTempDir(String tempDir) {
 | 
					
						
							|  |  |  |     this.tempDir = tempDir;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public boolean isForwardHeadersEnabled() {
 | 
					
						
							| 
									
										
										
										
											2023-12-01 14:34:50 +01:00
										 |  |  |     return getEnvWithDefault("FORWARD_HEADERS_ENABLED", forwardHeadersEnabled);
 | 
					
						
							| 
									
										
										
										
											2023-11-29 18:14:03 +01:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public void setForwardHeadersEnabled(boolean forwardHeadersEnabled) {
 | 
					
						
							|  |  |  |     this.forwardHeadersEnabled = forwardHeadersEnabled;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static int getEnvWithDefault(String envKey, int configValue) {
 | 
					
						
							|  |  |  |     String value = getEnv(envKey);
 | 
					
						
							|  |  |  |     return value != null ? Integer.parseInt(value) : configValue;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static String getEnvWithDefault(String envKey, String configValue) {
 | 
					
						
							|  |  |  |     String value = getEnv(envKey);
 | 
					
						
							|  |  |  |     return value != null ? value : configValue;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static boolean getEnvWithDefault(String envKey, boolean configValue) {
 | 
					
						
							|  |  |  |     String value = getEnv(envKey);
 | 
					
						
							| 
									
										
										
										
											2024-02-02 15:10:32 +01:00
										 |  |  |     return value != null ? Boolean.parseBoolean(value) : configValue;
 | 
					
						
							| 
									
										
										
										
											2023-11-29 18:14:03 +01:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private static String getEnv(String envKey) {
 | 
					
						
							|  |  |  |     return System.getenv(SCM_SERVER_PREFIX + envKey);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | }
 |