forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractor.go
119 lines (92 loc) · 3.48 KB
/
extractor.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
package clientcontext
import (
"context"
"fmt"
"github.com/kyma-project/kyma/components/connector-service/internal/certificates"
"github.com/kyma-project/kyma/components/connector-service/internal/apperrors"
)
const (
MetadataURLFormat = "%s/%s/v1/metadata/services"
EventsURLFormat = "%s/%s/v1/events"
EventsInfoURLFormat = "%s/%s/v1/events/subscribed"
RuntimeDefaultCommonName = "*Runtime*"
)
type ConnectorClientExtractor func(ctx context.Context) (ClientCertContextService, apperrors.AppError)
type ApplicationContextExtractor func(ctx context.Context) (ApplicationContext, apperrors.AppError)
type ContextExtractor struct {
subjectDefaults certificates.CSRSubject
}
func NewContextExtractor(subjectDefaults certificates.CSRSubject) *ContextExtractor {
return &ContextExtractor{
subjectDefaults: subjectDefaults,
}
}
func (ext *ContextExtractor) CreateApplicationClientContextService(ctx context.Context) (ClientCertContextService, apperrors.AppError) {
appCtx, err := ExtractApplicationContext(ctx)
if err != nil {
return nil, err
}
subject := ext.prepareSubject(appCtx.Tenant, appCtx.Group, appCtx.Application)
apiHosts, ok := ctx.Value(ApiURLsKey).(ApiURLs)
if !ok {
return newClientCertificateContext(appCtx, subject), nil
}
extendedCtx := ExtendedApplicationContext{
ApplicationContext: appCtx,
RuntimeURLs: prepareRuntimeURLs(appCtx, apiHosts),
}
return newClientCertificateContext(extendedCtx, subject), nil
}
func prepareRuntimeURLs(appCtx ApplicationContext, apiHosts ApiURLs) RuntimeURLs {
metadataURL := ""
eventsURL := ""
eventsInfoURL := ""
if apiHosts.MetadataBaseURL != "" {
metadataURL = fmt.Sprintf(MetadataURLFormat, apiHosts.MetadataBaseURL, appCtx.GetApplication())
}
if apiHosts.EventsBaseURL != "" {
eventsURL = fmt.Sprintf(EventsURLFormat, apiHosts.EventsBaseURL, appCtx.GetApplication())
eventsInfoURL = fmt.Sprintf(EventsInfoURLFormat, apiHosts.EventsBaseURL, appCtx.GetApplication())
}
return RuntimeURLs{
MetadataURL: metadataURL,
EventsURL: eventsURL,
EventsInfoURL: eventsInfoURL,
}
}
func (ext *ContextExtractor) prepareSubject(org, orgUnit, commonName string) certificates.CSRSubject {
organization := org
organizationalUnit := orgUnit
if isEmpty(organization) {
organization = ext.subjectDefaults.Organization
}
if isEmpty(organizationalUnit) {
organizationalUnit = ext.subjectDefaults.OrganizationalUnit
}
return certificates.CSRSubject{
Organization: organization,
OrganizationalUnit: organizationalUnit,
CommonName: commonName,
Country: ext.subjectDefaults.Country,
Locality: ext.subjectDefaults.Locality,
Province: ext.subjectDefaults.Province,
}
}
func (ext *ContextExtractor) CreateClusterClientContextService(ctx context.Context) (ClientCertContextService, apperrors.AppError) {
clusterCtx, ok := ctx.Value(ClusterContextKey).(ClusterContext)
if !ok {
return nil, apperrors.Internal("Failed to extract ClusterContext from request")
}
subject := ext.prepareSubject(clusterCtx.Tenant, clusterCtx.Group, RuntimeDefaultCommonName)
return newClientCertificateContext(clusterCtx, subject), nil
}
func ExtractApplicationContext(ctx context.Context) (ApplicationContext, apperrors.AppError) {
appCtx, ok := ctx.Value(ApplicationContextKey).(ApplicationContext)
if !ok {
return ApplicationContext{}, apperrors.Internal("Failed to extract ApplicationContext from request")
}
return appCtx, nil
}
func isEmpty(str string) bool {
return str == ""
}