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.
[feature] Read KMS Signer public keys in openssh authorized keys form…
…at (#32)
- Loading branch information
Eduardo Lopez
authored
May 11, 2020
1 parent
b4a2417
commit c3adfd6
Showing
2,314 changed files
with
224 additions
and
740,324 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
dist: trusty | ||
language: go | ||
go: | ||
- "1.13.1" | ||
- '1.14' | ||
install: | ||
- make setup | ||
jobs: | ||
|
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
module github.com/chanzuckerberg/terraform-provider-bless | ||
|
||
go 1.12 | ||
go 1.14 | ||
|
||
require ( | ||
github.com/aws/aws-sdk-go v1.23.10 | ||
github.com/aws/aws-sdk-go v1.30.23 | ||
github.com/gobuffalo/packr v1.30.1 | ||
github.com/hashicorp/terraform-plugin-sdk v1.0.0 | ||
github.com/pkg/errors v0.8.1 | ||
github.com/stretchr/testify v1.4.0 | ||
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 | ||
github.com/hashicorp/terraform-plugin-sdk v1.12.0 | ||
github.com/pkg/errors v0.9.1 | ||
github.com/stretchr/testify v1.5.1 | ||
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79 | ||
) |
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,73 @@ | ||
package provider | ||
|
||
import ( | ||
"crypto/x509" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/service/kms" | ||
"github.com/chanzuckerberg/terraform-provider-bless/pkg/aws" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/pkg/errors" | ||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
const () | ||
|
||
func KMSPublicKey() *schema.Resource { | ||
kmsPublicKey := newDataKMSPublicKey() | ||
|
||
return &schema.Resource{ | ||
Read: kmsPublicKey.Read, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
schemaKmsKeyID: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The kms key we should get the public key", | ||
}, | ||
|
||
// computed | ||
schemaPublicKey: { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "This is the CA public key in openssh format", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func newDataKMSPublicKey() *dataKMSPublicKey { | ||
return &dataKMSPublicKey{} | ||
} | ||
|
||
type dataKMSPublicKey struct{} | ||
|
||
func (l *dataKMSPublicKey) Read(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) | ||
|
||
svc := awsClient.KMS.Svc | ||
|
||
fmt.Printf("nil svc: %#v", svc == nil) | ||
|
||
output, err := svc.GetPublicKey( | ||
&kms.GetPublicKeyInput{KeyId: &kmsKeyID}, | ||
) | ||
if err != nil { | ||
return errors.Wrap(err, "error getting kms public key") | ||
} | ||
pub, err := x509.ParsePKIXPublicKey(output.PublicKey) | ||
if err != nil { | ||
return errors.Wrap(err, "could not parse kms public key") | ||
} | ||
sshPub, err := ssh.NewPublicKey(pub) | ||
if err != nil { | ||
return errors.Wrap(err, "could not ssh parse kms public key") | ||
} | ||
d.SetId(*output.KeyId) //nolint | ||
d.Set(schemaPublicKey, string(ssh.MarshalAuthorizedKey(sshPub))) //nolint | ||
return nil | ||
} |
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,64 @@ | ||
package provider_test | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/x509" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/kms" | ||
tf "github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestKMSPublicKey(t *testing.T) { | ||
r := require.New(t) | ||
providers, kmsMock := getTestProviders() | ||
|
||
priv, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) | ||
r.NoError(err) | ||
|
||
derBytes, err := x509.MarshalPKIXPublicKey(priv.Public()) | ||
r.NoError(err) | ||
output := &kms.GetPublicKeyOutput{ | ||
PublicKey: derBytes, | ||
KeyId: aws.String("key id"), | ||
} | ||
|
||
kmsMock.On("GetPublicKey", mock.Anything).Return(output, nil) | ||
|
||
tf.Test(t, tf.TestCase{ | ||
Providers: providers, | ||
Steps: []tf.TestStep{ | ||
tf.TestStep{ | ||
Config: ` | ||
provider "bless" { | ||
region = "us-east-1" | ||
} | ||
data "bless_kms_public_key" "bless" { | ||
kms_key_id = "testo" | ||
} | ||
output "public_key" { | ||
value = "${data.bless_kms_public_key.bless.public_key}" | ||
} | ||
`, | ||
Check: func(s *terraform.State) error { | ||
publicSSHUntyped := s.RootModule().Outputs["public_key"].Value | ||
publicSSH, ok := publicSSHUntyped.(string) | ||
r.True(ok) | ||
r.Regexp( | ||
regexp.MustCompile("^ecdsa-sha2-nistp384 "), | ||
string(publicSSH)) | ||
return nil | ||
}, | ||
}, | ||
}, | ||
}) | ||
} |
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
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
Oops, something went wrong.