Skip to content

Proposal to fix LDAP crashes #14

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
43 changes: 35 additions & 8 deletions ldap.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"crypto/tls"
"fmt"
"strconv"
"time"

"crypto/tls"
log "github.com/sirupsen/logrus"
"gopkg.in/ldap.v2"
"strconv"
)

type LDAPConnection struct {
Expand Down Expand Up @@ -33,15 +35,40 @@ func (c *LDAPConnection) Init(
func (c *LDAPConnection) reconnectToLDAP() {
lcon, err := ldap.DialTLS("tcp", c.host,
&tls.Config{ServerName: "ldap.csh.rit.edu"})
if err != nil {
c.app.db.AddLog(0, "ldap connection error: "+err.Error())
log.Fatal(err)

maxReconnectionAttempts := 3 // TODO (wilnil): Use an environment variable?
attempts := 0

for err != nil {
if attempts < maxReconnectionAttempts {
c.app.db.AddLog(0, "ldap connection error: "+err.Error())
log.Info(err)
time.Sleep(2 * time.Second)
lcon, err = ldap.DialTLS("tcp", c.host,
&tls.Config{ServerName: "ldap.csh.rit.edu"})
} else {
c.app.db.AddLog(0, "ldap connection error: "+err.Error())
log.Fatal(err)
}
maxReconnectionAttempts++
}

err = lcon.Bind(c.bind_dn, c.bind_pw)
if err != nil {
c.app.db.AddLog(0, "ldap bind error: "+err.Error())
log.Fatal(err)

attempts = 0
for err != nil {
if attempts < maxReconnectionAttempts {
c.app.db.AddLog(0, "ldap bind error: "+err.Error())
log.Info(err)
Copy link
Member

Choose a reason for hiding this comment

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

Dunno if log has this but if there's a log.Error you should use that so error logs are printed to stderr

Copy link
Member Author

Choose a reason for hiding this comment

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

oh, brain, I totally didn't bother to look this up at 3AM

time.Sleep(2 * time.Second)
err = lcon.Bind(c.bind_dn, c.bind_pw)
} else {
c.app.db.AddLog(0, "ldap bind error: "+err.Error())
log.Fatal(err)
}
maxReconnectionAttempts++
}

c.con = lcon
}

Expand Down
2 changes: 1 addition & 1 deletion routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
csh_auth "github.com/liam-middlebrook/csh-auth"
log "github.com/sirupsen/logrus"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
_ "image/gif"
"io"
"net/http"
"strconv"
Expand Down