/
applicationproxy.go
226 lines (186 loc) · 7.58 KB
/
applicationproxy.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
package main
import (
"context"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/kyma-project/kyma/components/central-application-gateway/internal/csrf"
csrfClient "github.com/kyma-project/kyma/components/central-application-gateway/internal/csrf/client"
csrfStrategy "github.com/kyma-project/kyma/components/central-application-gateway/internal/csrf/strategy"
"github.com/kyma-project/kyma/components/central-application-gateway/internal/externalapi"
"github.com/kyma-project/kyma/components/central-application-gateway/internal/metadata"
"github.com/kyma-project/kyma/components/central-application-gateway/internal/metadata/applications"
"github.com/kyma-project/kyma/components/central-application-gateway/internal/metadata/secrets"
"github.com/kyma-project/kyma/components/central-application-gateway/internal/metadata/serviceapi"
"github.com/kyma-project/kyma/components/central-application-gateway/internal/proxy"
"github.com/kyma-project/kyma/components/central-application-gateway/pkg/apperrors"
"github.com/kyma-project/kyma/components/central-application-gateway/pkg/authorization"
"github.com/kyma-project/kyma/components/central-application-gateway/pkg/client/clientset/versioned"
"github.com/kyma-project/kyma/components/central-application-gateway/pkg/httptools"
"github.com/oklog/run"
"go.uber.org/zap"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
shutdownTimeout = 2 * time.Second
)
func main() {
setupLogger := zap.Must(zap.NewProduction())
defer func(setupLogger *zap.Logger) {
err := setupLogger.Sync()
if err != nil {
panic(err)
}
}(setupLogger)
setupLogger.Info("Starting Application Gateway")
options := parseArgs(setupLogger)
logCfg := zap.NewProductionConfig()
logCfg.Level.SetLevel(*options.logLevel)
log, err := logCfg.Build()
zap.ReplaceGlobals(log)
defer func(log *zap.Logger) {
err := log.Sync()
if err != nil {
panic(err)
}
}(log)
if err != nil {
setupLogger.Fatal("Couldn't initiate logger", zap.Error(err))
}
k8sConfig, err := clientcmd.BuildConfigFromFlags(options.apiServerURL, options.kubeConfig)
if err != nil {
log.Fatal("Error reading in cluster config", zap.Error(err))
}
coreClientset, err := kubernetes.NewForConfig(k8sConfig)
if err != nil {
log.Fatal("Error creating core clientset", zap.Error(err))
}
serviceDefinitionService, err := newServiceDefinitionService(
k8sConfig,
coreClientset,
options.applicationSecretsNamespace,
)
if err != nil {
log.Fatal("Unable to create ServiceDefinitionService:'", zap.Error(err))
}
internalHandler := newInternalHandler(serviceDefinitionService, options)
internalHandlerForCompass := newInternalHandlerForCompass(serviceDefinitionService, options)
externalHandler := externalapi.NewHandler(logCfg.Level)
internalHandler = httptools.RequestLogger("Internal handler: ", internalHandler)
internalHandlerForCompass = httptools.RequestLogger("Internal handler: ", internalHandlerForCompass)
externalHandler = httptools.RequestLogger("External handler: ", externalHandler)
externalSrv := &http.Server{
Addr: ":" + strconv.Itoa(options.externalAPIPort),
Handler: externalHandler,
ReadTimeout: time.Duration(options.requestTimeout) * time.Second,
WriteTimeout: time.Duration(options.requestTimeout) * time.Second,
}
internalSrv := &http.Server{
Addr: ":" + strconv.Itoa(options.proxyPort),
Handler: internalHandler,
ReadTimeout: time.Duration(options.requestTimeout) * time.Second,
}
internalSrvCompass := &http.Server{
Addr: ":" + strconv.Itoa(options.proxyPortCompass),
Handler: internalHandlerForCompass,
ReadTimeout: time.Duration(options.requestTimeout) * time.Second,
}
var g run.Group
addHttpServerToRunGroup("external-api", &g, externalSrv)
addHttpServerToRunGroup("proxy-kyma-os", &g, internalSrv)
addHttpServerToRunGroup("proxy-kyma-mps", &g, internalSrvCompass)
addInterruptSignalToRunGroup(&g)
err = g.Run()
if err != nil && err != http.ErrServerClosed {
log.Fatal("Servers encountered error", zap.Error(err))
}
}
func addHttpServerToRunGroup(name string, g *run.Group, srv *http.Server) {
log := zap.L().Sugar()
log.Infof("Starting %s HTTP server on %s", name, srv.Addr)
ln, err := net.Listen("tcp", srv.Addr)
if err != nil {
log.Fatalf("Unable to start %s HTTP server: '%s'", name, err)
}
g.Add(func() error {
defer log.Infof("Server %s finished", name)
return srv.Serve(ln)
}, func(error) {
log.Infof("Shutting down %s HTTP server on %s", name, srv.Addr)
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
err = srv.Shutdown(ctx)
if err != nil && err != http.ErrServerClosed {
log.Warnf("HTTP server shutdown %s failed: %s", name, err)
}
})
}
func addInterruptSignalToRunGroup(g *run.Group) {
cancelInterrupt := make(chan struct{})
g.Add(func() error {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
select {
case <-cancelInterrupt:
case sig := <-c:
zap.L().Sugar().Infof("received signal %s", sig)
}
return nil
}, func(error) {
close(cancelInterrupt)
})
}
func newInternalHandler(serviceDefinitionService metadata.ServiceDefinitionService, options options) http.Handler {
authStrategyFactory := newAuthenticationStrategyFactory(options.proxyTimeout)
csrfCl := newCSRFClient(options.proxyTimeout)
csrfTokenStrategyFactory := csrfStrategy.NewTokenStrategyFactory(csrfCl)
return proxy.New(serviceDefinitionService, authStrategyFactory, csrfTokenStrategyFactory, getProxyConfig(options))
}
func newInternalHandlerForCompass(serviceDefinitionService metadata.ServiceDefinitionService, options options) http.Handler {
authStrategyFactory := newAuthenticationStrategyFactory(options.proxyTimeout)
csrfCl := newCSRFClient(options.proxyTimeout)
csrfTokenStrategyFactory := csrfStrategy.NewTokenStrategyFactory(csrfCl)
return proxy.NewForCompass(serviceDefinitionService, authStrategyFactory, csrfTokenStrategyFactory, getProxyConfig(options))
}
func getProxyConfig(options options) proxy.Config {
return proxy.Config{
ProxyTimeout: options.proxyTimeout,
ProxyCacheTTL: options.proxyCacheTTL,
}
}
func newAuthenticationStrategyFactory(oauthClientTimeout int) authorization.StrategyFactory {
return authorization.NewStrategyFactory(authorization.FactoryConfiguration{
OAuthClientTimeout: oauthClientTimeout,
})
}
func newServiceDefinitionService(k8sConfig *restclient.Config, coreClientset kubernetes.Interface, namespace string) (metadata.ServiceDefinitionService, error) {
applicationServiceRepository, apperror := newApplicationRepository(k8sConfig)
if apperror != nil {
return nil, apperror
}
secretsRepository := newSecretsRepository(coreClientset, namespace)
serviceAPIService := serviceapi.NewService(secretsRepository)
return metadata.NewServiceDefinitionService(serviceAPIService, applicationServiceRepository), nil
}
func newApplicationRepository(config *restclient.Config) (applications.ServiceRepository, apperrors.AppError) {
applicationClientset, err := versioned.NewForConfig(config)
if err != nil {
return nil, apperrors.Internal("failed to create k8s application client, %s", err)
}
rei := applicationClientset.ApplicationconnectorV1alpha1().Applications()
return applications.NewServiceRepository(rei), nil
}
func newSecretsRepository(coreClientset kubernetes.Interface, namespace string) secrets.Repository {
sei := coreClientset.CoreV1().Secrets(namespace)
return secrets.NewRepository(sei)
}
func newCSRFClient(timeout int) csrf.Client {
cache := csrfClient.NewTokenCache()
return csrfClient.New(timeout, cache)
}