-
-
Notifications
You must be signed in to change notification settings - Fork 584
feat(cassandra): add ssl option cassandra #3151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 14 commits
ba9ac99
a87e16b
d7c7be5
22c2887
dfe89e6
59a1733
4e7b372
875e63f
8e1b349
26a38e1
7d76d50
cc1dd06
71100a9
f07aea2
ac19eb4
5b636f7
2ad9f7b
7409635
4c2e3dc
6da1e1e
9a4f4f2
5242c32
18214a0
8ba3319
60622d4
38320ac
d1724a5
b05ef84
86f661b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import ( | |
| "io" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/docker/go-connections/nat" | ||
|
|
||
|
|
@@ -14,28 +15,35 @@ import ( | |
| ) | ||
|
|
||
| const ( | ||
| port = nat.Port("9042/tcp") | ||
| port = nat.Port("9042/tcp") | ||
| securePort = nat.Port("9142/tcp") // Common port for SSL/TLS connections | ||
| ) | ||
|
|
||
| // CassandraContainer represents the Cassandra container type used in the module | ||
| type CassandraContainer struct { | ||
| testcontainers.Container | ||
| useTLS bool | ||
| } | ||
|
|
||
| // ConnectionHost returns the host and port of the cassandra container, using the default, native 9000 port, and | ||
| // ConnectionHost returns the host and port of the cassandra container, using the default, native port, | ||
| // obtaining the host and exposed port from the container | ||
| func (c *CassandraContainer) ConnectionHost(ctx context.Context) (string, error) { | ||
| host, err := c.Host(ctx) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| port, err := c.MappedPort(ctx, port) | ||
| // Use the secure port if TLS is enabled | ||
| portToUse := port | ||
| if c.useTLS { | ||
| portToUse = securePort | ||
| } | ||
|
|
||
| mappedPort, err := c.MappedPort(ctx, portToUse) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return host + ":" + port.Port(), nil | ||
| return host + ":" + mappedPort.Port(), nil | ||
| } | ||
|
|
||
| // WithConfigFile sets the YAML config file to be used for the cassandra container | ||
|
|
@@ -49,7 +57,6 @@ func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption { | |
| FileMode: 0o755, | ||
| } | ||
| req.Files = append(req.Files, cf) | ||
|
|
||
| return nil | ||
| } | ||
| } | ||
|
|
@@ -66,15 +73,72 @@ func WithInitScripts(scripts ...string) testcontainers.CustomizeRequestOption { | |
| FileMode: 0o755, | ||
| } | ||
| initScripts = append(initScripts, cf) | ||
|
|
||
| execs = append(execs, initScript{File: cf.ContainerFilePath}) | ||
| } | ||
|
|
||
| req.Files = append(req.Files, initScripts...) | ||
| return testcontainers.WithAfterReadyCommand(execs...)(req) | ||
| } | ||
| } | ||
|
|
||
| // SSLOptions contains the configuration options for setting up SSL/TLS | ||
| type SSLOptions struct { | ||
| KeystorePath string | ||
| KeystorePassword string | ||
| CertPath string | ||
| RequireClientAuth bool | ||
| } | ||
|
|
||
| // WithSSL enables SSL/TLS support on the Cassandra container | ||
| func WithSSL(sslOpts SSLOptions) testcontainers.CustomizeRequestOption { | ||
| return func(req *testcontainers.GenericContainerRequest) error { | ||
| // Add the secure port to the exposed ports | ||
| hasSecurePort := false | ||
| for _, p := range req.ExposedPorts { | ||
| if p == string(securePort) { | ||
| hasSecurePort = true | ||
| break | ||
| } | ||
| } | ||
| if !hasSecurePort { | ||
| req.ExposedPorts = append(req.ExposedPorts, string(securePort)) | ||
| } | ||
|
|
||
| // Set SSL environment variables | ||
| if req.Env == nil { | ||
| req.Env = make(map[string]string) | ||
| } | ||
|
|
||
| // If keystore path is provided, copy it to the container | ||
| if sslOpts.KeystorePath != "" { | ||
| keystoreFile := testcontainers.ContainerFile{ | ||
| HostFilePath: sslOpts.KeystorePath, | ||
| ContainerFilePath: "/etc/cassandra/conf/keystore.jks", | ||
| FileMode: 0o644, | ||
| } | ||
| req.Files = append(req.Files, keystoreFile) | ||
| } | ||
|
|
||
| if sslOpts.CertPath != "" { | ||
| certFile := testcontainers.ContainerFile{ | ||
| HostFilePath: sslOpts.CertPath, | ||
| ContainerFilePath: "/etc/cassandra/conf/cassandra.crt", | ||
| FileMode: 0o644, | ||
| } | ||
| req.Files = append(req.Files, certFile) | ||
| } | ||
|
|
||
| // Mark that SSL is enabled for later use | ||
| req.Labels = mergeMap(req.Labels, map[string]string{"testcontainers.cassandra.ssl": "true"}) | ||
|
||
|
|
||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // WithTLS is an alias for WithSSL for user convenience | ||
| func WithTLS(opts SSLOptions) testcontainers.CustomizeRequestOption { | ||
|
||
| return WithSSL(opts) | ||
| } | ||
|
|
||
| // Deprecated: use Run instead | ||
| // RunContainer creates an instance of the Cassandra container type | ||
| func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*CassandraContainer, error) { | ||
|
|
@@ -87,37 +151,55 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom | |
| Image: img, | ||
| ExposedPorts: []string{string(port)}, | ||
| Env: map[string]string{ | ||
| "CASSANDRA_SNITCH": "GossipingPropertyFileSnitch", | ||
| "JVM_OPTS": "-Dcassandra.skip_wait_for_gossip_to_settle=0 -Dcassandra.initial_token=0", | ||
| "HEAP_NEWSIZE": "128M", | ||
| "MAX_HEAP_SIZE": "1024M", | ||
| "CASSANDRA_ENDPOINT_SNITCH": "GossipingPropertyFileSnitch", | ||
| "CASSANDRA_DC": "datacenter1", | ||
| "CASSANDRA_SNITCH": "GossipingPropertyFileSnitch", | ||
| "JVM_OPTS": "-Dcassandra.skip_wait_for_gossip_to_settle=0 -Dcassandra.initial_token=0", | ||
| "HEAP_NEWSIZE": "128M", | ||
| "MAX_HEAP_SIZE": "1024M", | ||
| "CASSANDRA_ENDPOINT_SNITCH": "GossipingPropertyFileSnitch", | ||
| "CASSANDRA_DC": "datacenter1", | ||
| "CASSANDRA_SKIP_WAIT_FOR_GOSSIP": "1", | ||
| "CASSANDRA_START_NATIVE_TRANSPORT": "true", | ||
|
||
| }, | ||
| WaitingFor: wait.ForAll( | ||
| wait.ForListeningPort(port), | ||
| wait.ForExec([]string{"cqlsh", "-e", "SELECT bootstrapped FROM system.local"}).WithResponseMatcher(func(body io.Reader) bool { | ||
| data, _ := io.ReadAll(body) | ||
| return strings.Contains(string(data), "COMPLETED") | ||
| }), | ||
| }).WithStartupTimeout(2*time.Minute), | ||
|
||
| ), | ||
| } | ||
|
|
||
| c := &CassandraContainer{} | ||
|
||
|
|
||
| genericContainerReq := testcontainers.GenericContainerRequest{ | ||
| ContainerRequest: req, | ||
| Started: true, | ||
| } | ||
|
|
||
| sslEnabled := false | ||
| for _, opt := range opts { | ||
| if err := opt.Customize(&genericContainerReq); err != nil { | ||
| return nil, err | ||
| } | ||
| // Detect if WithSSL/WithTLS was used by checking the label | ||
| if genericContainerReq.Labels != nil && genericContainerReq.Labels["testcontainers.cassandra.ssl"] == "true" { | ||
| sslEnabled = true | ||
| } | ||
| } | ||
|
|
||
| // If SSL is enabled, add a TLS wait strategy for the keystore file and SSL CQL port | ||
| if sslEnabled { | ||
| genericContainerReq.WaitingFor = wait.ForAll( | ||
| genericContainerReq.WaitingFor, | ||
| wait.ForListeningPort(securePort).WithStartupTimeout(3*time.Minute), | ||
| ) | ||
| } | ||
|
|
||
| container, err := testcontainers.GenericContainer(ctx, genericContainerReq) | ||
| var c *CassandraContainer | ||
| if container != nil { | ||
| c = &CassandraContainer{Container: container} | ||
| c.Container = container | ||
| // Set useTLS based on sslEnabled | ||
| c.useTLS = sslEnabled | ||
| } | ||
|
|
||
| if err != nil { | ||
|
|
@@ -126,3 +208,18 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom | |
|
|
||
| return c, nil | ||
| } | ||
|
|
||
| // mergeMap is a helper to merge two string maps | ||
| func mergeMap(a, b map[string]string) map[string]string { | ||
| if a == nil && b == nil { | ||
| return nil | ||
| } | ||
| result := make(map[string]string) | ||
| for k, v := range a { | ||
| result[k] = v | ||
| } | ||
| for k, v := range b { | ||
| result[k] = v | ||
| } | ||
| return result | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: do we need to two ports or can it just be one which is either SSL or not?