2018-04-05 11:46:04 +00:00
|
|
|
//
|
|
|
|
// DISCLAIMER
|
|
|
|
//
|
2021-10-09 14:47:18 +00:00
|
|
|
// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany
|
2018-04-05 11:46:04 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// Copyright holder is ArangoDB GmbH, Cologne, Germany
|
|
|
|
//
|
|
|
|
// Author Ewout Prangsma
|
|
|
|
//
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2021-03-23 15:47:28 +00:00
|
|
|
"context"
|
2018-04-05 11:46:04 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"net/http"
|
2018-07-02 12:58:06 +00:00
|
|
|
"strings"
|
2018-04-05 11:46:04 +00:00
|
|
|
"time"
|
|
|
|
|
2021-07-08 10:11:39 +00:00
|
|
|
operatorHTTP "github.com/arangodb/kube-arangodb/pkg/util/http"
|
|
|
|
"github.com/arangodb/kube-arangodb/pkg/version"
|
|
|
|
|
2021-01-08 14:35:38 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/errors"
|
|
|
|
|
2019-09-23 07:00:10 +00:00
|
|
|
"github.com/arangodb-helper/go-certificates"
|
2018-07-02 07:54:19 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2019-09-23 07:00:10 +00:00
|
|
|
"github.com/jessevdk/go-assets"
|
2019-07-08 09:49:32 +00:00
|
|
|
prometheus "github.com/prometheus/client_golang/prometheus/promhttp"
|
2018-07-02 10:40:32 +00:00
|
|
|
"github.com/rs/zerolog"
|
2019-09-27 11:04:23 +00:00
|
|
|
core "k8s.io/api/core/v1"
|
2018-04-05 11:46:04 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
2018-07-02 07:54:19 +00:00
|
|
|
|
2018-07-02 12:58:06 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/dashboard"
|
2018-07-02 07:54:19 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/probe"
|
2018-04-05 11:46:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config settings for the Server
|
|
|
|
type Config struct {
|
2018-07-02 16:24:42 +00:00
|
|
|
Namespace string
|
2018-04-05 11:46:04 +00:00
|
|
|
Address string // Address to listen on
|
|
|
|
TLSSecretName string // Name of secret containing TLS certificate
|
|
|
|
TLSSecretNamespace string // Namespace of secret containing TLS certificate
|
|
|
|
PodName string // Name of the Pod we're running in
|
|
|
|
PodIP string // IP address of the Pod we're running in
|
2018-07-06 10:44:43 +00:00
|
|
|
AdminSecretName string // Name of basic authentication secret containing the admin username+password of the dashboard
|
|
|
|
AllowAnonymous bool // If set, anonymous access to dashboard is allowed
|
2018-04-05 11:46:04 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 07:00:10 +00:00
|
|
|
type OperatorDependency struct {
|
|
|
|
Enabled bool
|
2020-03-10 09:26:38 +00:00
|
|
|
Probe *probe.ReadyProbe
|
2019-09-23 07:00:10 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 07:54:19 +00:00
|
|
|
// Dependencies of the Server
|
|
|
|
type Dependencies struct {
|
2019-09-23 07:00:10 +00:00
|
|
|
Log zerolog.Logger
|
|
|
|
LivenessProbe *probe.LivenessProbe
|
|
|
|
Deployment OperatorDependency
|
|
|
|
DeploymentReplication OperatorDependency
|
|
|
|
Storage OperatorDependency
|
2019-09-27 11:04:23 +00:00
|
|
|
Backup OperatorDependency
|
2021-12-31 11:38:53 +00:00
|
|
|
Apps OperatorDependency
|
2022-01-06 20:27:01 +00:00
|
|
|
ClusterSync OperatorDependency
|
2019-09-23 07:00:10 +00:00
|
|
|
Operators Operators
|
|
|
|
Secrets corev1.SecretInterface
|
2018-07-02 10:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Operators is the API provided to the server for accessing the various operators.
|
|
|
|
type Operators interface {
|
|
|
|
// Return the deployment operator (if any)
|
|
|
|
DeploymentOperator() DeploymentOperator
|
2018-07-10 10:01:09 +00:00
|
|
|
// Return the deployment replication operator (if any)
|
|
|
|
DeploymentReplicationOperator() DeploymentReplicationOperator
|
2018-07-09 09:05:31 +00:00
|
|
|
// Return the local storage operator (if any)
|
|
|
|
StorageOperator() StorageOperator
|
2018-07-11 10:28:34 +00:00
|
|
|
// FindOtherOperators looks up references to other operators in the same Kubernetes cluster.
|
|
|
|
FindOtherOperators() []OperatorReference
|
2018-07-02 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 11:46:04 +00:00
|
|
|
// Server is the HTTPS server for the operator.
|
|
|
|
type Server struct {
|
2018-07-02 16:24:42 +00:00
|
|
|
cfg Config
|
2018-07-02 07:54:19 +00:00
|
|
|
deps Dependencies
|
2018-04-05 11:46:04 +00:00
|
|
|
httpServer *http.Server
|
2018-07-06 10:44:43 +00:00
|
|
|
auth *serverAuthentication
|
2018-04-05 11:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates a new server, fetching/preparing a TLS certificate.
|
2018-07-02 07:54:19 +00:00
|
|
|
func NewServer(cli corev1.CoreV1Interface, cfg Config, deps Dependencies) (*Server, error) {
|
2018-04-05 11:46:04 +00:00
|
|
|
httpServer := &http.Server{
|
|
|
|
Addr: cfg.Address,
|
|
|
|
ReadTimeout: time.Second * 30,
|
|
|
|
ReadHeaderTimeout: time.Second * 15,
|
|
|
|
WriteTimeout: time.Second * 30,
|
|
|
|
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
|
|
|
|
}
|
|
|
|
|
|
|
|
var cert, key string
|
|
|
|
if cfg.TLSSecretName != "" && cfg.TLSSecretNamespace != "" {
|
|
|
|
// Load TLS certificate from secret
|
2021-03-23 15:47:28 +00:00
|
|
|
s, err := cli.Secrets(cfg.TLSSecretNamespace).Get(context.Background(), cfg.TLSSecretName, metav1.GetOptions{})
|
2018-04-05 11:46:04 +00:00
|
|
|
if err != nil {
|
2021-01-08 14:35:38 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2018-04-05 11:46:04 +00:00
|
|
|
}
|
2019-09-27 11:04:23 +00:00
|
|
|
certBytes, found := s.Data[core.TLSCertKey]
|
2018-04-05 11:46:04 +00:00
|
|
|
if !found {
|
2021-01-08 14:35:38 +00:00
|
|
|
return nil, errors.WithStack(errors.Newf("No %s found in secret %s", core.TLSCertKey, cfg.TLSSecretName))
|
2018-04-05 11:46:04 +00:00
|
|
|
}
|
2019-09-27 11:04:23 +00:00
|
|
|
keyBytes, found := s.Data[core.TLSPrivateKeyKey]
|
2018-04-05 11:46:04 +00:00
|
|
|
if !found {
|
2021-01-08 14:35:38 +00:00
|
|
|
return nil, errors.WithStack(errors.Newf("No %s found in secret %s", core.TLSPrivateKeyKey, cfg.TLSSecretName))
|
2018-04-05 11:46:04 +00:00
|
|
|
}
|
|
|
|
cert = string(certBytes)
|
|
|
|
key = string(keyBytes)
|
|
|
|
} else {
|
|
|
|
// Secret not specified, create our own TLS certificate
|
|
|
|
options := certificates.CreateCertificateOptions{
|
|
|
|
CommonName: cfg.PodName,
|
|
|
|
Hosts: []string{cfg.PodName, cfg.PodIP},
|
|
|
|
ValidFrom: time.Now(),
|
|
|
|
ValidFor: time.Hour * 24 * 365 * 10,
|
|
|
|
IsCA: false,
|
|
|
|
ECDSACurve: "P256",
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
cert, key, err = certificates.CreateCertificate(options, nil)
|
|
|
|
if err != nil {
|
2021-01-08 14:35:38 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2018-04-05 11:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tlsConfig, err := createTLSConfig(cert, key)
|
|
|
|
if err != nil {
|
2021-01-08 14:35:38 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2018-04-05 11:46:04 +00:00
|
|
|
}
|
|
|
|
tlsConfig.BuildNameToCertificate()
|
|
|
|
httpServer.TLSConfig = tlsConfig
|
|
|
|
|
2018-07-02 10:40:32 +00:00
|
|
|
// Builder server
|
|
|
|
s := &Server{
|
2018-07-02 16:24:42 +00:00
|
|
|
cfg: cfg,
|
2018-07-02 10:40:32 +00:00
|
|
|
deps: deps,
|
|
|
|
httpServer: httpServer,
|
2018-07-06 10:44:43 +00:00
|
|
|
auth: newServerAuthentication(deps.Log, deps.Secrets, cfg.AdminSecretName, cfg.AllowAnonymous),
|
2018-07-02 10:40:32 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 07:54:19 +00:00
|
|
|
// Build router
|
2019-07-08 13:41:29 +00:00
|
|
|
gin.SetMode(gin.ReleaseMode)
|
2018-07-02 07:54:19 +00:00
|
|
|
r := gin.New()
|
|
|
|
r.Use(gin.Recovery())
|
|
|
|
r.GET("/health", gin.WrapF(deps.LivenessProbe.LivenessHandler))
|
2019-09-23 07:00:10 +00:00
|
|
|
|
2021-07-08 10:11:39 +00:00
|
|
|
versionV1Responser, err := operatorHTTP.NewSimpleJSONResponse(version.GetVersionV1())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
r.GET("/_api/version", gin.WrapF(versionV1Responser.ServeHTTP))
|
|
|
|
r.GET("/api/v1/version", gin.WrapF(versionV1Responser.ServeHTTP))
|
|
|
|
|
2019-09-23 07:00:10 +00:00
|
|
|
var readyProbes []*probe.ReadyProbe
|
|
|
|
if deps.Deployment.Enabled {
|
|
|
|
r.GET("/ready/deployment", gin.WrapF(deps.Deployment.Probe.ReadyHandler))
|
|
|
|
readyProbes = append(readyProbes, deps.Deployment.Probe)
|
|
|
|
}
|
|
|
|
if deps.DeploymentReplication.Enabled {
|
|
|
|
r.GET("/ready/deployment-replication", gin.WrapF(deps.DeploymentReplication.Probe.ReadyHandler))
|
|
|
|
readyProbes = append(readyProbes, deps.DeploymentReplication.Probe)
|
|
|
|
}
|
|
|
|
if deps.Storage.Enabled {
|
|
|
|
r.GET("/ready/storage", gin.WrapF(deps.Storage.Probe.ReadyHandler))
|
|
|
|
readyProbes = append(readyProbes, deps.Storage.Probe)
|
|
|
|
}
|
|
|
|
r.GET("/ready", gin.WrapF(ready(readyProbes...)))
|
2018-07-02 07:54:19 +00:00
|
|
|
r.GET("/metrics", gin.WrapH(prometheus.Handler()))
|
2018-07-06 10:44:43 +00:00
|
|
|
r.POST("/login", s.auth.handleLogin)
|
|
|
|
api := r.Group("/api", s.auth.checkAuthentication)
|
2018-07-02 10:40:32 +00:00
|
|
|
{
|
|
|
|
api.GET("/operators", s.handleGetOperators)
|
|
|
|
|
|
|
|
// Deployment operator
|
|
|
|
api.GET("/deployment", s.handleGetDeployments)
|
2018-07-05 15:16:36 +00:00
|
|
|
api.GET("/deployment/:name", s.handleGetDeploymentDetails)
|
2018-07-09 09:05:31 +00:00
|
|
|
|
2018-07-10 10:01:09 +00:00
|
|
|
// Deployment replication operator
|
|
|
|
api.GET("/deployment-replication", s.handleGetDeploymentReplications)
|
|
|
|
api.GET("/deployment-replication/:name", s.handleGetDeploymentReplicationDetails)
|
|
|
|
|
2018-07-09 09:05:31 +00:00
|
|
|
// Local storage operator
|
|
|
|
api.GET("/storage", s.handleGetLocalStorages)
|
|
|
|
api.GET("/storage/:name", s.handleGetLocalStorageDetails)
|
2018-07-02 10:40:32 +00:00
|
|
|
}
|
2018-07-02 12:58:06 +00:00
|
|
|
// Dashboard
|
2018-07-02 13:41:44 +00:00
|
|
|
r.GET("/", createAssetFileHandler(dashboard.Assets.Files["index.html"]))
|
2018-07-02 12:58:06 +00:00
|
|
|
for path, file := range dashboard.Assets.Files {
|
|
|
|
localPath := "/" + strings.TrimPrefix(path, "/")
|
2018-07-02 13:41:44 +00:00
|
|
|
r.GET(localPath, createAssetFileHandler(file))
|
2018-07-02 12:58:06 +00:00
|
|
|
}
|
2018-07-02 07:54:19 +00:00
|
|
|
httpServer.Handler = r
|
|
|
|
|
2018-07-02 10:40:32 +00:00
|
|
|
return s, nil
|
2018-04-05 11:46:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 13:41:44 +00:00
|
|
|
// createAssetFileHandler creates a gin handler to serve the content
|
|
|
|
// of the given asset file.
|
|
|
|
func createAssetFileHandler(file *assets.File) func(c *gin.Context) {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
http.ServeContent(c.Writer, c.Request, file.Name(), file.ModTime(), file)
|
|
|
|
}
|
2018-07-02 12:58:06 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 11:46:04 +00:00
|
|
|
// Run the server until the program stops.
|
|
|
|
func (s *Server) Run() error {
|
2018-07-02 10:40:32 +00:00
|
|
|
s.deps.Log.Info().Msgf("Serving on %s", s.httpServer.Addr)
|
2018-04-05 11:46:04 +00:00
|
|
|
if err := s.httpServer.ListenAndServeTLS("", ""); err != nil && err != http.ErrServerClosed {
|
2021-01-08 14:35:38 +00:00
|
|
|
return errors.WithStack(err)
|
2018-04-05 11:46:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createTLSConfig creates a TLS config based on given config
|
|
|
|
func createTLSConfig(cert, key string) (*tls.Config, error) {
|
|
|
|
var result *tls.Config
|
|
|
|
c, err := tls.X509KeyPair([]byte(cert), []byte(key))
|
|
|
|
if err != nil {
|
2021-01-08 14:35:38 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2018-04-05 11:46:04 +00:00
|
|
|
}
|
|
|
|
result = &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{c},
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
2019-09-23 07:00:10 +00:00
|
|
|
|
2020-03-10 09:26:38 +00:00
|
|
|
func ready(probes ...*probe.ReadyProbe) func(w http.ResponseWriter, r *http.Request) {
|
2019-09-23 07:00:10 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-03-10 09:26:38 +00:00
|
|
|
for _, probe := range probes {
|
2019-09-23 07:00:10 +00:00
|
|
|
if !probe.IsReady() {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
2020-03-10 09:26:38 +00:00
|
|
|
}
|