/
server.go
137 lines (115 loc) · 3.6 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cmd
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"os/signal"
"syscall"
"time"
"github.com/alecthomas/kong"
"github.com/chezmoi-sh/yaldap/internal/ldap/auth"
"github.com/chezmoi-sh/yaldap/pkg/ldap"
"github.com/chezmoi-sh/yaldap/pkg/ldap/directory"
yamldir "github.com/chezmoi-sh/yaldap/pkg/ldap/directory/yaml"
"github.com/chezmoi-sh/yaldap/pkg/utils"
"github.com/jimlambrt/gldap"
"golang.org/x/sync/errgroup"
)
type Server struct {
*Base `kong:"-"`
ListenAddr string `name:"listen-address" help:"Address to listen on" default:":389"`
Backend struct {
Name string `name:"name" help:"Backend which stores the data" enum:"yaml" required:"" placeholder:"BACKEND"`
URL string `name:"url" help:"URL used to connect to the backend" required:"" placeholder:"URL"`
} `embed:"" prefix:"backend."`
TLS struct {
Enable bool `name:"tls" help:"Enable TLS" default:"false" negatable:""`
MutualTLS bool `name:"mtls" help:"Enable mutual TLS" default:"false" negatable:""`
CAFile []byte `name:"tls.ca" help:"Path to the CA file" optional:"" type:"filecontent" placeholder:"PATH"`
CertFile []byte `name:"tls.cert" help:"Path to the certificate file" optional:"" type:"filecontent" placeholder:"PATH"`
KeyFile []byte `name:"tls.key" help:"Path to the key file" optional:"" type:"filecontent" placeholder:"PATH"`
} `embed:""`
SessionTTL time.Duration `name:"session-ttl" help:"Duration of a BIND session before it expires" default:"168h"`
}
// Run starts the yaLDAP server using the configuration passed to the command.
func (s Server) Run(_ *kong.Context) error {
logger := s.Logger()
directory, err := s.NewDirectory()
if err != nil {
return err
}
tlsConfig, err := s.TLSConfig()
if err != nil {
return err
}
server, err := gldap.NewServer(
gldap.WithLogger(&utils.HashicorpLoggerWrapper{Logger: logger}),
)
if err != nil {
return err
}
ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
err = server.Router(ldap.NewMux(logger, directory, auth.NewSessions(ctx, s.SessionTTL)))
if err != nil {
return err
}
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
return server.Run(s.ListenAddr, gldap.WithTLSConfig(tlsConfig))
})
// Graceful shutdown.
<-ctx.Done()
if err := server.Stop(); err != nil {
return err
}
return g.Wait()
}
func (s Server) NewDirectory() (directory.Directory, error) {
// Get the directory builder based on the backend name.
switch s.Backend.Name {
case "yaml": //nolint:goconst
return yamldir.NewDirectory(s.Backend.URL)
default:
return nil, fmt.Errorf("unknown backend: %s, only `yaml` is supported", s.Backend.Name)
}
}
func (s Server) TLSConfig() (*tls.Config, error) {
if !s.TLS.Enable && !s.TLS.MutualTLS {
return nil, nil
}
cert, err := tls.X509KeyPair(s.TLS.CertFile, s.TLS.KeyFile)
if err != nil {
return nil, err
}
if !s.TLS.MutualTLS {
// No mutual TLS, just return the certificate.
return &tls.Config{
Certificates: []tls.Certificate{cert},
}, nil
}
// CA certificates are encoded in PEM format, so we need to decode it
// first in order to use it.
caCertPool := x509.NewCertPool()
caPEMBlock := s.TLS.CAFile
for {
var caDERBlock *pem.Block
caDERBlock, caPEMBlock = pem.Decode(caPEMBlock)
if caDERBlock == nil {
break
}
if caDERBlock.Type == "CERTIFICATE" {
cert, err := x509.ParseCertificate(caDERBlock.Bytes)
if err != nil {
return nil, err
}
caCertPool.AddCert(cert)
}
}
return &tls.Config{
Certificates: []tls.Certificate{cert},
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
}, nil
}