Skip to content

Commit 29c8ffc

Browse files
committed
Synchronize SSH keys on login with LDAP
1 parent 4586ae3 commit 29c8ffc

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

models/login_source.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,13 @@ func LoginViaLDAP(user *User, login, password string, source *LoginSource, autoR
393393
return nil, ErrUserNotExist{0, login, 0}
394394
}
395395

396+
var isAttributeSSHPublicKeySet = len(strings.TrimSpace(source.LDAP().AttributeSSHPublicKey)) > 0
397+
396398
if !autoRegister {
399+
if isAttributeSSHPublicKeySet && synchronizeLdapSSHPublicKeys(user, source, sr.SSHPublicKey) {
400+
RewriteAllPublicKeys()
401+
}
402+
397403
return user, nil
398404
}
399405

@@ -421,7 +427,14 @@ func LoginViaLDAP(user *User, login, password string, source *LoginSource, autoR
421427
IsActive: true,
422428
IsAdmin: sr.IsAdmin,
423429
}
424-
return user, CreateUser(user)
430+
431+
err := CreateUser(user)
432+
433+
if err == nil && isAttributeSSHPublicKeySet && addLdapSSHPublicKeys(user, source, sr.SSHPublicKey) {
434+
RewriteAllPublicKeys()
435+
}
436+
437+
return user, err
425438
}
426439

427440
// _________ __________________________

models/user.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,8 @@ func deleteKeysMarkedForDeletion(keys []string) (bool, error) {
14211421
return sshKeysNeedUpdate, nil
14221422
}
14231423

1424-
func addLdapSSHPublicKeys(s *LoginSource, usr *User, SSHPublicKeys []string) bool {
1424+
// addLdapSSHPublicKeys add a users public keys. Returns true if there are changes.
1425+
func addLdapSSHPublicKeys(usr *User, s *LoginSource, SSHPublicKeys []string) bool {
14251426
var sshKeysNeedUpdate bool
14261427
for _, sshKey := range SSHPublicKeys {
14271428
_, _, _, _, err := ssh.ParseAuthorizedKey([]byte(sshKey))
@@ -1440,7 +1441,8 @@ func addLdapSSHPublicKeys(s *LoginSource, usr *User, SSHPublicKeys []string) boo
14401441
return sshKeysNeedUpdate
14411442
}
14421443

1443-
func synchronizeLdapSSHPublicKeys(s *LoginSource, SSHPublicKeys []string, usr *User) bool {
1444+
// synchronizeLdapSSHPublicKeys updates a users public keys. Returns true if there are changes.
1445+
func synchronizeLdapSSHPublicKeys(usr *User, s *LoginSource, SSHPublicKeys []string) bool {
14441446
var sshKeysNeedUpdate bool
14451447

14461448
log.Trace("synchronizeLdapSSHPublicKeys[%s]: Handling LDAP Public SSH Key synchronization for user %s", s.Name, usr.Name)
@@ -1479,7 +1481,7 @@ func synchronizeLdapSSHPublicKeys(s *LoginSource, SSHPublicKeys []string, usr *U
14791481
newLdapSSHKeys = append(newLdapSSHKeys, LDAPPublicSSHKey)
14801482
}
14811483
}
1482-
if addLdapSSHPublicKeys(s, usr, newLdapSSHKeys) {
1484+
if addLdapSSHPublicKeys(usr, s, newLdapSSHKeys) {
14831485
sshKeysNeedUpdate = true
14841486
}
14851487

@@ -1581,15 +1583,15 @@ func SyncExternalUsers() {
15811583
log.Error(4, "SyncExternalUsers[%s]: Error creating user %s: %v", s.Name, su.Username, err)
15821584
} else if isAttributeSSHPublicKeySet {
15831585
log.Trace("SyncExternalUsers[%s]: Adding LDAP Public SSH Keys for user %s", s.Name, usr.Name)
1584-
if addLdapSSHPublicKeys(s, usr, su.SSHPublicKey) {
1586+
if addLdapSSHPublicKeys(usr, s, su.SSHPublicKey) {
15851587
sshKeysNeedUpdate = true
15861588
}
15871589
}
15881590
} else if updateExisting {
15891591
existingUsers = append(existingUsers, usr.ID)
15901592

15911593
// Synchronize SSH Public Key if that attribute is set
1592-
if isAttributeSSHPublicKeySet && synchronizeLdapSSHPublicKeys(s, su.SSHPublicKey, usr) {
1594+
if isAttributeSSHPublicKeySet && synchronizeLdapSSHPublicKeys(usr, s, su.SSHPublicKey) {
15931595
sshKeysNeedUpdate = true
15941596
}
15951597

modules/auth/ldap/ldap.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,10 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) *SearchResul
229229
return nil
230230
}
231231

232-
log.Trace("Fetching attributes '%v', '%v', '%v', '%v' with filter %s and base %s", ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail, userFilter, userDN)
232+
log.Trace("Fetching attributes '%v', '%v', '%v', '%v', '%v' with filter %s and base %s", ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail, ls.AttributeSSHPublicKey, userFilter, userDN)
233233
search := ldap.NewSearchRequest(
234234
userDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, userFilter,
235-
[]string{ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail},
235+
[]string{ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail, ls.AttributeSSHPublicKey},
236236
nil)
237237

238238
sr, err := l.Search(search)
@@ -253,6 +253,7 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) *SearchResul
253253
firstname := sr.Entries[0].GetAttributeValue(ls.AttributeName)
254254
surname := sr.Entries[0].GetAttributeValue(ls.AttributeSurname)
255255
mail := sr.Entries[0].GetAttributeValue(ls.AttributeMail)
256+
sshPublicKey := sr.Entries[0].GetAttributeValues(ls.AttributeSSHPublicKey)
256257
isAdmin := checkAdmin(l, ls, userDN)
257258

258259
if !directBind && ls.AttributesInBind {
@@ -264,11 +265,12 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) *SearchResul
264265
}
265266

266267
return &SearchResult{
267-
Username: username,
268-
Name: firstname,
269-
Surname: surname,
270-
Mail: mail,
271-
IsAdmin: isAdmin,
268+
Username: username,
269+
Name: firstname,
270+
Surname: surname,
271+
Mail: mail,
272+
SSHPublicKey: sshPublicKey,
273+
IsAdmin: isAdmin,
272274
}
273275
}
274276

0 commit comments

Comments
 (0)