Skip to content

Commit

Permalink
Add AWS MFA (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Lopez authored Sep 22, 2018
1 parent 55e1818 commit 09e7061
Show file tree
Hide file tree
Showing 70 changed files with 77,516 additions and 142 deletions.
23 changes: 14 additions & 9 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 29 additions & 17 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
[[constraint]]
name = "github.com/aws/aws-sdk-go"
version = "1.15.36"
version = "1.15.39"

[[constraint]]
name = "github.com/blang/semver"
version = "3.5.1"

[[constraint]]
branch = "master"
name = "github.com/chanzuckerberg/go-kmsauth"

[[constraint]]
branch = "master"
name = "github.com/chanzuckerberg/go-misc"

[[constraint]]
branch = "master"
name = "github.com/hashicorp/go-getter"

[[constraint]]
name = "github.com/hashicorp/go-multierror"
version = "1.0.0"

[[constraint]]
name = "github.com/mitchellh/go-homedir"
version = "1.0.0"
Expand All @@ -14,6 +30,10 @@
name = "github.com/pkg/errors"
version = "0.8.0"

[[constraint]]
name = "github.com/segmentio/go-prompt"
branch = "master"

[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.0.6"
Expand All @@ -23,25 +43,17 @@
version = "0.0.3"

[[constraint]]
branch = "master"
name = "golang.org/x/crypto"

[prune]
go-tests = true
unused-packages = true
name = "github.com/stretchr/testify"
version = "1.2.2"

[[constraint]]
branch = "master"
name = "github.com/chanzuckerberg/go-kmsauth"

[[constraint]]
branch = "master"
name = "github.com/chanzuckerberg/go-misc"
name = "golang.org/x/crypto"

[[constraint]]
name = "github.com/segmentio/go-prompt"
branch = "master"
name = "gopkg.in/yaml.v2"
version = "2.2.1"

[[constraint]]
branch = "master"
name = "github.com/hashicorp/go-getter"
[prune]
go-tests = true
unused-packages = true
1 change: 0 additions & 1 deletion cmd/import-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ var importConfigCmd = &cobra.Command{
if err != nil {
return err
}
conf.SetPaths(configFileExpanded)

// Try to use the default id_rsa key
conf.ClientConfig.SSHPrivateKey = path.Join(sshDirExpanded, "id_rsa")
Expand Down
9 changes: 7 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ var initCmd = &cobra.Command{
if err != nil {
return errs.ErrMissingConfig
}
conf := config.DefaultConfig()
conf, err := config.DefaultConfig()
if err != nil {
return err
}

// override the config path if needed
configFileExpanded, err := homedir.Expand(configFile)
if err != nil {
return errors.Wrapf(err, "Could not expand %s", configFile)
}
conf.SetPaths(configFileExpanded)
conf.ClientConfig.ConfigFile = configFileExpanded

// Ask for some user values
conf.ClientConfig.SSHPrivateKey = prompt.StringRequired("path to the ssh private key to use")
Expand Down
44 changes: 25 additions & 19 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cmd

import (
"fmt"
"path"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
bless "github.com/chanzuckerberg/blessclient/pkg/bless"
Expand All @@ -30,7 +28,7 @@ var runCmd = &cobra.Command{
Short: "run requests a certificate",
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
log.Info("Running blessclient")
log.Debugf("Running blessclient v%s", util.VersionCacheKey())
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return errs.ErrMissingConfig
Expand All @@ -56,24 +54,35 @@ var runCmd = &cobra.Command{
return errors.Wrap(err, "Could not create aws session")
}

mfaTokenProvider := util.TokenProvider("AWS MFA token:")
var regionErrors error
for _, region := range conf.LambdaConfig.Regions {
// for things meant to be run as a user
userConf := &aws.Config{
awsUserSessionProviderConf := &aws.Config{
Region: aws.String(region.AWSRegion),
}
awsSessionProviderClient := cziAWS.New(sess).WithAllServices(awsUserSessionProviderConf)

awsSessionTokenProvider := cziAWS.NewUserTokenProvider(conf.GetAWSSessionCachePath(), awsSessionProviderClient, mfaTokenProvider)
userConf := &aws.Config{
Region: aws.String(region.AWSRegion),
Credentials: credentials.NewCredentials(awsSessionTokenProvider),
}
// for things meant to be run as an assumed role
roleCreds := stscreds.NewCredentials(
sess,
conf.LambdaConfig.RoleARN, func(p *stscreds.AssumeRoleProvider) {
p.TokenProvider = stscreds.StdinTokenProvider
},
)
roleConf := &aws.Config{
Credentials: roleCreds,
Region: aws.String(region.AWSRegion),
Region: aws.String(region.AWSRegion),
Credentials: stscreds.NewCredentials(
sess,
conf.LambdaConfig.RoleARN, func(p *stscreds.AssumeRoleProvider) {
p.TokenProvider = stscreds.StdinTokenProvider
},
),
}
awsClient := cziAWS.New(sess).WithIAM(userConf).WithLambda(roleConf).WithKMS(userConf)

awsClient := cziAWS.New(sess).
WithIAM(userConf).
WithKMS(userConf).
WithSTS(userConf).
WithLambda(roleConf)

user, err := awsClient.IAM.GetCurrentUser()
if err != nil {
Expand All @@ -83,9 +92,6 @@ var runCmd = &cobra.Command{
return errors.New("AWS returned nil user")
}

regionCacheFile := fmt.Sprintf("%s.json", region.AWSRegion)
regionalKMSAuthCache := path.Join(conf.ClientConfig.KMSAuthCacheDir, util.VersionCacheKey(), regionCacheFile)

kmsauthContext := &kmsauth.AuthContextV2{
From: *user.UserName,
To: conf.LambdaConfig.FunctionName,
Expand All @@ -96,7 +102,7 @@ var runCmd = &cobra.Command{
region.KMSAuthKeyID,
kmsauth.TokenVersion2,
conf.ClientConfig.CertLifetime.AsDuration(),
&regionalKMSAuthCache,
aws.String(conf.GetKMSAuthCachePath(region.AWSRegion)),
kmsauthContext,
awsClient,
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/bless/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (c *Client) RequestCert() error {
return nil
}

log.Infof("Requesting new cert")
log.Debug("Requesting new cert")
pubKey, err := s.ReadPublicKey()
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions pkg/bless/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
"testing"
"time"

"github.com/chanzuckerberg/blessclient/pkg/errs"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/chanzuckerberg/blessclient/pkg/bless"
"github.com/chanzuckerberg/blessclient/pkg/config"
"github.com/chanzuckerberg/blessclient/pkg/errs"
"github.com/chanzuckerberg/go-kmsauth"
cziAws "github.com/chanzuckerberg/go-misc/aws"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -275,10 +274,11 @@ func testConfig(t *testing.T) (*config.Config, []string) {
a.Nil(err)

conf := &config.Config{
ClientConfig: config.ClientConfig{},
ClientConfig: config.ClientConfig{
ConfigFile: path.Join(dirName, "config.yml"),
},
LambdaConfig: config.LambdaConfig{},
}
conf.SetPaths(path.Join(dirName, "config.yml"))
conf.ClientConfig.SSHPrivateKey = f.Name()
conf.ClientConfig.RemoteUsers = []string{"test-principal"}

Expand Down
Loading

0 comments on commit 09e7061

Please sign in to comment.