Skip to content

Commit

Permalink
Adds mongo ssh debug methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzelei committed Jun 20, 2024
1 parent cfa7596 commit 20eb452
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
18 changes: 13 additions & 5 deletions backend/pkg/mongoconnect/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log/slog"
"net/url"
"sync"

mgmtv1alpha1 "github.com/nucleuscloud/neosync/backend/gen/go/protos/mgmt/v1alpha1"
Expand Down Expand Up @@ -58,12 +59,14 @@ func (w *WrappedMongoClient) Open(ctx context.Context) (*mongo.Client, error) {
return nil, err

Check warning on line 59 in backend/pkg/mongoconnect/connector.go

View check run for this annotation

Codecov / codecov/patch

backend/pkg/mongoconnect/connector.go#L56-L59

Added lines #L56 - L59 were not covered by tests
}
<-ready
w.logger.Info("tunnel is now ready", "isopen", w.details.Tunnel.IsOpen())

Check warning on line 62 in backend/pkg/mongoconnect/connector.go

View check run for this annotation

Codecov / codecov/patch

backend/pkg/mongoconnect/connector.go#L61-L62

Added lines #L61 - L62 were not covered by tests
}
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
w.logger.Info("connecting to mongo instance", "url", w.details.String())

Check warning on line 65 in backend/pkg/mongoconnect/connector.go

View check run for this annotation

Codecov / codecov/patch

backend/pkg/mongoconnect/connector.go#L65

Added line #L65 was not covered by tests
opts := options.Client().ApplyURI(w.details.String()).SetServerAPIOptions(serverAPI)
client, err := mongo.Connect(ctx, opts)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to connect to mongo instance: %w", err)

Check warning on line 69 in backend/pkg/mongoconnect/connector.go

View check run for this annotation

Codecov / codecov/patch

backend/pkg/mongoconnect/connector.go#L69

Added line #L69 was not covered by tests
}
w.client = client
return client, nil
Expand All @@ -79,6 +82,7 @@ func (w *WrappedMongoClient) Close(ctx context.Context) error {
w.client = nil
err := client.Disconnect(ctx)
if w.details.Tunnel != nil && w.details.Tunnel.IsOpen() {
w.logger.Debug("closing tunnel...")
w.details.Tunnel.Close()

Check warning on line 86 in backend/pkg/mongoconnect/connector.go

View check run for this annotation

Codecov / codecov/patch

backend/pkg/mongoconnect/connector.go#L83-L86

Added lines #L83 - L86 were not covered by tests
}
return err

Check warning on line 88 in backend/pkg/mongoconnect/connector.go

View check run for this annotation

Codecov / codecov/patch

backend/pkg/mongoconnect/connector.go#L88

Added line #L88 was not covered by tests
Expand Down Expand Up @@ -119,9 +123,13 @@ func (c *ConnectionDetails) GetTunnel() *sshtunnel.Sshtunnel {
func (c *ConnectionDetails) String() string {
if c.Tunnel != nil && c.Tunnel.IsOpen() {
localhost, port := c.Tunnel.GetLocalHostPort()
newDetails := *c.Details
newDetails.Hosts = []string{fmt.Sprintf("%s:%d", localhost, port)}
return newDetails.String()
parseUrl, err := url.Parse(c.Details.String())
if err != nil {
return "" // todo

Check warning on line 128 in backend/pkg/mongoconnect/connector.go

View check run for this annotation

Codecov / codecov/patch

backend/pkg/mongoconnect/connector.go#L124-L128

Added lines #L124 - L128 were not covered by tests
}
parseUrl.Host = fmt.Sprintf("%s:%d", localhost, port)
parseUrl.Scheme = "mongodb"
return parseUrl.String()

Check warning on line 132 in backend/pkg/mongoconnect/connector.go

View check run for this annotation

Codecov / codecov/patch

backend/pkg/mongoconnect/connector.go#L130-L132

Added lines #L130 - L132 were not covered by tests
}
return c.Details.String()
}
Expand Down Expand Up @@ -165,7 +173,7 @@ func GetConnectionDetails(
return nil, err
}
var publickey ssh.PublicKey
if tunnelCfg.GetKnownHostPublicKey() == "" {
if tunnelCfg.GetKnownHostPublicKey() != "" {

Check warning on line 176 in backend/pkg/mongoconnect/connector.go

View check run for this annotation

Codecov / codecov/patch

backend/pkg/mongoconnect/connector.go#L176

Added line #L176 was not covered by tests
publickey, err = sshtunnel.ParseSshKey(tunnelCfg.GetKnownHostPublicKey())
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion backend/pkg/sshtunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (t *Sshtunnel) serve(listener net.Listener, ready chan<- any, logger *slog.
t.isOpen = false
go func() {
t.shutdowns.Range(func(key, value any) bool {
logger.Debug("shutting down tunnel session", "key", key)

Check warning on line 115 in backend/pkg/sshtunnel/tunnel.go

View check run for this annotation

Codecov / codecov/patch

backend/pkg/sshtunnel/tunnel.go#L115

Added line #L115 was not covered by tests
sd, ok := value.(chan any)
if ok {
sd <- struct{}{}
Expand Down Expand Up @@ -212,7 +213,7 @@ func (s *Sshtunnel) getSshClient(
if err != nil {
return nil, err
}
logger.Debug(fmt.Sprintf("conntected to %s", addr))
logger.Debug(fmt.Sprintf("[ssh-client] conntected to %s", addr))

Check warning on line 216 in backend/pkg/sshtunnel/tunnel.go

View check run for this annotation

Codecov / codecov/patch

backend/pkg/sshtunnel/tunnel.go#L216

Added line #L216 was not covered by tests
s.sshclient = client
return client, nil
}
Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/sshtunnel/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func getPlaintextPrivateKeyAuthMethod(keyBytes []byte) (ssh.AuthMethod, error) {

func ParseSshKey(keyString string) (ssh.PublicKey, error) {
// Parse the key
publicKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(keyString)) //nolint
publicKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(keyString)) //nolint:dogsled
if err != nil {
return nil, fmt.Errorf("failed to parse public key: %v", err)
}
Expand Down

0 comments on commit 20eb452

Please sign in to comment.