|
| 1 | +package assets |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "crypto/x509" |
| 6 | + "fmt" |
| 7 | + "math/big" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/go-logr/logr" |
| 11 | + "github.com/openshift/library-go/pkg/crypto" |
| 12 | + v1 "k8s.io/api/core/v1" |
| 13 | + "k8s.io/apimachinery/pkg/util/sets" |
| 14 | + "k8s.io/apiserver/pkg/authentication/user" |
| 15 | +) |
| 16 | + |
| 17 | +const certificateLifetime = time.Duration(crypto.DefaultCertificateLifetimeInDays) * 24 * time.Hour |
| 18 | +const GRPCSecretName = "thanos-grpc-secret" |
| 19 | + |
| 20 | +// Taken from |
| 21 | +// https://github.com/openshift/library-go/blob/08c2fd1b452520da35ad210930ea9d100545589a/pkg/operator/certrotation/signer.go#L68-L86 |
| 22 | +// without refresh time handling. We just take care of rotation if we reach 1/5 of the validity timespan before expiration. |
| 23 | +func needsNewCert(notBefore, notAfter time.Time, now func() time.Time) bool { |
| 24 | + maxWait := notAfter.Sub(notBefore) / 5 |
| 25 | + latestTime := notAfter.Add(-maxWait) |
| 26 | + return now().After(latestTime) |
| 27 | +} |
| 28 | + |
| 29 | +// Taken from |
| 30 | +// https://github.com/openshift/cluster-monitoring-operator/blob/765d0b0369b176a5997d787b6710783437172879/pkg/manifests/tls.go#L113 |
| 31 | +func RotateGRPCSecret(s *v1.Secret, logger logr.Logger) (bool, error) { |
| 32 | + var ( |
| 33 | + curCA, newCA *crypto.CA |
| 34 | + curCABytes, crtPresent = s.Data["ca.crt"] |
| 35 | + curCAKeyBytes, keyPresent = s.Data["ca.key"] |
| 36 | + rotate = !crtPresent || !keyPresent |
| 37 | + ) |
| 38 | + |
| 39 | + if crtPresent && keyPresent { |
| 40 | + var err error |
| 41 | + curCA, err = crypto.GetCAFromBytes(curCABytes, curCAKeyBytes) |
| 42 | + if err != nil { |
| 43 | + logger.Info(fmt.Sprintf("generating a new CA due to error reading CA: %v", err)) |
| 44 | + rotate = true |
| 45 | + } else if needsNewCert(curCA.Config.Certs[0].NotBefore, curCA.Config.Certs[0].NotAfter, time.Now) { |
| 46 | + logger.Info("generating new CA, because the current one is older than 1/5 of it validity timestamp") |
| 47 | + rotate = true |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if !rotate { |
| 52 | + return rotate, nil |
| 53 | + } |
| 54 | + |
| 55 | + if curCA == nil { |
| 56 | + newCAConfig, err := crypto.MakeSelfSignedCAConfig( |
| 57 | + fmt.Sprintf("%s@%d", "openshift-cluster-monitoring", time.Now().Unix()), |
| 58 | + crypto.DefaultCertificateLifetimeInDays, |
| 59 | + ) |
| 60 | + if err != nil { |
| 61 | + return rotate, fmt.Errorf("error generating self signed CA: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + newCA = &crypto.CA{ |
| 65 | + SerialGenerator: &crypto.RandomSerialGenerator{}, |
| 66 | + Config: newCAConfig, |
| 67 | + } |
| 68 | + } else { |
| 69 | + template := curCA.Config.Certs[0] |
| 70 | + now := time.Now() |
| 71 | + template.NotBefore = now.Add(-1 * time.Second) |
| 72 | + template.NotAfter = now.Add(certificateLifetime) |
| 73 | + template.SerialNumber = template.SerialNumber.Add(template.SerialNumber, big.NewInt(1)) |
| 74 | + |
| 75 | + newCACert, err := createCertificate(template, template, template.PublicKey, curCA.Config.Key) |
| 76 | + if err != nil { |
| 77 | + return rotate, fmt.Errorf("error rotating CA: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + newCA = &crypto.CA{ |
| 81 | + SerialGenerator: &crypto.RandomSerialGenerator{}, |
| 82 | + Config: &crypto.TLSCertificateConfig{ |
| 83 | + Certs: []*x509.Certificate{newCACert}, |
| 84 | + Key: curCA.Config.Key, |
| 85 | + }, |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + newCABytes, newCAKeyBytes, err := newCA.Config.GetPEMBytes() |
| 90 | + if err != nil { |
| 91 | + return rotate, fmt.Errorf("error getting PEM bytes from CA: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + s.Data["ca.crt"] = newCABytes |
| 95 | + s.Data["ca.key"] = newCAKeyBytes |
| 96 | + |
| 97 | + { |
| 98 | + cfg, err := newCA.MakeClientCertificateForDuration( |
| 99 | + &user.DefaultInfo{ |
| 100 | + Name: "thanos-querier", |
| 101 | + }, |
| 102 | + time.Duration(crypto.DefaultCertificateLifetimeInDays)*24*time.Hour, |
| 103 | + ) |
| 104 | + if err != nil { |
| 105 | + return rotate, fmt.Errorf("error making client certificate: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + crt, key, err := cfg.GetPEMBytes() |
| 109 | + if err != nil { |
| 110 | + return rotate, fmt.Errorf("error getting PEM bytes for thanos querier client certificate: %w", err) |
| 111 | + } |
| 112 | + s.Data["thanos-querier-client.crt"] = crt |
| 113 | + s.Data["thanos-querier-client.key"] = key |
| 114 | + } |
| 115 | + |
| 116 | + { |
| 117 | + cfg, err := newCA.MakeServerCert( |
| 118 | + sets.NewString("prometheus-grpc"), |
| 119 | + crypto.DefaultCertificateLifetimeInDays, |
| 120 | + ) |
| 121 | + if err != nil { |
| 122 | + return rotate, fmt.Errorf("error making server certificate: %w", err) |
| 123 | + } |
| 124 | + |
| 125 | + crt, key, err := cfg.GetPEMBytes() |
| 126 | + if err != nil { |
| 127 | + return rotate, fmt.Errorf("error getting PEM bytes for prometheus-k8s server certificate: %w", err) |
| 128 | + } |
| 129 | + s.Data["prometheus-server.crt"] = crt |
| 130 | + s.Data["prometheus-server.key"] = key |
| 131 | + } |
| 132 | + |
| 133 | + return rotate, nil |
| 134 | +} |
| 135 | + |
| 136 | +// createCertificate creates a new certificate and returns it in x509.Certificate form. |
| 137 | +func createCertificate(template, parent *x509.Certificate, pub, priv interface{}) (*x509.Certificate, error) { |
| 138 | + rawCert, err := x509.CreateCertificate(rand.Reader, template, parent, pub, priv) |
| 139 | + if err != nil { |
| 140 | + return nil, fmt.Errorf("error creating certificate: %w", err) |
| 141 | + } |
| 142 | + parsedCerts, err := x509.ParseCertificates(rawCert) |
| 143 | + if err != nil { |
| 144 | + return nil, fmt.Errorf("error parsing certificate: %w", err) |
| 145 | + } |
| 146 | + return parsedCerts[0], nil |
| 147 | +} |
0 commit comments