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

Try reconnecting Mongo on EOF #3269

Merged
merged 3 commits into from
Aug 31, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions plugins/database/mongodb/mongodb.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package mongodb

import (
"io"
"strings"
"time"

"encoding/json"

"fmt"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/builtin/logical/database/dbplugin"
"github.com/hashicorp/vault/plugins"
Expand Down Expand Up @@ -124,7 +127,21 @@ func (m *MongoDB) CreateUser(statements dbplugin.Statements, usernameConfig dbpl
}

err = session.DB(mongoCS.DB).Run(createUserCmd, nil)
if err != nil {
switch err {
case nil:
case io.EOF || strings.Contains(err.Error(), "EOF"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to compare err with io.EOF? I wonder if this will compile since io.EOF does not return a bool.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

io.EOF is the following:

var EOF = errors.New("EOF")

As such I am indeed trying to compare it directly, but in case it's simply another actual error type that contains EOF in its string, I'm also doing the string check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since its a error object and not a bool, I suspect if this will compile.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be a comma instead of a ||?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you're totally right about that, which is really amusing because the original reporter swore that he'd tried this branch and it didn't fix his problem. I'm suspicious.

if err := m.ConnectionProducer.Close(); err != nil {
return "", "", errwrap.Wrapf("error closing EOF'd mongo connection: {{err}}", err)
}
session, err := m.getConnection()
if err != nil {
return "", "", err
}
err = session.DB(mongoCS.DB).Run(createUserCmd, nil)
if err != nil {
return "", "", err
}
default:
return "", "", err
}

Expand Down Expand Up @@ -165,7 +182,22 @@ func (m *MongoDB) RevokeUser(statements dbplugin.Statements, username string) er
}

err = session.DB(db).RemoveUser(username)
if err != nil && err != mgo.ErrNotFound {
switch err {
case nil:
case mgo.ErrNotFound:
case io.EOF || strings.Contains(err.Error(), "EOF"):
if err := m.ConnectionProducer.Close(); err != nil {
return errwrap.Wrapf("error closing EOF'd mongo connection: {{err}}", err)
}
session, err := m.getConnection()
if err != nil {
return err
}
err = session.DB(db).RemoveUser(username)
if err != nil {
return err
}
default:
return err
}

Expand Down