2015-11-11 01:08:40 +05:30
|
|
|
package functional
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2016-02-26 01:27:26 +05:30
|
|
|
"net"
|
2015-11-11 01:08:40 +05:30
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/coreos/dex/connector"
|
|
|
|
"gopkg.in/ldap.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ldapHost string
|
|
|
|
ldapPort uint16
|
|
|
|
ldapBindDN string
|
|
|
|
ldapBindPw string
|
|
|
|
)
|
|
|
|
|
2016-02-26 01:27:26 +05:30
|
|
|
type LDAPServer struct {
|
|
|
|
Host string
|
|
|
|
Port uint16
|
|
|
|
BindDN string
|
|
|
|
BindPw string
|
|
|
|
}
|
2015-11-11 01:08:40 +05:30
|
|
|
|
2016-02-26 01:27:26 +05:30
|
|
|
const (
|
|
|
|
ldapEnvHost = "DEX_TEST_LDAP_HOST"
|
|
|
|
ldapEnvBindName = "DEX_TEST_LDAP_BINDNAME"
|
|
|
|
ldapEnvBindPass = "DEX_TEST_LDAP_BINDPASS"
|
|
|
|
)
|
2015-11-11 01:08:40 +05:30
|
|
|
|
2016-02-26 01:27:26 +05:30
|
|
|
func ldapServer(t *testing.T) LDAPServer {
|
|
|
|
host := os.Getenv(ldapEnvHost)
|
|
|
|
if host == "" {
|
|
|
|
t.Fatalf("%s not set", ldapEnvHost)
|
|
|
|
}
|
|
|
|
var port uint64 = 389
|
|
|
|
if h, p, err := net.SplitHostPort(host); err == nil {
|
|
|
|
port, err = strconv.ParseUint(p, 10, 16)
|
2015-11-11 01:08:40 +05:30
|
|
|
if err != nil {
|
2016-02-26 01:27:26 +05:30
|
|
|
t.Fatalf("failed to parse port: %v", err)
|
2015-11-11 01:08:40 +05:30
|
|
|
}
|
2016-02-26 01:27:26 +05:30
|
|
|
host = h
|
2015-11-11 01:08:40 +05:30
|
|
|
}
|
2016-02-26 01:27:26 +05:30
|
|
|
return LDAPServer{host, uint16(port), os.Getenv(ldapEnvBindName), os.Getenv(ldapEnvBindPass)}
|
2015-11-11 01:08:40 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestLDAPConnect(t *testing.T) {
|
2016-02-26 01:27:26 +05:30
|
|
|
server := ldapServer(t)
|
|
|
|
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", server.Host, server.Port))
|
2015-11-11 01:08:40 +05:30
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-02-26 01:27:26 +05:30
|
|
|
err = l.Bind(server.BindDN, server.BindPw)
|
2015-11-11 01:08:40 +05:30
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
l.Close()
|
|
|
|
}
|
|
|
|
|
2016-02-26 01:27:26 +05:30
|
|
|
func TestConnectorLDAPHealthy(t *testing.T) {
|
|
|
|
server := ldapServer(t)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
config connector.LDAPConnectorConfig
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
config: connector.LDAPConnectorConfig{
|
|
|
|
ID: "ldap",
|
|
|
|
ServerHost: server.Host,
|
|
|
|
ServerPort: server.Port + 1,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: connector.LDAPConnectorConfig{
|
|
|
|
ID: "ldap",
|
|
|
|
ServerHost: server.Host,
|
|
|
|
ServerPort: server.Port,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: connector.LDAPConnectorConfig{
|
|
|
|
ID: "ldap",
|
|
|
|
ServerHost: server.Host,
|
|
|
|
ServerPort: server.Port,
|
|
|
|
UseTLS: true,
|
|
|
|
CertFile: "/tmp/ldap.crt",
|
|
|
|
KeyFile: "/tmp/ldap.key",
|
|
|
|
CaFile: "/tmp/openldap-ca.pem",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: connector.LDAPConnectorConfig{
|
|
|
|
ID: "ldap",
|
|
|
|
ServerHost: server.Host,
|
|
|
|
ServerPort: server.Port + 247, // 636
|
|
|
|
UseSSL: true,
|
|
|
|
CertFile: "/tmp/ldap.crt",
|
|
|
|
KeyFile: "/tmp/ldap.key",
|
|
|
|
CaFile: "/tmp/openldap-ca.pem",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
templates := template.New(connector.LDAPLoginPageTemplateName)
|
|
|
|
c, err := tt.config.Connector(url.URL{}, nil, templates)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: failed to create connector: %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := c.Healthy(); err != nil {
|
|
|
|
if !tt.wantErr {
|
|
|
|
t.Errorf("case %d: Healthy() returned error: %v", i, err)
|
|
|
|
}
|
|
|
|
} else if tt.wantErr {
|
|
|
|
t.Errorf("case %d: expected Healthy() to fail", i)
|
|
|
|
}
|
2015-11-11 01:08:40 +05:30
|
|
|
}
|
|
|
|
}
|