forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.go
701 lines (627 loc) · 24.1 KB
/
extension.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
package extension
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strings"
"time"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
applisters "github.com/argoproj/argo-cd/v2/pkg/client/listers/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/server/rbacpolicy"
"github.com/argoproj/argo-cd/v2/util/argo"
"github.com/argoproj/argo-cd/v2/util/db"
"github.com/argoproj/argo-cd/v2/util/security"
"github.com/argoproj/argo-cd/v2/util/settings"
)
const (
URLPrefix = "/extensions"
DefaultConnectionTimeout = 2 * time.Second
DefaultKeepAlive = 15 * time.Second
DefaultIdleConnectionTimeout = 60 * time.Second
DefaultMaxIdleConnections = 30
// HeaderArgoCDApplicationName defines the name of the
// expected application header to be passed to the extension
// handler. The header value must follow the format:
// "<namespace>:<app-name>"
// Example:
// Argocd-Application-Name: "namespace:app-name"
HeaderArgoCDApplicationName = "Argocd-Application-Name"
// HeaderArgoCDProjectName defines the name of the expected
// project header to be passed to the extension handler.
// Example:
// Argocd-Project-Name: "default"
HeaderArgoCDProjectName = "Argocd-Project-Name"
// HeaderArgoCDTargetClusterURL defines the target cluster URL
// that the Argo CD application is associated with. This header
// will be populated by the extension proxy and passed to the
// configured backend service. If this header is passed by
// the client, its value will be overriden by the extension
// handler.
//
// Example:
// Argocd-Target-Cluster-URL: "https://kubernetes.default.svc.cluster.local"
HeaderArgoCDTargetClusterURL = "Argocd-Target-Cluster-URL"
// HeaderArgoCDTargetClusterName defines the target cluster name
// that the Argo CD application is associated with. This header
// will be populated by the extension proxy and passed to the
// configured backend service. If this header is passed by
// the client, its value will be overriden by the extension
// handler.
HeaderArgoCDTargetClusterName = "Argocd-Target-Cluster-Name"
)
// RequestResources defines the authorization scope for
// an incoming request to a given extension. This struct
// is populated from pre-defined Argo CD headers.
type RequestResources struct {
ApplicationName string
ApplicationNamespace string
ProjectName string
}
// ValidateHeaders will validate the pre-defined Argo CD
// request headers for extensions and extract the resources
// information populating and returning a RequestResources
// object.
// The pre-defined headers are:
// - Argocd-Application-Name
// - Argocd-Project-Name
//
// The headers expected format is documented in each of the constant
// types defined for them.
func ValidateHeaders(r *http.Request) (*RequestResources, error) {
appHeader := r.Header.Get(HeaderArgoCDApplicationName)
if appHeader == "" {
return nil, fmt.Errorf("header %q must be provided", HeaderArgoCDApplicationName)
}
appNamespace, appName, err := getAppName(appHeader)
if err != nil {
return nil, fmt.Errorf("error getting app details: %s", err)
}
if !argo.IsValidNamespaceName(appNamespace) {
return nil, errors.New("invalid value for namespace")
}
if !argo.IsValidAppName(appName) {
return nil, errors.New("invalid value for application name")
}
projName := r.Header.Get(HeaderArgoCDProjectName)
if projName == "" {
return nil, fmt.Errorf("header %q must be provided", HeaderArgoCDProjectName)
}
if !argo.IsValidProjectName(projName) {
return nil, errors.New("invalid value for project name")
}
return &RequestResources{
ApplicationName: appName,
ApplicationNamespace: appNamespace,
ProjectName: projName,
}, nil
}
func getAppName(appHeader string) (string, string, error) {
parts := strings.Split(appHeader, ":")
if len(parts) != 2 {
return "", "", fmt.Errorf("invalid value for %q header: expected format: <namespace>:<app-name>", HeaderArgoCDApplicationName)
}
return parts[0], parts[1], nil
}
// ExtensionConfigs defines the configurations for all extensions
// retrieved from Argo CD configmap (argocd-cm).
type ExtensionConfigs struct {
Extensions []ExtensionConfig `yaml:"extensions"`
}
// ExtensionConfig defines the configuration for one extension.
type ExtensionConfig struct {
// Name defines the endpoint that will be used to register
// the extension route. Mandatory field.
Name string `yaml:"name"`
Backend BackendConfig `yaml:"backend"`
}
// BackendConfig defines the backend service configurations that will
// be used by an specific extension. An extension can have multiple services
// associated. This is necessary when Argo CD is managing applications in
// external clusters. In this case, each cluster may have its own backend
// service.
type BackendConfig struct {
ProxyConfig
Services []ServiceConfig `yaml:"services"`
}
// ServiceConfig provides the configuration for a backend service.
type ServiceConfig struct {
// URL is the address where the extension backend must be available.
// Mandatory field.
URL string `yaml:"url"`
// Cluster if provided, will have to match the application
// destination name to have requests properly forwarded to this
// service URL.
Cluster *ClusterConfig `yaml:"cluster,omitempty"`
// Headers if provided, the headers list will be added on all
// outgoing requests for this service config.
Headers []Header `yaml:"headers"`
}
// Header defines the header to be added in the proxy requests.
type Header struct {
// Name defines the name of the header. It is a mandatory field if
// a header is provided.
Name string `yaml:"name"`
// Value defines the value of the header. The actual value can be
// provided as verbatim or as a reference to an Argo CD secret key.
// In order to provide it as a reference, it is necessary to prefix
// it with a dollar sign.
// Example:
// value: '$some.argocd.secret.key'
// In the example above, the value will be replaced with the one from
// the argocd-secret with key 'some.argocd.secret.key'.
Value string `yaml:"value"`
}
type ClusterConfig struct {
// Server specifies the URL of the target cluster's Kubernetes control plane API. This must be set if Name is not set.
Server string `yaml:"server"`
// Name is an alternate way of specifying the target cluster by its symbolic name. This must be set if Server is not set.
Name string `yaml:"name"`
}
// ProxyConfig allows configuring connection behaviour between Argo CD
// API Server and the backend service.
type ProxyConfig struct {
// ConnectionTimeout is the maximum amount of time a dial to
// the extension server will wait for a connect to complete.
// Default: 2 seconds
ConnectionTimeout time.Duration `yaml:"connectionTimeout"`
// KeepAlive specifies the interval between keep-alive probes
// for an active network connection between the API server and
// the extension server.
// Default: 15 seconds
KeepAlive time.Duration `yaml:"keepAlive"`
// IdleConnectionTimeout is the maximum amount of time an idle
// (keep-alive) connection between the API server and the extension
// server will remain idle before closing itself.
// Default: 60 seconds
IdleConnectionTimeout time.Duration `yaml:"idleConnectionTimeout"`
// MaxIdleConnections controls the maximum number of idle (keep-alive)
// connections between the API server and the extension server.
// Default: 30
MaxIdleConnections int `yaml:"maxIdleConnections"`
}
// SettingsGetter defines the contract to retrieve Argo CD Settings.
type SettingsGetter interface {
Get() (*settings.ArgoCDSettings, error)
}
// DefaultSettingsGetter is the real settings getter implementation.
type DefaultSettingsGetter struct {
settingsMgr *settings.SettingsManager
}
// NewDefaultSettingsGetter returns a new default settings getter.
func NewDefaultSettingsGetter(mgr *settings.SettingsManager) *DefaultSettingsGetter {
return &DefaultSettingsGetter{
settingsMgr: mgr,
}
}
// Get will retrieve the Argo CD settings.
func (s *DefaultSettingsGetter) Get() (*settings.ArgoCDSettings, error) {
return s.settingsMgr.GetSettings()
}
// ProjectGetter defines the contract to retrieve Argo CD Project.
type ProjectGetter interface {
Get(name string) (*v1alpha1.AppProject, error)
GetClusters(project string) ([]*v1alpha1.Cluster, error)
}
// DefaultProjectGetter is the real ProjectGetter implementation.
type DefaultProjectGetter struct {
projLister applisters.AppProjectNamespaceLister
db db.ArgoDB
}
// NewDefaultProjectGetter returns a new default project getter
func NewDefaultProjectGetter(lister applisters.AppProjectNamespaceLister, db db.ArgoDB) *DefaultProjectGetter {
return &DefaultProjectGetter{
projLister: lister,
db: db,
}
}
// Get will retrieve the live AppProject state.
func (p *DefaultProjectGetter) Get(name string) (*v1alpha1.AppProject, error) {
return p.projLister.Get(name)
}
// GetClusters will retrieve the clusters configured by a project.
func (p *DefaultProjectGetter) GetClusters(project string) ([]*v1alpha1.Cluster, error) {
return p.db.GetProjectClusters(context.TODO(), project)
}
// ApplicationGetter defines the contract to retrieve the application resource.
type ApplicationGetter interface {
Get(ns, name string) (*v1alpha1.Application, error)
}
// DefaultApplicationGetter is the real application getter implementation.
type DefaultApplicationGetter struct {
appLister applisters.ApplicationLister
}
// NewDefaultApplicationGetter returns the default application getter.
func NewDefaultApplicationGetter(al applisters.ApplicationLister) *DefaultApplicationGetter {
return &DefaultApplicationGetter{
appLister: al,
}
}
// Get will retrieve the application resorce for the given namespace and name.
func (a *DefaultApplicationGetter) Get(ns, name string) (*v1alpha1.Application, error) {
return a.appLister.Applications(ns).Get(name)
}
// RbacEnforcer defines the contract to enforce rbac rules
type RbacEnforcer interface {
EnforceErr(rvals ...interface{}) error
}
// Manager is the object that will be responsible for registering
// and handling proxy extensions.
type Manager struct {
log *log.Entry
settings SettingsGetter
application ApplicationGetter
project ProjectGetter
rbac RbacEnforcer
registry ExtensionRegistry
}
// NewManager will initialize a new manager.
func NewManager(log *log.Entry, sg SettingsGetter, ag ApplicationGetter, pg ProjectGetter, rbac RbacEnforcer) *Manager {
return &Manager{
log: log,
settings: sg,
application: ag,
project: pg,
rbac: rbac,
}
}
// ExtensionRegistry is an in memory registry that contains contains all
// proxies for all extensions. The key is the extension name defined in
// the Argo CD configmap.
type ExtensionRegistry map[string]ProxyRegistry
// ProxyRegistry is an in memory registry that contains all proxies for a
// given extension. Different extensions will have independent proxy registries.
// This is required to address the use case when one extension is configured with
// multiple backend services in different clusters.
type ProxyRegistry map[ProxyKey]*httputil.ReverseProxy
// NewProxyRegistry will instantiate a new in memory registry for proxies.
func NewProxyRegistry() ProxyRegistry {
r := make(map[ProxyKey]*httputil.ReverseProxy)
return r
}
// ProxyKey defines the struct used as a key in the proxy registry
// map (ProxyRegistry).
type ProxyKey struct {
extensionName string
clusterName string
clusterServer string
}
// proxyKey will build the key to be used in the proxyByCluster
// map.
func proxyKey(extName, cName, cServer string) ProxyKey {
return ProxyKey{
extensionName: extName,
clusterName: cName,
clusterServer: cServer,
}
}
func parseAndValidateConfig(s *settings.ArgoCDSettings) (*ExtensionConfigs, error) {
if s.ExtensionConfig == "" {
return nil, fmt.Errorf("no extensions configurations found")
}
extConfigMap := map[string]interface{}{}
err := yaml.Unmarshal([]byte(s.ExtensionConfig), &extConfigMap)
if err != nil {
return nil, fmt.Errorf("invalid extension config: %s", err)
}
parsedExtConfig := settings.ReplaceMapSecrets(extConfigMap, s.Secrets)
parsedExtConfigBytes, err := yaml.Marshal(parsedExtConfig)
if err != nil {
return nil, fmt.Errorf("error marshaling parsed extension config: %s", err)
}
configs := ExtensionConfigs{}
err = yaml.Unmarshal(parsedExtConfigBytes, &configs)
if err != nil {
return nil, fmt.Errorf("invalid parsed extension config: %s", err)
}
err = validateConfigs(&configs)
if err != nil {
return nil, fmt.Errorf("validation error: %s", err)
}
return &configs, nil
}
func validateConfigs(configs *ExtensionConfigs) error {
nameSafeRegex := regexp.MustCompile(`^[A-Za-z0-9-_]+$`)
exts := make(map[string]struct{})
for _, ext := range configs.Extensions {
if ext.Name == "" {
return fmt.Errorf("extensions.name must be configured")
}
if !nameSafeRegex.MatchString(ext.Name) {
return fmt.Errorf("invalid extensions.name: only alphanumeric characters, hyphens, and underscores are allowed")
}
if _, found := exts[ext.Name]; found {
return fmt.Errorf("duplicated extension found in the configs for %q", ext.Name)
}
exts[ext.Name] = struct{}{}
svcTotal := len(ext.Backend.Services)
if svcTotal == 0 {
return fmt.Errorf("no backend service configured for extension %s", ext.Name)
}
for _, svc := range ext.Backend.Services {
if svc.URL == "" {
return fmt.Errorf("extensions.backend.services.url must be configured")
}
if svcTotal > 1 && svc.Cluster == nil {
return fmt.Errorf("extensions.backend.services.cluster must be configured when defining more than one service per extension")
}
if svc.Cluster != nil {
if svc.Cluster.Name == "" && svc.Cluster.Server == "" {
return fmt.Errorf("cluster.name or cluster.server must be defined when cluster is provided in the configuration")
}
}
if len(svc.Headers) > 0 {
for _, header := range svc.Headers {
if header.Name == "" {
return fmt.Errorf("header.name must be defined when providing service headers in the configuration")
}
if header.Value == "" {
return fmt.Errorf("header.value must be defined when providing service headers in the configuration")
}
}
}
}
}
return nil
}
// NewProxy will instantiate a new reverse proxy based on the provided
// targetURL and config.
func NewProxy(targetURL string, headers []Header, config ProxyConfig) (*httputil.ReverseProxy, error) {
url, err := url.Parse(targetURL)
if err != nil {
return nil, fmt.Errorf("failed to parse proxy URL: %s", err)
}
proxy := &httputil.ReverseProxy{
Transport: newTransport(config),
Director: func(req *http.Request) {
req.Host = url.Host
req.URL.Scheme = url.Scheme
req.URL.Host = url.Host
req.Header.Set("Host", url.Host)
req.Header.Del("Authorization")
req.Header.Del("Cookie")
for _, header := range headers {
req.Header.Set(header.Name, header.Value)
}
},
}
return proxy, nil
}
// newTransport will build a new transport to be used in the proxy
// applying default values if not defined in the given config.
func newTransport(config ProxyConfig) *http.Transport {
applyProxyConfigDefaults(&config)
return &http.Transport{
DialContext: (&net.Dialer{
Timeout: config.ConnectionTimeout,
KeepAlive: config.KeepAlive,
}).DialContext,
MaxIdleConns: config.MaxIdleConnections,
IdleConnTimeout: config.IdleConnectionTimeout,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
}
func applyProxyConfigDefaults(c *ProxyConfig) {
if c.ConnectionTimeout == 0 {
c.ConnectionTimeout = DefaultConnectionTimeout
}
if c.KeepAlive == 0 {
c.KeepAlive = DefaultKeepAlive
}
if c.IdleConnectionTimeout == 0 {
c.IdleConnectionTimeout = DefaultIdleConnectionTimeout
}
if c.MaxIdleConnections == 0 {
c.MaxIdleConnections = DefaultMaxIdleConnections
}
}
// RegisterExtensions will retrieve all extensions configurations
// and update the extension registry.
func (m *Manager) RegisterExtensions() error {
settings, err := m.settings.Get()
if err != nil {
return fmt.Errorf("error getting settings: %s", err)
}
err = m.UpdateExtensionRegistry(settings)
if err != nil {
return fmt.Errorf("error updating extension registry: %s", err)
}
return nil
}
// UpdateExtensionRegistry will first parse and validate the extensions
// configurations from the given settings. If no errors are found, it will
// iterate over the given configurations building a new extension registry.
// At the end, it will update the manager with the newly created registry.
func (m *Manager) UpdateExtensionRegistry(s *settings.ArgoCDSettings) error {
extConfigs, err := parseAndValidateConfig(s)
if err != nil {
return fmt.Errorf("error parsing extension config: %s", err)
}
extReg := make(map[string]ProxyRegistry)
for _, ext := range extConfigs.Extensions {
proxyReg := NewProxyRegistry()
singleBackend := len(ext.Backend.Services) == 1
for _, service := range ext.Backend.Services {
proxy, err := NewProxy(service.URL, service.Headers, ext.Backend.ProxyConfig)
if err != nil {
return fmt.Errorf("error creating proxy: %s", err)
}
err = appendProxy(proxyReg, ext.Name, service, proxy, singleBackend)
if err != nil {
return fmt.Errorf("error appending proxy: %s", err)
}
}
extReg[ext.Name] = proxyReg
}
m.registry = extReg
return nil
}
// appendProxy will append the given proxy in the given registry. Will use
// the provided extName and service to determine the map key. The key must
// be unique in the map. If the map already has the key and error is returned.
func appendProxy(registry ProxyRegistry,
extName string,
service ServiceConfig,
proxy *httputil.ReverseProxy,
singleBackend bool) error {
if singleBackend {
key := proxyKey(extName, "", "")
if _, exist := registry[key]; exist {
return fmt.Errorf("duplicated proxy configuration found for extension key %q", key)
}
registry[key] = proxy
return nil
}
// This is the case where there are more than one backend configured
// for this extension. In this case we need to add the provided cluster
// configurations for proper correlation to find which proxy to use
// while handling requests.
if service.Cluster.Name != "" {
key := proxyKey(extName, service.Cluster.Name, "")
if _, exist := registry[key]; exist {
return fmt.Errorf("duplicated proxy configuration found for extension key %q", key)
}
registry[key] = proxy
}
if service.Cluster.Server != "" {
key := proxyKey(extName, "", service.Cluster.Server)
if _, exist := registry[key]; exist {
return fmt.Errorf("duplicated proxy configuration found for extension key %q", key)
}
registry[key] = proxy
}
return nil
}
// authorize will enforce rbac rules are satified for the given RequestResources.
// The following validations are executed:
// - enforce the subject has permission to read application/project provided
// in HeaderArgoCDApplicationName and HeaderArgoCDProjectName.
// - enforce the subject has permission to invoke the extension identified by
// extName.
// - enforce that the project has permission to access the destination cluster.
//
// If all validations are satified it will return the Application resource
func (m *Manager) authorize(ctx context.Context, rr *RequestResources, extName string) (*v1alpha1.Application, error) {
if m.rbac == nil {
return nil, fmt.Errorf("rbac enforcer not set in extension manager")
}
appRBACName := security.RBACName(rr.ApplicationNamespace, rr.ProjectName, rr.ApplicationNamespace, rr.ApplicationName)
if err := m.rbac.EnforceErr(ctx.Value("claims"), rbacpolicy.ResourceApplications, rbacpolicy.ActionGet, appRBACName); err != nil {
return nil, fmt.Errorf("application authorization error: %s", err)
}
if err := m.rbac.EnforceErr(ctx.Value("claims"), rbacpolicy.ResourceExtensions, rbacpolicy.ActionInvoke, extName); err != nil {
return nil, fmt.Errorf("unauthorized to invoke extension %q: %s", extName, err)
}
// just retrieve the app after checking if subject has access to it
app, err := m.application.Get(rr.ApplicationNamespace, rr.ApplicationName)
if err != nil {
return nil, fmt.Errorf("error getting application: %s", err)
}
if app == nil {
return nil, fmt.Errorf("invalid Application provided in the %q header", HeaderArgoCDApplicationName)
}
if app.Spec.GetProject() != rr.ProjectName {
return nil, fmt.Errorf("project mismatch provided in the %q header", HeaderArgoCDProjectName)
}
proj, err := m.project.Get(app.Spec.GetProject())
if err != nil {
return nil, fmt.Errorf("error getting project: %s", err)
}
if proj == nil {
return nil, fmt.Errorf("invalid project provided in the %q header", HeaderArgoCDProjectName)
}
permitted, err := proj.IsDestinationPermitted(app.Spec.Destination, m.project.GetClusters)
if err != nil {
return nil, fmt.Errorf("error validating project destinations: %s", err)
}
if !permitted {
return nil, fmt.Errorf("the provided project is not allowed to access the cluster configured in the Application destination")
}
return app, nil
}
// findProxy will search the given registry to find the correct proxy to use
// based on the given extName and dest.
func findProxy(registry ProxyRegistry, extName string, dest v1alpha1.ApplicationDestination) (*httputil.ReverseProxy, error) {
// First try to find the proxy in the registry just by the extension name.
// This is the simple case for extensions with only one backend service.
key := proxyKey(extName, "", "")
if proxy, found := registry[key]; found {
return proxy, nil
}
// If extension has multiple backend services configured, the correct proxy
// needs to be searched by the ApplicationDestination.
key = proxyKey(extName, dest.Name, dest.Server)
if proxy, found := registry[key]; found {
return proxy, nil
}
return nil, fmt.Errorf("no proxy found for extension %q", extName)
}
// ProxyRegistry returns the proxy registry associated for the given
// extension name.
func (m *Manager) ProxyRegistry(name string) (ProxyRegistry, bool) {
pReg, found := m.registry[name]
return pReg, found
}
// CallExtension returns a handler func responsible for forwarding requests to the
// extension service. The request will be sanitized by removing sensitive headers.
func (m *Manager) CallExtension() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
segments := strings.Split(strings.TrimPrefix(r.URL.Path, "/"), "/")
if segments[0] != "extensions" {
http.Error(w, fmt.Sprintf("Invalid URL: first segment must be %s", URLPrefix), http.StatusBadRequest)
return
}
extName := segments[1]
if extName == "" {
http.Error(w, "Invalid URL: extension name must be provided", http.StatusBadRequest)
return
}
extName = strings.ReplaceAll(extName, "\n", "")
extName = strings.ReplaceAll(extName, "\r", "")
reqResources, err := ValidateHeaders(r)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid headers: %s", err), http.StatusBadRequest)
return
}
app, err := m.authorize(r.Context(), reqResources, extName)
if err != nil {
m.log.Infof("unauthorized extension request: %s", err)
http.Error(w, "Unauthorized extension request", http.StatusUnauthorized)
return
}
proxyRegistry, ok := m.ProxyRegistry(extName)
if !ok {
m.log.Warnf("proxy extension warning: attempt to call unregistered extension: %s", extName)
http.Error(w, "Extension not found", http.StatusNotFound)
return
}
proxy, err := findProxy(proxyRegistry, extName, app.Spec.Destination)
if err != nil {
m.log.Errorf("findProxy error: %s", err)
http.Error(w, "invalid extension", http.StatusBadRequest)
return
}
prepareRequest(r, extName, app)
m.log.Debugf("proxing request for extension %q", extName)
proxy.ServeHTTP(w, r)
}
}
// prepareRequest is reponsible for preparing and cleaning the given
// request, removing sensitive information before forwarding it to the
// proxy extension.
func prepareRequest(r *http.Request, extName string, app *v1alpha1.Application) {
r.URL.Path = strings.TrimPrefix(r.URL.Path, fmt.Sprintf("%s/%s", URLPrefix, extName))
if app.Spec.Destination.Name != "" {
r.Header.Set(HeaderArgoCDTargetClusterName, app.Spec.Destination.Name)
}
if app.Spec.Destination.Server != "" {
r.Header.Set(HeaderArgoCDTargetClusterURL, app.Spec.Destination.Server)
}
}