go.temporal.io/server/components/callbacks/config.go

154 LOC · 52 covered · 102 uncovered · 25 ranges · 85 concepts · 20 introducers · 43 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 package callbacks
2
3 import (
4 "net/url"
5 "regexp"
6 "strings"
7 "time"
8
9 "go.temporal.io/server/chasm"
10 "go.temporal.io/server/common/backoff"
11 "go.temporal.io/server/common/dynamicconfig"
12 "go.temporal.io/server/common/nexus"
13 "google.golang.org/grpc/codes"
14 "google.golang.org/grpc/status"
15 )
16
17 var RequestTimeout = dynamicconfig.NewDestinationDurationSetting(
18 "component.callbacks.request.timeout",
19 time.Second*10,
20 `RequestTimeout is the timeout for executing a single callback request.`,
21 )
22
23 var RetryPolicyInitialInterval = dynamicconfig.NewGlobalDurationSetting(
24 "component.callbacks.retryPolicy.initialInterval",
25 time.Second,
26 `The initial backoff interval between every callback request attempt for a given callback.`,
27 )
28
29 var RetryPolicyMaximumInterval = dynamicconfig.NewGlobalDurationSetting(
30 "component.callbacks.retryPolicy.maxInterval",
31 time.Hour,
32 `The maximum backoff interval between every callback request attempt for a given callback.`,
33 )
34
35 type Config struct {
36 RequestTimeout dynamicconfig.DurationPropertyFnWithDestinationFilter
37 RetryPolicy func() backoff.RetryPolicy
38 }
39
40 > func ConfigProvider(dc *dynamicconfig.Collection) *Config { fx.go ×44
41 > return &Config{
42 > RequestTimeout: RequestTimeout.Get(dc),
43 > RetryPolicy: func() backoff.RetryPolicy {
44 return backoff.NewExponentialRetryPolicy(
45 RetryPolicyInitialInterval.Get(dc)(),
46 ).WithMaximumInterval(
47 RetryPolicyMaximumInterval.Get(dc)(),
48 ).WithExpirationInterval(
49 backoff.NoInterval,
50 )
51 },
52 }
53 }
54
55 type AddressMatchRules struct {
56 Rules []AddressMatchRule
57 }
58
59 > func (a AddressMatchRules) validate(rawURL string) error { config.go ×1
60 > // Exact match only; no path, query, or fragment allowed for system URL
61 > if rawURL == nexus.SystemCallbackURL || rawURL == chasm.NexusCompletionHandlerURL {
62 > return nil config.go ×1
63 > }
64 > u, err := url.Parse(rawURL) config.go ×2
65 > if err != nil {
66 return status.Errorf(codes.InvalidArgument, "invalid callback url: %v", err)
67 }
68 > if u.Scheme != "http" && u.Scheme != "https" { config.go ×2
69 > return status.Errorf(codes.InvalidArgument, "invalid url: unknown scheme: %v", u) config.go ×1
70 > }
71 > if u.Host == "" { config.go ×1
72 > return status.Errorf(codes.InvalidArgument, "invalid url: missing host") config.go ×1
73 > }
74 > for _, rule := range a.Rules { config.go ×1
75 > allow, err := rule.allow(u) config.go ×2
76 > if err != nil {
77 > return err config.go ×2
78 > }
79 > if allow { config.go ×1
80 > return nil config.go ×1
81 > }
82 }
83 > return status.Errorf(codes.InvalidArgument, "invalid url: url does not match any configured callback address: %v", u) config.go ×1
84 }
85
86 type AddressMatchRule struct {
87 Regexp *regexp.Regexp
88 AllowInsecure bool
89 }
90
91 // Allow validates the URL by:
92 // 1. true, nil if the provided url matches the rule and passed validation
93 // for the given rule.
94 // 2. false, nil if the URL does not match the rule.
95 // 3. It false, error if there is a match and the URL fails validation
96 > func (a AddressMatchRule) allow(u *url.URL) (bool, error) { config.go ×2
97 > if !a.Regexp.MatchString(u.Host) {
98 > return false, nil config.go ×1
99 > }
100 > if a.AllowInsecure { config.go ×1
101 > return true, nil config.go ×1
102 > }
103 > if u.Scheme != "https" { config.go ×1
104 > return false, config.go ×2
105 > status.Errorf(codes.InvalidArgument,
106 > "invalid url: callback address does not allow insecure connections: %v", u)
107 > }
108 > return true, nil config.go ×1
109 }
110
111 func allowedAddressConverter(val any) (AddressMatchRules, error) {
112 type entry struct {
113 Pattern string
114 AllowInsecure bool
115 }
116 intermediate, err := dynamicconfig.ConvertStructure[[]entry](nil)(val)
117 if err != nil {
118 return AddressMatchRules{}, err
119 }
120
121 configs := []AddressMatchRule{}
122 for _, e := range intermediate {
123 if e.Pattern == "" {
124 // Skip configs with missing / unparsable Pattern
125 continue
126 }
127 re, err := regexp.Compile(addressPatternToRegexp(e.Pattern))
128 if err != nil {
129 // Skip configs with malformed Pattern
130 continue
131 }
132 configs = append(configs, AddressMatchRule{
133 Regexp: re,
134 AllowInsecure: e.AllowInsecure,
135 })
136 }
137 return AddressMatchRules{Rules: configs}, nil
138 }
139
140 > func addressPatternToRegexp(pattern string) string { config.go ×3
141 > var result strings.Builder
142 > result.WriteString("^")
143 > first := true
144 > for literal := range strings.SplitSeq(pattern, "*") {
145 > if !first {
146 > // Replace * with .* config.go ×1
147 > result.WriteString(".*")
148 > }
149 > result.WriteString(regexp.QuoteMeta(literal)) config.go ×3
150 > first = false
151 }
152 > result.WriteString("$") config.go ×3
153 > return result.String()
154 }