webhook.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. Copyright © 2022 NAME HERE <EMAIL ADDRESS>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package controller
  14. import (
  15. "context"
  16. "crypto/tls"
  17. "fmt"
  18. "net/http"
  19. "os"
  20. "os/signal"
  21. "strings"
  22. "syscall"
  23. "time"
  24. "github.com/spf13/cobra"
  25. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  26. clientgoscheme "k8s.io/client-go/kubernetes/scheme"
  27. ctrl "sigs.k8s.io/controller-runtime"
  28. "sigs.k8s.io/controller-runtime/pkg/metrics/server"
  29. "sigs.k8s.io/controller-runtime/pkg/webhook"
  30. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  31. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  32. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  33. "github.com/external-secrets/external-secrets/pkg/controllers/crds"
  34. )
  35. const (
  36. errCreateWebhook = "unable to create webhook"
  37. )
  38. func init() {
  39. // kubernetes schemes
  40. utilruntime.Must(clientgoscheme.AddToScheme(scheme))
  41. // external-secrets schemes
  42. utilruntime.Must(esv1.AddToScheme(scheme))
  43. utilruntime.Must(esv1beta1.AddToScheme(scheme))
  44. utilruntime.Must(esv1alpha1.AddToScheme(scheme))
  45. }
  46. var webhookCmd = &cobra.Command{
  47. Use: "webhook",
  48. Short: "Webhook implementation for ExternalSecrets and SecretStores.",
  49. Long: `Webhook implementation for ExternalSecrets and SecretStores.
  50. For more information visit https://external-secrets.io`,
  51. Run: func(cmd *cobra.Command, args []string) {
  52. setupLogger()
  53. c := crds.CertInfo{
  54. CertDir: certDir,
  55. CertName: "tls.crt",
  56. KeyName: "tls.key",
  57. CAName: "ca.crt",
  58. }
  59. err := waitForCerts(c, time.Minute*2)
  60. if err != nil {
  61. setupLog.Error(err, "unable to validate certificates")
  62. os.Exit(1)
  63. }
  64. ctx, cancel := context.WithCancel(context.Background())
  65. go func(c crds.CertInfo, dnsName string, every time.Duration) {
  66. sigs := make(chan os.Signal, 1)
  67. signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
  68. ticker := time.NewTicker(every)
  69. for {
  70. select {
  71. case <-sigs:
  72. cancel()
  73. case <-ticker.C:
  74. setupLog.Info("validating certs")
  75. err = crds.CheckCerts(c, dnsName, time.Now().Add(certLookaheadInterval))
  76. if err != nil {
  77. setupLog.Error(err, "certs are not valid at now + lookahead, triggering shutdown", "certLookahead", certLookaheadInterval.String())
  78. cancel()
  79. return
  80. }
  81. setupLog.Info("certs are valid")
  82. }
  83. }
  84. }(c, dnsName, certCheckInterval)
  85. cipherList, err := getTLSCipherSuitesIDs(tlsCiphers)
  86. if err != nil {
  87. ctrl.Log.Error(err, "unable to fetch tls ciphers")
  88. os.Exit(1)
  89. }
  90. mgrTLSOptions := func(cfg *tls.Config) {
  91. cfg.CipherSuites = cipherList
  92. }
  93. mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
  94. Scheme: scheme,
  95. Metrics: server.Options{
  96. BindAddress: metricsAddr,
  97. },
  98. HealthProbeBindAddress: healthzAddr,
  99. WebhookServer: webhook.NewServer(webhook.Options{
  100. CertDir: certDir,
  101. Port: port,
  102. TLSOpts: []func(*tls.Config){
  103. mgrTLSOptions,
  104. func(c *tls.Config) {
  105. c.MinVersion = tlsVersion(tlsMinVersion)
  106. },
  107. },
  108. }),
  109. })
  110. if err != nil {
  111. setupLog.Error(err, "unable to start manager")
  112. os.Exit(1)
  113. }
  114. if err = (&esv1.ExternalSecret{}).SetupWebhookWithManager(mgr); err != nil {
  115. setupLog.Error(err, errCreateWebhook, "webhook", "ExternalSecret-v1")
  116. os.Exit(1)
  117. }
  118. if err = (&esv1.SecretStore{}).SetupWebhookWithManager(mgr); err != nil {
  119. setupLog.Error(err, errCreateWebhook, "webhook", "SecretStore-v1")
  120. os.Exit(1)
  121. }
  122. if err = (&esv1.ClusterSecretStore{}).SetupWebhookWithManager(mgr); err != nil {
  123. setupLog.Error(err, errCreateWebhook, "webhook", "ClusterSecretStore-v1")
  124. os.Exit(1)
  125. }
  126. err = mgr.AddReadyzCheck("certs", func(_ *http.Request) error {
  127. return crds.CheckCerts(c, dnsName, time.Now().Add(time.Hour))
  128. })
  129. if err != nil {
  130. setupLog.Error(err, "unable to add certs readyz check")
  131. os.Exit(1)
  132. }
  133. setupLog.Info("starting manager")
  134. if err := mgr.Start(ctx); err != nil {
  135. setupLog.Error(err, "problem running manager")
  136. os.Exit(1)
  137. }
  138. },
  139. }
  140. // tlsVersion converts from human-readable TLS version (for example "1.1")
  141. // to the values accepted by tls.Config (for example 0x301).
  142. func tlsVersion(version string) uint16 {
  143. switch version {
  144. case "":
  145. return tls.VersionTLS10
  146. case "1.0":
  147. return tls.VersionTLS10
  148. case "1.1":
  149. return tls.VersionTLS11
  150. case "1.2":
  151. return tls.VersionTLS12
  152. case "1.3":
  153. return tls.VersionTLS13
  154. default:
  155. return tls.VersionTLS13
  156. }
  157. }
  158. // waitForCerts waits until the certificates become ready.
  159. // If they don't become ready within a given time duration
  160. // this function returns an error.
  161. // certs are generated by the certcontroller.
  162. func waitForCerts(c crds.CertInfo, timeout time.Duration) error {
  163. ctx, cancel := context.WithTimeout(context.Background(), timeout)
  164. defer cancel()
  165. for {
  166. setupLog.Info("validating certs")
  167. err := crds.CheckCerts(c, dnsName, time.Now().Add(time.Hour))
  168. if err == nil {
  169. return nil
  170. }
  171. setupLog.Error(err, "invalid certs. retrying...")
  172. <-time.After(time.Second * 10)
  173. if ctx.Err() != nil {
  174. return ctx.Err()
  175. }
  176. }
  177. }
  178. func getTLSCipherSuitesIDs(cipherListString string) ([]uint16, error) {
  179. if cipherListString == "" {
  180. return nil, nil
  181. }
  182. cipherList := strings.Split(cipherListString, ",")
  183. cipherIds := map[string]uint16{}
  184. for _, cs := range tls.CipherSuites() {
  185. cipherIds[cs.Name] = cs.ID
  186. }
  187. ret := make([]uint16, 0, len(cipherList))
  188. for _, c := range cipherList {
  189. id, ok := cipherIds[c]
  190. if !ok {
  191. return ret, fmt.Errorf("cipher %s was not found", c)
  192. }
  193. ret = append(ret, id)
  194. }
  195. return ret, nil
  196. }
  197. func init() {
  198. rootCmd.AddCommand(webhookCmd)
  199. webhookCmd.Flags().StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
  200. webhookCmd.Flags().StringVar(&healthzAddr, "healthz-addr", ":8081", "The address the health endpoint binds to.")
  201. webhookCmd.Flags().IntVar(&port, "port", 10250, "Port number that the webhook server will serve.")
  202. webhookCmd.Flags().StringVar(&dnsName, "dns-name", "localhost", "DNS name to validate certificates with")
  203. webhookCmd.Flags().StringVar(&certDir, "cert-dir", "/tmp/k8s-webhook-server/serving-certs", "path to check for certs")
  204. webhookCmd.Flags().StringVar(&zapTimeEncoding, "zap-time-encoding", "epoch", "Zap time encoding (one of 'epoch', 'millis', 'nano', 'iso8601', 'rfc3339' or 'rfc3339nano')")
  205. webhookCmd.Flags().StringVar(&loglevel, "loglevel", "info", "loglevel to use, one of: debug, info, warn, error, dpanic, panic, fatal")
  206. webhookCmd.Flags().DurationVar(&certCheckInterval, "check-interval", 5*time.Minute, "certificate check interval")
  207. webhookCmd.Flags().DurationVar(&certLookaheadInterval, "lookahead-interval", crds.LookaheadInterval, "certificate check interval")
  208. // https://go.dev/blog/tls-cipher-suites explains the ciphers selection process
  209. webhookCmd.Flags().StringVar(&tlsCiphers, "tls-ciphers", "", "comma separated list of tls ciphers allowed."+
  210. " This does not apply to TLS 1.3 as the ciphers are selected automatically."+
  211. " The order of this list does not give preference to the ciphers, the ordering is done automatically."+
  212. " Full lists of available ciphers can be found at https://pkg.go.dev/crypto/tls#pkg-constants."+
  213. " E.g. 'TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256'")
  214. webhookCmd.Flags().StringVar(&tlsMinVersion, "tls-min-version", "1.2", "minimum version of TLS supported.")
  215. }