This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_impl.go
173 lines (161 loc) · 3.79 KB
/
client_impl.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
package configuration
import (
"context"
"errors"
"net"
"time"
"github.com/containerssh/http"
"github.com/containerssh/log"
"github.com/containerssh/metrics"
)
type client struct {
httpClient http.Client
logger log.Logger
backendRequestsMetric metrics.SimpleCounter
backendFailureMetric metrics.SimpleCounter
}
func (c *client) Get(
ctx context.Context,
username string,
remoteAddr net.TCPAddr,
connectionID string,
) (AppConfig, error) {
if c.httpClient == nil {
return AppConfig{}, nil
}
logger := c.logger.
WithLabel("connectionId", connectionID).
WithLabel("username", username)
request, response := c.createRequestResponse(username, remoteAddr, connectionID)
var lastError error = nil
var lastLabels []metrics.MetricLabel
loop:
for {
lastLabels = []metrics.MetricLabel{}
if lastError != nil {
lastLabels = append(
lastLabels,
metrics.Label("retry", "1"),
)
} else {
lastLabels = append(
lastLabels,
metrics.Label("retry", "0"),
)
}
c.logAttempt(logger, lastLabels)
lastError = c.configServerRequest(&request, &response)
if lastError == nil {
c.logConfigResponse(logger)
return response.Config, nil
}
reason := c.getReason(lastError)
lastLabels = append(lastLabels, metrics.Label("reason", reason))
c.logTemporaryFailure(logger, lastError, reason, lastLabels)
select {
case <-ctx.Done():
break loop
case <-time.After(10 * time.Second):
}
}
return c.logAndReturnPermanentFailure(lastError, lastLabels, logger)
}
func (c *client) createRequestResponse(username string, remoteAddr net.TCPAddr, connectionID string) (
ConfigRequest,
ConfigResponseBody,
) {
request := ConfigRequest{
Username: username,
RemoteAddr: remoteAddr.IP.String(),
ConnectionID: connectionID,
SessionID: connectionID,
}
response := ConfigResponseBody{}
return request, response
}
func (c *client) logAttempt(logger log.Logger, lastLabels []metrics.MetricLabel) {
logger.Debug(
log.NewMessage(
MConfig,
"Configuration request",
),
)
c.backendRequestsMetric.Increment(lastLabels...)
}
func (c *client) logAndReturnPermanentFailure(
lastError error,
lastLabels []metrics.MetricLabel,
logger log.Logger,
) (AppConfig, error) {
err := log.Wrap(
lastError,
EConfigBackendError,
"Configuration request to backend failed, giving up",
)
c.backendFailureMetric.Increment(
append(
[]metrics.MetricLabel{
metrics.Label("type", "hard"),
}, lastLabels...,
)...,
)
logger.Error(err)
return AppConfig{}, err
}
func (c *client) logTemporaryFailure(
logger log.Logger,
lastError error,
reason string,
lastLabels []metrics.MetricLabel,
) {
logger.Debug(
log.Wrap(
lastError,
EConfigBackendError,
"Configuration request to backend failed, retrying in 10 seconds",
).
Label("reason", reason),
)
c.backendFailureMetric.Increment(
append(
[]metrics.MetricLabel{
metrics.Label("type", "soft"),
}, lastLabels...,
)...,
)
}
func (c *client) getReason(lastError error) string {
var typedErr log.Message
reason := log.EUnknownError
if errors.As(lastError, &typedErr) {
reason = typedErr.Code()
}
return reason
}
func (c *client) logConfigResponse(
logger log.Logger,
) {
logger.Debug(
log.NewMessage(
MConfigSuccess,
"User-specific configuration received",
),
)
}
func (c *client) configServerRequest(requestObject interface{}, response interface{}) error {
statusCode, err := c.httpClient.Post("", requestObject, response)
if err != nil {
return err
}
if statusCode != 200 {
return log.UserMessage(
EInvalidStatus,
// The message indicates authentication because the config server is
// called at config-time.
"Cannot authenticate at this time.",
"Configuration server responded with an invalid status code: %d",
statusCode,
)
}
return nil
}