-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.go
More file actions
43 lines (40 loc) · 1.54 KB
/
Copy pathprotocol.go
File metadata and controls
43 lines (40 loc) · 1.54 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
package clientkit
import (
"errors"
"fmt"
"strings"
)
// MaxClientProtocolBytes is the maximum byte length accepted by
// ValidateClientProtocol.
const MaxClientProtocolBytes = 32
// ValidateClientProtocol verifies a stable, low-cardinality, telemetry-safe
// client-family category. Protocols must contain 1 through
// MaxClientProtocolBytes lowercase ASCII letters, digits, periods,
// underscores, or hyphens; must begin and end with a letter or digit; and must
// not contain consecutive periods.
//
// A protocol identifies the concrete client family, such as "http" or "tcp".
// It must not contain an endpoint, URL, address, connection string, tenant, or
// other sensitive or high-cardinality configuration.
func ValidateClientProtocol(protocol string) error {
if protocol == "" {
return errors.New("clientkit: protocol is required")
}
if strings.TrimSpace(protocol) != protocol {
return errors.New("clientkit: protocol must not include surrounding whitespace")
}
if len(protocol) > MaxClientProtocolBytes {
return fmt.Errorf("clientkit: protocol exceeds %d bytes", MaxClientProtocolBytes)
}
if strings.Contains(protocol, "..") || !clientNameAlphanumeric(protocol[0]) || !clientNameAlphanumeric(protocol[len(protocol)-1]) {
return fmt.Errorf("clientkit: invalid protocol %q", protocol)
}
for index := 0; index < len(protocol); index++ {
value := protocol[index]
if clientNameAlphanumeric(value) || value == '-' || value == '_' || value == '.' {
continue
}
return fmt.Errorf("clientkit: invalid protocol %q", protocol)
}
return nil
}