Skip to content
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

Support DNS seedlist / mongodb+srv:// #112

Open
benweissmann opened this issue Feb 19, 2018 · 10 comments
Open

Support DNS seedlist / mongodb+srv:// #112

benweissmann opened this issue Feb 19, 2018 · 10 comments

Comments

@benweissmann
Copy link

Mongo 3.6 adds support for using DNS SRV records to configure the initial set of ReplicaSet members instead of providing them in the connection string itself. See: https://docs.mongodb.com/manual/reference/connection-string/#dns-seedlist-connection-format and https://github.com/mongodb/specifications/blob/master/source/initial-dns-seedlist-discovery/initial-dns-seedlist-discovery.rst

It would be great to see support for this in mgo!

@domodwyer
Copy link

Hi @benweissmann

This sounds like a cool addition and should be relatively easy! We don't have a 3.6 testing environment built up yet but it's in the works, I'll leave this ticket open in the meantime.

Saying that, we'll happily accept a PR for this!

Dom

@simagix
Copy link

simagix commented Oct 1, 2018

I forked but was not able to run unit tests b/c of the error below.

imports github.com/globalsign/mgo/internal/scram: use of internal package not allowed

Here are the changes to support dns seedlist :

$ git diff session.go
diff --git a/session.go b/session.go
index cd2a53e..488712f 100644
--- a/session.go
+++ b/session.go
@@ -788,7 +788,13 @@ type urlInfoOption struct {
 }

 func extractURL(s string) (*urlInfo, error) {
-       s = strings.TrimPrefix(s, "mongodb://")
+       isSRV := false
+       if strings.Index(s, "mongodb+srv://") == 0 {
+               isSRV = true
+               s = strings.TrimPrefix(s, "mongodb+srv://")
+       } else {
+               s = strings.TrimPrefix(s, "mongodb://")
+       }
        info := &urlInfo{options: []urlInfoOption{}}

        if c := strings.Index(s, "?"); c != -1 {
@@ -824,6 +830,32 @@ func extractURL(s string) (*urlInfo, error) {
                s = s[:c]
        }
        info.addrs = strings.Split(s, ",")
+       if isSRV == true {
+               // auto turn off ssl
+               info.options = append(info.options, urlInfoOption{key: "ssl", value: "true"})
+               srvAddr := info.addrs[0]
+               params, pe := net.LookupTXT(srvAddr)
+               if pe != nil {
+                       return nil, fmt.Errorf(pe.Error())
+               }
+               for _, pair := range strings.FieldsFunc(params[0], isOptSep) {
+                       l := strings.SplitN(pair, "=", 2)
+                       if len(l) != 2 || l[0] == "" || l[1] == "" {
+                               return nil, errors.New("connection option must be key=value: " + pair)
+                       }
+                       info.options = append(info.options, urlInfoOption{key: l[0], value: l[1]})
+               }
+               _, addrs, le := net.LookupSRV("mongodb", "tcp", srvAddr)
+               if le != nil {
+                       return nil, fmt.Errorf(le.Error())
+               }
+               addresses := make([]string, len(addrs))
+               for i, addr := range addrs {
+                       address := strings.TrimSuffix(addr.Target, ".")
+                       addresses[i] = fmt.Sprintf("%s:%d", address, addr.Port)
+               }
+               info.addrs = addresses
+       }
        return info, nil
 }

@@ -2912,7 +2944,6 @@ func (p *Pipe) SetMaxTime(d time.Duration) *Pipe {
        return p
 }

@mhutter
Copy link

mhutter commented May 26, 2019

This would make the MGO driver compatible with MongoDB Atlas

@tnerolftnerolf
Copy link

Any news?

@frontmill
Copy link

It would really be great to see this implemented

@bonczj
Copy link

bonczj commented May 22, 2020

I spent some time today porting the logic from the new, official Mongo driver back to mgo. We are running off of master, so I started my branch from there as it makes it easier for us to pick up without grabbing new features off of the development branch.

@maitesin I know that the contributions should be off of release. Before I send a PR over to you, does it have to be against development branch? While I know we are running against master, I don't know what most people are using. It is also unclear if/when a new release will be cut.

@slaveofcode
Copy link

It's 2021, any updates on this issue?

@mhutter
Copy link

mhutter commented Mar 23, 2021

Well, there now is an official Driver from MongoDB which also covers this use case.

@simagix
Copy link

simagix commented Mar 23, 2021

I have switched to the official mongodb-go-driver. Here are some examples to help your transition, https://github.com/simagix/mongo-go-examples/tree/master/examples.

@Neustradamus
Copy link

@ all: I wish you a Happy New Year 2022!

Any news about it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants