|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.buildpack.platform.docker.configuration; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.lang.invoke.MethodHandles; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.security.MessageDigest; |
| 25 | +import java.security.NoSuchAlgorithmException; |
| 26 | +import java.util.HexFormat; |
| 27 | + |
| 28 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 29 | +import com.fasterxml.jackson.databind.JsonNode; |
| 30 | +import com.fasterxml.jackson.databind.node.NullNode; |
| 31 | + |
| 32 | +import org.springframework.boot.buildpack.platform.json.MappedObject; |
| 33 | +import org.springframework.boot.buildpack.platform.json.SharedObjectMapper; |
| 34 | +import org.springframework.boot.buildpack.platform.system.Environment; |
| 35 | + |
| 36 | +/** |
| 37 | + * Docker configuration stored in metadata files managed by the Docker CLI. |
| 38 | + * |
| 39 | + * @author Scott Frederick |
| 40 | + */ |
| 41 | +final class DockerConfigurationMetadata { |
| 42 | + |
| 43 | + private static final String DOCKER_CONFIG = "DOCKER_CONFIG"; |
| 44 | + |
| 45 | + private static final String DEFAULT_CONTEXT = "default"; |
| 46 | + |
| 47 | + private static final String CONFIG_DIR = ".docker"; |
| 48 | + |
| 49 | + private static final String CONTEXTS_DIR = "contexts"; |
| 50 | + |
| 51 | + private static final String META_DIR = "meta"; |
| 52 | + |
| 53 | + private static final String TLS_DIR = "tls"; |
| 54 | + |
| 55 | + private static final String DOCKER_ENDPOINT = "docker"; |
| 56 | + |
| 57 | + private static final String CONFIG_FILE_NAME = "config.json"; |
| 58 | + |
| 59 | + private static final String CONTEXT_FILE_NAME = "meta.json"; |
| 60 | + |
| 61 | + private final String configLocation; |
| 62 | + |
| 63 | + private final DockerConfig config; |
| 64 | + |
| 65 | + private final DockerContext context; |
| 66 | + |
| 67 | + private DockerConfigurationMetadata(String configLocation, DockerConfig config, DockerContext context) { |
| 68 | + this.configLocation = configLocation; |
| 69 | + this.config = config; |
| 70 | + this.context = context; |
| 71 | + } |
| 72 | + |
| 73 | + DockerConfig getConfiguration() { |
| 74 | + return this.config; |
| 75 | + } |
| 76 | + |
| 77 | + DockerContext getContext() { |
| 78 | + return this.context; |
| 79 | + } |
| 80 | + |
| 81 | + DockerContext forContext(String context) { |
| 82 | + return createDockerContext(this.configLocation, context); |
| 83 | + } |
| 84 | + |
| 85 | + static DockerConfigurationMetadata from(Environment environment) { |
| 86 | + String configLocation = (environment.get(DOCKER_CONFIG) != null) ? environment.get(DOCKER_CONFIG) |
| 87 | + : Path.of(System.getProperty("user.home"), CONFIG_DIR).toString(); |
| 88 | + DockerConfig dockerConfig = createDockerConfig(configLocation); |
| 89 | + DockerContext dockerContext = createDockerContext(configLocation, dockerConfig.getCurrentContext()); |
| 90 | + return new DockerConfigurationMetadata(configLocation, dockerConfig, dockerContext); |
| 91 | + } |
| 92 | + |
| 93 | + private static DockerConfig createDockerConfig(String configLocation) { |
| 94 | + Path path = Path.of(configLocation, CONFIG_FILE_NAME); |
| 95 | + if (!path.toFile().exists()) { |
| 96 | + return DockerConfig.empty(); |
| 97 | + } |
| 98 | + try { |
| 99 | + return DockerConfig.fromJson(readPathContent(path)); |
| 100 | + } |
| 101 | + catch (JsonProcessingException ex) { |
| 102 | + throw new IllegalStateException("Error parsing Docker configuration file '" + path + "'", ex); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static DockerContext createDockerContext(String configLocation, String currentContext) { |
| 107 | + if (currentContext == null || DEFAULT_CONTEXT.equals(currentContext)) { |
| 108 | + return DockerContext.empty(); |
| 109 | + } |
| 110 | + Path metaPath = Path.of(configLocation, CONTEXTS_DIR, META_DIR, asHash(currentContext), CONTEXT_FILE_NAME); |
| 111 | + Path tlsPath = Path.of(configLocation, CONTEXTS_DIR, TLS_DIR, asHash(currentContext), DOCKER_ENDPOINT); |
| 112 | + if (!metaPath.toFile().exists()) { |
| 113 | + throw new IllegalArgumentException("Docker context '" + currentContext + "' does not exist"); |
| 114 | + } |
| 115 | + try { |
| 116 | + DockerContext context = DockerContext.fromJson(readPathContent(metaPath)); |
| 117 | + if (tlsPath.toFile().isDirectory()) { |
| 118 | + return context.withTlsPath(tlsPath.toString()); |
| 119 | + } |
| 120 | + return context; |
| 121 | + } |
| 122 | + catch (JsonProcessingException ex) { |
| 123 | + throw new IllegalStateException("Error parsing Docker context metadata file '" + metaPath + "'", ex); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private static String asHash(String currentContext) { |
| 128 | + try { |
| 129 | + MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| 130 | + byte[] hash = digest.digest(currentContext.getBytes(StandardCharsets.UTF_8)); |
| 131 | + return HexFormat.of().formatHex(hash); |
| 132 | + } |
| 133 | + catch (NoSuchAlgorithmException ex) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private static String readPathContent(Path path) { |
| 139 | + try { |
| 140 | + return Files.readString(path); |
| 141 | + } |
| 142 | + catch (IOException ex) { |
| 143 | + throw new IllegalStateException("Error reading Docker configuration file '" + path + "'", ex); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + static final class DockerConfig extends MappedObject { |
| 148 | + |
| 149 | + private final String currentContext; |
| 150 | + |
| 151 | + private DockerConfig(JsonNode node) { |
| 152 | + super(node, MethodHandles.lookup()); |
| 153 | + this.currentContext = valueAt("/currentContext", String.class); |
| 154 | + } |
| 155 | + |
| 156 | + String getCurrentContext() { |
| 157 | + return this.currentContext; |
| 158 | + } |
| 159 | + |
| 160 | + static DockerConfig fromJson(String json) throws JsonProcessingException { |
| 161 | + return new DockerConfig(SharedObjectMapper.get().readTree(json)); |
| 162 | + } |
| 163 | + |
| 164 | + static DockerConfig empty() { |
| 165 | + return new DockerConfig(NullNode.instance); |
| 166 | + } |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | + static final class DockerContext extends MappedObject { |
| 171 | + |
| 172 | + private final String dockerHost; |
| 173 | + |
| 174 | + private final Boolean skipTlsVerify; |
| 175 | + |
| 176 | + private final String tlsPath; |
| 177 | + |
| 178 | + private DockerContext(JsonNode node, String tlsPath) { |
| 179 | + super(node, MethodHandles.lookup()); |
| 180 | + this.dockerHost = valueAt("/Endpoints/" + DOCKER_ENDPOINT + "/Host", String.class); |
| 181 | + this.skipTlsVerify = valueAt("/Endpoints/" + DOCKER_ENDPOINT + "/SkipTLSVerify", Boolean.class); |
| 182 | + this.tlsPath = tlsPath; |
| 183 | + } |
| 184 | + |
| 185 | + String getDockerHost() { |
| 186 | + return this.dockerHost; |
| 187 | + } |
| 188 | + |
| 189 | + Boolean isTlsVerify() { |
| 190 | + return this.skipTlsVerify != null && !this.skipTlsVerify; |
| 191 | + } |
| 192 | + |
| 193 | + String getTlsPath() { |
| 194 | + return this.tlsPath; |
| 195 | + } |
| 196 | + |
| 197 | + DockerContext withTlsPath(String tlsPath) { |
| 198 | + return new DockerContext(this.getNode(), tlsPath); |
| 199 | + } |
| 200 | + |
| 201 | + static DockerContext fromJson(String json) throws JsonProcessingException { |
| 202 | + return new DockerContext(SharedObjectMapper.get().readTree(json), null); |
| 203 | + } |
| 204 | + |
| 205 | + static DockerContext empty() { |
| 206 | + return new DockerContext(NullNode.instance, null); |
| 207 | + } |
| 208 | + |
| 209 | + } |
| 210 | + |
| 211 | +} |
0 commit comments