webhook.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 cmd
  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. "go.uber.org/zap/zapcore"
  26. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  27. clientgoscheme "k8s.io/client-go/kubernetes/scheme"
  28. ctrl "sigs.k8s.io/controller-runtime"
  29. "sigs.k8s.io/controller-runtime/pkg/log/zap"
  30. "sigs.k8s.io/controller-runtime/pkg/metrics/server"
  31. "sigs.k8s.io/controller-runtime/pkg/webhook"
  32. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  33. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  34. "github.com/external-secrets/external-secrets/pkg/controllers/crds"
  35. )
  36. const (
  37. errCreateWebhook = "unable to create webhook"
  38. )
  39. func init() {
  40. // kubernetes schemes
  41. utilruntime.Must(clientgoscheme.AddToScheme(scheme))
  42. // external-secrets schemes
  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. var lvl zapcore.Level
  53. var enc zapcore.TimeEncoder
  54. c := crds.CertInfo{
  55. CertDir: certDir,
  56. CertName: "tls.crt",
  57. KeyName: "tls.key",
  58. CAName: "ca.crt",
  59. }
  60. lvlErr := lvl.UnmarshalText([]byte(loglevel))
  61. if lvlErr != nil {
  62. setupLog.Error(lvlErr, "error unmarshalling loglevel")
  63. os.Exit(1)
  64. }
  65. encErr := enc.UnmarshalText([]byte(zapTimeEncoding))
  66. if encErr != nil {
  67. setupLog.Error(encErr, "error unmarshalling timeEncoding")
  68. os.Exit(1)
  69. }
  70. opts := zap.Options{
  71. Level: lvl,
  72. TimeEncoder: enc,
  73. }
  74. logger := zap.New(zap.UseFlagOptions(&opts))
  75. ctrl.SetLogger(logger)
  76. err := waitForCerts(c, time.Minute*2)
  77. if err != nil {
  78. setupLog.Error(err, "unable to validate certificates")
  79. os.Exit(1)
  80. }
  81. ctx, cancel := context.WithCancel(context.Background())
  82. go func(c crds.CertInfo, dnsName string, every time.Duration) {
  83. sigs := make(chan os.Signal, 1)
  84. signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
  85. ticker := time.NewTicker(every)
  86. for {
  87. select {
  88. case <-sigs:
  89. cancel()
  90. case <-ticker.C:
  91. setupLog.Info("validating certs")
  92. err = crds.CheckCerts(c, dnsName, time.Now().Add(certLookaheadInterval))
  93. if err != nil {
  94. setupLog.Error(err, "certs are not valid at now + lookahead, triggering shutdown", "certLookahead", certLookaheadInterval.String())
  95. cancel()
  96. return
  97. }
  98. setupLog.Info("certs are valid")
  99. }
  100. }
  101. }(c, dnsName, certCheckInterval)
  102. cipherList, err := getTLSCipherSuitesIDs(tlsCiphers)
  103. if err != nil {
  104. ctrl.Log.Error(err, "unable to fetch tls ciphers")
  105. os.Exit(1)
  106. }
  107. mgrTLSOptions := func(cfg *tls.Config) {
  108. cfg.CipherSuites = cipherList
  109. }
  110. mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
  111. Scheme: scheme,
  112. Metrics: server.Options{
  113. BindAddress: metricsAddr,
  114. },
  115. HealthProbeBindAddress: healthzAddr,
  116. WebhookServer: webhook.NewServer(webhook.Options{
  117. CertDir: certDir,
  118. Port: port,
  119. TLSOpts: []func(*tls.Config){
  120. mgrTLSOptions,
  121. func(c *tls.Config) {
  122. c.MinVersion = tlsVersion(tlsMinVersion)
  123. },
  124. },
  125. }),
  126. })
  127. if err != nil {
  128. setupLog.Error(err, "unable to start manager")
  129. os.Exit(1)
  130. }
  131. if err = (&esv1beta1.ExternalSecret{}).SetupWebhookWithManager(mgr); err != nil {
  132. setupLog.Error(err, errCreateWebhook, "webhook", "ExternalSecret-v1beta1")
  133. os.Exit(1)
  134. }
  135. if err = (&esv1beta1.SecretStore{}).SetupWebhookWithManager(mgr); err != nil {
  136. setupLog.Error(err, errCreateWebhook, "webhook", "SecretStore-v1beta1")
  137. os.Exit(1)
  138. }
  139. if err = (&esv1beta1.ClusterSecretStore{}).SetupWebhookWithManager(mgr); err != nil {
  140. setupLog.Error(err, errCreateWebhook, "webhook", "ClusterSecretStore-v1beta1")
  141. os.Exit(1)
  142. }
  143. if err = (&esv1alpha1.ExternalSecret{}).SetupWebhookWithManager(mgr); err != nil {
  144. setupLog.Error(err, errCreateWebhook, "webhook", "ExternalSecret-v1alpha1")
  145. os.Exit(1)
  146. }
  147. if err = (&esv1alpha1.SecretStore{}).SetupWebhookWithManager(mgr); err != nil {
  148. setupLog.Error(err, errCreateWebhook, "webhook", "SecretStore-v1alpha1")
  149. os.Exit(1)
  150. }
  151. if err = (&esv1alpha1.ClusterSecretStore{}).SetupWebhookWithManager(mgr); err != nil {
  152. setupLog.Error(err, errCreateWebhook, "webhook", "ClusterSecretStore-v1alpha1")
  153. os.Exit(1)
  154. }
  155. err = mgr.AddReadyzCheck("certs", func(_ *http.Request) error {
  156. return crds.CheckCerts(c, dnsName, time.Now().Add(time.Hour))
  157. })
  158. if err != nil {
  159. setupLog.Error(err, "unable to add certs readyz check")
  160. os.Exit(1)
  161. }
  162. setupLog.Info("starting manager")
  163. if err := mgr.Start(ctx); err != nil {
  164. setupLog.Error(err, "problem running manager")
  165. os.Exit(1)
  166. }
  167. },
  168. }
  169. // tlsVersion converts from human-readable TLS version (for example "1.1")
  170. // to the values accepted by tls.Config (for example 0x301).
  171. func tlsVersion(version string) uint16 {
  172. switch version {
  173. case "":
  174. return tls.VersionTLS10
  175. case "1.0":
  176. return tls.VersionTLS10
  177. case "1.1":
  178. return tls.VersionTLS11
  179. case "1.2":
  180. return tls.VersionTLS12
  181. case "1.3":
  182. return tls.VersionTLS13
  183. default:
  184. return tls.VersionTLS13
  185. }
  186. }
  187. // waitForCerts waits until the certificates become ready.
  188. // If they don't become ready within a given time duration
  189. // this function returns an error.
  190. // certs are generated by the certcontroller.
  191. func waitForCerts(c crds.CertInfo, timeout time.Duration) error {
  192. ctx, cancel := context.WithTimeout(context.Background(), timeout)
  193. defer cancel()
  194. for {
  195. setupLog.Info("validating certs")
  196. err := crds.CheckCerts(c, dnsName, time.Now().Add(time.Hour))
  197. if err == nil {
  198. return nil
  199. }
  200. setupLog.Error(err, "invalid certs. retrying...")
  201. <-time.After(time.Second * 10)
  202. if ctx.Err() != nil {
  203. return ctx.Err()
  204. }
  205. }
  206. }
  207. func getTLSCipherSuitesIDs(cipherListString string) ([]uint16, error) {
  208. if cipherListString == "" {
  209. return nil, nil
  210. }
  211. cipherList := strings.Split(cipherListString, ",")
  212. cipherIds := map[string]uint16{}
  213. for _, cs := range tls.CipherSuites() {
  214. cipherIds[cs.Name] = cs.ID
  215. }
  216. ret := make([]uint16, 0, len(cipherList))
  217. for _, c := range cipherList {
  218. id, ok := cipherIds[c]
  219. if !ok {
  220. return ret, fmt.Errorf("cipher %s was not found", c)
  221. }
  222. ret = append(ret, id)
  223. }
  224. return ret, nil
  225. }
  226. func init() {
  227. rootCmd.AddCommand(webhookCmd)
  228. webhookCmd.Flags().StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
  229. webhookCmd.Flags().StringVar(&healthzAddr, "healthz-addr", ":8081", "The address the health endpoint binds to.")
  230. webhookCmd.Flags().IntVar(&port, "port", 10250, "Port number that the webhook server will serve.")
  231. webhookCmd.Flags().StringVar(&dnsName, "dns-name", "localhost", "DNS name to validate certificates with")
  232. webhookCmd.Flags().StringVar(&certDir, "cert-dir", "/tmp/k8s-webhook-server/serving-certs", "path to check for certs")
  233. webhookCmd.Flags().StringVar(&zapTimeEncoding, "zap-time-encoding", "epoch", "Zap time encoding (one of 'epoch', 'millis', 'nano', 'iso8601', 'rfc3339' or 'rfc3339nano')")
  234. webhookCmd.Flags().StringVar(&loglevel, "loglevel", "info", "loglevel to use, one of: debug, info, warn, error, dpanic, panic, fatal")
  235. webhookCmd.Flags().DurationVar(&certCheckInterval, "check-interval", 5*time.Minute, "certificate check interval")
  236. webhookCmd.Flags().DurationVar(&certLookaheadInterval, "lookahead-interval", crds.LookaheadInterval, "certificate check interval")
  237. // https://go.dev/blog/tls-cipher-suites explains the ciphers selection process
  238. webhookCmd.Flags().StringVar(&tlsCiphers, "tls-ciphers", "", "comma separated list of tls ciphers allowed."+
  239. " This does not apply to TLS 1.3 as the ciphers are selected automatically."+
  240. " The order of this list does not give preference to the ciphers, the ordering is done automatically."+
  241. " Full lists of available ciphers can be found at https://pkg.go.dev/crypto/tls#pkg-constants."+
  242. " E.g. 'TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256'")
  243. webhookCmd.Flags().StringVar(&tlsMinVersion, "tls-min-version", "1.2", "minimum version of TLS supported.")
  244. }