This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate ECDSA keypairsThese are smaller so we can inject them into the lambda environment easily
- Loading branch information
1 parent
bef3d4c
commit cde350b
Showing
680 changed files
with
67,698 additions
and
14,912 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,101 @@ | ||
package provider | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
|
||
"github.com/chanzuckerberg/terraform-provider-bless/pkg/aws" | ||
"github.com/chanzuckerberg/terraform-provider-bless/pkg/util" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// ECDSACA is an ecdsa CA resource | ||
func ECDSACA() *schema.Resource { | ||
ca := newResourceECDSACA() | ||
return &schema.Resource{ | ||
Create: ca.Create, | ||
Read: ca.Read, | ||
Delete: ca.Delete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
schemaKmsKeyID: &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The kms key with which we should encrypt the CA password.", | ||
ForceNew: true, | ||
}, | ||
|
||
// computed | ||
schemaEncryptedPrivateKey: &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "This is the base64 encoded CA encrypted private key.", | ||
}, | ||
schemaPublicKey: &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "This is the plaintext CA public key in openssh format.", | ||
}, | ||
schemaEncryptedPassword: &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "This is the kms encrypted password.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// resourceCA is a namespace | ||
type resourceECDSACA struct{} | ||
|
||
func newResourceECDSACA() *resourceECDSACA { | ||
return &resourceECDSACA{} | ||
} | ||
|
||
// Create creates a CA | ||
func (ca *resourceECDSACA) Create(d *schema.ResourceData, meta interface{}) error { | ||
awsClient, ok := meta.(*aws.Client) | ||
if !ok { | ||
return errors.New("meta is not of type *aws.Client") | ||
} | ||
|
||
kmsKeyID := d.Get(schemaKmsKeyID).(string) | ||
keyPair, err := ca.createKeypair() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
encryptedPassword, err := awsClient.KMS.EncryptBytes(keyPair.Password, kmsKeyID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.Set(schemaEncryptedPrivateKey, keyPair.B64EncryptedPrivateKey) | ||
d.Set(schemaPublicKey, keyPair.PublicKey) | ||
d.Set(schemaEncryptedPassword, encryptedPassword) | ||
d.SetId(util.HashForState(keyPair.PublicKey)) | ||
return nil | ||
} | ||
|
||
// Read reads the ca | ||
func (ca *resourceECDSACA) Read(d *schema.ResourceData, meta interface{}) error { | ||
return nil | ||
} | ||
|
||
// Delete deletes the ca | ||
func (ca *resourceECDSACA) Delete(d *schema.ResourceData, meta interface{}) error { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
// ------------ helpers ------------------ | ||
func (ca *resourceECDSACA) createKeypair() (*util.CA, error) { | ||
// generate private key | ||
privateKey, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Private key generation failed") | ||
} | ||
return util.NewCA(privateKey, privateKey.Public(), caPasswordBytes) | ||
} |
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,74 @@ | ||
package provider_test | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/base64" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/kms" | ||
r "github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestCreateECDSA(t *testing.T) { | ||
a := assert.New(t) | ||
providers, kmsMock := getTestProviders() | ||
|
||
ciphertext := make([]byte, 10) | ||
rand.Read(ciphertext) | ||
output := &kms.EncryptOutput{ | ||
CiphertextBlob: ciphertext, | ||
} | ||
kmsMock.On("Encrypt", mock.Anything).Return(output, nil) | ||
|
||
r.Test(t, r.TestCase{ | ||
Providers: providers, | ||
Steps: []r.TestStep{ | ||
r.TestStep{ | ||
Config: ` | ||
provider "bless" { | ||
region = "us-east-1" | ||
} | ||
resource "bless_ecdsa_ca" "bless" { | ||
kms_key_id = "testo" | ||
} | ||
output "ecdsa_private_key" { | ||
value = "${bless_ecdsa_ca.bless.encrypted_ca}" | ||
} | ||
output "ecdsa_public_key" { | ||
value = "${bless_ecdsa_ca.bless.public_key}" | ||
} | ||
output "ecdsa_password" { | ||
value = "${bless_ecdsa_ca.bless.encrypted_password}" | ||
} | ||
`, | ||
Check: func(s *terraform.State) error { | ||
privateUntyped := s.RootModule().Outputs["ecdsa_private_key"].Value | ||
private, ok := privateUntyped.(string) | ||
a.True(ok) | ||
bytesPrivate, err := base64.StdEncoding.DecodeString(private) | ||
a.Nil(err) | ||
a.Regexp( | ||
regexp.MustCompile("^-----BEGIN EC PRIVATE KEY-----"), | ||
string(bytesPrivate)) | ||
a.Regexp( | ||
regexp.MustCompile(`AES-256-CBC`), | ||
string(bytesPrivate)) | ||
publicSSHUntyped := s.RootModule().Outputs["ecdsa_public_key"].Value | ||
publicSSH, ok := publicSSHUntyped.(string) | ||
a.True(ok) | ||
a.Regexp( | ||
regexp.MustCompile("^ecdsa-sha2-nistp521 "), | ||
string(publicSSH)) | ||
return nil | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
Oops, something went wrong.