Skip to content

Commit dc46ed5

Browse files
BupycHukademidoff
andauthored
PMM-13543 Support socket path. (#961)
* PMM-13543 Support socket path. PMM-13477 Support of PSMDB 8.0. * Apply suggestions from code review Co-authored-by: Alex Demidoff <[email protected]> --------- Co-authored-by: Alex Demidoff <[email protected]>
1 parent a9fe1e0 commit dc46ed5

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

exporter/diagnostic_data_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (d *diagnosticDataCollector) collect(ch chan<- prometheus.Metric) {
117117
debugResult(logger, m)
118118

119119
// MongoDB 8.0 splits the diagnostic data into multiple blocks, so we need to merge them
120-
if d.buildInfo.VersionArray[0] >= 8 { //nolint:gomnd
120+
if _, ok := m["common"]; ok {
121121
b := bson.M{}
122122
for _, mv := range m {
123123
block, ok := mv.(bson.M)

main.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,36 @@ func parseURIList(uriList []string, logger *logrus.Logger, splitCluster bool) []
268268
return URIs
269269
}
270270

271+
// buildURIManually builds the URI manually by checking if the user and password are supplied
272+
func buildURIManually(uri string, user string, password string) string {
273+
uriArray := strings.SplitN(uri, "://", 2) //nolint:mnd
274+
prefix := uriArray[0] + "://"
275+
uri = uriArray[1]
276+
277+
// IF user@pass not contained in uri AND custom user and pass supplied in arguments
278+
// DO concat a new uri with user and pass arguments value
279+
if !strings.Contains(uri, "@") && user != "" && password != "" {
280+
// add user and pass to the uri
281+
uri = fmt.Sprintf("%s:%s@%s", user, password, uri)
282+
}
283+
284+
// add back prefix after adding the user and pass
285+
uri = prefix + uri
286+
287+
return uri
288+
}
289+
271290
func buildURI(uri string, user string, password string, log *logrus.Logger) string {
272291
defaultPrefix := "mongodb://" // default prefix
273-
matchRegexp := regexp.MustCompile(`^mongodb(\+srv)?://`)
274292

275-
// Split the uri defaultPrefix if there is any
276-
if !matchRegexp.MatchString(uri) {
293+
if !strings.HasPrefix(uri, defaultPrefix) && !strings.HasPrefix(uri, "mongodb+srv://") {
277294
uri = defaultPrefix + uri
278295
}
279296
parsedURI, err := url.Parse(uri)
280297
if err != nil {
281-
log.Fatalf("Failed to parse URI %s: %v", uri, err)
282-
return uri
298+
// PMM generates URI with escaped path to socket file, so url.Parse fails
299+
// in this case we build URI manually
300+
return buildURIManually(uri, user, password)
283301
}
284302

285303
if parsedURI.User == nil && user != "" && password != "" {

main_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,48 @@ func TestBuildURI(t *testing.T) {
227227
newPassword: "yyy?!#$%^&*()_+",
228228
expect: "mongodb://xxx%3F%21%23$%25%5E&%2A%28%29_+:yyy%3F%21%23$%25%5E&%2A%28%[email protected]",
229229
},
230+
{
231+
situation: "path to socket",
232+
origin: "mongodb:///tmp/mongodb-27017.sock",
233+
newUser: "",
234+
newPassword: "",
235+
expect: "mongodb:///tmp/mongodb-27017.sock",
236+
},
237+
{
238+
situation: "path to socket with params",
239+
origin: "mongodb://username:s3cur3%20p%40$$w0r4.@%2Fvar%2Frun%2Fmongodb%2Fmongodb.sock/database?connectTimeoutMS=1000&directConnection=true&serverSelectionTimeoutMS=1000",
240+
newUser: "",
241+
newPassword: "",
242+
expect: "mongodb://username:s3cur3%20p%40$$w0r4.@%2Fvar%2Frun%2Fmongodb%2Fmongodb.sock/database?connectTimeoutMS=1000&directConnection=true&serverSelectionTimeoutMS=1000",
243+
},
244+
{
245+
situation: "path to socket with auth",
246+
origin: "mongodb://xxx:yyy@/tmp/mongodb-27017.sock",
247+
newUser: "",
248+
newPassword: "",
249+
expect: "mongodb://xxx:yyy@/tmp/mongodb-27017.sock",
250+
},
251+
{
252+
situation: "path to socket with auth and user params",
253+
origin: "mongodb:///tmp/mongodb-27017.sock",
254+
newUser: "xxx",
255+
newPassword: "yyy",
256+
expect: "mongodb://xxx:yyy@/tmp/mongodb-27017.sock",
257+
},
258+
{
259+
situation: "path to socket without prefix",
260+
origin: "/tmp/mongodb-27017.sock",
261+
newUser: "",
262+
newPassword: "",
263+
expect: "mongodb:///tmp/mongodb-27017.sock",
264+
},
265+
{
266+
situation: "path to socket without prefix with auth",
267+
origin: "/tmp/mongodb-27017.sock",
268+
newUser: "xxx",
269+
newPassword: "yyy",
270+
expect: "mongodb://xxx:yyy@/tmp/mongodb-27017.sock",
271+
},
230272
}
231273
for _, tc := range tests {
232274
t.Run(tc.situation, func(t *testing.T) {

0 commit comments

Comments
 (0)