-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Tests: Fix name of resource * Create test cert for test * Fix public key reference * Fix deletion * Remove singleton check * Revert "Remove singleton check" This reverts commit 2f2187f.
- Loading branch information
1 parent
53f2c78
commit 5e3499b
Showing
3 changed files
with
69 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package provider | ||
|
||
import ( | ||
"bytes" | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"fmt" | ||
"math/big" | ||
"time" | ||
) | ||
|
||
func generateTestCACertificate(org string) (caPEM []byte, err error) { | ||
limit := new(big.Int).Lsh(big.NewInt(1), 128) | ||
serialNumber, err := rand.Int(rand.Reader, limit) | ||
if err != nil { | ||
return nil, fmt.Errorf("error generating serial number: %w", err) | ||
} | ||
|
||
now := time.Now().UTC() | ||
conf := &x509.Certificate{ | ||
SerialNumber: serialNumber, | ||
Subject: pkix.Name{ | ||
Organization: []string{org}, | ||
}, | ||
NotBefore: now.Add(-time.Minute), // grace of 1 min | ||
NotAfter: now.Add(time.Hour), | ||
IsCA: true, | ||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCRLSign | x509.KeyUsageCertSign, | ||
BasicConstraintsValid: true, | ||
} | ||
|
||
key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to generate key: %w", err) | ||
} | ||
|
||
cert, err := x509.CreateCertificate(rand.Reader, conf, conf, &key.PublicKey, key) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to generate certificate: %w", err) | ||
} | ||
caPEMBuffer := new(bytes.Buffer) | ||
err = pem.Encode(caPEMBuffer, &pem.Block{ | ||
Type: "CERTIFICATE", | ||
Bytes: cert, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return caPEMBuffer.Bytes(), nil | ||
} |