-
Notifications
You must be signed in to change notification settings - Fork 0
/
krb5.go
335 lines (294 loc) · 9.9 KB
/
krb5.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Package krb5 implements the integratedauth.IntegratedAuthenticator interface in order to provide kerberos/active directory (Windows) based authentication.
package krb5
import (
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"strconv"
"strings"
"github.com/jcmturner/gokrb5/v8/client"
"github.com/jcmturner/gokrb5/v8/config"
"github.com/jcmturner/gokrb5/v8/credentials"
"github.com/jcmturner/gokrb5/v8/gssapi"
"github.com/jcmturner/gokrb5/v8/keytab"
"github.com/jcmturner/gokrb5/v8/spnego"
"github.com/microsoft/go-mssqldb/integratedauth"
"github.com/microsoft/go-mssqldb/msdsn"
)
const (
keytabConfigFile = "krb5-configfile"
keytabFile = "krb5-keytabfile"
keytabCache = "krb5-keytabcachefile"
realm = "krb5-realm"
dnsLookupKDC = "krb5-dnslookupkdc"
udpPreferenceLimit = "krb5-udppreferencelimit"
)
var (
ErrRequiredParametersMissing = errors.New("failed to create krb5 client from login parameters")
ErrRealmRequiredWithUsernameAndPassword = errors.New("krb5-realm is required to login with krb5 when using user id and password")
ErrKrb5ConfigFileRequiredWithUsernameAndPassword = errors.New("krb5-configfile is required to login with krb5 when using user id and password")
ErrUsernameRequiredWithKeytab = errors.New("user id is required to login with krb5 when using krb5-keytabfile")
ErrRealmRequiredWithKeytab = errors.New("krb5-realm is required to login with krb5 when using krb5-keytabfile")
ErrKrb5ConfigFileRequiredWithKeytab = errors.New("krb5-configfile is required to login with krb5 when using krb5-keytabfile")
ErrKrb5ConfigFileDoesNotExist = errors.New("krb5-configfile does not exist")
ErrKeytabFileDoesNotExist = errors.New("krb5-keytabfile does not exist")
ErrKrb5ConfigFileRequiredWithKeytabCache = errors.New("krb5-configfile is required to login with krb5 when using krb5-keytabcachefile")
ErrKeytabCacheFileDoesNotExist = errors.New("krb5-keytabcachefile does not exist")
)
var (
_ integratedauth.IntegratedAuthenticator = (*krbAuth)(nil)
fileExists = fileExistsOS
AuthProviderFunc integratedauth.Provider = integratedauth.ProviderFunc(getAuth)
)
func init() {
err := integratedauth.SetIntegratedAuthenticationProvider("krb5", AuthProviderFunc)
if err != nil {
panic(err)
}
}
func getAuth(config msdsn.Config) (integratedauth.IntegratedAuthenticator, error) {
krb5Config, err := readKrb5Config(config)
if err != nil {
return nil, err
}
err = validateKrb5LoginParams(krb5Config)
if err != nil {
return nil, err
}
return &krbAuth{
krb5Config: krb5Config,
}, nil
}
type loginMethod uint8
const (
none loginMethod = iota
usernameAndPassword
keyTabFile
cachedCredentialsFile
)
type krb5Login struct {
Krb5ConfigFile string
KeytabFile string
KeytabCacheFile string
Realm string
UserName string
Password string
ServerSPN string
DNSLookupKDC bool
UDPPreferenceLimit int
loginMethod loginMethod
}
// copies string parameters from connection string, parses optional parameters
func readKrb5Config(config msdsn.Config) (*krb5Login, error) {
login := &krb5Login{
Krb5ConfigFile: config.Parameters[keytabConfigFile],
KeytabFile: config.Parameters[keytabFile],
KeytabCacheFile: config.Parameters[keytabCache],
Realm: config.Parameters[realm],
UserName: config.User,
Password: config.Password,
ServerSPN: config.ServerSPN,
DNSLookupKDC: true,
UDPPreferenceLimit: 1,
loginMethod: none,
}
// read optional parameters
val, ok := config.Parameters[dnsLookupKDC]
if ok {
parsed, err := strconv.ParseBool(val)
if err != nil {
return nil, fmt.Errorf("invalid '%s' parameter '%s': %s", dnsLookupKDC, val, err.Error())
}
login.DNSLookupKDC = parsed
}
val, ok = config.Parameters[udpPreferenceLimit]
if ok {
parsed, err := strconv.Atoi(val)
if err != nil {
return nil, fmt.Errorf("invalid '%s' parameter '%s': %s", udpPreferenceLimit, val, err.Error())
}
login.UDPPreferenceLimit = parsed
}
return login, nil
}
func validateKrb5LoginParams(krbLoginParams *krb5Login) error {
switch {
// using explicit credentials
case krbLoginParams.UserName != "" && krbLoginParams.Password != "":
if krbLoginParams.Realm == "" {
return ErrRealmRequiredWithUsernameAndPassword
}
if krbLoginParams.Krb5ConfigFile == "" {
return ErrKrb5ConfigFileRequiredWithUsernameAndPassword
}
if ok, err := fileExists(krbLoginParams.Krb5ConfigFile, ErrKrb5ConfigFileDoesNotExist); !ok {
return err
}
krbLoginParams.loginMethod = usernameAndPassword
return nil
//using a keytab file
case krbLoginParams.KeytabFile != "":
if krbLoginParams.UserName == "" {
return ErrUsernameRequiredWithKeytab
}
if krbLoginParams.Realm == "" {
return ErrRealmRequiredWithKeytab
}
if krbLoginParams.Krb5ConfigFile == "" {
return ErrKrb5ConfigFileRequiredWithKeytab
}
if ok, err := fileExists(krbLoginParams.Krb5ConfigFile, ErrKrb5ConfigFileDoesNotExist); !ok {
return err
}
if ok, err := fileExists(krbLoginParams.KeytabFile, ErrKeytabFileDoesNotExist); !ok {
return err
}
krbLoginParams.loginMethod = keyTabFile
return nil
// using a credential cache file
case krbLoginParams.KeytabCacheFile != "":
if krbLoginParams.Krb5ConfigFile == "" {
return ErrKrb5ConfigFileRequiredWithKeytabCache
}
if ok, err := fileExists(krbLoginParams.Krb5ConfigFile, ErrKrb5ConfigFileDoesNotExist); !ok {
return err
}
if ok, err := fileExists(krbLoginParams.KeytabCacheFile, ErrKeytabCacheFileDoesNotExist); !ok {
return err
}
krbLoginParams.loginMethod = cachedCredentialsFile
return nil
default:
return ErrRequiredParametersMissing
}
}
func fileExistsOS(filename string, errWhenFileNotFound error) (bool, error) {
_, err := os.Stat(filename)
if err == nil {
return true, nil
}
if errors.Is(err, os.ErrNotExist) {
return false, errWhenFileNotFound
}
return false, fmt.Errorf("%v : %w", errWhenFileNotFound, err)
}
// krbAuth implements the integratedauth.IntegratedAuthenticator interface. It is responsible for kerberos Service Provider Negotiation.
type krbAuth struct {
krb5Config *krb5Login
spnegoClient *spnego.SPNEGO
krb5Client *client.Client
}
func (k *krbAuth) InitialBytes() ([]byte, error) {
krbClient, err := getKrb5Client(k.krb5Config)
if err != nil {
return nil, err
}
err = krbClient.Login()
if err != nil {
return nil, err
}
k.krb5Client = krbClient
k.spnegoClient = spnego.SPNEGOClient(k.krb5Client, canonicalize(k.krb5Config.ServerSPN))
tkn, err := k.spnegoClient.InitSecContext()
if err != nil {
return nil, err
}
return tkn.Marshal()
}
func (k *krbAuth) NextBytes(bytes []byte) ([]byte, error) {
var resp spnego.SPNEGOToken
if err := resp.Unmarshal(bytes); err != nil {
return nil, err
}
ok, status := resp.Verify()
if ok { // we're ok, done
return nil, nil
}
switch status.Code {
case gssapi.StatusContinueNeeded:
return nil, nil
default:
return nil, fmt.Errorf("bad status: %+v", status)
}
}
func (k *krbAuth) Free() {
if k.krb5Client != nil {
k.krb5Client.Destroy()
k.krb5Client = nil
}
}
func getKrb5Client(krbLoginParams *krb5Login) (*client.Client, error) {
cfg, err := newKrb5ConfigFromFile(krbLoginParams)
if err != nil {
return nil, err
}
switch krbLoginParams.loginMethod {
case usernameAndPassword:
return clientFromUsernameAndPassword(krbLoginParams, cfg)
case keyTabFile:
return clientFromKeytab(krbLoginParams, cfg)
case cachedCredentialsFile:
return clientFromCredentialCache(krbLoginParams, cfg)
default:
return nil, ErrRequiredParametersMissing
}
}
func newKrb5ConfigFromFile(krb5Login *krb5Login) (*config.Config, error) {
f, err := os.Open(krb5Login.Krb5ConfigFile)
if err != nil {
return nil, err
}
defer f.Close()
cfg, err := config.NewFromReader(f)
if err != nil {
return nil, err
}
cfg.LibDefaults.DNSLookupKDC = krb5Login.DNSLookupKDC
cfg.LibDefaults.UDPPreferenceLimit = krb5Login.UDPPreferenceLimit
return cfg, nil
}
// creates a client from hardcoded user id & password credentials in the connection string in addition to realm
func clientFromUsernameAndPassword(krb5Login *krb5Login, cfg *config.Config) (*client.Client, error) {
return client.NewWithPassword(krb5Login.UserName, krb5Login.Realm, krb5Login.Password, cfg, client.DisablePAFXFAST(true)), nil
}
// loads keytab file specified in keytabFile and creates a client from its content, username and realm
func clientFromKeytab(krb5Login *krb5Login, cfg *config.Config) (*client.Client, error) {
data, err := ioutil.ReadFile(krb5Login.KeytabFile)
if err != nil {
return nil, err
}
var kt = &keytab.Keytab{}
err = kt.Unmarshal(data)
if err != nil {
return nil, err
}
return client.NewWithKeytab(krb5Login.UserName, krb5Login.Realm, kt, cfg, client.DisablePAFXFAST(true)), nil
}
// loads credential cache file specified in keytabCache parameter and creates a client
func clientFromCredentialCache(krb5Login *krb5Login, cfg *config.Config) (*client.Client, error) {
cache, err := credentials.LoadCCache(krb5Login.KeytabCacheFile)
if err != nil {
return nil, err
}
return client.NewFromCCache(cache, cfg, client.DisablePAFXFAST(true))
}
// responsible for transforming network CNames into their actual Hostname.
// For cases where service tickets can only be bound to hostnames, not cnames.
func canonicalize(service string) string {
parts := strings.SplitAfterN(service, "/", 2)
if len(parts) != 2 {
return service
}
host, port, err := net.SplitHostPort(parts[1])
if err != nil {
return service
}
cname, err := net.LookupCNAME(strings.ToLower(host))
if err != nil {
return service
}
// Put service back together with cname (stripped of trailing .) and port
return parts[0] + net.JoinHostPort(cname[:len(cname)-1], port)
}