|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package server |
| 24 | + |
| 25 | +import ( |
| 26 | + "crypto/tls" |
| 27 | + "fmt" |
| 28 | + "net/http" |
| 29 | + "time" |
| 30 | + |
| 31 | + certificates "github.com/arangodb-helper/go-certificates" |
| 32 | + "k8s.io/api/core/v1" |
| 33 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 34 | + corev1 "k8s.io/client-go/kubernetes/typed/core/v1" |
| 35 | +) |
| 36 | + |
| 37 | +// Config settings for the Server |
| 38 | +type Config struct { |
| 39 | + Address string // Address to listen on |
| 40 | + TLSSecretName string // Name of secret containing TLS certificate |
| 41 | + TLSSecretNamespace string // Namespace of secret containing TLS certificate |
| 42 | + PodName string // Name of the Pod we're running in |
| 43 | + PodIP string // IP address of the Pod we're running in |
| 44 | +} |
| 45 | + |
| 46 | +// Server is the HTTPS server for the operator. |
| 47 | +type Server struct { |
| 48 | + httpServer *http.Server |
| 49 | +} |
| 50 | + |
| 51 | +// NewServer creates a new server, fetching/preparing a TLS certificate. |
| 52 | +func NewServer(cli corev1.CoreV1Interface, handler http.Handler, cfg Config) (*Server, error) { |
| 53 | + httpServer := &http.Server{ |
| 54 | + Addr: cfg.Address, |
| 55 | + Handler: handler, |
| 56 | + ReadTimeout: time.Second * 30, |
| 57 | + ReadHeaderTimeout: time.Second * 15, |
| 58 | + WriteTimeout: time.Second * 30, |
| 59 | + TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)), |
| 60 | + } |
| 61 | + |
| 62 | + var cert, key string |
| 63 | + if cfg.TLSSecretName != "" && cfg.TLSSecretNamespace != "" { |
| 64 | + // Load TLS certificate from secret |
| 65 | + s, err := cli.Secrets(cfg.TLSSecretNamespace).Get(cfg.TLSSecretName, metav1.GetOptions{}) |
| 66 | + if err != nil { |
| 67 | + return nil, maskAny(err) |
| 68 | + } |
| 69 | + certBytes, found := s.Data[v1.TLSCertKey] |
| 70 | + if !found { |
| 71 | + return nil, maskAny(fmt.Errorf("No %s found in secret %s", v1.TLSCertKey, cfg.TLSSecretName)) |
| 72 | + } |
| 73 | + keyBytes, found := s.Data[v1.TLSPrivateKeyKey] |
| 74 | + if !found { |
| 75 | + return nil, maskAny(fmt.Errorf("No %s found in secret %s", v1.TLSPrivateKeyKey, cfg.TLSSecretName)) |
| 76 | + } |
| 77 | + cert = string(certBytes) |
| 78 | + key = string(keyBytes) |
| 79 | + } else { |
| 80 | + // Secret not specified, create our own TLS certificate |
| 81 | + options := certificates.CreateCertificateOptions{ |
| 82 | + CommonName: cfg.PodName, |
| 83 | + Hosts: []string{cfg.PodName, cfg.PodIP}, |
| 84 | + ValidFrom: time.Now(), |
| 85 | + ValidFor: time.Hour * 24 * 365 * 10, |
| 86 | + IsCA: false, |
| 87 | + ECDSACurve: "P256", |
| 88 | + } |
| 89 | + var err error |
| 90 | + cert, key, err = certificates.CreateCertificate(options, nil) |
| 91 | + if err != nil { |
| 92 | + return nil, maskAny(err) |
| 93 | + } |
| 94 | + } |
| 95 | + tlsConfig, err := createTLSConfig(cert, key) |
| 96 | + if err != nil { |
| 97 | + return nil, maskAny(err) |
| 98 | + } |
| 99 | + tlsConfig.BuildNameToCertificate() |
| 100 | + httpServer.TLSConfig = tlsConfig |
| 101 | + |
| 102 | + return &Server{ |
| 103 | + httpServer: httpServer, |
| 104 | + }, nil |
| 105 | +} |
| 106 | + |
| 107 | +// Run the server until the program stops. |
| 108 | +func (s *Server) Run() error { |
| 109 | + if err := s.httpServer.ListenAndServeTLS("", ""); err != nil && err != http.ErrServerClosed { |
| 110 | + return maskAny(err) |
| 111 | + } |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +// createTLSConfig creates a TLS config based on given config |
| 116 | +func createTLSConfig(cert, key string) (*tls.Config, error) { |
| 117 | + var result *tls.Config |
| 118 | + c, err := tls.X509KeyPair([]byte(cert), []byte(key)) |
| 119 | + if err != nil { |
| 120 | + return nil, maskAny(err) |
| 121 | + } |
| 122 | + result = &tls.Config{ |
| 123 | + Certificates: []tls.Certificate{c}, |
| 124 | + } |
| 125 | + return result, nil |
| 126 | +} |
0 commit comments