mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-03 00:40:25 +02:00
integration: add tool for renewing test certs (#4742)
Add a utility for updating the integration test certificates. It takes three file paths: the existing certificate, the CA certificate, and the CA key. It will update the NotBefore and NotAfter timestamps and the certificate signature, overwriting the existing certificate. Example usage: cd integration/tpl/files go run renew-cert.go trusted.pem ca.pem ca-key.pem
This commit is contained in:
parent
cfc339548f
commit
0d29401192
1 changed files with 74 additions and 0 deletions
74
integration/tpl/files/renew-cert.go
Normal file
74
integration/tpl/files/renew-cert.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
//go:build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 4 {
|
||||
fmt.Fprintln(os.Stderr, "usage: go run renew-cert.go <cert-path> <ca-path> <ca-key-path>")
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
certPath := os.Args[1]
|
||||
b, err := os.ReadFile(certPath)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
block, _ := pem.Decode(b)
|
||||
tpl, err := x509.ParseCertificate(block.Bytes)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// Parse the CA certificate and private key.
|
||||
b, err = os.ReadFile(os.Args[2])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
block, _ = pem.Decode(b)
|
||||
caCert, err := x509.ParseCertificate(block.Bytes)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
b, err = os.ReadFile(os.Args[3])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
block, _ = pem.Decode(b)
|
||||
caKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
if tpl.CheckSignatureFrom(caCert) != nil {
|
||||
log.Fatalln("error: cert was not issued by the provided CA")
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
tpl.NotBefore = now
|
||||
tpl.NotAfter = now.Add(3650 * 24 * time.Hour)
|
||||
|
||||
// Issue the new certificate.
|
||||
b, err = x509.CreateCertificate(rand.Reader, tpl, caCert, tpl.PublicKey, caKey)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// Write the new certificate to a PEM file.
|
||||
b = pem.EncodeToMemory(&pem.Block{
|
||||
Type: "CERTIFICATE",
|
||||
Bytes: b,
|
||||
})
|
||||
if err := os.WriteFile(certPath, b, 0644); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue