- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 583
 
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
          
     Open
      
      
            MitulShah1
  wants to merge
  29
  commits into
  testcontainers:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
MitulShah1:add-ssl-option-cassandra
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from 19 commits
      Commits
    
    
            Show all changes
          
          
            29 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      ba9ac99
              
                Added SSL Option
              
              
                MitulShah1 a87e16b
              
                Passed tests
              
              
                MitulShah1 d7c7be5
              
                Implemented SSL Support
              
              
                MitulShah1 22c2887
              
                Implemented SSL Support
              
              
                MitulShah1 dfe89e6
              
                Implemented SSL Support
              
              
                MitulShah1 59a1733
              
                Merge branch 'main' into add-ssl-option-cassandra
              
              
                MitulShah1 4e7b372
              
                Merge branch 'main' into add-ssl-option-cassandra
              
              
                MitulShah1 875e63f
              
                Merge remote-tracking branch 'origin/add-ssl-option-cassandra' into a…
              
              
                MitulShah1 8e1b349
              
                Revert go.mod and go.sum
              
              
                MitulShah1 26a38e1
              
                Revert go.mod and go.sum
              
              
                MitulShah1 7d76d50
              
                Golint fix
              
              
                MitulShah1 cc1dd06
              
                Golint fix
              
              
                MitulShah1 71100a9
              
                Govet fix
              
              
                MitulShah1 f07aea2
              
                Merge branch 'main' into add-ssl-option-cassandra
              
              
                MitulShah1 ac19eb4
              
                reduce wait time, revert container place and removed WithTLS options
              
              
                MitulShah1 5b636f7
              
                Added SSL With option
              
              
                MitulShah1 2ad9f7b
              
                Merge branch 'main' into add-ssl-option-cassandra
              
              
                 7409635
              
                Added SSL With option
              
              
                MitulShah1 4c2e3dc
              
                Devide Run function n sub function to reduce complexity
              
              
                MitulShah1 6da1e1e
              
                make Options private
              
              
                MitulShah1 9a4f4f2
              
                removed setupTls function and moved to WithSSL Options
              
              
                MitulShah1 5242c32
              
                Fix Sec warning InsecureSkipVerify
              
              
                MitulShah1 18214a0
              
                Fix lint
              
              
                MitulShah1 8ba3319
              
                Merge branch 'main' into add-ssl-option-cassandra
              
              
                MitulShah1 60622d4
              
                Merge branch 'main' into add-ssl-option-cassandra
              
              
                MitulShah1 38320ac
              
                Merge branch 'main' into add-ssl-option-cassandra
              
              
                MitulShah1 d1724a5
              
                Merge branch 'main' into add-ssl-option-cassandra
              
              
                mdelapenya b05ef84
              
                Merge branch 'testcontainers:main' into add-ssl-option-cassandra
              
              
                MitulShah1 86f661b
              
                Merge branch 'main' into add-ssl-option-cassandra
              
              
                MitulShah1 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
          Some comments aren't visible on the classic Files Changed page.
        
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -2,10 +2,14 @@ package cassandra | |
| 
     | 
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
| 
     | 
||
| "github.com/docker/go-connections/nat" | ||
| 
     | 
||
| 
        
          
        
         | 
    @@ -14,28 +18,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 | ||
| settings Options | ||
| } | ||
| 
     | 
||
| // 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.settings.IsTLSEnabled { | ||
| 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 +60,6 @@ func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption { | |
| FileMode: 0o755, | ||
| } | ||
| req.Files = append(req.Files, cf) | ||
| 
     | 
||
| return nil | ||
| } | ||
| } | ||
| 
        
          
        
         | 
    @@ -66,10 +76,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) | ||
| } | ||
| 
        
          
        
         | 
    @@ -81,11 +89,62 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize | |
| return Run(ctx, "cassandra:4.1.3", opts...) | ||
| } | ||
| 
     | 
