forked from saucelabs/forwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkerberos.go
More file actions
167 lines (136 loc) · 5.09 KB
/
kerberos.go
File metadata and controls
167 lines (136 loc) · 5.09 KB
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
// Copyright 2022-2024 Sauce Labs Inc., all rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package forwarder
import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/jcmturner/gokrb5/v8/client"
"github.com/jcmturner/gokrb5/v8/config"
"github.com/jcmturner/gokrb5/v8/keytab"
"github.com/jcmturner/gokrb5/v8/krberror"
"github.com/jcmturner/gokrb5/v8/spnego"
"github.com/saucelabs/forwarder/log"
)
type KerberosConfig struct {
Enabled bool
RunDiagnostics bool
AuthUpstreamProxy bool
CfgFilePath string
KeyTabFilePath string
UserName string
UserRealm string
// no matching and wildcards like in MITMHosts
KerberosEnabledHosts []string
}
type KerberosAdapter interface {
ConnectToKDC() error
GetSPNForHost(hostname string) (string, error)
GetSPNEGOHeaderValue(spn string) (string, error)
GetConfig() *KerberosConfig
GetProxyAuthHeader(_ context.Context, proxyURL *url.URL, _ string) (http.Header, error)
}
func DefaultKerberosConfig() *KerberosConfig {
return &KerberosConfig{
// default zero values are fine
}
}
type KerberosClient struct {
configuration KerberosConfig
krb5client client.Client
log log.StructuredLogger
}
func NewKerberosAdapter(cnf KerberosConfig, log log.StructuredLogger) (*KerberosClient, error) {
// technically this should not happen as adapter should not be initialized without
// proper config present, but better safe than sorry
if cnf.CfgFilePath == "" {
return nil, errors.New("kerberos config file (krb5.conf) not specified")
}
if cnf.KeyTabFilePath == "" {
return nil, errors.New("kerberos keytab file not specified")
}
if cnf.UserName == "" {
return nil, errors.New("kerberos username not specified")
}
if cnf.UserRealm == "" {
return nil, errors.New("kerberos user realm not specified")
}
krb5Config, err := config.Load(cnf.CfgFilePath)
if err != nil {
return nil, fmt.Errorf("error loading kerberos config file %s: %w", cnf.CfgFilePath, err)
}
krb5Keytab, err := keytab.Load(cnf.KeyTabFilePath)
if err != nil {
return nil, fmt.Errorf("error loading kerberos keytab file %s: %w", cnf.KeyTabFilePath, err)
}
krb5Client := client.NewWithKeytab(cnf.UserName, cnf.UserRealm, krb5Keytab, krb5Config)
return &KerberosClient{configuration: cnf, krb5client: *krb5Client, log: log}, nil
}
func (a *KerberosClient) GetConfig() *KerberosConfig {
return &a.configuration
}
func (a *KerberosClient) ConnectToKDC() error {
a.log.Debug("Logging to KDC server")
loginErr := a.krb5client.Login()
if loginErr != nil && !a.configuration.RunDiagnostics {
return fmt.Errorf("kerberos KDC login: %w", loginErr)
}
if loginErr != nil && a.configuration.RunDiagnostics {
a.log.Error("kerberos KDC login failed but running diagnostics anyway", "error", loginErr)
}
a.log.Info("Kerberos KDC login successful")
// run diagnostics even if login failed
if a.configuration.RunDiagnostics {
a.log.Warn("Kerberos diagnostics mode - diagnostic info will be printed to stdout and forwarder process will exit.")
buf := new(bytes.Buffer)
err := a.krb5client.Diagnostics(buf)
// We need to print directly to stdout as it contains a nested structured text.
// Does not really matter as diagnostics mode should be used on local console only.
fmt.Printf("%s", buf.String()) //nolint
if err != nil {
return fmt.Errorf("kerberos configuration potential problems: %w", err)
}
return errors.New("no kerberos configuration problems found. Exiting process")
}
return nil
}
func (a *KerberosClient) GetSPNForHost(hostname string) (string, error) {
// static for now but in the future we may want to do DNS queries for CNAME
return "HTTP/" + hostname, nil
}
// GetSPNEGOHeaderValue accepts SPN service name and returns header value that should
// be put inside Authorization or Proxy-Authorization header.
func (a *KerberosClient) GetSPNEGOHeaderValue(spn string) (string, error) {
a.log.Debug("Generating SPNEGO header value for SPN: ", spn)
cli := spnego.SPNEGOClient(&a.krb5client, spn)
err := cli.AcquireCred()
if err != nil {
return "", fmt.Errorf("could not acquire SPNEGO client credential: %w", err)
}
secContext, err := cli.InitSecContext()
if err != nil {
return "", fmt.Errorf("could not initialize SPNEGO context for SPN %s: %w", spn, err)
}
nb, err := secContext.Marshal()
if err != nil {
return "", krberror.Errorf(err, krberror.EncodingError, "could not marshal SPNEGO")
}
return "Negotiate " + base64.StdEncoding.EncodeToString(nb), nil
}
func (a *KerberosClient) GetProxyAuthHeader(_ context.Context, proxyURL *url.URL, _ string) (http.Header, error) {
spn := "HTTP/" + proxyURL.Hostname()
SPNEGOHeaderValue, err := a.GetSPNEGOHeaderValue(spn)
if err != nil {
return nil, fmt.Errorf("failed to get Kerberos SPNEGO authentication header for sauce proxy SPN: %s: %w", spn, err)
}
authHeader := make(http.Header, 1)
authHeader.Set("Proxy-Authorization", SPNEGOHeaderValue)
return authHeader, nil
}