Skip to content

Commit

Permalink
more refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
myleshorton committed Nov 27, 2023
1 parent 5027582 commit f3b86be
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
28 changes: 14 additions & 14 deletions common/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ const (
// This standardizes the keys we use for storing data in the request context
// and for reporting to teleport.
const (
Platform = "client_platform"
Version = "client_version"
Locale = "client_locale"
AppVersion = "client_app_version"
LibraryVersion = "client_lib_version"
App = "client_app"
DeviceID = "device_id"
OriginHost = "origin_host"
OriginPort = "origin_port"
ProbingError = "probing_error"
ClientIP = "client_ip"
ThrottleSettings = "throttle_settings"
TimeZone = "time_zone"
SupportDataCaps = "supported_data_caps"
Platform = "client_platform"
Version = "client_version"
Locale = "client_locale"
AppVersion = "client_app_version"
LibraryVersion = "client_lib_version"
App = "client_app"
DeviceID = "device_id"
OriginHost = "origin_host"
OriginPort = "origin_port"
ProbingError = "probing_error"
ClientIP = "client_ip"
ThrottleSettings = "throttle_settings"
TimeZone = "time_zone"
SupportedDataCaps = "supported_data_caps"
)
4 changes: 2 additions & 2 deletions http_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ func TestDirectNoDevice(t *testing.T) {
}

var buf [400]byte
_, err = conn.Read(buf[:])
conn.Read(buf[:])
if !assert.Contains(t, string(buf[:]), connectResp,
"should get 404 Not Found because no token was provided") {
t.FailNow()
Expand Down Expand Up @@ -602,7 +602,7 @@ func TestInvalidRequest(t *testing.T) {
}

buf := [400]byte{}
_, err = conn.Read(buf[:])
conn.Read(buf[:])
assert.Contains(t, string(buf[:]), connectResp, "should 400")

}
Expand Down
26 changes: 14 additions & 12 deletions opsfilter/opsfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,23 @@ func (f *opsfilter) Apply(cs *filters.ConnectionState, req *http.Request, next f
common.ClientIP: clientIP,
}

addMeasuredHeader := func(key string, headerValue interface{}) {
if headerValue != nil && headerValue != "" {
headerArray, ok := headerValue.([]string)
if ok && len(headerArray) == 0 {
return
}
measuredCtx[key] = headerValue
addVal := func(ctxKey string, val interface{}) {
if val != nil && val != "" {
measuredCtx[ctxKey] = val
}
}

addArrayHeader := func(ctxKey, headerKey string) {
vals := req.Header.Values(headerKey)
if len(vals) == 0 {
return
}
addVal(ctxKey, vals)
}

addStringHeader := func(ctxKey, headerKey string) {
val := req.Header.Get(headerKey)
if val != "" {
measuredCtx[ctxKey] = val
}
addVal(ctxKey, val)
}

// On persistent HTTP connections, some or all of the below may be missing on requests after the first. By only setting
Expand All @@ -84,12 +86,12 @@ func (f *opsfilter) Apply(cs *filters.ConnectionState, req *http.Request, next f
addStringHeader(common.App, common.AppHeader)
addStringHeader(common.Locale, common.LocaleHeader)
addStringHeader(common.TimeZone, common.TimeZoneHeader)
addMeasuredHeader(common.SupportDataCaps, req.Header[common.SupportedDataCapsHeader])
addArrayHeader(common.SupportedDataCaps, common.SupportedDataCapsHeader)

netx.WalkWrapped(cs.Downstream(), func(conn net.Conn) bool {
pdc, ok := conn.(tlslistener.ProbingDetectingConn)
if ok {
addMeasuredHeader(common.ProbingError, pdc.ProbingError())
addVal(common.ProbingError, pdc.ProbingError())
return false
}
return true
Expand Down
2 changes: 1 addition & 1 deletion redis/measured_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func submit(countryLookup geo.CountryLookup, rc *redis.Client, scriptSHA string,
}

var supportedDataCaps []string
_supportedDataCaps, ok := sac.ctx[common.SupportDataCaps]
_supportedDataCaps, ok := sac.ctx[common.SupportedDataCaps]
if ok {
supportedDataCaps = _supportedDataCaps.([]string)
}
Expand Down

0 comments on commit f3b86be

Please sign in to comment.