-
-
Notifications
You must be signed in to change notification settings - Fork 588
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
Changes from 30 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
879e285
fd5772d
545a00e
19f4879
50579f9
42f32ec
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 |
|---|---|---|
|
|
@@ -2,28 +2,38 @@ package cassandra | |
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "fmt" | ||
| "io" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/testcontainers/testcontainers-go" | ||
| "github.com/testcontainers/testcontainers-go/wait" | ||
| ) | ||
|
|
||
| const ( | ||
| 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 | ||
| settings Options | ||
| } | ||
|
|
||
| // ConnectionHost returns the host and port of the cassandra container, using the default, native 9042 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) { | ||
| return c.PortEndpoint(ctx, port, "") | ||
| // Use the secure port if TLS is enabled | ||
| portToUse := port | ||
| if c.settings.tlsConfig != nil { | ||
| portToUse = securePort | ||
| } | ||
|
|
||
| return c.PortEndpoint(ctx, portToUse, "") | ||
| } | ||
|
|
||
| // WithConfigFile sets the YAML config file to be used for the cassandra container | ||
|
|
@@ -37,7 +47,6 @@ func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption { | |
| FileMode: 0o755, | ||
| } | ||
| req.Files = append(req.Files, cf) | ||
|
|
||
| return nil | ||
| } | ||
| } | ||
|
|
@@ -54,10 +63,8 @@ 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) | ||
| } | ||
|
|
@@ -71,9 +78,9 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize | |
|
|
||
| // Run creates an instance of the Cassandra container type | ||
| func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*CassandraContainer, error) { | ||
| moduleOpts := []testcontainers.ContainerCustomizer{ | ||
| testcontainers.WithExposedPorts(port), | ||
| testcontainers.WithEnv(map[string]string{ | ||
| req := testcontainers.ContainerRequest{ | ||
| Image: img, | ||
| Env: map[string]string{ | ||
| "CASSANDRA_SNITCH": "GossipingPropertyFileSnitch", | ||
| "JVM_OPTS": "-Dcassandra.skip_wait_for_gossip_to_settle=0 -Dcassandra.initial_token=0", | ||
| "HEAP_NEWSIZE": "128M", | ||
|
|
@@ -94,8 +101,8 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom | |
|
|
||
| ctr, err := testcontainers.Run(ctx, img, moduleOpts...) | ||
| var c *CassandraContainer | ||
| if ctr != nil { | ||
| c = &CassandraContainer{Container: ctr} | ||
| if container != nil { | ||
| c = &CassandraContainer{Container: container, settings: settings} | ||
| } | ||
|
|
||
| if err != nil { | ||
|
|
@@ -104,3 +111,11 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom | |
|
|
||
| return c, nil | ||
| } | ||
|
|
||
| // TLSConfig returns the TLS configuration for the Cassandra container, nil if TLS is not enabled. | ||
| func (c *CassandraContainer) TLSConfig() *tls.Config { | ||
|
||
| if c.settings.tlsConfig == nil { | ||
| return nil | ||
| } | ||
| return c.settings.tlsConfig.Config | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| package cassandra | ||
|
|
||
| import ( | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
|
|
||
| "github.com/testcontainers/testcontainers-go" | ||
| ) | ||
|
|
||
| // TLSConfig represents the TLS configuration for Cassandra | ||
| type TLSConfig struct { | ||
|
||
| KeystorePath string | ||
| CertificatePath string | ||
| Config *tls.Config | ||
| } | ||
|
|
||
| // Options represents the configuration options for the Cassandra container | ||
| type Options struct { | ||
| tlsConfig *TLSConfig | ||
| } | ||
|
|
||
| // Option is an option for the Cassandra container. | ||
| type Option func(*testcontainers.GenericContainerRequest, *Options) error | ||
|
|
||
| // Customize implements the testcontainers.ContainerCustomizer interface | ||
| func (o Option) Customize(req *testcontainers.GenericContainerRequest) error { | ||
| return o(req, &Options{}) | ||
|
||
| } | ||
|
|
||
| // WithSSL enables SSL/TLS support on the Cassandra container | ||
| func WithSSL() Option { | ||
| return func(req *testcontainers.GenericContainerRequest, settings *Options) error { | ||
| req.ExposedPorts = append(req.ExposedPorts, string(securePort)) | ||
|
|
||
| keystorePath, certPath, err := GenerateJKSKeystore() | ||
| if err != nil { | ||
| return fmt.Errorf("create SSL certs: %w", err) | ||
| } | ||
|
|
||
| req.Files = append(req.Files, | ||
| testcontainers.ContainerFile{ | ||
| HostFilePath: keystorePath, | ||
| ContainerFilePath: "/etc/cassandra/conf/keystore.jks", | ||
| FileMode: 0o644, | ||
| }, | ||
| testcontainers.ContainerFile{ | ||
| HostFilePath: certPath, | ||
| ContainerFilePath: "/etc/cassandra/conf/cassandra.crt", | ||
| FileMode: 0o644, | ||
| }) | ||
|
|
||
| certPEM, err := os.ReadFile(certPath) | ||
| if err != nil { | ||
| return fmt.Errorf("error while read certificate: %w", err) | ||
| } | ||
|
|
||
| certPool := x509.NewCertPool() | ||
| if !certPool.AppendCertsFromPEM(certPEM) { | ||
| return errors.New("failed to append certificate to pool") | ||
| } | ||
|
|
||
| settings.tlsConfig = &TLSConfig{ | ||
| KeystorePath: keystorePath, | ||
| CertificatePath: certPath, | ||
| Config: &tls.Config{ | ||
| RootCAs: certPool, | ||
| ServerName: "localhost", | ||
| MinVersion: tls.VersionTLS12, | ||
| }, | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // GenerateJKSKeystore generates a JKS keystore with a self-signed cert using keytool, and extracts the public cert for Go client trust. | ||
| func GenerateJKSKeystore() (keystorePath, certPath string, err error) { | ||
mdelapenya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| tmpDir := os.TempDir() | ||
| keystorePath = filepath.Join(tmpDir, "keystore.jks") | ||
| keystorePassword := "changeit" | ||
| certPath = filepath.Join(tmpDir, "cert.pem") | ||
|
|
||
| // Remove existing keystore if it exists | ||
| os.Remove(keystorePath) | ||
|
|
||
| // Generate keystore with self-signed certificate | ||
| cmd := exec.Command( | ||
| "keytool", "-genkeypair", | ||
| "-alias", "cassandra", | ||
| "-keyalg", "RSA", | ||
| "-keysize", "2048", | ||
| "-storetype", "JKS", | ||
| "-keystore", keystorePath, | ||
| "-storepass", keystorePassword, | ||
| "-keypass", keystorePassword, | ||
| "-dname", "CN=localhost, OU=Test, O=Test, C=US", | ||
| "-validity", "365", | ||
| ) | ||
| if err := cmd.Run(); err != nil { | ||
| return "", "", fmt.Errorf("failed to generate keystore: %w", err) | ||
| } | ||
|
|
||
| // Export the public certificate for Go client trust | ||
| cmd = exec.Command( | ||
| "keytool", "-exportcert", | ||
| "-alias", "cassandra", | ||
| "-keystore", keystorePath, | ||
| "-storepass", keystorePassword, | ||
| "-rfc", | ||
| "-file", certPath, | ||
| ) | ||
| if err := cmd.Run(); err != nil { | ||
| return "", "", fmt.Errorf("failed to export certificate: %w", err) | ||
| } | ||
|
|
||
| return keystorePath, certPath, nil | ||
| } | ||
|
|
||
| // TLSConfig returns the TLS configuration | ||
| func (o *Options) TLSConfig() *TLSConfig { | ||
| return o.tlsConfig | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.