@@ -19,10 +19,12 @@ import (
1919 "github.com/AdRoll/hologram/protocol"
2020 "github.com/AdRoll/hologram/server"
2121 "github.com/goamz/goamz/sts"
22+ "github.com/nmcclain/ldap"
2223 "github.com/peterbourgon/g2s"
2324 . "github.com/smartystreets/goconvey/convey"
2425 "golang.org/x/crypto/ssh"
2526 "io"
27+ "reflect"
2628 "testing"
2729 "time"
2830)
@@ -76,10 +78,57 @@ func (*dummyCredentials) AssumeRole(user *server.User, role string) (*sts.Creden
7678 }, nil
7779}
7880
81+ type DummyLDAP struct {
82+ username string
83+ password string
84+ sshKeys []string
85+ req * ldap.ModifyRequest
86+ }
87+
88+ func (l * DummyLDAP ) Search (* ldap.SearchRequest ) (* ldap.SearchResult , error ) {
89+ return & ldap.SearchResult {
90+ Entries : []* ldap.Entry {
91+ & ldap.Entry {DN : "something" ,
92+ Attributes : []* ldap.EntryAttribute {
93+ & ldap.EntryAttribute {
94+ Name : "cn" ,
95+ Values : []string {l .username },
96+ },
97+ & ldap.EntryAttribute {
98+ Name : "userPassword" ,
99+ Values : []string {l .password },
100+ },
101+ & ldap.EntryAttribute {
102+ Name : "sshPublicKey" ,
103+ Values : l .sshKeys ,
104+ },
105+ },
106+ },
107+ },
108+ }, nil
109+ }
110+
111+ func (l * DummyLDAP ) Modify (mr * ldap.ModifyRequest ) error {
112+ if reflect .DeepEqual (mr , l .req ) {
113+ l .sshKeys = []string {"test" }
114+ }
115+ return nil
116+ }
117+
79118func TestServerStateMachine (t * testing.T ) {
119+ // This silly thing is needed for equality testing for the LDAP dummy.
120+ neededModifyRequest := ldap .NewModifyRequest ("something" )
121+ neededModifyRequest .Add ("sshPublicKey" , []string {"test" })
122+
80123 Convey ("Given a state machine setup with a null logger" , t , func () {
81124 authenticator := & DummyAuthenticator {& server.User {Username : "words" }}
82- testServer := server .New (authenticator , & dummyCredentials {}, "default" , g2s .Noop ())
125+ ldap := & DummyLDAP {
126+ username : "ari.adair" ,
127+ password : "098f6bcd4621d373cade4e832627b4f6" ,
128+ sshKeys : []string {},
129+ req : neededModifyRequest ,
130+ }
131+ testServer := server .New (authenticator , & dummyCredentials {}, "default" , g2s .Noop (), ldap )
83132 r , w := io .Pipe ()
84133
85134 testConnection := protocol .NewMessageConnection (ReadWriter (r , w ))
@@ -180,5 +229,41 @@ func TestServerStateMachine(t *testing.T) {
180229 So (credsMsg .GetServerResponse ().GetVerificationFailure (), ShouldNotBeNil )
181230 })
182231 })
232+
233+ Convey ("When a request to add an SSH key comes in" , func () {
234+ user := "ari.adair"
235+ password := "098f6bcd4621d373cade4e832627b4f6"
236+ sshKey := "test"
237+ testMessage := & protocol.Message {
238+ ServerRequest : & protocol.ServerRequest {
239+ AddSSHkey : & protocol.AddSSHKey {
240+ Username : & user ,
241+ Passwordhash : & password ,
242+ Sshkeybytes : & sshKey ,
243+ },
244+ },
245+ }
246+
247+ testConnection .Write (testMessage )
248+ Convey ("If this request is valid" , func () {
249+ msg , err := testConnection .Read ()
250+ if err != nil {
251+ t .Fatal (err )
252+ }
253+
254+ if msg .GetSuccess () == nil {
255+ t .Fail ()
256+ }
257+ Convey ("It should add the SSH key to the user." , func () {
258+ So (ldap .sshKeys [0 ], ShouldEqual , sshKey )
259+ Convey ("If the user tries to add the same SSH key" , func () {
260+ testConnection .Write (testMessage )
261+ Convey ("It should not insert the same key twice." , func () {
262+ So (len (ldap .sshKeys ), ShouldEqual , 1 )
263+ })
264+ })
265+ })
266+ })
267+ })
183268 })
184269}
0 commit comments