Archived
Compare commits
24
Commits
v1.0.2
..
1c11acface
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c11acface | ||
|
|
481922d384 | ||
|
|
fb06f6b358 | ||
|
|
b42ce808ab | ||
|
|
067e327eb8 | ||
|
|
04e8f0cac3 | ||
|
|
677f52b135 | ||
|
|
981388784b | ||
|
|
2b15c04be0 | ||
|
|
bc0001dbb8 | ||
|
|
d43520ba95 | ||
|
|
34f9ff99ee | ||
|
|
c0fc119dc2 | ||
|
|
8edfbe1ace | ||
|
|
ec6eb5ec8b | ||
|
|
595d18b72f | ||
|
|
b63cce671c | ||
|
|
77c556aa67 | ||
|
|
ee666f6e08 | ||
|
|
87a42299e6 | ||
|
|
3ce798aced | ||
|
|
b4ee79589b | ||
|
|
9d6f62b4a3 | ||
|
|
d541062eda |
@@ -1,5 +1,5 @@
|
|||||||
build: clean
|
build: clean
|
||||||
CGO_ENABLED=0 go build -ldflags="-s -w" -o dist/ .
|
go build -ldflags="-s -w" -o dist/ .
|
||||||
|
|
||||||
test: clean
|
test: clean
|
||||||
go run .
|
go run .
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
# ProxmoxAAS LDAP - Simple REST API for LDAP
|
|
||||||
|
|
||||||
ProxmoxAAS LDAP provides a simple API for managing users and groups in a simplified LDAP server. Expected LDAP configuration can be initialized using [open-ldap-setup](https://git.tronnet.net/tronnet/open-ldap-setup).
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
### Prerequisites
|
|
||||||
|
|
||||||
- Initialized LDAP server with the following configuration
|
|
||||||
- Structure
|
|
||||||
- Users: ou=people,...
|
|
||||||
- objectType: inetOrgPerson
|
|
||||||
- At least 1 user which is a member of admin group
|
|
||||||
- Groups: ou=groups,...
|
|
||||||
- objectType: groupOfNames
|
|
||||||
- At least 1 admin group
|
|
||||||
- Permissions:
|
|
||||||
- Admin group should have write access
|
|
||||||
- Users should have write access to own attributes (cn, sn, userPassword)
|
|
||||||
- Enable anonymous binding
|
|
||||||
- Load MemberOf Policy:
|
|
||||||
- olcMemberOfDangling: ignore
|
|
||||||
- olcMemberOfRefInt: TRUE
|
|
||||||
- olcMemberOfGroupOC: groupOfNames
|
|
||||||
- olcMemberOfMemberAD: member
|
|
||||||
- olcMemberOfMemberOfAD: memberOf
|
|
||||||
- Password Policy and TLS are recommended but not required
|
|
||||||
|
|
||||||
### Installation
|
|
||||||
|
|
||||||
1. Download `proxmoxaas-ldap` binary and `template.config.json` file from [releases](releases)
|
|
||||||
2. Rename `template.config.json` to `config.json` and modify:
|
|
||||||
- ldapURL: url to the ldap server ie. `ldap://ldap.domain.net`
|
|
||||||
- baseDN: base DN ie. `dc=domain,dc=net`
|
|
||||||
- sessionSecretKey: random value used to randomize cookie values, replace with any sufficiently large random string
|
|
||||||
3. Run the binary
|
|
||||||
+9
-18
@@ -15,7 +15,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var LDAPSessions map[string]*LDAPClient
|
var LDAPSessions map[string]*LDAPClient
|
||||||
var APIVersion = "1.0.2"
|
|
||||||
|
|
||||||
func Run() {
|
func Run() {
|
||||||
gob.Register(LDAPClient{})
|
gob.Register(LDAPClient{})
|
||||||
@@ -39,10 +38,6 @@ func Run() {
|
|||||||
|
|
||||||
LDAPSessions = make(map[string]*LDAPClient)
|
LDAPSessions = make(map[string]*LDAPClient)
|
||||||
|
|
||||||
router.GET("/version", func(c *gin.Context) {
|
|
||||||
c.JSON(http.StatusOK, gin.H{"version": APIVersion})
|
|
||||||
})
|
|
||||||
|
|
||||||
router.POST("/ticket", func(c *gin.Context) {
|
router.POST("/ticket", func(c *gin.Context) {
|
||||||
var body Login
|
var body Login
|
||||||
if err := c.ShouldBind(&body); err != nil { // bad request from binding
|
if err := c.ShouldBind(&body); err != nil { // bad request from binding
|
||||||
@@ -122,22 +117,18 @@ func Run() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var body User
|
||||||
|
if err := c.ShouldBind(&body); err != nil { // bad request from binding
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"auth": false, "error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// check if user already exists
|
// check if user already exists
|
||||||
status, res := LDAPSession.GetUser(c.Param("userid"))
|
status, res := LDAPSession.GetUser(c.Param("userid"))
|
||||||
if status != 200 && ldap.IsErrorWithCode(res["error"].(error), ldap.LDAPResultNoSuchObject) { // user does not already exist, create new user
|
if status != 200 && ldap.IsErrorWithCode(res["error"].(error), ldap.LDAPResultNoSuchObject) { // user does not already exist, create new user
|
||||||
var body UserRequired // all user attributes required for new users
|
|
||||||
if err := c.ShouldBind(&body); err != nil { // attempt to bind user data
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"auth": false, "error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
status, res = LDAPSession.AddUser(c.Param("userid"), body)
|
status, res = LDAPSession.AddUser(c.Param("userid"), body)
|
||||||
c.JSON(status, res)
|
c.JSON(status, res)
|
||||||
} else { // user already exists, attempt to modify user
|
} else { // user already exists, attempt to modify user
|
||||||
var body UserOptional // all user attributes optional for new users
|
|
||||||
if err := c.ShouldBind(&body); err != nil { // attempt to bind user data
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"auth": false, "error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
status, res = LDAPSession.ModUser(c.Param("userid"), body)
|
status, res = LDAPSession.ModUser(c.Param("userid"), body)
|
||||||
c.JSON(status, res)
|
c.JSON(status, res)
|
||||||
}
|
}
|
||||||
@@ -235,12 +226,12 @@ func Run() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if group already exists
|
// check if user already exists
|
||||||
status, res := LDAPSession.GetGroup(c.Param("groupid"))
|
status, res := LDAPSession.GetGroup(c.Param("groupid"))
|
||||||
if status != 200 && ldap.IsErrorWithCode(res["error"].(error), ldap.LDAPResultNoSuchObject) { // group does not already exist, create new group
|
if status != 200 && ldap.IsErrorWithCode(res["error"].(error), ldap.LDAPResultNoSuchObject) { // user does not already exist, create new user
|
||||||
status, res = LDAPSession.AddGroup(c.Param("groupid"), body)
|
status, res = LDAPSession.AddGroup(c.Param("groupid"), body)
|
||||||
c.JSON(status, res)
|
c.JSON(status, res)
|
||||||
} else { // group already exists, attempt to modify group
|
} else { // user already exists, attempt to modify user
|
||||||
status, res = LDAPSession.ModGroup(c.Param("groupid"), body)
|
status, res = LDAPSession.ModGroup(c.Param("groupid"), body)
|
||||||
c.JSON(status, res)
|
c.JSON(status, res)
|
||||||
}
|
}
|
||||||
|
|||||||
+63
-43
@@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/go-ldap/ldap/v3"
|
"github.com/go-ldap/ldap/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LDAPClient wrapper struct containing the connection, baseDN, peopleDN, and groupsDN
|
|
||||||
type LDAPClient struct {
|
type LDAPClient struct {
|
||||||
client *ldap.Conn
|
client *ldap.Conn
|
||||||
basedn string
|
basedn string
|
||||||
@@ -16,7 +15,6 @@ type LDAPClient struct {
|
|||||||
groupsdn string
|
groupsdn string
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns a new LDAPClient from the config
|
|
||||||
func NewLDAPClient(config Config) (*LDAPClient, error) {
|
func NewLDAPClient(config Config) (*LDAPClient, error) {
|
||||||
LDAPConn, err := ldap.DialURL(config.LdapURL)
|
LDAPConn, err := ldap.DialURL(config.LdapURL)
|
||||||
return &LDAPClient{
|
return &LDAPClient{
|
||||||
@@ -27,7 +25,6 @@ func NewLDAPClient(config Config) (*LDAPClient, error) {
|
|||||||
}, err
|
}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// bind a user using username and password to the LDAPClient
|
|
||||||
func (l LDAPClient) BindUser(username string, password string) error {
|
func (l LDAPClient) BindUser(username string, password string) error {
|
||||||
userdn := fmt.Sprintf("uid=%s,%s", username, l.peopledn)
|
userdn := fmt.Sprintf("uid=%s,%s", username, l.peopledn)
|
||||||
return l.client.Bind(userdn, password)
|
return l.client.Bind(userdn, password)
|
||||||
@@ -37,8 +34,8 @@ func (l LDAPClient) GetAllUsers() (int, gin.H) {
|
|||||||
searchRequest := ldap.NewSearchRequest(
|
searchRequest := ldap.NewSearchRequest(
|
||||||
l.peopledn, // The base dn to search
|
l.peopledn, // The base dn to search
|
||||||
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
||||||
"(&(objectClass=inetOrgPerson))", // The filter to apply
|
"(&(objectClass=inetOrgPerson))", // The filter to apply
|
||||||
[]string{"dn", "cn", "sn", "mail", "uid", "memberOf"}, // A list attributes to retrieve
|
[]string{"dn", "cn", "sn", "mail", "uid"}, // A list attributes to retrieve
|
||||||
nil,
|
nil,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -53,8 +50,15 @@ func (l LDAPClient) GetAllUsers() (int, gin.H) {
|
|||||||
var results = []gin.H{} // create list of results
|
var results = []gin.H{} // create list of results
|
||||||
|
|
||||||
for _, entry := range searchResponse.Entries { // for each result,
|
for _, entry := range searchResponse.Entries { // for each result,
|
||||||
user := LDAPEntryToLDAPUser(entry)
|
results = append(results, gin.H{
|
||||||
results = append(results, LDAPUserToGin(user))
|
"dn": entry.DN,
|
||||||
|
"attributes": gin.H{
|
||||||
|
"cn": entry.GetAttributeValue("cn"),
|
||||||
|
"sn": entry.GetAttributeValue("sn"),
|
||||||
|
"mail": entry.GetAttributeValue("mail"),
|
||||||
|
"uid": entry.GetAttributeValue("uid"),
|
||||||
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return http.StatusOK, gin.H{
|
return http.StatusOK, gin.H{
|
||||||
@@ -64,36 +68,7 @@ func (l LDAPClient) GetAllUsers() (int, gin.H) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l LDAPClient) GetUser(uid string) (int, gin.H) {
|
func (l LDAPClient) AddUser(uid string, user User) (int, gin.H) {
|
||||||
searchRequest := ldap.NewSearchRequest( // setup search for user by uid
|
|
||||||
fmt.Sprintf("uid=%s,%s", uid, l.peopledn), // The base dn to search
|
|
||||||
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
|
||||||
"(&(objectClass=inetOrgPerson))", // The filter to apply
|
|
||||||
[]string{"dn", "cn", "sn", "mail", "uid", "memberOf"}, // A list attributes to retrieve
|
|
||||||
nil,
|
|
||||||
)
|
|
||||||
|
|
||||||
searchResponse, err := l.client.Search(searchRequest) // perform search
|
|
||||||
if err != nil {
|
|
||||||
return http.StatusBadRequest, gin.H{
|
|
||||||
"ok": false,
|
|
||||||
"error": err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
entry := searchResponse.Entries[0]
|
|
||||||
|
|
||||||
user := LDAPEntryToLDAPUser(entry)
|
|
||||||
result := LDAPUserToGin(user)
|
|
||||||
|
|
||||||
return http.StatusOK, gin.H{
|
|
||||||
"ok": true,
|
|
||||||
"error": nil,
|
|
||||||
"user": result,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l LDAPClient) AddUser(uid string, user UserRequired) (int, gin.H) {
|
|
||||||
if user.CN == "" || user.SN == "" || user.UserPassword == "" {
|
if user.CN == "" || user.SN == "" || user.UserPassword == "" {
|
||||||
return http.StatusBadRequest, gin.H{
|
return http.StatusBadRequest, gin.H{
|
||||||
"ok": false,
|
"ok": false,
|
||||||
@@ -107,7 +82,7 @@ func (l LDAPClient) AddUser(uid string, user UserRequired) (int, gin.H) {
|
|||||||
)
|
)
|
||||||
addRequest.Attribute("sn", []string{user.SN})
|
addRequest.Attribute("sn", []string{user.SN})
|
||||||
addRequest.Attribute("cn", []string{user.CN})
|
addRequest.Attribute("cn", []string{user.CN})
|
||||||
addRequest.Attribute("userPassword", []string{user.UserPassword})
|
addRequest.Attribute("userPassword", []string{user.CN})
|
||||||
addRequest.Attribute("objectClass", []string{"inetOrgPerson"})
|
addRequest.Attribute("objectClass", []string{"inetOrgPerson"})
|
||||||
|
|
||||||
err := l.client.Add(addRequest)
|
err := l.client.Add(addRequest)
|
||||||
@@ -124,7 +99,42 @@ func (l LDAPClient) AddUser(uid string, user UserRequired) (int, gin.H) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l LDAPClient) ModUser(uid string, user UserOptional) (int, gin.H) {
|
func (l LDAPClient) GetUser(uid string) (int, gin.H) {
|
||||||
|
searchRequest := ldap.NewSearchRequest( // setup search for user by uid
|
||||||
|
fmt.Sprintf("uid=%s,%s", uid, l.peopledn), // The base dn to search
|
||||||
|
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
||||||
|
"(&(objectClass=inetOrgPerson))", // The filter to apply
|
||||||
|
[]string{"dn", "cn", "sn", "mail", "uid"}, // A list attributes to retrieve
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
|
||||||
|
searchResponse, err := l.client.Search(searchRequest) // perform search
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusBadRequest, gin.H{
|
||||||
|
"ok": false,
|
||||||
|
"error": err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := searchResponse.Entries[0]
|
||||||
|
result := gin.H{
|
||||||
|
"dn": entry.DN,
|
||||||
|
"attributes": gin.H{
|
||||||
|
"cn": entry.GetAttributeValue("cn"),
|
||||||
|
"sn": entry.GetAttributeValue("sn"),
|
||||||
|
"mail": entry.GetAttributeValue("mail"),
|
||||||
|
"uid": entry.GetAttributeValue("uid"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return http.StatusOK, gin.H{
|
||||||
|
"ok": true,
|
||||||
|
"error": nil,
|
||||||
|
"user": result,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l LDAPClient) ModUser(uid string, user User) (int, gin.H) {
|
||||||
if user.CN == "" && user.SN == "" && user.UserPassword == "" {
|
if user.CN == "" && user.SN == "" && user.UserPassword == "" {
|
||||||
return http.StatusBadRequest, gin.H{
|
return http.StatusBadRequest, gin.H{
|
||||||
"ok": false,
|
"ok": false,
|
||||||
@@ -204,8 +214,13 @@ func (l LDAPClient) GetAllGroups() (int, gin.H) {
|
|||||||
var results = []gin.H{} // create list of results
|
var results = []gin.H{} // create list of results
|
||||||
|
|
||||||
for _, entry := range searchResponse.Entries { // for each result,
|
for _, entry := range searchResponse.Entries { // for each result,
|
||||||
group := LDAPEntryToLDAPGroup(entry)
|
results = append(results, gin.H{
|
||||||
results = append(results, LDAPGroupToGin(group))
|
"dn": entry.DN,
|
||||||
|
"attributes": gin.H{
|
||||||
|
"cn": entry.GetAttributeValue("cn"),
|
||||||
|
"member": entry.GetAttributeValues("member"),
|
||||||
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return http.StatusOK, gin.H{
|
return http.StatusOK, gin.H{
|
||||||
@@ -233,8 +248,13 @@ func (l LDAPClient) GetGroup(gid string) (int, gin.H) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
entry := searchResponse.Entries[0]
|
entry := searchResponse.Entries[0]
|
||||||
group := LDAPEntryToLDAPGroup(entry)
|
result := gin.H{
|
||||||
result := LDAPGroupToGin(group)
|
"dn": entry.DN,
|
||||||
|
"attributes": gin.H{
|
||||||
|
"cn": entry.GetAttributeValue("cn"),
|
||||||
|
"member": entry.GetAttributeValues("member"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
return http.StatusOK, gin.H{
|
return http.StatusOK, gin.H{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
|
|||||||
+1
-79
@@ -4,9 +4,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/go-ldap/ldap/v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@@ -41,86 +38,11 @@ type Login struct { // login body struct
|
|||||||
Password string `form:"password" binding:"required"`
|
Password string `form:"password" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LDAPUserAttributes struct {
|
type User struct { // add or modify user body struct
|
||||||
CN string
|
|
||||||
SN string
|
|
||||||
Mail string
|
|
||||||
UID string
|
|
||||||
MemberOf []string
|
|
||||||
}
|
|
||||||
|
|
||||||
type LDAPUser struct {
|
|
||||||
DN string
|
|
||||||
Attributes LDAPUserAttributes
|
|
||||||
}
|
|
||||||
|
|
||||||
func LDAPEntryToLDAPUser(entry *ldap.Entry) LDAPUser {
|
|
||||||
return LDAPUser{
|
|
||||||
DN: entry.DN,
|
|
||||||
Attributes: LDAPUserAttributes{
|
|
||||||
CN: entry.GetAttributeValue("cn"),
|
|
||||||
SN: entry.GetAttributeValue("sn"),
|
|
||||||
Mail: entry.GetAttributeValue("mail"),
|
|
||||||
UID: entry.GetAttributeValue("uid"),
|
|
||||||
MemberOf: entry.GetAttributeValues("memberOf"),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func LDAPUserToGin(user LDAPUser) gin.H {
|
|
||||||
return gin.H{
|
|
||||||
"dn": user.DN,
|
|
||||||
"attributes": gin.H{
|
|
||||||
"cn": user.Attributes.CN,
|
|
||||||
"sn": user.Attributes.SN,
|
|
||||||
"mail": user.Attributes.Mail,
|
|
||||||
"uid": user.Attributes.UID,
|
|
||||||
"memberOf": user.Attributes.MemberOf,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type LDAPGroupAttributes struct {
|
|
||||||
CN string
|
|
||||||
Member []string
|
|
||||||
}
|
|
||||||
|
|
||||||
type LDAPGroup struct {
|
|
||||||
DN string
|
|
||||||
Attributes LDAPGroupAttributes
|
|
||||||
}
|
|
||||||
|
|
||||||
func LDAPEntryToLDAPGroup(entry *ldap.Entry) LDAPGroup {
|
|
||||||
return LDAPGroup{
|
|
||||||
DN: entry.DN,
|
|
||||||
Attributes: LDAPGroupAttributes{
|
|
||||||
CN: entry.GetAttributeValue("cn"),
|
|
||||||
Member: entry.GetAttributeValues("member"),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func LDAPGroupToGin(group LDAPGroup) gin.H {
|
|
||||||
return gin.H{
|
|
||||||
"dn": group.DN,
|
|
||||||
"attributes": gin.H{
|
|
||||||
"cn": group.Attributes.CN,
|
|
||||||
"member": group.Attributes.Member,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type UserOptional struct { // add or modify user body struct
|
|
||||||
CN string `form:"cn"`
|
CN string `form:"cn"`
|
||||||
SN string `form:"sn"`
|
SN string `form:"sn"`
|
||||||
UserPassword string `form:"userpassword"`
|
UserPassword string `form:"userpassword"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserRequired struct { // add or modify user body struct
|
|
||||||
CN string `form:"cn" binding:"required"`
|
|
||||||
SN string `form:"sn" binding:"required"`
|
|
||||||
UserPassword string `form:"userpassword" binding:"required"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Group struct { // add or modify group body struct
|
type Group struct { // add or modify group body struct
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,6 @@
|
|||||||
"path": "/",
|
"path": "/",
|
||||||
"httpOnly": true,
|
"httpOnly": true,
|
||||||
"secure": false,
|
"secure": false,
|
||||||
"maxAge": 7200
|
"maxAge": 7200000
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user