This repository was archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient_impl.go
More file actions
251 lines (236 loc) · 5.83 KB
/
Copy pathclient_impl.go
File metadata and controls
251 lines (236 loc) · 5.83 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
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
package auth
import (
"context"
"errors"
"net"
"strings"
"time"
"github.com/containerssh/http"
"github.com/containerssh/log"
"github.com/containerssh/metrics"
)
type httpAuthClient struct {
timeout time.Duration
httpClient http.Client
endpoint string
logger log.Logger
metrics metrics.Collector
backendRequestsMetric metrics.SimpleCounter
backendFailureMetric metrics.SimpleCounter
authSuccessMetric metrics.GeoCounter
authFailureMetric metrics.GeoCounter
enablePassword bool
enablePubKey bool
}
func (client *httpAuthClient) Password(
username string,
password []byte,
connectionID string,
remoteAddr net.IP,
) (bool, error) {
if !client.enablePassword {
err := log.UserMessage(
EDisabled,
"Password authentication failed.",
"Password authentication is disabled.",
)
client.logger.Debug(err)
return false, err
}
url := client.endpoint + "/password"
method := "Password"
authType := "password"
authRequest := PasswordAuthRequest{
Username: username,
RemoteAddress: remoteAddr.String(),
ConnectionID: connectionID,
SessionID: connectionID,
Password: password,
}
return client.processAuthWithRetry(username, method, authType, connectionID, url, authRequest, remoteAddr)
}
func (client *httpAuthClient) PubKey(
username string,
pubKey string,
connectionID string,
remoteAddr net.IP,
) (bool, error) {
if !client.enablePubKey {
err := log.UserMessage(
EDisabled,
"Public key authentication failed.",
"Public key authentication is disabled.",
)
client.logger.Debug(err)
return false, err
}
url := client.endpoint + "/pubkey"
authRequest := PublicKeyAuthRequest{
Username: username,
RemoteAddress: remoteAddr.String(),
ConnectionID: connectionID,
SessionID: connectionID,
PublicKey: pubKey,
}
method := "Public key"
authType := "pubkey"
return client.processAuthWithRetry(username, method, authType, connectionID, url, authRequest, remoteAddr)
}
func (client *httpAuthClient) processAuthWithRetry(
username string,
method string,
authType string,
connectionID string,
url string,
authRequest interface{},
remoteAddr net.IP,
) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), client.timeout)
defer cancel()
var lastError error
var lastLabels []metrics.MetricLabel
logger := client.logger.
WithLabel("connectionId", connectionID).
WithLabel("username", username).
WithLabel("url", url).
WithLabel("authtype", authType)
loop:
for {
lastLabels = []metrics.MetricLabel{
metrics.Label("authtype", authType),
}
if lastError != nil {
lastLabels = append(
lastLabels,
metrics.Label("retry", "1"),
)
} else {
lastLabels = append(
lastLabels,
metrics.Label("retry", "0"),
)
}
client.logAttempt(logger, method, lastLabels)
authResponse := &ResponseBody{}
lastError = client.authServerRequest(url, authRequest, authResponse)
if lastError == nil {
client.logAuthResponse(logger, method, authResponse, lastLabels, remoteAddr)
return authResponse.Success, nil
}
reason := client.getReason(lastError)
lastLabels = append(lastLabels, metrics.Label("reason", reason))
client.logTemporaryFailure(logger, lastError, method, reason, lastLabels)
select {
case <-ctx.Done():
break loop
case <-time.After(10 * time.Second):
}
}
return client.logAndReturnPermanentFailure(lastError, method, lastLabels, logger)
}
func (client *httpAuthClient) logAttempt(logger log.Logger, method string, lastLabels []metrics.MetricLabel) {
logger.Debug(
log.NewMessage(
MAuth,
"%s authentication request",
method,
),
)
client.backendRequestsMetric.Increment(lastLabels...)
}
func (client *httpAuthClient) logAndReturnPermanentFailure(
lastError error,
method string,
lastLabels []metrics.MetricLabel,
logger log.Logger,
) (bool, error) {
err := log.Wrap(
lastError,
EAuthBackendError,
"Backend request for %s authentication failed, giving up",
strings.ToLower(method),
)
client.backendFailureMetric.Increment(
append(
[]metrics.MetricLabel{
metrics.Label("type", "hard"),
}, lastLabels...,
)...,
)
logger.Error(err)
return false, err
}
func (client *httpAuthClient) logTemporaryFailure(
logger log.Logger,
lastError error,
method string,
reason string,
lastLabels []metrics.MetricLabel,
) {
logger.Debug(
log.Wrap(
lastError,
EAuthBackendError,
"%s authentication request to backend failed, retrying in 10 seconds",
method,
).
Label("reason", reason),
)
client.backendFailureMetric.Increment(
append(
[]metrics.MetricLabel{
metrics.Label("type", "soft"),
}, lastLabels...,
)...,
)
}
func (client *httpAuthClient) getReason(lastError error) string {
var typedErr log.Message
reason := log.EUnknownError
if errors.As(lastError, &typedErr) {
reason = typedErr.Code()
}
return reason
}
func (client *httpAuthClient) logAuthResponse(
logger log.Logger,
method string,
authResponse *ResponseBody,
labels []metrics.MetricLabel,
remoteAddr net.IP,
) {
if authResponse.Success {
logger.Debug(
log.NewMessage(
MAuthSuccessful,
"%s authentication successful",
method,
),
)
client.authSuccessMetric.Increment(remoteAddr, labels...)
} else {
logger.Debug(
log.NewMessage(
EAuthFailed,
"%s authentication failed",
method,
),
)
client.authFailureMetric.Increment(remoteAddr, labels...)
}
}
func (client *httpAuthClient) authServerRequest(endpoint string, requestObject interface{}, response interface{}) error {
statusCode, err := client.httpClient.Post(endpoint, requestObject, response)
if err != nil {
return err
}
if statusCode != 200 {
return log.UserMessage(
EInvalidStatus,
"Cannot authenticate at this time.",
"auth server responded with an invalid status code: %d",
statusCode,
)
}
return nil
}