forked from saiya/dsps
/
channel_atom.go
141 lines (120 loc) · 3.48 KB
/
channel_atom.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package channel
import (
"context"
"fmt"
"golang.org/x/xerrors"
"github.com/m3dev/dsps/server/config"
"github.com/m3dev/dsps/server/domain"
jwtv "github.com/m3dev/dsps/server/jwt/validator"
"github.com/m3dev/dsps/server/webhook/outgoing"
)
// channelAtom is an Channel implementation corresponds to a ChannelConfiguration
type channelAtom struct {
// ChannelConfig that this object corresponds to.
config *config.ChannelConfig
JwtValidatorTemplate jwtv.Template
OutgoingWebHookTemplates []outgoing.ClientTemplate
}
func newChannelAtom(ctx context.Context, config *config.ChannelConfig, deps ProviderDeps, validate bool) (*channelAtom, error) {
if err := deps.validateProviderDeps(); err != nil {
return nil, err
}
atom := &channelAtom{
config: config,
}
if validate {
if err := atom.validate(); err != nil {
return nil, err
}
}
if config.Jwt != nil {
jvt, err := jwtv.NewTemplate(ctx, config.Jwt, deps.Clock)
if err != nil {
return nil, err
}
atom.JwtValidatorTemplate = jvt
}
atom.OutgoingWebHookTemplates = make([]outgoing.ClientTemplate, 0, len(config.Webhooks))
for i := range config.Webhooks {
tpl, err := outgoing.NewClientTemplate(ctx, &config.Webhooks[i], deps.Telemetry, deps.Sentry)
if err != nil {
return nil, err
}
atom.OutgoingWebHookTemplates = append(atom.OutgoingWebHookTemplates, tpl)
}
return atom, nil
}
func (c *channelAtom) Shutdown(ctx context.Context) {
for _, webhook := range c.OutgoingWebHookTemplates {
webhook.Close()
}
}
func (c *channelAtom) String() string {
return c.config.Regex.String()
}
func (c *channelAtom) GetFileDescriptorPressure() int {
result := 0
for _, webhook := range c.OutgoingWebHookTemplates {
result += webhook.GetFileDescriptorPressure()
}
return result
}
func (c *channelAtom) JWTClockSkewLeewayMax() domain.Duration {
if c.JwtValidatorTemplate == nil {
return domain.Duration{}
}
return c.JwtValidatorTemplate.JWTClockSkewLeewayMax()
}
func (c *channelAtom) validate() error {
if err := c.validateTemplateStrings(); err != nil {
return err
}
return nil
}
func (c *channelAtom) validateTemplateStrings() error {
templates := make(map[string]domain.TemplateString)
for i, webhook := range c.config.Webhooks {
templates[fmt.Sprintf("webhooks[%d].url", i)] = *webhook.URL
for name, tpl := range webhook.Headers {
templates[fmt.Sprintf("webhooks[%d].headers.%s", i, name)] = tpl
}
}
if jwt := c.config.Jwt; jwt != nil {
for claim, tpls := range jwt.Claims {
for i, tpl := range tpls.Templates {
templates[fmt.Sprintf("jwt.claims.%s[%d]", claim, i)] = tpl
}
}
}
dummy := c.dummyTemplateEnvironment()
for path, tpl := range templates {
if _, err := tpl.Execute(dummy); err != nil {
return xerrors.Errorf("invalid template found on %s: %w", path, err)
}
}
return nil
}
func (c *channelAtom) TemplateEnvironmentOf(id domain.ChannelID) domain.TemplateStringEnv {
matches := c.config.Regex.Match(true, string(id))
if matches == nil {
return nil
}
return map[string]interface{}{
"channel": matches,
}
}
func (c *channelAtom) dummyTemplateEnvironment() domain.TemplateStringEnv {
dummyRegexMatches := make(map[string]string)
for _, name := range c.config.Regex.GroupNames() {
dummyRegexMatches[name] = "dummy"
}
return map[string]interface{}{
"channel": dummyRegexMatches,
}
}
func (c *channelAtom) IsMatch(id domain.ChannelID) bool {
return c.config.Regex.Match(true, string(id)) != nil
}
func (c *channelAtom) Expire() domain.Duration {
return *c.config.Expire
}