||
| // setupTLS configures TLS settings for the Cassandra container. | ||
| func setupTLS(settings *Options) ([]string, []wait.Strategy, []testcontainers.ContainerCustomizer, error) { | ||
| exposePort := []string{string(port)} | ||
| waitStrategies := []wait.Strategy{ | ||
| 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(1 * time.Minute), | ||
| } | ||
| var tcOpts []testcontainers.ContainerCustomizer | ||
| 
     | 
||
| if settings.IsTLSEnabled { | ||
| exposePort = append(exposePort, string(securePort)) | ||
| waitStrategies = append(waitStrategies, wait.ForListeningPort(securePort).WithStartupTimeout(1*time.Minute)) | ||
| 
     | 
||
| keystorePath, certPath, err := GenerateJKSKeystore() | ||
| if err != nil { | ||
| return nil, nil, nil, fmt.Errorf("create SSL certs: %w", err) | ||
| } | ||
| 
     | 
||
| tcOpts = append(tcOpts, testcontainers.WithFiles( | ||
| 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 nil, nil, nil, fmt.Errorf("error while read certificate: %w", err) | ||
| } | ||
| 
     | 
||
| certPool := x509.NewCertPool() | ||
| certPool.AppendCertsFromPEM(certPEM) | ||
| 
     | 
||
| settings.TLSConfig = &tls.Config{ | ||
| RootCAs: certPool, | ||
| InsecureSkipVerify: true, | ||
| ServerName: "localhost", | ||
| MinVersion: tls.VersionTLS12, | ||
| } | ||
| } | ||
| 
     | 
||
| return exposePort, waitStrategies, tcOpts, nil | ||
| } | ||
| 
     | 
||
| // Run creates an instance of the Cassandra container type | ||
| func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*CassandraContainer, error) { | ||
| req := testcontainers.ContainerRequest{ | ||
| Image: img, | ||
| ExposedPorts: []string{string(port)}, | ||
| Image: img, | ||
| Env: map[string]string{ | ||
| "CASSANDRA_SNITCH": "GossipingPropertyFileSnitch", | ||
| "JVM_OPTS": "-Dcassandra.skip_wait_for_gossip_to_settle=0 -Dcassandra.initial_token=0", | ||
| 
        
          
        
         | 
    @@ -94,21 +153,35 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom | |
| "CASSANDRA_ENDPOINT_SNITCH": "GossipingPropertyFileSnitch", | ||
| "CASSANDRA_DC": "datacenter1", | ||
| }, | ||
| 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") | ||
| }), | ||
| ), | ||
| } | ||
| 
     | 
||
| genericContainerReq := testcontainers.GenericContainerRequest{ | ||
| ContainerRequest: req, | ||
| Started: true, | ||
| } | ||
| 
     | 
||
| var settings Options | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bug: settings is never set, you still need to check if you have an Option in the for loop and process it to ensure it set correctly. If your tests are passing they shouldn't so might need a fix there too.  | 
||
| for _, opt := range opts { | ||
| if opt, ok := opt.(Option); ok { | ||
| if err := opt(&settings); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| exposePort, waitStrategies, tcOpts, err := setupTLS(&settings) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| 
     | 
||
| tcOpts = append(tcOpts, testcontainers.WithExposedPorts(exposePort...)) | ||
| tcOpts = append(tcOpts, testcontainers.WithWaitStrategy(waitStrategies...)) | ||
| 
     | 
||
| // Append the customizers passed to the Run function. | ||
| tcOpts = append(tcOpts, opts...) | ||
| 
     | 
||
| // Apply the testcontainers customizers. | ||
| for _, opt := range tcOpts { | ||
                
      
                  stevenh marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| if err := opt.Customize(&genericContainerReq); err != nil { | ||
| return nil, err | ||
| } | ||
| 
        
          
        
         | 
    @@ -117,7 +190,7 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom | |
| container, err := testcontainers.GenericContainer(ctx, genericContainerReq) | ||
| var c *CassandraContainer | ||
| if container != nil { | ||
| c = &CassandraContainer{Container: container} | ||
| c = &CassandraContainer{Container: container, settings: settings} | ||
| } | ||
| 
     | 
||
| if err != nil { | ||
| 
        
          
        
         | 
    @@ -126,3 +199,8 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom | |
| 
     | 
||
| return c, nil | ||
| } | ||
| 
     | 
||
| // TLSConfig returns the TLS configuration for the Redis container, nil if TLS is not enabled. | ||
| func (c *CassandraContainer) TLSConfig() *tls.Config { | ||
| return c.settings.TLSConfig | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
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?