-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfiguration.go
193 lines (160 loc) · 5.22 KB
/
configuration.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package config
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"atomys.codes/webhooked/pkg/factory"
"atomys.codes/webhooked/pkg/storage"
)
var (
currentConfig = &Configuration{}
// ErrSpecNotFound is returned when the spec is not found
ErrSpecNotFound = errors.New("spec not found")
// defaultTemplate is the default template for the payload
// when no template is defined
defaultTemplate = `{{ .Payload }}`
)
// Load loads the configuration from the viper configuration file
// if an error is occurred, it will be returned
func Load() error {
err := viper.Unmarshal(¤tConfig, viper.DecodeHook(factory.DecodeHook))
if err != nil {
return err
}
for _, spec := range currentConfig.Specs {
if err := loadSecurityFactory(spec); err != nil {
return err
}
if spec.Formatting, err = loadTemplate(spec.Formatting, nil); err != nil {
return fmt.Errorf("configured storage for %s received an error: %s", spec.Name, err.Error())
}
if err = loadStorage(spec); err != nil {
return fmt.Errorf("configured storage for %s received an error: %s", spec.Name, err.Error())
}
}
log.Info().Msgf("Load %d configurations", len(currentConfig.Specs))
return Validate(currentConfig)
}
// loadSecurityFactory loads the security factory for the given spec
// if an error is occurred, return an error
func loadSecurityFactory(spec *WebhookSpec) error {
spec.SecurityPipeline = factory.NewPipeline()
for _, security := range spec.Security {
for securityName, securityConfig := range security {
f, ok := factory.GetFactoryByName(securityName)
if !ok {
return fmt.Errorf("security factory \"%s\" in %s specification is not a valid factory", securityName, spec.Name)
}
for _, input := range securityConfig.Inputs {
f.WithInput(input.Name, input)
}
spec.SecurityPipeline.AddFactory(f.WithID(securityConfig.ID).WithConfig(securityConfig.Specs))
}
}
log.Debug().Msgf("%d security factories loaded for spec %s", spec.SecurityPipeline.FactoryCount(), spec.Name)
return nil
}
// Validate the configuration file and her content
func Validate(config *Configuration) error {
var uniquenessName = make(map[string]bool)
var uniquenessUrl = make(map[string]bool)
for _, spec := range config.Specs {
log.Debug().Str("name", spec.Name).Msgf("Load spec: %+v", spec)
// Validate the uniqueness of all name
if _, ok := uniquenessName[spec.Name]; ok {
return fmt.Errorf("specification name %s must be unique", spec.Name)
}
uniquenessName[spec.Name] = true
// Validate the uniqueness of all entrypoints
if _, ok := uniquenessUrl[spec.EntrypointURL]; ok {
return fmt.Errorf("specification entrypoint url %s must be unique", spec.EntrypointURL)
}
uniquenessUrl[spec.EntrypointURL] = true
}
return nil
}
// loadStorage registers the storage and validate it
// if the storage is not found or an error is occurred during the
// initialization or connection, the error is returned during the
// validation
func loadStorage(spec *WebhookSpec) (err error) {
for _, s := range spec.Storage {
s.Client, err = storage.Load(s.Type, s.Specs)
if err != nil {
return fmt.Errorf("storage %s cannot be loaded properly: %s", s.Type, err.Error())
}
if s.Formatting, err = loadTemplate(s.Formatting, spec.Formatting); err != nil {
return fmt.Errorf("storage %s cannot be loaded properly: %s", s.Type, err.Error())
}
}
log.Debug().Msgf("%d storages loaded for spec %s", len(spec.Storage), spec.Name)
return
}
// loadTemplate loads the template for the given `spec`. When no spec is defined
// we try to load the template from the parentSpec and fallback to the default
// template if parentSpec is not given.
func loadTemplate(spec, parentSpec *FormattingSpec) (*FormattingSpec, error) {
if spec == nil {
spec = &FormattingSpec{}
}
if spec.TemplateString != "" {
spec.Template = spec.TemplateString
return spec, nil
}
if spec.TemplatePath != "" {
file, err := os.OpenFile(spec.TemplatePath, os.O_RDONLY, 0666)
if err != nil {
return spec, err
}
defer file.Close()
var buffer bytes.Buffer
_, err = io.Copy(&buffer, file)
if err != nil {
return spec, err
}
spec.Template = buffer.String()
return spec, nil
}
if parentSpec != nil {
if parentSpec.Template == "" {
var err error
parentSpec, err = loadTemplate(parentSpec, nil)
if err != nil {
return spec, err
}
}
spec.Template = parentSpec.Template
} else {
spec.Template = defaultTemplate
}
return spec, nil
}
// Current returns the aftual configuration
func Current() *Configuration {
return currentConfig
}
// GetSpec returns the spec for the given name, if no entry
// is found, ErrSpecNotFound is returned
func (c *Configuration) GetSpec(name string) (*WebhookSpec, error) {
for _, spec := range c.Specs {
if spec.Name == name {
return spec, nil
}
}
log.Error().Err(ErrSpecNotFound).Msgf("Spec %s not found", name)
return nil, ErrSpecNotFound
}
// GetSpecByEndpoint returns the spec for the given endpoint, if no entry
// is found, ErrSpecNotFound is returned
func (c *Configuration) GetSpecByEndpoint(endpoint string) (*WebhookSpec, error) {
for _, spec := range c.Specs {
if spec.EntrypointURL == endpoint {
return spec, nil
}
}
return nil, ErrSpecNotFound
}