Skip to content

Commit

Permalink
add cmd to restore ssl/tls on create steps
Browse files Browse the repository at this point in the history
Signed-off-by: Thiago Pagotto <pagottoo@gmail.com>
  • Loading branch information
pagottoo committed Jul 20, 2022
1 parent f37a431 commit 97fd053
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 21 deletions.
4 changes: 3 additions & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ to quickly create a Cobra application.`,
terraform.ApplyBaseTerraform(dryRun, directory)
progressPrinter.IncrementTracker("step-softserve", 1)

restoreSSLCmd.Run(cmd, args)

//! soft-serve was just applied

softserve.CreateSoftServe(dryRun, config.KubeConfigPath)
Expand Down Expand Up @@ -104,6 +106,7 @@ to quickly create a Cobra application.`,

//! argocd was just helm installed
waitArgoCDToBeReady(dryRun)

informUser("ArgoCD Ready")
progressPrinter.IncrementTracker("step-argo", 1)

Expand Down Expand Up @@ -146,7 +149,6 @@ to quickly create a Cobra application.`,
informUser("Syncing the registry application")
argocd.SyncArgocdApplication(dryRun, "registry", token)
progressPrinter.IncrementTracker("step-argo", 1)

// todo, need to stall until the registry has synced, then get to ui asap

//! skip this if syncing from argocd and not helm installing
Expand Down
15 changes: 4 additions & 11 deletions cmd/restoreSSL.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,13 @@ This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("restoreSSL called")
ssl.RestoreSSL()
err := ssl.RestoreSSL()
if err != nil {
fmt.Println("Bucket not found, missing SSL backup, assuming first installation")
}
},
}

func init() {
rootCmd.AddCommand(restoreSSLCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// restoreSSLCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// restoreSSLCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
3 changes: 2 additions & 1 deletion internal/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ func DownloadBucket(bucket string, destFolder string) error {
})

if err != nil {
panic("Couldn't list bucket contents")
log.Printf("Couldn't list bucket contents")
return fmt.Errorf("Couldn't list bucket contents")
}

for _, object := range listObjsResponse.Contents {
Expand Down
64 changes: 57 additions & 7 deletions internal/ssl/ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ssl
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/kubefirst/kubefirst/internal/k8s"
"github.com/kubefirst/kubefirst/pkg"
"github.com/spf13/viper"
yaml2 "gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -93,7 +95,7 @@ func GetBackupCertificates() (string, error) {
log.Println("getting certificates")
certificates, err := getItemsToBackup("cert-manager.io", "v1", "certificates", namespaces, "")
if err != nil {
log.Panic(err)
return "", fmt.Errorf("erro: %s", err)
}
for _, cert := range certificates {
fullPath := strings.Replace(cert, config.CertsPath, "/certs", 1)
Expand All @@ -105,7 +107,7 @@ func GetBackupCertificates() (string, error) {
query := ".metadata.annotations[\"cert-manager.io/issuer-kind\"] == \"ClusterIssuer\""
secrets, err := getItemsToBackup("", "v1", "secrets", namespaces, query)
if err != nil {
log.Panic(err)
return "", fmt.Errorf("erro: %s", err)
}
for _, secret := range secrets {
fullPath := strings.Replace(secret, config.CertsPath, "/secrets", 1)
Expand All @@ -117,7 +119,7 @@ func GetBackupCertificates() (string, error) {
log.Println("getting clusterissuers")
clusterIssuers, err := getItemsToBackup("cert-manager.io", "v1", "clusterissuers", emptyNS, "")
if err != nil {
log.Panic(err)
return "", fmt.Errorf("erro: %s", err)
}
for _, clusterissuer := range clusterIssuers {
fullPath := strings.Replace(clusterissuer, config.CertsPath, "/clusterissuers", 1)
Expand All @@ -128,7 +130,7 @@ func GetBackupCertificates() (string, error) {
return "Backuped Cert-Manager resources finished successfully!", nil
}

func RestoreSSL() {
func RestoreSSL() error {
config := configs.ReadConfig()

for _, ns := range namespaces {
Expand All @@ -137,15 +139,63 @@ func RestoreSSL() {
log.Print("failed to create ns: %s, assuming that exists...", err)
}
}
aws.DownloadBucket("k1-kube1st.com", config.CertsPath)
bucketName := fmt.Sprintf("k1-%s", viper.GetString("aws.hostedzonename"))
aws.DownloadBucket(bucketName, config.CertsPath)
//! We need apply secrets firstly than other resources, accordingly with cert-manager docs
pathsRestored := []string{"secrets", "certs", "clusterissuers"}
for _, path := range pathsRestored {
log.Print(path)
//clean yaml
//TODO filter yaml extension
files, err := ioutil.ReadDir(fmt.Sprintf("%s/%s", filepath.Join(config.CertsPath, path), "/"))
if err != nil {
return fmt.Errorf("erro: %s", err)
}

for _, f := range files {
log.Println(f.Name())
pathyaml := fmt.Sprintf("%s/%s", filepath.Join(config.CertsPath, path), f.Name())

yfile, err := ioutil.ReadFile(pathyaml)

if err != nil {
return fmt.Errorf("erro: %s", err)
}

data := make(map[interface{}]interface{})

err = yaml2.Unmarshal(yfile, &data)

if err != nil {
return fmt.Errorf("erro: %s", err)
}

metadataMap := data["metadata"].(map[interface{}]interface{})
delete(metadataMap, "resourceVersion")
delete(metadataMap, "uid")
delete(metadataMap, "creationTimestamp")
delete(metadataMap, "managedFields")
data["metadata"] = metadataMap
dataCleaned, err := yaml2.Marshal(&data)

if err != nil {
return fmt.Errorf("erro: %s", err)
}

err = ioutil.WriteFile(fmt.Sprintf("%s%s", pathyaml, ".clean"), dataCleaned, 0644)

if err != nil {
return fmt.Errorf("erro: %s", err)
}

log.Println("yaml cleaned written")
}

log.Printf("applying the folder: %s", path)
_, _, err := pkg.ExecShellReturnStrings(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "apply", "-f", filepath.Join(config.CertsPath, path))
_, _, err = pkg.ExecShellReturnStrings(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "apply", "-f", filepath.Join(config.CertsPath, path))
if err != nil {
log.Printf("failed to apply %s: %s, assuming that exists...", path, err)
}
}

return nil
}
2 changes: 1 addition & 1 deletion pkg/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func CreateFile(fileName string, fileContent []byte) error {
}

func CreateFullPath(p string) (*os.File, error) {
if err := os.MkdirAll(filepath.Dir(p), 0770); err != nil {
if err := os.MkdirAll(filepath.Dir(p), 0777); err != nil {
return nil, err
}
return os.Create(p)
Expand Down

0 comments on commit 97fd053

Please sign in to comment.