|
| 1 | +/** |
| 2 | + * ContainerProxy |
| 3 | + * |
| 4 | + * Copyright (C) 2016-2021 Open Analytics |
| 5 | + * |
| 6 | + * =========================================================================== |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the Apache License as published by |
| 10 | + * The Apache Software Foundation, either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * Apache License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the Apache License |
| 19 | + * along with this program. If not, see <http://www.apache.org/licenses/> |
| 20 | + */ |
| 21 | +package eu.openanalytics.containerproxy.service; |
| 22 | + |
| 23 | + |
| 24 | +import com.fasterxml.jackson.databind.MapperFeature; |
| 25 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 27 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 28 | +import com.google.common.base.Charsets; |
| 29 | +import eu.openanalytics.containerproxy.ContainerProxyApplication; |
| 30 | +import org.apache.commons.lang.StringUtils; |
| 31 | +import org.apache.logging.log4j.LogManager; |
| 32 | +import org.apache.logging.log4j.Logger; |
| 33 | +import org.springframework.core.env.Environment; |
| 34 | +import org.springframework.stereotype.Service; |
| 35 | + |
| 36 | +import javax.annotation.PostConstruct; |
| 37 | +import javax.inject.Inject; |
| 38 | +import java.io.File; |
| 39 | +import java.io.IOException; |
| 40 | +import java.math.BigInteger; |
| 41 | +import java.nio.file.Paths; |
| 42 | +import java.security.MessageDigest; |
| 43 | +import java.security.NoSuchAlgorithmException; |
| 44 | +import java.util.UUID; |
| 45 | + |
| 46 | +/** |
| 47 | + * Very simple service that keeps track of the identifiers of this ShinyProxy instance + server. |
| 48 | + */ |
| 49 | +@Service |
| 50 | +public class IdentifierService { |
| 51 | + |
| 52 | + /** |
| 53 | + * String that identifies this "run" or "server" of ShinyProxy. |
| 54 | + * This is unique every time ShinyProxy starts and independent of the InstanceId. |
| 55 | + */ |
| 56 | + public String runtimeId = null; |
| 57 | + |
| 58 | + /** |
| 59 | + * String that identifies this "instance" of ShinyProxy Configuration. |
| 60 | + * This value is determined by the configuration (i.e. application.yml) of ShinyProxy. |
| 61 | + * It is not unique across multiple runs or multiple servers. |
| 62 | + * This value only changes when the configuration changes. |
| 63 | + */ |
| 64 | + public String instanceId = null; |
| 65 | + |
| 66 | + /** |
| 67 | + * String identifying the realm ShinyProxy operatores in. |
| 68 | + */ |
| 69 | + public String realmId = null; |
| 70 | + |
| 71 | + private final Logger logger = LogManager.getLogger(getClass()); |
| 72 | + |
| 73 | + @Inject |
| 74 | + private Environment environment; |
| 75 | + |
| 76 | + @PostConstruct |
| 77 | + public void init() throws IOException, NoSuchAlgorithmException { |
| 78 | + String podName = environment.getProperty("SP_KUBE_POD_NAME"); |
| 79 | + if (podName != null) { |
| 80 | + runtimeId = StringUtils.right(podName, 4); |
| 81 | + } else { |
| 82 | + runtimeId = UUID.randomUUID().toString(); |
| 83 | + } |
| 84 | + |
| 85 | + logger.info("ShinyProxy runtimeId: " + runtimeId); |
| 86 | + |
| 87 | + instanceId = calculateInstanceId(); |
| 88 | + logger.info("ShinyProxy instanceID (hash of config): " + instanceId); |
| 89 | + |
| 90 | + realmId = environment.getProperty("proxy.realm-id"); |
| 91 | + if (realmId != null) { |
| 92 | + logger.info("ShinyProxy realmId: " + realmId); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private File getPathToConfigFile() { |
| 97 | + String path = environment.getProperty("spring.config.location"); |
| 98 | + if (path != null) { |
| 99 | + return Paths.get(path).toFile(); |
| 100 | + } |
| 101 | + |
| 102 | + File file = Paths.get(ContainerProxyApplication.CONFIG_FILENAME).toFile(); |
| 103 | + if (file.exists()) { |
| 104 | + return file; |
| 105 | + } |
| 106 | + |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Calculates a hash of the config file (i.e. application.yaml). |
| 112 | + */ |
| 113 | + private String calculateInstanceId() throws IOException, NoSuchAlgorithmException { |
| 114 | + /** |
| 115 | + * We need a hash of some "canonical" version of the config file. |
| 116 | + * The hash should not change when e.g. comments are added to the file. |
| 117 | + * Therefore we read the application.yml file into an Object and then |
| 118 | + * dump it again into YAML. We also sort the keys of maps and properties so that |
| 119 | + * the order does not matter for the resulting hash. |
| 120 | + */ |
| 121 | + ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); |
| 122 | + objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); |
| 123 | + objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); |
| 124 | + |
| 125 | + File file = getPathToConfigFile(); |
| 126 | + if (file == null) { |
| 127 | + // this should only happen in tests |
| 128 | + instanceId = "unknown-instance-id"; |
| 129 | + return instanceId; |
| 130 | + } |
| 131 | + |
| 132 | + Object parsedConfig = objectMapper.readValue(file, Object.class); |
| 133 | + String canonicalConfigFile = objectMapper.writeValueAsString(parsedConfig); |
| 134 | + |
| 135 | + MessageDigest digest = MessageDigest.getInstance("SHA-1"); |
| 136 | + digest.reset(); |
| 137 | + digest.update(canonicalConfigFile.getBytes(Charsets.UTF_8)); |
| 138 | + instanceId = String.format("%040x", new BigInteger(1, digest.digest())); |
| 139 | + return instanceId; |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments