-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10024 from terraform-providers/v-terraform-provid…
…er-tls provider: Implement replacement functions for tls_private_key and tls_self_signed_certificate test configuration resources
- Loading branch information
Showing
3 changed files
with
117 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package aws | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"math/big" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const ( | ||
pemBlockTypeCertificate = `CERTIFICATE` | ||
pemBlockTypeRsaPrivateKey = `RSA PRIVATE KEY` | ||
) | ||
|
||
var tlsX509CertificateSerialNumberLimit = new(big.Int).Lsh(big.NewInt(1), 128) | ||
|
||
// tlsRsaPrivateKeyPem generates a RSA private key PEM string. | ||
// Wrap with tlsPemEscapeNewlines() to allow simple fmt.Sprintf() | ||
// configurations such as: private_key_pem = "%[1]s" | ||
func tlsRsaPrivateKeyPem(bits int) string { | ||
key, err := rsa.GenerateKey(rand.Reader, bits) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
block := &pem.Block{ | ||
Bytes: x509.MarshalPKCS1PrivateKey(key), | ||
Type: pemBlockTypeRsaPrivateKey, | ||
} | ||
|
||
return string(pem.EncodeToMemory(block)) | ||
} | ||
|
||
// tlsRsaX509SelfSignedCertificatePem generates a x509 certificate PEM string. | ||
// Wrap with tlsPemEscapeNewlines() to allow simple fmt.Sprintf() | ||
// configurations such as: private_key_pem = "%[1]s" | ||
func tlsRsaX509SelfSignedCertificatePem(keyPem, commonName string) string { | ||
keyBlock, _ := pem.Decode([]byte(keyPem)) | ||
|
||
key, err := x509.ParsePKCS1PrivateKey(keyBlock.Bytes) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
serialNumber, err := rand.Int(rand.Reader, tlsX509CertificateSerialNumberLimit) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
certificate := &x509.Certificate{ | ||
BasicConstraintsValid: true, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment, | ||
NotAfter: time.Now().Add(24 * time.Hour), | ||
NotBefore: time.Now(), | ||
SerialNumber: serialNumber, | ||
Subject: pkix.Name{ | ||
CommonName: commonName, | ||
Organization: []string{"ACME Examples, Inc"}, | ||
}, | ||
} | ||
|
||
certificateBytes, err := x509.CreateCertificate(rand.Reader, certificate, certificate, &key.PublicKey, key) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
certificateBlock := &pem.Block{ | ||
Bytes: certificateBytes, | ||
Type: pemBlockTypeCertificate, | ||
} | ||
|
||
return string(pem.EncodeToMemory(certificateBlock)) | ||
} | ||
|
||
func tlsPemEscapeNewlines(pem string) string { | ||
return strings.ReplaceAll(pem, "\n", "\\n") | ||
} |
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,23 @@ | ||
package aws | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestTlsRsaPrivateKeyPem(t *testing.T) { | ||
key := tlsRsaPrivateKeyPem(2048) | ||
|
||
if !strings.Contains(key, pemBlockTypeRsaPrivateKey) { | ||
t.Errorf("key does not contain RSA PRIVATE KEY: %s", key) | ||
} | ||
} | ||
|
||
func TestTlsRsaX509CertificatePem(t *testing.T) { | ||
key := tlsRsaPrivateKeyPem(2048) | ||
certificate := tlsRsaX509SelfSignedCertificatePem(key, "example.com") | ||
|
||
if !strings.Contains(certificate, pemBlockTypeCertificate) { | ||
t.Errorf("key does not contain CERTIFICATE: %s", certificate) | ||
} | ||
} |