|
| 1 | +/* |
| 2 | + * Copyright 2003-2021 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 | +package io.micronaut.testresources.azure.cosmos; |
| 17 | + |
| 18 | +import io.micronaut.testresources.testcontainers.AbstractTestContainersProvider; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | +import org.testcontainers.containers.CosmosDBEmulatorContainer; |
| 22 | +import org.testcontainers.utility.DockerImageName; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.security.KeyStoreException; |
| 27 | +import java.security.NoSuchAlgorithmException; |
| 28 | +import java.security.cert.CertificateException; |
| 29 | +import java.time.Duration; |
| 30 | +import java.util.Collection; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.Optional; |
| 34 | +import java.util.Set; |
| 35 | + |
| 36 | +public class CosmosTestResourcesProvider extends AbstractTestContainersProvider<CosmosDBEmulatorContainer> { |
| 37 | + private static final Logger LOGGER = LoggerFactory.getLogger(CosmosTestResourcesProvider.class); |
| 38 | + private static final Duration STARTUP_TIMEOUT = Duration.ofMinutes(5); |
| 39 | + |
| 40 | + public static final String SIMPLE_NAME = "cosmosdb"; |
| 41 | + public static final String DEFAULT_IMAGE = "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest"; |
| 42 | + |
| 43 | + public static final String ENDPOINT = "azure.cosmos.endpoint"; |
| 44 | + public static final String KEY = "azure.cosmos.key"; |
| 45 | + public static final List<String> RESOLVABLE_PROPERTIES_LIST = List.of(ENDPOINT, KEY); |
| 46 | + public static final Set<String> RESOLVABLE_PROPERTIES_SET = Set.of(ENDPOINT, KEY); |
| 47 | + |
| 48 | + @Override |
| 49 | + protected String getSimpleName() { |
| 50 | + return SIMPLE_NAME; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected String getDefaultImageName() { |
| 55 | + return DEFAULT_IMAGE; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public List<String> getResolvableProperties(Map<String, Collection<String>> propertyEntries, Map<String, Object> testResourcesConfig) { |
| 60 | + return RESOLVABLE_PROPERTIES_LIST; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected boolean shouldAnswer(String propertyName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfig) { |
| 65 | + return RESOLVABLE_PROPERTIES_SET.contains(propertyName); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected CosmosDBEmulatorContainer createContainer(DockerImageName imageName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfig) { |
| 70 | + return new CosmosDBEmulatorWithKeystoreContainer(imageName).withStartupTimeout(STARTUP_TIMEOUT); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected Optional<String> resolveProperty(String propertyName, CosmosDBEmulatorContainer container) { |
| 75 | + if (RESOLVABLE_PROPERTIES_SET.contains(propertyName)) { |
| 76 | + return Optional.ofNullable(switch (propertyName) { |
| 77 | + case ENDPOINT -> container.getEmulatorEndpoint(); |
| 78 | + case KEY -> container.getEmulatorKey(); |
| 79 | + default -> null; |
| 80 | + }); |
| 81 | + } |
| 82 | + return Optional.empty(); |
| 83 | + } |
| 84 | + |
| 85 | + private static class CosmosDBEmulatorWithKeystoreContainer extends CosmosDBEmulatorContainer { |
| 86 | + public CosmosDBEmulatorWithKeystoreContainer(DockerImageName imageName) { |
| 87 | + super(imageName); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void start() { |
| 92 | + super.start(); |
| 93 | + configureKeyStore(this); |
| 94 | + } |
| 95 | + |
| 96 | + private void configureKeyStore(CosmosDBEmulatorContainer emulator) { |
| 97 | + try { |
| 98 | + var keyStoreFile = Files.createTempFile("azure-cosmos-emulator", ".keystore"); |
| 99 | + var keyStore = emulator.buildNewKeyStore(); |
| 100 | + try (var outputStream = Files.newOutputStream(keyStoreFile)) { |
| 101 | + keyStore.store(outputStream, emulator.getEmulatorKey().toCharArray()); |
| 102 | + } |
| 103 | + } catch (IOException | KeyStoreException | NoSuchAlgorithmException | |
| 104 | + CertificateException ex) { |
| 105 | + LOGGER.error("Cannot create keystore for CosmosDB", ex); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | +} |
0 commit comments