Skip to content

Commit

Permalink
Some cleanup around prompts / credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
bmonkman committed Jun 23, 2020
1 parent 0665b3f commit 4f37973
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 144 deletions.
37 changes: 2 additions & 35 deletions internal/init/init.go
Expand Up @@ -6,11 +6,6 @@ import (
"path"
"sync"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/commitdev/zero/internal/config/globalconfig"
"github.com/commitdev/zero/internal/config/moduleconfig"
"github.com/commitdev/zero/internal/config/projectconfig"
Expand Down Expand Up @@ -242,7 +237,7 @@ func mapVendorToPrompts(projectCred globalconfig.ProjectCredential, vendor strin
Default: projectCred.AWSResourceConfig.AccessKeyID,
},
CustomCondition(customAwsMustInputCondition),
project.ValidateAKID,
ValidateAKID,
},
{
moduleconfig.Parameter{
Expand All @@ -251,7 +246,7 @@ func mapVendorToPrompts(projectCred globalconfig.ProjectCredential, vendor strin
Default: projectCred.AWSResourceConfig.SecretAccessKey,
},
CustomCondition(customAwsMustInputCondition),
project.ValidateSAK,
ValidateSAK,
},
}
prompts = append(prompts, awsPrompts...)
Expand Down Expand Up @@ -311,34 +306,6 @@ func chooseStack(reg registry.Registry) []string {
return registry.GetModulesByName(reg, providerResult)
}

func fillProviderDetails(projectConfig *projectconfig.ZeroProjectConfig, s project.Secrets) {
if projectConfig.Infrastructure.AWS != nil {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(projectConfig.Infrastructure.AWS.Region),
Credentials: credentials.NewStaticCredentials(s.AWS.AccessKeyID, s.AWS.SecretAccessKey, ""),
})

svc := sts.New(sess)
input := &sts.GetCallerIdentityInput{}

awsCaller, err := svc.GetCallerIdentity(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
exit.Error(aerr.Error())
}
} else {
exit.Error(err.Error())
}
}

if awsCaller != nil && awsCaller.Account != nil {
projectConfig.Infrastructure.AWS.AccountID = *awsCaller.Account
}
}
}

func defaultProjConfig() projectconfig.ZeroProjectConfig {
return projectconfig.ZeroProjectConfig{
Name: "",
Expand Down
19 changes: 19 additions & 0 deletions internal/init/prompts.go
@@ -1,6 +1,7 @@
package init

import (
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -68,6 +69,24 @@ func SpecificValueValidation(values ...string) func(string) error {
}
}

func ValidateAKID(input string) error {
// 20 uppercase alphanumeric characters
var awsAccessKeyIDPat = regexp.MustCompile(`^[A-Z0-9]{20}$`)
if !awsAccessKeyIDPat.MatchString(input) {
return errors.New("Invalid aws_access_key_id")
}
return nil
}

func ValidateSAK(input string) error {
// 40 base64 characters
var awsSecretAccessKeyPat = regexp.MustCompile(`^[A-Za-z0-9/+=]{40}$`)
if !awsSecretAccessKeyPat.MatchString(input) {
return errors.New("Invalid aws_secret_access_key")
}
return nil
}

// TODO: validation / allow prompt retry ...etc
func (p PromptHandler) GetParam(projectParams map[string]string) string {
var err error
Expand Down
109 changes: 0 additions & 109 deletions pkg/credentials/credentials.go
@@ -1,42 +1,16 @@
package credentials

import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"regexp"

"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/commitdev/zero/internal/config/globalconfig"
"github.com/commitdev/zero/internal/config/projectconfig"
"github.com/manifoldco/promptui"
)

// Secrets - AWS prompted credentials
type Secrets struct {
AWS AWS
CircleCIKey string
GithubToken string
}

type AWS struct {
AccessKeyID string
SecretAccessKey string
}

func MakeAwsEnvars(cfg *projectconfig.ZeroProjectConfig, awsSecrets Secrets) []string {
env := os.Environ()
env = append(env, fmt.Sprintf("AWS_ACCESS_KEY_ID=%s", awsSecrets.AWS.AccessKeyID))
env = append(env, fmt.Sprintf("AWS_SECRET_ACCESS_KEY=%s", awsSecrets.AWS.SecretAccessKey))
env = append(env, fmt.Sprintf("AWS_DEFAULT_REGION=%s", cfg.Infrastructure.AWS.Region))

return env
}

func AwsCredsPath() string {
usr, err := user.Current()
if err != nil {
Expand Down Expand Up @@ -82,86 +56,3 @@ func GetAWSProfiles() ([]string, error) {
}
return profiles, nil
}

func ValidateAKID(input string) error {
// 20 uppercase alphanumeric characters
var awsAccessKeyIDPat = regexp.MustCompile(`^[A-Z0-9]{20}$`)
if !awsAccessKeyIDPat.MatchString(input) {
return errors.New("Invalid aws_access_key_id")
}
return nil
}

func ValidateSAK(input string) error {
// 40 base64 characters
var awsSecretAccessKeyPat = regexp.MustCompile(`^[A-Za-z0-9/+=]{40}$`)
if !awsSecretAccessKeyPat.MatchString(input) {
return errors.New("Invalid aws_secret_access_key")
}
return nil
}

func promptAWSCredentials(secrets *Secrets) {
accessKeyIDPrompt := promptui.Prompt{
Label: "Aws Access Key ID ",
Validate: ValidateAKID,
}

accessKeyIDResult, err := accessKeyIDPrompt.Run()

if err != nil {
log.Fatalf("Prompt failed %v\n", err)
panic(err)
}

secretAccessKeyPrompt := promptui.Prompt{
Label: "Aws Secret Access Key ",
Validate: ValidateSAK,
Mask: '*',
}

secretAccessKeyResult, err := secretAccessKeyPrompt.Run()

if err != nil {
log.Fatalf("Prompt failed %v\n", err)
panic(err)
}

secrets.AWS.AccessKeyID = accessKeyIDResult
secrets.AWS.SecretAccessKey = secretAccessKeyResult
}

func promptGitHubCredentials(secrets *Secrets) {
}

func promptCircleCICredentials(secrets *Secrets) {
validateKey := func(input string) error {
// 40 base64 characters
var awsSecretAccessKeyPat = regexp.MustCompile(`^[A-Za-z0-9]{40}$`)
if !awsSecretAccessKeyPat.MatchString(input) {
return errors.New("Invalid CircleCI API Key")
}
return nil
}

prompt := promptui.Prompt{
Label: "Please enter your CircleCI API key (you can create one at https://circleci.com/account/api) ",
Validate: validateKey,
}

key, err := prompt.Run()

if err != nil {
log.Fatalf("Prompt failed %v\n", err)
panic(err)
}
secrets.CircleCIKey = key
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

0 comments on commit 4f37973

Please sign in to comment.