1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
package utils
import (
"context"
"crypto/rand"
"crypto/rsa"
"net"
"os"
"path/filepath"
"time"
"github.com/ansible/receptor/pkg/certificates"
)
// GenerateCA generates a CA certificate and key.
func GenerateCA(name, commonName string) (string, string, error) {
dir, err := os.MkdirTemp("", "")
if err != nil {
return "", "", err
}
keyPath := filepath.Join(dir, name+".key")
crtPath := filepath.Join(dir, name+".crt")
// Create our certificate and private key
CA, err := certificates.CreateCA(&certificates.CertOptions{CommonName: commonName, Bits: 2048}, &certificates.RsaWrapper{})
if err != nil {
return "", "", err
}
err = certificates.SaveToPEMFile(crtPath, []interface{}{CA.Certificate}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
err = certificates.SaveToPEMFile(keyPath, []interface{}{CA.PrivateKey}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
return keyPath, crtPath, nil
}
// GenerateCert generates a private and public key for testing in the directory specified.
func GenerateCert(name, commonName string, dnsNames, nodeIDs []string) (string, string, error) {
dir, err := os.MkdirTemp("", "")
if err != nil {
return "", "", err
}
keyPath := filepath.Join(dir, name+".key")
crtPath := filepath.Join(dir, name+".crt")
// Create a temporary CA to sign this certificate
CA, err := certificates.CreateCA(&certificates.CertOptions{CommonName: "temp ca", Bits: 2048}, &certificates.RsaWrapper{})
if err != nil {
return "", "", err
}
// Create certificate request
req, key, err := certificates.CreateCertReqWithKey(&certificates.CertOptions{
CommonName: commonName,
Bits: 2048,
CertNames: certificates.CertNames{
DNSNames: dnsNames,
NodeIDs: nodeIDs,
},
})
if err != nil {
return "", "", err
}
// Sign the certificate
cert, err := certificates.SignCertReq(req, CA, &certificates.CertOptions{})
if err != nil {
return "", "", err
}
// Save cert and key to files
err = certificates.SaveToPEMFile(crtPath, []interface{}{cert}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
err = certificates.SaveToPEMFile(keyPath, []interface{}{key}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
return keyPath, crtPath, nil
}
// GenerateCertWithCA generates a private and public key for testing in the directory
// specified using the ca specified.
func GenerateCertWithCA(name, caKeyPath, caCrtPath, commonName string, dnsNames, nodeIDs []string) (string, string, error) {
dir, err := os.MkdirTemp("", "")
if err != nil {
return "", "", err
}
CA := &certificates.CA{}
CA.Certificate, err = certificates.LoadCertificate(caCrtPath, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
CA.PrivateKey, err = certificates.LoadPrivateKey(caKeyPath, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
keyPath := filepath.Join(dir, name+".key")
crtPath := filepath.Join(dir, name+".crt")
// Create certificate request
req, key, err := certificates.CreateCertReqWithKey(&certificates.CertOptions{
CommonName: commonName,
Bits: 2048,
CertNames: certificates.CertNames{
DNSNames: dnsNames,
NodeIDs: nodeIDs,
},
})
if err != nil {
return "", "", err
}
// Sign the certificate
cert, err := certificates.SignCertReq(req, CA, &certificates.CertOptions{})
if err != nil {
return "", "", err
}
// Save cert and key to files
err = certificates.SaveToPEMFile(crtPath, []interface{}{cert}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
err = certificates.SaveToPEMFile(keyPath, []interface{}{key}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
return keyPath, crtPath, nil
}
func GenerateRSAPair() (string, string, error) {
dir, err := os.MkdirTemp("", "")
if err != nil {
return "", "", err
}
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return "", "", err
}
publicKey := &privateKey.PublicKey
privateKeyPath := filepath.Join(dir, "private.pem")
err = certificates.SaveToPEMFile(privateKeyPath, []interface{}{privateKey}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
publicKeyPath := filepath.Join(dir, "public.pem")
err = certificates.SaveToPEMFile(publicKeyPath, []interface{}{publicKey}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
return privateKeyPath, publicKeyPath, nil
}
// CheckUntilTimeout Polls the check function until the context expires, in
// which case it returns false.
func CheckUntilTimeout(ctx context.Context, interval time.Duration, check func() bool) bool {
for ready := check(); !ready; ready = check() {
if ctx.Err() != nil {
return false
}
time.Sleep(interval)
}
return true
}
// CheckUntilTimeoutWithErr does the same as CheckUntilTimeout but requires the
// check function returns (bool, error), and will return an error immediately
// if the check function returns an error.
func CheckUntilTimeoutWithErr(ctx context.Context, interval time.Duration, check func() (bool, error)) (bool, error) {
for ready, err := check(); !ready; ready, err = check() {
if err != nil {
return false, err
}
if ctx.Err() != nil {
return false, nil //nolint:nilerr // Make this nice later.
}
time.Sleep(interval)
}
return true, nil
}
func GetFreeTCPPort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}
|