-
Notifications
You must be signed in to change notification settings - Fork 18
/
client_context_from_config.go
84 lines (73 loc) · 2.47 KB
/
client_context_from_config.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
package ldclient
import (
"errors"
"regexp"
"github.com/launchdarkly/go-sdk-common/v3/ldlog"
"github.com/launchdarkly/go-server-sdk/v7/internal"
"github.com/launchdarkly/go-server-sdk/v7/ldcomponents"
"github.com/launchdarkly/go-server-sdk/v7/subsystems"
)
var validTagKeyOrValueRegex = regexp.MustCompile(`(?s)^[\w.-]*$`)
func newClientContextFromConfig(
sdkKey string,
config Config,
) (*internal.ClientContextImpl, error) {
if !stringIsValidHTTPHeaderValue(sdkKey) {
// We want to fail fast in this case, because if we got as far as trying to make an HTTP request
// to LaunchDarkly with a malformed key, the Go HTTP client unfortunately would include the
// actual Authorization header value in its error message, which could end up in logs - and the
// value might be a real SDK key that just has (for instance) a newline at the end of it, so it
// would be sensitive information.
return nil, errors.New("SDK key contains invalid characters")
}
basicConfig := subsystems.BasicClientContext{
SDKKey: sdkKey,
Offline: config.Offline,
ServiceEndpoints: config.ServiceEndpoints,
}
loggingFactory := config.Logging
if loggingFactory == nil {
loggingFactory = ldcomponents.Logging()
}
logging, err := loggingFactory.Build(basicConfig)
if err != nil {
return nil, err
}
basicConfig.Logging = logging
basicConfig.ApplicationInfo.ApplicationID = validateTagValue(config.ApplicationInfo.ApplicationID,
"ApplicationID", logging.Loggers)
basicConfig.ApplicationInfo.ApplicationVersion = validateTagValue(config.ApplicationInfo.ApplicationVersion,
"ApplicationVersion", logging.Loggers)
httpFactory := config.HTTP
if httpFactory == nil {
httpFactory = ldcomponents.HTTPConfiguration()
}
http, err := httpFactory.Build(basicConfig)
if err != nil {
return nil, err
}
basicConfig.HTTP = http
return &internal.ClientContextImpl{BasicClientContext: basicConfig}, nil
}
func stringIsValidHTTPHeaderValue(s string) bool {
for _, ch := range s {
if ch < 32 || ch > 127 {
return false
}
}
return true
}
func validateTagValue(value, name string, loggers ldlog.Loggers) string {
if value == "" {
return ""
}
if len(value) > 64 {
loggers.Warnf("Value of Config.ApplicationInfo.%s was longer than 64 characters and was discarded", name)
return ""
}
if !validTagKeyOrValueRegex.MatchString(value) {
loggers.Warnf("Value of Config.ApplicationInfo.%s contained invalid characters and was discarded", name)
return ""
}
return value
}