Skip to content

Commit

Permalink
better detect generate cert failure and only save .pem files
Browse files Browse the repository at this point in the history
  • Loading branch information
kahgeh-work committed Sep 22, 2020
1 parent 064e0d0 commit b550268
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
4 changes: 0 additions & 4 deletions aws/aws_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ func (session *SsmSession) Restore(parameters []ssm.Parameter, folderPath string
}

func (session *SsmSession) Save(name string, content string, keyId string, path string) {
if strings.HasPrefix(name, ".") {
log.Printf("skipping %q ", name)
return
}
api := session.api
tier := ssm.ParameterTierStandard
if len(content) > 4000 {
Expand Down
20 changes: 14 additions & 6 deletions cmd/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"log"
"os"
"os/exec"
"strings"
)

// saveCmd represents the save command
Expand Down Expand Up @@ -53,7 +54,6 @@ func createFolderIfNotExist(folderPath string) error {
}

func getFiles(folderPath string) []os.FileInfo {

f, err := os.Open(folderPath)
if err != nil {
log.Fatal(err)
Expand All @@ -67,10 +67,16 @@ func generateCert(domainName string, domainEmail string) {
cmd := exec.Command("certbot", "certonly", "--standalone", "-d", domainName, "--email", domainEmail, "-n", "--agree-tos", "--expand")
out, _ := cmd.CombinedOutput()
err := cmd.Run()
if err != nil {
log.Fatalf("failed to generate cert %s\n%s\n", err, string(out))

output := string(out)
successText := "certificate and chain have been saved at"
log.Printf("%s\n", output)
if strings.Contains(output, successText) {
log.Println("successfully generated cert")
return
}
log.Printf("%s\n", string(out))

log.Fatalf("failed to generate cert %s\n", err.Error())
}

func save(_ *cobra.Command, _ []string) {
Expand All @@ -86,7 +92,6 @@ func save(_ *cobra.Command, _ []string) {
log.Fatal(err)
return
}

ssmSession := session.NewSsmSession()
if exists, parameters := ssmSession.Exists(paramStorePath, validDays); exists {
err := createFolderIfNotExist(folderPath)
Expand All @@ -106,8 +111,11 @@ func save(_ *cobra.Command, _ []string) {
files := getFiles(folderPath)
for _, file := range files {
fileName := file.Name()
if !strings.HasSuffix(fileName, ".pem") {
log.Printf("skipping non .pem file %q...", fileName)
continue
}
filePath := fmt.Sprintf("%s/%s", folderPath, fileName)

content, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit b550268

Please sign in to comment.