@@ -8,83 +8,109 @@ import com.google.common.collect.ImmutableMap
8
8
import io.airbyte.cdk.integrations.util.HostPortResolver
9
9
import io.airbyte.cdk.testutils.ContainerFactory
10
10
import io.airbyte.commons.json.Jsons
11
+ import java.io.IOException
12
+ import java.util.*
13
+ import java.util.function.Consumer
11
14
import org.apache.commons.lang3.tuple.ImmutablePair
12
15
import org.testcontainers.containers.Container
13
16
import org.testcontainers.containers.GenericContainer
14
17
import org.testcontainers.containers.JdbcDatabaseContainer
15
18
import org.testcontainers.containers.Network
16
19
import org.testcontainers.images.builder.ImageFromDockerfile
17
20
import org.testcontainers.utility.DockerImageName
18
- import java.io.IOException
19
- import java.util.*
20
- import java.util.function.Consumer
21
21
22
22
class SshBastionContainer : AutoCloseable {
23
- class SshBastionContainerFactory : ContainerFactory <GenericContainer <* >? >() {
23
+ class SshBastionContainerFactory : ContainerFactory <GenericContainer <* >>() {
24
24
override fun createNewContainer (imageName : DockerImageName ? ): GenericContainer <* >? {
25
- val container: GenericContainer <* > = GenericContainer <Any ?>(ImageFromDockerfile (" bastion-test" )
26
- .withFileFromClasspath(" Dockerfile" , " bastion/Dockerfile" ))
25
+ val container: GenericContainer <* > =
26
+ GenericContainer <Nothing >(
27
+ ImageFromDockerfile (" bastion-test" )
28
+ .withFileFromClasspath(" Dockerfile" , " bastion/Dockerfile" )
29
+ )
27
30
.withExposedPorts(22 )
28
31
return container
29
32
}
30
33
31
- fun exclusive (network : Network ? ): GenericContainer <* >? {
32
- val imageModifier = Consumer { c: GenericContainer <* >? ->
33
- c!! .withNetwork(network)
34
- }
35
- val container = super .exclusive(" bastion-test" , NamedContainerModifierImpl (" withNetwork" , imageModifier))
34
+ fun exclusive (network : Network ): GenericContainer <* >? {
35
+ val imageModifier = Consumer { c: GenericContainer <* > -> c!! .withNetwork(network) }
36
+ val container =
37
+ super .exclusive(
38
+ " bastion-test" ,
39
+ NamedContainerModifierImpl (" withNetwork" , imageModifier)
40
+ )
36
41
return container
37
42
}
38
43
}
39
44
40
45
var container: GenericContainer <* >? = null
41
46
private set
42
47
43
- fun initAndStartBastion (network : Network ? ) {
48
+ fun initAndStartBastion (network : Network ) {
44
49
container = factory!! .exclusive(network)
45
50
container!! .start()
46
51
}
47
52
48
53
@Throws(IOException ::class , InterruptedException ::class )
49
- fun getTunnelMethod (tunnelMethod : SshTunnel .TunnelMethod ? ,
50
- innerAddress : Boolean ): JsonNode ? {
51
- val containerAddress = if (innerAddress) getInnerContainerAddress(container) else getOuterContainerAddress(container)
52
- return Jsons .jsonNode(ImmutableMap .builder<Any ?, Any ?>()
53
- .put(" tunnel_host" ,
54
- Objects .requireNonNull(containerAddress!! .left))
54
+ fun getTunnelMethod (tunnelMethod : SshTunnel .TunnelMethod ? , innerAddress : Boolean ): JsonNode ? {
55
+ val containerAddress =
56
+ if (innerAddress) getInnerContainerAddress(container)
57
+ else getOuterContainerAddress(container)
58
+ return Jsons .jsonNode(
59
+ ImmutableMap .builder<Any ?, Any ?>()
60
+ .put(" tunnel_host" , Objects .requireNonNull(containerAddress!! .left))
55
61
.put(" tunnel_method" , tunnelMethod)
56
62
.put(" tunnel_port" , containerAddress.right)
57
63
.put(" tunnel_user" , SSH_USER )
58
- .put(" tunnel_user_password" , if (tunnelMethod == SshTunnel .TunnelMethod .SSH_PASSWORD_AUTH ) SSH_PASSWORD else " " )
59
- .put(" ssh_key" , if (tunnelMethod == SshTunnel .TunnelMethod .SSH_KEY_AUTH ) container!! .execInContainer(" cat" , " var/bastion/id_rsa" ).stdout else " " )
60
- .build())
64
+ .put(
65
+ " tunnel_user_password" ,
66
+ if (tunnelMethod == SshTunnel .TunnelMethod .SSH_PASSWORD_AUTH ) SSH_PASSWORD
67
+ else " "
68
+ )
69
+ .put(
70
+ " ssh_key" ,
71
+ if (tunnelMethod == SshTunnel .TunnelMethod .SSH_KEY_AUTH )
72
+ container!! .execInContainer(" cat" , " var/bastion/id_rsa" ).stdout
73
+ else " "
74
+ )
75
+ .build()
76
+ )
61
77
}
62
78
63
79
@Throws(IOException ::class , InterruptedException ::class )
64
- fun getTunnelConfig (tunnelMethod : SshTunnel .TunnelMethod ? ,
65
- builderWithSchema : ImmutableMap .Builder <Any ?, Any ?>? ,
66
- innerAddress : Boolean ): JsonNode ? {
67
- return Jsons .jsonNode(builderWithSchema
80
+ fun getTunnelConfig (
81
+ tunnelMethod : SshTunnel .TunnelMethod ? ,
82
+ builderWithSchema : ImmutableMap .Builder <Any ?, Any ?>? ,
83
+ innerAddress : Boolean
84
+ ): JsonNode ? {
85
+ return Jsons .jsonNode(
86
+ builderWithSchema!!
68
87
.put(" tunnel_method" , getTunnelMethod(tunnelMethod, innerAddress))
69
- .build())
88
+ .build()
89
+ )
70
90
}
71
91
72
92
fun getBasicDbConfigBuider (db : JdbcDatabaseContainer <* >? ): ImmutableMap .Builder <Any ?, Any ?>? {
73
93
return getBasicDbConfigBuider(db, db!! .databaseName)
74
94
}
75
95
76
- fun getBasicDbConfigBuider (db : JdbcDatabaseContainer <* >? , schemas : MutableList <String ?>? ): ImmutableMap .Builder <Any ?, Any ?>? {
96
+ fun getBasicDbConfigBuider (
97
+ db : JdbcDatabaseContainer <* >? ,
98
+ schemas : MutableList <String ?>?
99
+ ): ImmutableMap .Builder <Any ?, Any ?>? {
77
100
return getBasicDbConfigBuider(db, db!! .databaseName)!! .put(" schemas" , schemas)
78
101
}
79
102
80
- fun getBasicDbConfigBuider (db : JdbcDatabaseContainer <* >? , schemaName : String? ): ImmutableMap .Builder <Any ?, Any ?>? {
103
+ fun getBasicDbConfigBuider (
104
+ db : JdbcDatabaseContainer <* >? ,
105
+ schemaName : String?
106
+ ): ImmutableMap .Builder <Any ?, Any ?>? {
81
107
return ImmutableMap .builder<Any ?, Any ?>()
82
- .put(" host" , Objects .requireNonNull(HostPortResolver .resolveHost(db)))
83
- .put(" username" , db!! .username)
84
- .put(" password" , db.password)
85
- .put(" port" , HostPortResolver .resolvePort(db))
86
- .put(" database" , schemaName)
87
- .put(" ssl" , false )
108
+ .put(" host" , Objects .requireNonNull(HostPortResolver .resolveHost(db)))
109
+ .put(" username" , db!! .username)
110
+ .put(" password" , db.password)
111
+ .put(" port" , HostPortResolver .resolvePort(db))
112
+ .put(" database" , schemaName)
113
+ .put(" ssl" , false )
88
114
}
89
115
90
116
fun stopAndCloseContainers (db : JdbcDatabaseContainer <* >? ) {
@@ -109,29 +135,37 @@ class SshBastionContainer : AutoCloseable {
109
135
private val SSH_PASSWORD : String? = " secret"
110
136
111
137
/* *
112
- * Returns the inner docker network ip address and port of a container. This can be used to reach a
113
- * container from another container running on the same network
138
+ * Returns the inner docker network ip address and port of a container. This can be used to
139
+ * reach a container from another container running on the same network
114
140
*
115
141
* @param container container
116
142
* @return a pair of host and port
117
143
*/
118
144
fun getInnerContainerAddress (container : Container <* >? ): ImmutablePair <String ?, Int ?>? {
119
145
return ImmutablePair .of(
120
- container!! .containerInfo.networkSettings.networks.entries.stream().findFirst().get().value.ipAddress,
121
- container.exposedPorts.stream().findFirst().get())
146
+ container!!
147
+ .containerInfo
148
+ .networkSettings
149
+ .networks
150
+ .entries
151
+ .stream()
152
+ .findFirst()
153
+ .get()
154
+ .value
155
+ .ipAddress,
156
+ container.exposedPorts.stream().findFirst().get()
157
+ )
122
158
}
123
159
124
160
/* *
125
- * Returns the outer docker network ip address and port of a container. This can be used to reach a
126
- * container from the host machine
161
+ * Returns the outer docker network ip address and port of a container. This can be used to
162
+ * reach a container from the host machine
127
163
*
128
164
* @param container container
129
165
* @return a pair of host and port
130
166
*/
131
167
fun getOuterContainerAddress (container : Container <* >? ): ImmutablePair <String ?, Int ?>? {
132
- return ImmutablePair .of(
133
- container!! .host,
134
- container.firstMappedPort)
168
+ return ImmutablePair .of(container!! .host, container.firstMappedPort)
135
169
}
136
170
}
137
171
}
0 commit comments