-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathEmbeddedAzuriteBootstrapConfiguration.java
More file actions
183 lines (157 loc) · 9.24 KB
/
EmbeddedAzuriteBootstrapConfiguration.java
File metadata and controls
183 lines (157 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.playtika.testcontainer.azurite;
import com.playtika.testcontainer.common.spring.DockerPresenceBootstrapConfiguration;
import com.playtika.testcontainer.common.utils.ContainerUtils;
import com.playtika.testcontainer.toxiproxy.ToxiproxyClientProxy;
import com.playtika.testcontainer.toxiproxy.ToxiproxyHelper;
import com.playtika.testcontainer.toxiproxy.condition.ConditionalOnToxiProxyEnabled;
import eu.rekawek.toxiproxy.ToxiproxyClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.testcontainers.azure.AzuriteContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.toxiproxy.ToxiproxyContainer;
import org.testcontainers.utility.MountableFile;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Optional;
import static com.playtika.testcontainer.azurite.AzuriteProperties.AZURITE_BEAN_NAME;
import static com.playtika.testcontainer.azurite.AzuriteProperties.DEFAULT_CERT_CLASSPATH;
import static com.playtika.testcontainer.azurite.AzuriteProperties.DEFAULT_KEY_CLASSPATH;
import static com.playtika.testcontainer.common.utils.ContainerUtils.configureCommonsAndStart;
@Slf4j
@Configuration
@ConditionalOnExpression("${embedded.containers.enabled:true}")
@AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class)
@ConditionalOnProperty(name = "embedded.azurite.enabled", matchIfMissing = true)
@EnableConfigurationProperties(AzuriteProperties.class)
public class EmbeddedAzuriteBootstrapConfiguration {
private static final String AZURITE_BLOB_NETWORK_ALIAS = "azurite-blob.testcontainer.docker";
@Bean
@ConditionalOnToxiProxyEnabled(module = "azurite")
ToxiproxyClientProxy azuriteBlobContainerProxy(ToxiproxyClient toxiproxyClient,
ToxiproxyContainer toxiproxyContainer,
@Qualifier(AZURITE_BEAN_NAME) AzuriteContainer azurite,
AzuriteProperties properties,
ConfigurableEnvironment environment) {
ToxiproxyClientProxy proxy = ToxiproxyHelper.createProxy(
toxiproxyClient,
toxiproxyContainer,
azurite,
properties.getBlobStoragePort(),
"azurite");
ToxiproxyHelper.registerProxyEnvironment(proxy, "embedded.azurite", "embeddedAzuriteBlobToxiproxyInfo", environment, "blobStoragePort");
return proxy;
}
@Bean
@ConditionalOnToxiProxyEnabled(module = "azurite")
ToxiproxyClientProxy azuriteQueueContainerProxy(ToxiproxyClient toxiproxyClient,
ToxiproxyContainer toxiproxyContainer,
@Qualifier(AZURITE_BEAN_NAME) AzuriteContainer azurite,
AzuriteProperties properties,
ConfigurableEnvironment environment) {
ToxiproxyClientProxy proxy = ToxiproxyHelper.createProxy(
toxiproxyClient,
toxiproxyContainer,
azurite,
properties.getQueueStoragePort(),
"azurite");
ToxiproxyHelper.registerProxyEnvironment(proxy, "embedded.azurite", "embeddedAzuriteQueueToxiproxyInfo", environment, "queueStoragePor");
return proxy;
}
@Bean
@ConditionalOnToxiProxyEnabled(module = "azurite")
ToxiproxyClientProxy azuriteTableContainerProxy(ToxiproxyClient toxiproxyClient,
ToxiproxyContainer toxiproxyContainer,
@Qualifier(AZURITE_BEAN_NAME) AzuriteContainer azurite,
AzuriteProperties properties,
ConfigurableEnvironment environment) {
ToxiproxyClientProxy proxy = ToxiproxyHelper.createProxy(
toxiproxyClient,
toxiproxyContainer,
azurite,
properties.getTableStoragePort(),
"azurite");
ToxiproxyHelper.registerProxyEnvironment(proxy, "embedded.azurite", "embeddedAzuriteTableToxiproxyInfo", environment, "tableStoragePort");
return proxy;
}
@Bean(name = AZURITE_BEAN_NAME, destroyMethod = "stop")
public AzuriteContainer azurite(ConfigurableEnvironment environment,
AzuriteProperties properties,
Optional<Network> network) {
AzuriteContainer azuriteContainer = new AzuriteContainer(ContainerUtils.getDockerImageName(properties))
.withNetworkAliases(AZURITE_BLOB_NETWORK_ALIAS)
.withCreateContainerCmdModifier(cmd -> {
List<String> args = new ArrayList<>(Arrays.asList(cmd.getCmd()));
args.add("--skipApiVersionCheck");
if (properties.isOauthEnabled()) {
args.add("--oauth");
args.add("basic");
}
cmd.withCmd(args);
});
configureSsl(azuriteContainer, properties);
network.ifPresent(azuriteContainer::withNetwork);
azuriteContainer = (AzuriteContainer) configureCommonsAndStart(azuriteContainer, properties, log);
registerEnvironment(azuriteContainer, environment, properties);
return azuriteContainer;
}
private void configureSsl(AzuriteContainer azuriteContainer, AzuriteProperties properties) {
if (!properties.isHttpsEnabled()) {
return;
}
if (properties.isOauthEnabled()) {
log.info("Azurite OAuth enabled — HTTPS is required and will be configured.");
}
if (properties.getPfxCertPath() != null) {
azuriteContainer.withSsl(resolveMountableFile(properties.getPfxCertPath()), properties.getPfxPassword());
} else if (properties.getPemCertPath() != null && properties.getPemKeyPath() != null) {
azuriteContainer.withSsl(
resolveMountableFile(properties.getPemCertPath()),
resolveMountableFile(properties.getPemKeyPath()));
} else {
log.info("Azurite HTTPS enabled with embedded self-signed certificate.");
azuriteContainer.withSsl(
MountableFile.forClasspathResource(DEFAULT_CERT_CLASSPATH),
MountableFile.forClasspathResource(DEFAULT_KEY_CLASSPATH));
}
}
private MountableFile resolveMountableFile(String path) {
if (path.startsWith("classpath:")) {
return MountableFile.forClasspathResource(path.substring("classpath:".length()));
}
return MountableFile.forHostPath(path);
}
private void registerEnvironment(AzuriteContainer azurite,
ConfigurableEnvironment environment,
AzuriteProperties properties) {
Integer mappedBlobStoragePort = azurite.getMappedPort(properties.getBlobStoragePort());
Integer mappedQueueStoragePort = azurite.getMappedPort(properties.getQueueStoragePort());
Integer mappedTableStoragePort = azurite.getMappedPort(properties.getTableStoragePort());
String host = azurite.getHost();
String protocol = properties.isHttpsEnabled() ? "https" : "http";
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put("embedded.azurite.host", host);
map.put("embedded.azurite.blobStoragePort", mappedBlobStoragePort);
map.put("embedded.azurite.queueStoragePor", mappedQueueStoragePort);
map.put("embedded.azurite.tableStoragePort", mappedTableStoragePort);
map.put("embedded.azurite.account-name", AzuriteProperties.ACCOUNT_NAME);
map.put("embedded.azurite.account-key", AzuriteProperties.ACCOUNT_KEY);
map.put("embedded.azurite.blob-endpoint", protocol + "://" + host + ":" + mappedBlobStoragePort + "/" + AzuriteProperties.ACCOUNT_NAME);
map.put("embedded.azurite.queue-endpoint", protocol + "://" + host + ":" + mappedQueueStoragePort + "/" + AzuriteProperties.ACCOUNT_NAME);
map.put("embedded.azurite.table-endpoint", protocol + "://" + host + ":" + mappedTableStoragePort + "/" + AzuriteProperties.ACCOUNT_NAME);
map.put("embedded.azurite.networkAlias", AZURITE_BLOB_NETWORK_ALIAS);
log.info("Started Azurite. Connection details: {}", map);
MapPropertySource propertySource = new MapPropertySource("embeddedAzuriteInfo", map);
environment.getPropertySources().addFirst(propertySource);
}
}