Skip to content

Commit 5ab1c85

Browse files
committed
config: fix testdata for Go 1.18
The TLS certificates were signed using SHA-1 and Go 1.18 rejects them by default [1]. This commit provides certificates signed with SHA-256. The code to generate the certificates is based on existing code from the prometheus-operator project [2]. [1] https://tip.golang.org/doc/go1.18#sha1 [2] https://github.com/prometheus-operator/prometheus-operator/blob/main/scripts/certs/generate.go Signed-off-by: Simon Pasquier <[email protected]>
1 parent 26d4974 commit 5ab1c85

11 files changed

+555
-582
lines changed

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ include Makefile.common
1515

1616
.PHONY: test
1717
test:: deps check_license unused common-test lint
18+
19+
.PHONY: generate-testdata
20+
generate-testdata:
21+
@cd config && go run generate.go

config/generate.go

+245
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
// Copyright 2020 The Prometheus-operator Authors
2+
// Copyright 2022 The Prometheus Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
//go:build ignore
17+
// +build ignore
18+
19+
// Program generating TLS certificates and keys for the tests.
20+
package main
21+
22+
import (
23+
"bytes"
24+
"crypto/rand"
25+
"crypto/rsa"
26+
"crypto/x509"
27+
"crypto/x509/pkix"
28+
"encoding/pem"
29+
"fmt"
30+
"io"
31+
"io/ioutil"
32+
"log"
33+
"math/big"
34+
"net"
35+
"time"
36+
)
37+
38+
const (
39+
validityPeriod = 50 * 365 * 24 * time.Hour
40+
)
41+
42+
func EncodeCertificate(w io.Writer, cert *x509.Certificate) error {
43+
return pem.Encode(w, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw})
44+
}
45+
46+
func EncodeKey(w io.Writer, priv *rsa.PrivateKey) error {
47+
b, err := x509.MarshalPKCS8PrivateKey(priv)
48+
if err != nil {
49+
return fmt.Errorf("failed to marshal private key: %v", err)
50+
}
51+
52+
return pem.Encode(w, &pem.Block{Type: "PRIVATE KEY", Bytes: b})
53+
}
54+
55+
var serialNumber *big.Int
56+
57+
func init() {
58+
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
59+
60+
var err error
61+
serialNumber, err = rand.Int(rand.Reader, serialNumberLimit)
62+
if err != nil {
63+
panic(fmt.Errorf("failed to generate serial number: %v", err))
64+
}
65+
}
66+
67+
func SerialNumber() *big.Int {
68+
var serial big.Int
69+
70+
serial.Set(serialNumber)
71+
serialNumber.Add(&serial, big.NewInt(1))
72+
73+
return &serial
74+
75+
}
76+
77+
func GenerateCertificateAuthority(commonName string, parentCert *x509.Certificate, parentKey *rsa.PrivateKey) (*x509.Certificate, *rsa.PrivateKey, error) {
78+
now := time.Now()
79+
80+
caKey, err := rsa.GenerateKey(rand.Reader, 4096)
81+
if err != nil {
82+
return nil, nil, fmt.Errorf("failed to generate CA private key: %v", err)
83+
}
84+
85+
caCert := &x509.Certificate{
86+
SerialNumber: SerialNumber(),
87+
Subject: pkix.Name{
88+
Country: []string{"US"},
89+
Organization: []string{"Prometheus"},
90+
OrganizationalUnit: []string{"Prometheus Certificate Authority"},
91+
CommonName: commonName,
92+
},
93+
NotBefore: now,
94+
NotAfter: now.Add(validityPeriod),
95+
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageCertSign,
96+
IsCA: true,
97+
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
98+
BasicConstraintsValid: true,
99+
}
100+
101+
if parentCert == nil && parentKey == nil {
102+
parentCert = caCert
103+
parentKey = caKey
104+
}
105+
106+
b, err := x509.CreateCertificate(rand.Reader, caCert, parentCert, &caKey.PublicKey, parentKey)
107+
if err != nil {
108+
return nil, nil, fmt.Errorf("failed to create CA certificate: %v", err)
109+
}
110+
111+
caCert, err = x509.ParseCertificate(b)
112+
if err != nil {
113+
return nil, nil, fmt.Errorf("failed to decode CA certificate: %v", err)
114+
}
115+
116+
return caCert, caKey, nil
117+
}
118+
119+
func GenerateCertificate(caCert *x509.Certificate, caKey *rsa.PrivateKey, server bool, name string, ipAddresses ...net.IP) (*x509.Certificate, *rsa.PrivateKey, error) {
120+
now := time.Now()
121+
122+
key, err := rsa.GenerateKey(rand.Reader, 4096)
123+
if err != nil {
124+
return nil, nil, fmt.Errorf("failed to generate private key: %v", err)
125+
}
126+
127+
cert := &x509.Certificate{
128+
SerialNumber: SerialNumber(),
129+
Subject: pkix.Name{
130+
Country: []string{"US"},
131+
Organization: []string{"Prometheus"},
132+
CommonName: name,
133+
},
134+
NotBefore: now,
135+
NotAfter: now.Add(validityPeriod),
136+
KeyUsage: x509.KeyUsageKeyEncipherment,
137+
BasicConstraintsValid: true,
138+
}
139+
140+
if server {
141+
cert.DNSNames = []string{name}
142+
cert.IPAddresses = ipAddresses
143+
cert.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}
144+
} else {
145+
cert.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}
146+
}
147+
148+
if caCert == nil && caKey == nil {
149+
caCert = cert
150+
caKey = key
151+
}
152+
153+
b, err := x509.CreateCertificate(rand.Reader, cert, caCert, &key.PublicKey, caKey)
154+
if err != nil {
155+
return nil, nil, fmt.Errorf("failed to create certificate: %v", err)
156+
}
157+
158+
cert, err = x509.ParseCertificate(b)
159+
if err != nil {
160+
return nil, nil, fmt.Errorf("failed to decode certificate: %v", err)
161+
}
162+
163+
return cert, key, nil
164+
}
165+
166+
func writeCertificateAndKey(path string, cert *x509.Certificate, key *rsa.PrivateKey) error {
167+
var b bytes.Buffer
168+
169+
if err := EncodeCertificate(&b, cert); err != nil {
170+
return err
171+
}
172+
173+
if err := ioutil.WriteFile(fmt.Sprintf("%s.crt", path), b.Bytes(), 0644); err != nil {
174+
return err
175+
}
176+
177+
b.Reset()
178+
if err := EncodeKey(&b, key); err != nil {
179+
return err
180+
}
181+
182+
if err := ioutil.WriteFile(fmt.Sprintf("%s.key", path), b.Bytes(), 0644); err != nil {
183+
return err
184+
}
185+
186+
return nil
187+
}
188+
189+
func main() {
190+
log.Println("Generating root CA")
191+
rootCert, rootKey, err := GenerateCertificateAuthority("Prometheus Root CA", nil, nil)
192+
if err != nil {
193+
log.Fatal(err)
194+
}
195+
196+
log.Println("Generating CA")
197+
caCert, caKey, err := GenerateCertificateAuthority("Prometheus TLS CA", rootCert, rootKey)
198+
if err != nil {
199+
log.Fatal(err)
200+
}
201+
202+
log.Println("Generating server certificate")
203+
cert, key, err := GenerateCertificate(caCert, caKey, true, "localhost", net.IPv4(127, 0, 0, 1), net.IPv4(127, 0, 0, 0))
204+
if err != nil {
205+
log.Fatal(err)
206+
}
207+
208+
if err := writeCertificateAndKey("testdata/server", cert, key); err != nil {
209+
log.Fatal(err)
210+
}
211+
212+
log.Println("Generating client certificate")
213+
cert, key, err = GenerateCertificate(caCert, caKey, false, "localhost")
214+
if err != nil {
215+
log.Fatal(err)
216+
}
217+
218+
if err := writeCertificateAndKey("testdata/client", cert, key); err != nil {
219+
log.Fatal(err)
220+
}
221+
222+
log.Println("Generating self-signed client certificate")
223+
cert, key, err = GenerateCertificate(nil, nil, false, "localhost")
224+
if err != nil {
225+
log.Fatal(err)
226+
}
227+
228+
if err := writeCertificateAndKey("testdata/self-signed-client", cert, key); err != nil {
229+
log.Fatal(err)
230+
}
231+
232+
log.Println("Generating CA bundle")
233+
var b bytes.Buffer
234+
if err := EncodeCertificate(&b, caCert); err != nil {
235+
log.Fatal(err)
236+
}
237+
238+
if err := EncodeCertificate(&b, rootCert); err != nil {
239+
log.Fatal(err)
240+
}
241+
242+
if err := ioutil.WriteFile("testdata/tls-ca-chain.pem", b.Bytes(), 0644); err != nil {
243+
log.Fatal(err)
244+
}
245+
}

config/http_config_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ const (
4343
ServerCertificatePath = "testdata/server.crt"
4444
ServerKeyPath = "testdata/server.key"
4545
ClientCertificatePath = "testdata/client.crt"
46-
ClientKeyNoPassPath = "testdata/client-no-pass.key"
47-
InvalidCA = "testdata/client-no-pass.key"
46+
ClientKeyNoPassPath = "testdata/client.key"
47+
InvalidCA = "testdata/client.key"
4848
WrongClientCertPath = "testdata/self-signed-client.crt"
4949
WrongClientKeyPath = "testdata/self-signed-client.key"
5050
EmptyFile = "testdata/empty"

config/testdata/client-no-pass.key

-28
This file was deleted.

0 commit comments

Comments
 (0)