Просмотр исходного кода

fix: Fix/ldap tls (#162)

* optimize and fix tls of LDAP

* fix
Tao Chen месяцев назад: 11
Родитель
Сommit
77d7b43e21
4 измененных файлов с 31 добавлено и 8 удалено
  1. 1 0
      Dockerfile.dev
  2. 1 1
      conf/config.yaml
  3. 1 1
      config/ldap.go
  4. 28 6
      service/ldap.go

+ 1 - 0
Dockerfile.dev

@@ -76,6 +76,7 @@ COPY --from=builder-backend /app/release /app/
76 76
 COPY --from=builder-backend /app/conf /app/conf/
77 77
 COPY --from=builder-backend /app/resources /app/resources/
78 78
 COPY --from=builder-backend /app/docs /app/docs/
79
+COPY --from=builder-backend /app/http/templates /app/http/templates
79 80
 # Copy frontend build from builder2 stage
80 81
 COPY --from=builder-admin-frontend /frontend/dist/ /app/resources/admin/
81 82
 

+ 1 - 1
conf/config.yaml

@@ -45,7 +45,7 @@ jwt:
45 45
 ldap:
46 46
   enable: false
47 47
   url: "ldap://ldap.example.com:389"
48
-  tls: false
48
+  tls-ca-file: ""
49 49
   tls-verify: false
50 50
   base-dn: "dc=example,dc=com"
51 51
   bind-dn: "cn=admin,dc=example,dc=com"

+ 1 - 1
config/ldap.go

@@ -26,7 +26,7 @@ type LdapUser struct {
26 26
 type Ldap struct {
27 27
 	Enable       bool     `mapstructure:"enable"`
28 28
 	Url          string   `mapstructure:"url"`
29
-	TLS          bool     `mapstructure:"tls"`
29
+	TlsCaFile    string   `mapstructure:"tls-ca-file"`
30 30
 	TlsVerify    bool     `mapstructure:"tls-verify"`
31 31
 	BaseDn       string   `mapstructure:"base-dn"`
32 32
 	BindDn       string   `mapstructure:"bind-dn"`

+ 28 - 6
service/ldap.go

@@ -2,8 +2,11 @@ package service
2 2
 
3 3
 import (
4 4
 	"crypto/tls"
5
+	"crypto/x509"
5 6
 	"errors"
6 7
 	"fmt"
8
+	"net/url"
9
+	"os"
7 10
 	"strconv"
8 11
 	"strings"
9 12
 
@@ -14,6 +17,8 @@ import (
14 17
 )
15 18
 
16 19
 var (
20
+	ErrUrlParseFailed        = errors.New("UrlParseFailed")
21
+	ErrFileReadFailed        = errors.New("FileReadFailed")
17 22
 	ErrLdapNotEnabled        = errors.New("LdapNotEnabled")
18 23
 	ErrLdapUserDisabled      = errors.New("UserDisabledAtLdap")
19 24
 	ErrLdapUserNotFound      = errors.New("UserNotFound")
@@ -67,21 +72,38 @@ func (lu *LdapUser) ToUser(u *model.User) *model.User {
67 72
 
68 73
 // connectAndBind creates an LDAP connection, optionally starts TLS, and then binds using the provided credentials.
69 74
 func (ls *LdapService) connectAndBind(cfg *config.Ldap, username, password string) (*ldap.Conn, error) {
70
-	conn, err := ldap.DialURL(cfg.Url)
75
+	u, err := url.Parse(cfg.Url)
71 76
 	if err != nil {
72
-		return nil, errors.Join(ErrLdapConnectFailed, err)
77
+		return nil, errors.Join(ErrUrlParseFailed, err)
73 78
 	}
74 79
 
75
-	if cfg.TLS {
80
+	var conn *ldap.Conn
81
+	if u.Scheme == "ldaps" {
76 82
 		// WARNING: InsecureSkipVerify: true is not recommended for production
77
-		if err = conn.StartTLS(&tls.Config{InsecureSkipVerify: !cfg.TlsVerify}); err != nil {
78
-			conn.Close()
79
-			return nil, errors.Join(ErrLdapTlsFailed, err)
83
+		tlsConfig := &tls.Config{InsecureSkipVerify: !cfg.TlsVerify}
84
+		if cfg.TlsCaFile != "" {
85
+			caCert, err := os.ReadFile(cfg.TlsCaFile)
86
+			if err != nil {
87
+				return nil, errors.Join(ErrFileReadFailed, err)
88
+			}
89
+			caCertPool := x509.NewCertPool()
90
+			if !caCertPool.AppendCertsFromPEM(caCert) {
91
+				return nil, errors.Join(ErrLdapTlsFailed, errors.New("failed to append CA certificate"))
92
+			}
93
+			tlsConfig.RootCAs = caCertPool
80 94
 		}
95
+		conn, err = ldap.DialURL(cfg.Url, ldap.DialWithTLSConfig(tlsConfig))
96
+	} else {
97
+		conn, err = ldap.DialURL(cfg.Url)
98
+	}
99
+
100
+	if err != nil {
101
+		return nil, errors.Join(ErrLdapConnectFailed, err)
81 102
 	}
82 103
 
83 104
 	// Bind as the "service" user
84 105
 	if err = conn.Bind(username, password); err != nil {
106
+		fmt.Println("Bind failed")
85 107
 		conn.Close()
86 108
 		return nil, errors.Join(ErrLdapBindService, err)
87 109
 	}