-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
95 lines (80 loc) · 2.51 KB
/
helpers.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Apache License 2.0.
* See the file "LICENSE" for details.
*/
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024 Datadog, Inc.
package main
import (
"fmt"
"net/url"
"regexp"
"strings"
"unicode"
log "github.com/sirupsen/logrus"
"github.com/DataDog/dd-otel-host-profiler/reporter"
)
var ValidTagKeyRegex = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9-._/]+$`)
var ValidTagValueRegex = regexp.MustCompile(`^[a-zA-Z0-9-:._/]*$`)
// ValidateTags parses and validates user-specified tags.
// Each tag must match ValidTagRegex with ',' used as a separator.
// Tags that can't be validated are dropped.
// The empty string is returned if no tags can be validated.
func ValidateTags(tags string) reporter.Tags {
if tags == "" {
return nil
}
splitTags := strings.Split(tags, ",")
validatedTags := make(reporter.Tags, 0, len(splitTags))
for _, tag := range splitTags {
key, value, found := strings.Cut(tags, ":")
if !found || !ValidTagKeyRegex.MatchString(key) || !ValidTagValueRegex.MatchString(value) {
log.Warnf("Rejected user-specified tag '%s'", tag)
} else {
validatedTags = append(validatedTags, reporter.MakeTag(key, value))
}
}
return validatedTags
}
func addTagsFromArgs(tags *reporter.Tags, args *arguments) {
if args.environment != "" {
*tags = append(*tags, reporter.MakeTag("env", args.environment))
}
if args.serviceName != "" {
*tags = append(*tags, reporter.MakeTag("service", args.serviceName))
}
}
// isAPIKeyValid reports whether the given string is a structurally valid API key
func isAPIKeyValid(key string) bool {
if len(key) != 32 {
return false
}
for _, c := range key {
if c > unicode.MaxASCII || (!unicode.IsLower(c) && !unicode.IsNumber(c)) {
return false
}
}
return true
}
// isAPPKeyValid reports whether the given string is a structurally valid APP key
func isAPPKeyValid(key string) bool {
if len(key) != 40 {
return false
}
for _, c := range key {
if c > unicode.MaxASCII || (!unicode.IsLower(c) && !unicode.IsNumber(c)) {
return false
}
}
return true
}
func intakeURLForSite(site string) (string, error) {
u := fmt.Sprintf("https://intake.profile.%s/api/v2/profile", site)
_, err := url.Parse(u)
return u, err
}
func intakeURLForAgent(agentURL string) (string, error) {
const profilingEndPoint = "/profiling/v1/input"
return url.JoinPath(agentURL, profilingEndPoint)
}