-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkerberos_store.go
147 lines (135 loc) · 3.96 KB
/
kerberos_store.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
package kpx
import (
"crypto/sha1"
"encoding/base64"
"fmt"
"github.com/jcmturner/gokrb5/v8/client"
"github.com/jcmturner/gokrb5/v8/krberror"
"github.com/jcmturner/gokrb5/v8/spnego"
"github.com/palantir/stacktrace"
"strings"
"sync"
)
type KerberosStore struct {
kerberos *Kerberos
clients map[string]*KerberosClient
clientsMutex sync.Mutex
}
func NewKerberosStore(config *Config) (*KerberosStore, error) {
kerberos := NewKerberos(config)
err := kerberos.init()
if err != nil {
return nil, stacktrace.Propagate(err, "unable to initialize kerberos")
}
return &KerberosStore{
kerberos: kerberos,
clients: make(map[string]*KerberosClient),
}, nil
}
func (ks *KerberosStore) safeGetClient(key string) *KerberosClient {
ks.clientsMutex.Lock()
cl := ks.clients[key]
ks.clientsMutex.Unlock()
return cl
}
func (ks *KerberosStore) safeSaveClient(key string, client *KerberosClient) {
ks.clientsMutex.Lock()
ks.clients[key] = client
ks.clientsMutex.Unlock()
}
func (ks *KerberosStore) safeRemoveClient(key string) {
ks.clientsMutex.Lock()
ks.clients[key] = nil
ks.clientsMutex.Unlock()
}
// Try to login with the given credentials, only if not yet logged in
func (ks *KerberosStore) safeTryLogin(username, realm, password string, force bool) (*KerberosClient, error) {
// create key
key := ks.clientKey(username, realm, password)
// remove client to force login?
if force {
ks.safeRemoveClient(key)
}
// get existing client
kcl := ks.safeGetClient(key)
if kcl != nil {
return kcl, nil
}
// create new client
krbClient := ks.kerberos.NewWithPassword(username, realm, password)
if krbClient == nil {
return nil, nil
}
err := krbClient.Login()
if err != nil {
if e, ok := err.(krberror.Krberror); ok {
return nil, stacktrace.Propagate(err, "Invalid login/password for user '%s' on realm '%s'\n%s\n%s", username, realm, e.RootCause, strings.Join(e.EText, "\n"))
}
return nil, stacktrace.Propagate(err, "Invalid login/password for user '%s' on realm '%s'", username, realm)
}
// save client
kcl = NewKerberosClient(krbClient)
ks.safeSaveClient(key, kcl)
return kcl, nil
}
func (ks *KerberosStore) safeGetToken(username, realm, password, protocol string, host string) (*string, error) {
kcl, err := ks.safeTryLogin(username, realm, password, false)
if err != nil {
return nil, stacktrace.Propagate(err, "unable to login to kerberos")
}
if kcl == nil {
return &noAuth, nil
}
token, err := kcl.safeGetToken(protocol, host)
if err != nil {
kcl, err = ks.safeTryLogin(username, realm, password, true)
if kcl == nil {
return &noAuth, nil
}
if err != nil {
return nil, stacktrace.Propagate(err, "unable to login to kerberos")
}
token, err = kcl.safeGetToken(protocol, host)
if err != nil {
return nil, stacktrace.Propagate(err, "unable to get kerberos token")
}
}
return token, nil
}
func (ks *KerberosStore) clientKey(username string, realm string, password string) string {
hasher := sha1.New()
hasher.Write([]byte(password))
hash := hasher.Sum(nil)
key := fmt.Sprintf("%s\x00%s\x00%s", hash, username, realm)
return key
}
type KerberosClient struct {
mutex sync.Mutex
krbClient *client.Client
}
func NewKerberosClient(krbClient *client.Client) *KerberosClient {
return &KerberosClient{
krbClient: krbClient,
mutex: sync.Mutex{},
}
}
func (kc *KerberosClient) safeGetToken(protocol string, host string) (*string, error) {
kc.mutex.Lock()
defer kc.mutex.Unlock()
spn := protocol + "/" + host
s := spnego.SPNEGOClient(kc.krbClient, spn)
err := s.AcquireCred()
if err != nil {
return nil, stacktrace.Propagate(err, "unable to acquire client credential for spn: %s", spn)
}
st, err := s.InitSecContext()
if err != nil {
return nil, stacktrace.Propagate(err, "unable to initialize security context for spn: %s", spn)
}
nb, err := st.Marshal()
if err != nil {
return nil, stacktrace.Propagate(err, "unable to marshal security token for spn: %s", spn)
}
hs := base64.StdEncoding.EncodeToString(nb)
return &hs, nil
}