Secure storage and transmission of LDAP service credentials
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"gopkg.in/ldap.v2"
)
func main() {
r := gin.Default()
r.GET("/ldap", func(c *gin.Context) {
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
if err != nil {
c.JSON(500, gin.H{"error": "Unable to connect to LDAP server"})
return
}
defer l.Close()
err = l.Bind("cn=read-only-admin,dc=example,dc=com", "password")
if err != nil {
c.JSON(500, gin.H{"error": "Unable to bind to LDAP server"})
return
}
c.JSON(200, gin.H{"message": "Successfully connected to LDAP server"})
})
r.Run(":8080")
}
The above code is a simple Go application using the Gin framework and the
ldap.v2
package to connect to an LDAP server. The application exposes an endpoint
/ldap
which when hit, connects to an LDAP server, binds to it with a username and password, and sends a response to the client.
The vulnerability lies in the line:
err = l.Bind("cn=read-only-admin,dc=example,dc=com", "password")
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"gopkg.in/ldap.v2"
"os"
)
func main() {
r := gin.Default()
r.GET("/ldap", func(c *gin.Context) {
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", os.Getenv("LDAP_HOST"), 389))
if err != nil {
c.JSON(500, gin.H{"error": "Unable to connect to LDAP server"})
return
}
defer l.Close()
err = l.Bind(os.Getenv("LDAP_BIND_DN"), os.Getenv("LDAP_PASSWORD"))
if err != nil {
c.JSON(500, gin.H{"error": "Unable to bind to LDAP server"})
return
}
c.JSON(200, gin.H{"message": "Successfully connected to LDAP server"})
})
r.Run(":8080")
}
The original code had a vulnerability where the LDAP service credentials were exposed in plain text. This is a security risk as anyone with access to the code could see these credentials and potentially misuse them.
The updated code fixes this vulnerability by removing the hard-coded LDAP service credentials and instead retrieving them from environment variables. This is a more secure method of handling sensitive information such as credentials.
The
os.Getenv
function is used to retrieve the values of the
LDAP_HOST
,
LDAP_BIND_DN
, and
LDAP_PASSWORD
environment variables. These environment variables should be set in the environment where the application is running.
The
LDAP_HOST
environment variable should contain the host of the LDAP server. The
LDAP_BIND_DN
environment variable should contain the DN (Distinguished Name) to bind to the LDAP server. The
LDAP_PASSWORD
environment variable should contain the password to bind to the LDAP server.
By storing these credentials in environment variables, they are not exposed in the code and are only accessible to authorized users who have access to the environment where the application is running.
It's important to note that these environment variables should be protected and not exposed in any logs or error messages. They should also not be stored in the source code repository or any other place where they could be accessed by unauthorized users.
After updating the code, it's recommended to test the application to verify that it can still connect to the LDAP server using the credentials stored in the environment variables.