-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from jimlambrt/jimlambrt-another-refactor-with-…
…tests refactor/tests: another round of improving a few bits and adding tests
- Loading branch information
Showing
15 changed files
with
409 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ Example: | |
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"os/signal" | ||
|
@@ -46,17 +47,18 @@ func main() { | |
log.Fatalf("unable to create router: %s", err.Error()) | ||
} | ||
r.Bind(bindHandler) | ||
r.Search(searchHandler) | ||
s.Router(r) | ||
|
||
go s.Run(":10389") // listen on port 10389 | ||
|
||
// stop server gracefully when ctrl-c, sigint or sigterm occurs | ||
ch := make(chan os.Signal) | ||
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) | ||
<-ch | ||
close(ch) | ||
|
||
s.Stop() | ||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) | ||
defer stop() | ||
select { | ||
case <-ctx.Done(): | ||
log.Printf("\nstopping directory") | ||
s.Stop() | ||
} | ||
} | ||
|
||
func bindHandler(w *gldap.ResponseWriter, r *gldap.Request) { | ||
|
@@ -78,6 +80,51 @@ func bindHandler(w *gldap.ResponseWriter, r *gldap.Request) { | |
log.Println("bind success") | ||
return | ||
} | ||
|
||
func searchHandler(w *gldap.ResponseWriter, r *gldap.Request) { | ||
resp := r.NewSearchDoneResponse() | ||
defer func() { | ||
w.Write(resp) | ||
}() | ||
m, err := r.GetSearchMessage() | ||
if err != nil { | ||
log.Printf("not a search message: %s", err) | ||
return | ||
} | ||
log.Printf("search base dn: %s", m.BaseDN) | ||
log.Printf("search scope: %d", m.Scope) | ||
log.Printf("search filter: %s", m.Filter) | ||
|
||
if strings.Contains(m.Filter, "uid=alice") || m.BaseDN == "uid=alice,ou=people,cn=example,dc=org" { | ||
entry := r.NewSearchResponseEntry( | ||
"uid=alice,ou=people,cn=example,dc=org", | ||
gldap.WithAttributes(map[string][]string{ | ||
"objectclass": {"top", "person", "organizationalPerson", "inetOrgPerson"}, | ||
"uid": {"alice"}, | ||
"cn": {"alice eve smith"}, | ||
"givenname": {"alice"}, | ||
"sn": {"smith"}, | ||
"ou": {"people"}, | ||
"description": {"friend of Rivest, Shamir and Adleman"}, | ||
"password": {"{SSHA}U3waGJVC7MgXYc0YQe7xv7sSePuTP8zN"}, | ||
}), | ||
) | ||
entry.AddAttribute("email", []string{"[email protected]"}) | ||
w.Write(entry) | ||
resp.SetResultCode(gldap.ResultSuccess) | ||
} | ||
if m.BaseDN == "ou=people,cn=example,dc=org" { | ||
entry := r.NewSearchResponseEntry( | ||
"ou=people,cn=example,dc=org", | ||
gldap.WithAttributes(map[string][]string{ | ||
"objectclass": {"organizationalUnit"}, | ||
"ou": {"people"}, | ||
}), | ||
) | ||
w.Write(entry) | ||
resp.SetResultCode(gldap.ResultSuccess) | ||
} | ||
return | ||
} | ||
``` | ||
<hr> | ||
|
@@ -113,3 +160,4 @@ service much easier. | |
`testdirectory` is also a great working example of how you can use `gldap` to build a custom | ||
ldap server to meet your specific needs. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package gldap | ||
|
||
import ber "github.com/go-asn1-ber/asn1-ber" | ||
|
||
// Control defines a common interface for all ldap controls | ||
type Control interface { | ||
// GetControlType returns the OID | ||
GetControlType() string | ||
// Encode returns the ber packet representation | ||
Encode() *ber.Packet | ||
// String returns a human-readable description | ||
String() string | ||
} | ||
|
||
func encodeControls(controls []Control) *ber.Packet { | ||
packet := ber.Encode(ber.ClassContext, ber.TypeConstructed, 0, nil, "Controls") | ||
for _, control := range controls { | ||
packet.AppendChild(control.Encode()) | ||
} | ||
return packet | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
simple |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"log" | ||
"os" | ||
"os/signal" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
"github.com/jimlambrt/gldap" | ||
|
@@ -29,7 +30,7 @@ func main() { | |
log.Fatalf("unable to create router: %s", err.Error()) | ||
} | ||
r.Bind(bindHandler) | ||
r.Search(searchHandler) | ||
r.Search(searchHandler, gldap.WithLabel("All Searches")) | ||
s.Router(r) | ||
go s.Run(":10389") // listen on port 10389 | ||
|
||
|
@@ -64,7 +65,7 @@ func bindHandler(w *gldap.ResponseWriter, r *gldap.Request) { | |
} | ||
|
||
func searchHandler(w *gldap.ResponseWriter, r *gldap.Request) { | ||
resp := r.NewSearchDoneResponse() | ||
resp := r.NewSearchDoneResponse(gldap.WithResponseCode(gldap.ResultNoSuchObject)) | ||
defer func() { | ||
w.Write(resp) | ||
}() | ||
|
@@ -77,5 +78,34 @@ func searchHandler(w *gldap.ResponseWriter, r *gldap.Request) { | |
log.Printf("search scope: %d", m.Scope) | ||
log.Printf("search filter: %s", m.Filter) | ||
|
||
if strings.Contains(m.Filter, "uid=alice") || m.BaseDN == "uid=alice,ou=people,cn=example,dc=org" { | ||
entry := r.NewSearchResponseEntry( | ||
"uid=alice,ou=people,cn=example,dc=org", | ||
gldap.WithAttributes(map[string][]string{ | ||
"objectclass": {"top", "person", "organizationalPerson", "inetOrgPerson"}, | ||
"uid": {"alice"}, | ||
"cn": {"alice eve smith"}, | ||
"givenname": {"alice"}, | ||
"sn": {"smith"}, | ||
"ou": {"people"}, | ||
"description": {"friend of Rivest, Shamir and Adleman"}, | ||
"password": {"{SSHA}U3waGJVC7MgXYc0YQe7xv7sSePuTP8zN"}, | ||
}), | ||
) | ||
entry.AddAttribute("email", []string{"[email protected]"}) | ||
w.Write(entry) | ||
resp.SetResultCode(gldap.ResultSuccess) | ||
} | ||
if m.BaseDN == "ou=people,cn=example,dc=org" { | ||
entry := r.NewSearchResponseEntry( | ||
"ou=people,cn=example,dc=org", | ||
gldap.WithAttributes(map[string][]string{ | ||
"objectclass": {"organizationalUnit"}, | ||
"ou": {"people"}, | ||
}), | ||
) | ||
w.Write(entry) | ||
resp.SetResultCode(gldap.ResultSuccess) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package gldap | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-ldap/ldap/v3" | ||
"github.com/hashicorp/go-hclog" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMux_serve(t *testing.T) { | ||
t.Run("no-matching-handler", func(t *testing.T) { | ||
assert, require := assert.New(t), require.New(t) | ||
var buf bytes.Buffer | ||
testLogger := hclog.New(&hclog.LoggerOptions{ | ||
Name: "TestServer_Run-logger", | ||
Level: hclog.Debug, | ||
Output: &buf, | ||
}) | ||
s, err := NewServer(WithLogger(testLogger)) | ||
require.NoError(err) | ||
port := freePort(t) | ||
go func() { | ||
err = s.Run(fmt.Sprintf(":%d", port)) | ||
assert.NoError(err) | ||
}() | ||
defer s.Stop() | ||
time.Sleep(1 * time.Millisecond) | ||
client, err := ldap.DialURL(fmt.Sprintf("ldap://localhost:%d", port)) | ||
require.NoError(err) | ||
defer client.Close() | ||
err = client.UnauthenticatedBind("alice") | ||
require.Error(err) | ||
assert.Contains(strings.ToLower(err.Error()), "no matching handler found") | ||
}) | ||
t.Run("default-route", func(t *testing.T) { | ||
assert, require := assert.New(t), require.New(t) | ||
var buf bytes.Buffer | ||
testLogger := hclog.New(&hclog.LoggerOptions{ | ||
Name: "TestServer_Run-logger", | ||
Level: hclog.Debug, | ||
Output: &buf, | ||
}) | ||
s, err := NewServer(WithLogger(testLogger)) | ||
require.NoError(err) | ||
|
||
mux, err := NewMux() | ||
require.NoError(err) | ||
mux.DefaultRoute(func(w *ResponseWriter, req *Request) { | ||
resp := req.NewResponse(WithResponseCode(ResultUnwillingToPerform), WithDiagnosticMessage("default handler")) | ||
_ = w.Write(resp) | ||
}) | ||
s.Router(mux) | ||
|
||
port := freePort(t) | ||
go func() { | ||
err = s.Run(fmt.Sprintf(":%d", port)) | ||
assert.NoError(err) | ||
}() | ||
defer s.Stop() | ||
time.Sleep(1 * time.Millisecond) | ||
client, err := ldap.DialURL(fmt.Sprintf("ldap://localhost:%d", port)) | ||
require.NoError(err) | ||
defer client.Close() | ||
err = client.UnauthenticatedBind("alice") | ||
require.Error(err) | ||
assert.Contains(strings.ToLower(err.Error()), "default handler") | ||
}) | ||
t.Run("bad-parameters", func(t *testing.T) { | ||
assert, require := assert.New(t), require.New(t) | ||
m := &Mux{} | ||
assert.Panics( | ||
func() { m.serve(nil, &Request{}) }, | ||
"missing response writer", | ||
) | ||
|
||
var writerBuf bytes.Buffer | ||
var logBuf bytes.Buffer | ||
testLogger := hclog.New(&hclog.LoggerOptions{ | ||
Name: "TestServer_Run-logger", | ||
Level: hclog.Debug, | ||
Output: &logBuf, | ||
}) | ||
w, err := newResponseWriter(bufio.NewWriter(&writerBuf), &sync.Mutex{}, testLogger, 1, 2) | ||
require.NoError(err) | ||
m.serve(w, nil) | ||
assert.Contains(logBuf.String(), "missing request") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.