forked from kyma-incubator/compass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servers.go
95 lines (74 loc) · 3.6 KB
/
servers.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
package config
import (
"net/http"
timeouthandler "github.com/kyma-incubator/compass/components/director/pkg/handler"
"github.com/kyma-incubator/compass/components/connector/internal/healthz"
log "github.com/sirupsen/logrus"
"github.com/99designs/gqlgen/handler"
"github.com/gorilla/mux"
"github.com/kyma-incubator/compass/components/connector/internal/api"
"github.com/kyma-incubator/compass/components/connector/internal/certificates"
"github.com/kyma-incubator/compass/components/connector/internal/revocation"
"github.com/kyma-incubator/compass/components/connector/internal/tokens"
"github.com/kyma-incubator/compass/components/connector/pkg/graphql/externalschema"
"github.com/kyma-incubator/compass/components/connector/pkg/graphql/internalschema"
"github.com/kyma-incubator/compass/components/connector/pkg/oathkeeper"
)
func PrepareExternalGraphQLServer(cfg Config, certResolver api.CertificateResolver, authContextMiddleware mux.MiddlewareFunc) (*http.Server, error) {
gqlInternalCfg := externalschema.Config{
Resolvers: &api.ExternalResolver{CertificateResolver: certResolver},
}
externalExecutableSchema := externalschema.NewExecutableSchema(gqlInternalCfg)
externalRouter := mux.NewRouter()
externalRouter.HandleFunc("/", handler.Playground("Dataloader", cfg.PlaygroundAPIEndpoint))
externalRouter.HandleFunc(cfg.APIEndpoint, handler.GraphQL(externalExecutableSchema))
externalRouter.HandleFunc("/healthz", healthz.NewHTTPHandler(log.StandardLogger()))
externalRouter.Use(authContextMiddleware)
handlerWithTimeout, err := timeouthandler.WithTimeout(externalRouter, cfg.ServerTimeout)
if err != nil {
return nil, err
}
return &http.Server{
Addr: cfg.ExternalAddress,
Handler: handlerWithTimeout,
ReadHeaderTimeout: cfg.ServerTimeout,
}, nil
}
func PrepareInternalGraphQLServer(cfg Config, tokenResolver api.TokenResolver) (*http.Server, error) {
gqlInternalCfg := internalschema.Config{
Resolvers: &api.InternalResolver{TokenResolver: tokenResolver},
}
internalExecutableSchema := internalschema.NewExecutableSchema(gqlInternalCfg)
internalRouter := mux.NewRouter()
internalRouter.HandleFunc("/", handler.Playground("Dataloader", cfg.PlaygroundAPIEndpoint))
internalRouter.HandleFunc(cfg.APIEndpoint, handler.GraphQL(internalExecutableSchema))
handlerWithTimeout, err := timeouthandler.WithTimeout(internalRouter, cfg.ServerTimeout)
if err != nil {
return nil, err
}
return &http.Server{
Addr: cfg.InternalAddress,
Handler: handlerWithTimeout,
ReadHeaderTimeout: cfg.ServerTimeout,
}, nil
}
func PrepareHydratorServer(cfg Config, tokenService tokens.Service, subjectConsts certificates.CSRSubjectConsts, revokedCertsRepository revocation.RevokedCertificatesRepository) (*http.Server, error) {
certHeaderParser := oathkeeper.NewHeaderParser(cfg.CertificateDataHeader, subjectConsts)
validationHydrator := oathkeeper.NewValidationHydrator(tokenService, certHeaderParser, revokedCertsRepository)
router := mux.NewRouter()
router.Path("/health").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
v1Router := router.PathPrefix("/v1").Subrouter()
v1Router.HandleFunc("/tokens/resolve", validationHydrator.ResolveConnectorTokenHeader)
v1Router.HandleFunc("/certificate/data/resolve", validationHydrator.ResolveIstioCertHeader)
handlerWithTimeout, err := timeouthandler.WithTimeout(router, cfg.ServerTimeout)
if err != nil {
return nil, err
}
return &http.Server{
Addr: cfg.HydratorAddress,
Handler: handlerWithTimeout,
ReadHeaderTimeout: cfg.ServerTimeout,
}, nil
}