Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vault unseal #120

Merged
merged 24 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4edd3c2
fixes for vault unseal
johndietz Jul 15, 2022
d2df185
Merge branch 'go-cli' of github.com:kubefirst/kubefirst into vault-un…
johndietz Jul 15, 2022
c51c201
not my favorite commit
johndietz Jul 15, 2022
6600555
remove not my stuff
johndietz Jul 15, 2022
7699e5a
add reports package back
johndietz Jul 15, 2022
13592e3
viper check adjustments
johndietz Jul 15, 2022
9113a9a
viper check adjustments
johndietz Jul 15, 2022
fca9c8f
vault local address
johndietz Jul 17, 2022
24a0b41
changes, mostly formatting
jarededwards Jul 17, 2022
1454054
spaces
johndietz Jul 17, 2022
d4b6bf5
Merge branch 'vault-unseal' of github.com:kubefirst/kubefirst into va…
johndietz Jul 17, 2022
2e6c948
adjustments through latest provisioning
johndietz Jul 17, 2022
bb5745e
Vault unseal tweak 0718 (#121)
6za Jul 18, 2022
e4a6df8
adjustments for gitlab takeover
johndietz Jul 18, 2022
4bdfe44
merge conflicts resolved
johndietz Jul 18, 2022
73c30e6
unused ref
johndietz Jul 18, 2022
a8880b2
detokenization games
johndietz Jul 18, 2022
e3672c4
add slash to .git/ on detokenize denylist
johndietz Jul 18, 2022
e7cc7f8
adding registry sync after gitlab gitops registry
johndietz Jul 18, 2022
ef75bf9
terraform lock removal
johndietz Jul 18, 2022
19d12c9
adding argocd app host check and recycling/resyncing
johndietz Jul 18, 2022
87ec0d7
addressinging orchestration issues from last run
johndietz Jul 19, 2022
3e66064
port-forward fix
johndietz Jul 19, 2022
c1fe46c
merge go-cli into vault-unseal
johndietz Jul 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions cmd/argocdSync.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmd

import (
"github.com/kubefirst/kubefirst/internal/argocd"
"log"

"github.com/kubefirst/kubefirst/internal/argocd"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -20,10 +21,12 @@ This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {

dryRun, err := cmd.Flags().GetBool("dry-run")
if err != nil {
log.Panic(err)
}
// dryRun, err := cmd.Flags().GetBool("dry-run")
// if err != nil {
// log.Panic(err)
// }

dryRun := false

log.Println("dry run enabled:", dryRun)

Expand Down
7 changes: 4 additions & 3 deletions cmd/checktools.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"

"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/pkg"
"github.com/spf13/cobra"
Expand All @@ -27,13 +28,13 @@ var checktoolsCmd = &cobra.Command{
fmt.Printf("-> helm version:\n\t%s\n\t%s\n", helmVersion, helmStdErr)

if errKubectl != nil {
fmt.Println("failed to call kubectlVersionCmd.Run(): %v", errKubectl)
fmt.Printf("failed to call kubectlVersionCmd.Run(): %v", errKubectl)
}
if errHelm != nil {
fmt.Println("failed to call helmVersionCmd.Run(): %v", errHelm)
fmt.Printf("failed to call helmVersionCmd.Run(): %v", errHelm)
}
if errTerraform != nil {
fmt.Println("failed to call terraformVersionCmd.Run(): %v", errTerraform)
fmt.Printf("failed to call terraformVersionCmd.Run(): %v", errTerraform)
}

},
Expand Down
246 changes: 164 additions & 82 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ import (
"github.com/kubefirst/kubefirst/internal/argocd"
"github.com/kubefirst/kubefirst/internal/gitlab"
"github.com/kubefirst/kubefirst/internal/helm"
"github.com/kubefirst/kubefirst/internal/k8s"
"github.com/kubefirst/kubefirst/internal/progressPrinter"
"github.com/kubefirst/kubefirst/internal/reports"
"github.com/kubefirst/kubefirst/internal/softserve"
"github.com/kubefirst/kubefirst/internal/terraform"
"github.com/kubefirst/kubefirst/internal/vault"
"github.com/kubefirst/kubefirst/pkg"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)


// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create",
Expand Down Expand Up @@ -81,8 +85,8 @@ to quickly create a Cobra application.`,
defer kPortForwardSoftServe.Process.Signal(syscall.SIGTERM)
if err != nil {
// If it doesn't error, we kinda don't care much.
log.Println("Commad Execution STDOUT: %s", kPortForwardSoftServeOutb.String())
log.Println("Commad Execution STDERR: %s", kPortForwardSoftServeErrb.String())
log.Printf("Commad Execution STDOUT: %s", kPortForwardSoftServeOutb.String())
log.Printf("Commad Execution STDERR: %s", kPortForwardSoftServeErrb.String())
log.Panicf("error: failed to port-forward to soft-serve %s", err)
}
time.Sleep(20 * time.Second)
Expand All @@ -101,22 +105,22 @@ to quickly create a Cobra application.`,
waitArgoCDToBeReady(dryRun)
informUser("ArgoCD Ready")
progressPrinter.IncrementTracker("step-argo", 1)
if !dryRun {
var kPortForwardArgocdOutb, kPortForwardArgocdErrb bytes.Buffer
kPortForwardArgocd := exec.Command(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "-n", "argocd", "port-forward", "svc/argocd-server", "8080:80")
kPortForwardArgocd.Stdout = &kPortForwardArgocdOutb
kPortForwardArgocd.Stderr = &kPortForwardArgocdErrb
err = kPortForwardArgocd.Start()
defer kPortForwardArgocd.Process.Signal(syscall.SIGTERM)
if err != nil {
log.Println("Commad Execution STDOUT: %s", kPortForwardArgocdOutb.String())
log.Println("Commad Execution STDERR: %s", kPortForwardArgocdErrb.String())
log.Panicf("error: failed to port-forward to argocd in main thread %s", err)
}

log.Println("sleeping for 45 seconds, hurry up jared")
time.Sleep(45 * time.Second)
var kPortForwardArgocdOutb, kPortForwardArgocdErrb bytes.Buffer
kPortForwardArgocd := exec.Command(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "-n", "argocd", "port-forward", "svc/argocd-server", "8080:80")
kPortForwardArgocd.Stdout = &kPortForwardArgocdOutb
kPortForwardArgocd.Stderr = &kPortForwardArgocdErrb
err = kPortForwardArgocd.Start()
defer kPortForwardArgocd.Process.Signal(syscall.SIGTERM)
if err != nil {
log.Printf("Commad Execution STDOUT: %s", kPortForwardArgocdOutb.String())
log.Printf("Commad Execution STDERR: %s", kPortForwardArgocdErrb.String())
log.Panicf("error: failed to port-forward to argocd in main thread %s", err)
}

// log.Println("sleeping for 45 seconds, hurry up jared")
// time.Sleep(45 * time.Second)

informUser(fmt.Sprintf("ArgoCD available at %s", viper.GetString("argocd.local.service")))
progressPrinter.IncrementTracker("step-argo", 1)

Expand All @@ -128,15 +132,22 @@ to quickly create a Cobra application.`,
token := argocd.GetArgocdAuthToken(dryRun)
progressPrinter.IncrementTracker("step-argo", 1)

_, _, err = pkg.ExecShellReturnStrings(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "-n", "argocd", "apply", "-f", fmt.Sprintf("%s/gitops/components/helpers/registry.yaml", config.K1FolderPath))
if err != nil {
log.Panicf("failed to call execute kubectl apply of argocd patch to adopt gitlab: %s", err)
}
time.Sleep(45 * time.Second)
//TODO: ensure argocd is in a good heathy state before syncing the registry 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
log.Printf("sleeping for 30 seconds, hurry up jared sign into argocd %s", viper.GetString("argocd.admin.password"))
time.Sleep(30 * time.Second)
// log.Printf("sleeping for 30 seconds, hurry up jared sign into argocd %s", viper.GetString("argocd.admin.password"))
// time.Sleep(30 * time.Second)

//!
//* we need to stop here and wait for the vault namespace to exist and the vault pod to be ready
Expand All @@ -159,8 +170,8 @@ to quickly create a Cobra application.`,
log.Panicf("error: failed to port-forward to vault in main thread %s", err)
}
}
loopUntilPodIsReady()
initializeVaultAndAutoUnseal()
loopUntilPodIsReady(dryRun)
initializeVaultAndAutoUnseal(dryRun)
informUser(fmt.Sprintf("Vault available at %s", viper.GetString("vault.local.service")))
progressPrinter.IncrementTracker("step-gitlab", 1)

Expand All @@ -180,8 +191,8 @@ to quickly create a Cobra application.`,
defer kPortForwardGitlab.Process.Signal(syscall.SIGTERM)
if err != nil {
// If it doesn't error, we kinda don't care much.
log.Println("Commad Execution STDOUT: %s", kPortForwardGitlabOutb.String())
log.Println("Commad Execution STDERR: %s", kPortForwardGitlabErrb.String())
log.Printf("Commad Execution STDOUT: %s", kPortForwardGitlabOutb.String())
log.Printf("Commad Execution STDERR: %s", kPortForwardGitlabErrb.String())
log.Panicf("error: failed to port-forward to gitlab in main thread %s", err)
}
}
Expand All @@ -199,75 +210,146 @@ to quickly create a Cobra application.`,
gitlab.GitlabKeyUpload(dryRun)
informUser("Gitlab ready")
progressPrinter.IncrementTracker("step-gitlab", 1)
}
if !skipVault {

progressPrinter.AddTracker("step-vault", "Configure Vault", 4)
informUser("waiting for vault unseal")
/**

*/
waitVaultToBeRunning(dryRun)
informUser("Vault running")
progressPrinter.IncrementTracker("step-vault", 1)

waitForVaultUnseal(dryRun, config)
informUser("Vault unseal")
progressPrinter.IncrementTracker("step-vault", 1)

log.Println("configuring vault")
vault.ConfigureVault(dryRun)
informUser("Vault configured")
progressPrinter.IncrementTracker("step-vault", 1)

log.Println("creating vault configured secret")
createVaultConfiguredSecret(dryRun, config)
informUser("Vault secret created")
progressPrinter.IncrementTracker("step-vault", 1)
}

if !skipVault {

progressPrinter.AddTracker("step-vault", "Configure Vault", 4)
informUser("waiting for vault unseal")
/**

*/
waitVaultToBeRunning(dryRun)
informUser("Vault running")
progressPrinter.IncrementTracker("step-vault", 1)

waitForVaultUnseal(dryRun, config)
informUser("Vault unseal")
progressPrinter.IncrementTracker("step-vault", 1)
if !viper.GetBool("gitlab.oidc-created") {
progressPrinter.AddTracker("step-post-gitlab", "Finalize Gitlab updates", 5)
vault.AddGitlabOidcApplications(dryRun)
informUser("Added Gitlab OIDC")
progressPrinter.IncrementTracker("step-post-gitlab", 1)

log.Println("configuring vault")
vault.ConfigureVault(dryRun)
informUser("Vault configured")
progressPrinter.IncrementTracker("step-vault", 1)
informUser("Waiting for Gitlab dns to propagate before continuing")
gitlab.AwaitHost("gitlab", dryRun)
progressPrinter.IncrementTracker("step-post-gitlab", 1)

log.Println("creating vault configured secret")
createVaultConfiguredSecret(dryRun, config)
informUser("Vault secret created")
progressPrinter.IncrementTracker("step-vault", 1)
informUser("Pushing gitops repo to origin gitlab")
// refactor: sounds like a new functions, should PushGitOpsToGitLab be renamed/update signature?
viper.Set("gitlab.oidc-created", true)
viper.WriteConfig()
}
if !viper.GetBool("gitlab.gitops-pushed") {
gitlab.PushGitRepo(dryRun, config, "gitlab", "gitops") // todo: need to handle if this was already pushed, errors on failure)
progressPrinter.IncrementTracker("step-post-gitlab", 1)
// todo: keep one of the two git push functions, they're similar, but not exactly the same
//gitlab.PushGitOpsToGitLab(dryRun)
viper.Set("gitlab.gitops-pushed", true)
viper.WriteConfig()
}
if !dryRun && !viper.GetBool("argocd.oidc-patched") {
cfg := configs.ReadConfig()
config, err := clientcmd.BuildConfigFromFlags("", cfg.KubeConfigPath)
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}

if !viper.GetBool("gitlab.oidc-created") {
progressPrinter.AddTracker("step-post-gitlab", "Finalize Gitlab updates", 5)
vault.AddGitlabOidcApplications(dryRun)
informUser("Added Gitlab OIDC")
progressPrinter.IncrementTracker("step-post-gitlab", 1)

informUser("Waiting for Gitlab dns to propagate before continuing")
gitlab.AwaitGitlab(dryRun)
progressPrinter.IncrementTracker("step-post-gitlab", 1)
argocdSecretClient = clientset.CoreV1().Secrets("argocd")
patchSecret(argocdSecretClient, "argocd-secret", "oidc.gitlab.clientSecret", viper.GetString("gitlab.oidc.argocd.secret"))

informUser("Pushing gitops repo to origin gitlab")
// refactor: sounds like a new functions, should PushGitOpsToGitLab be renamed/update signature?
viper.Set("gitlab.oidc-created", true)
viper.WriteConfig()
}
if !viper.GetBool("gitlab.gitops-pushed") {
gitlab.PushGitRepo(dryRun, config, "gitlab", "gitops") // todo: need to handle if this was already pushed, errors on failure)
progressPrinter.IncrementTracker("step-post-gitlab", 1)
// todo: keep one of the two git push functions, they're similar, but not exactly the same
//gitlab.PushGitOpsToGitLab(dryRun)
viper.Set("gitlab.gitops-pushed", true)
viper.WriteConfig()
argocdPodClient := clientset.CoreV1().Pods("argocd")
argocdPodName := k8s.GetPodNameByLabel(argocdPodClient, "app.kubernetes.io/name=argocd-server")
k8s.DeletePodByName(argocdPodClient, argocdPodName)
viper.Set("argocd.oidc-patched", true)
viper.WriteConfig()
}
if !viper.GetBool("gitlab.metaphor-pushed") {
informUser("Pushing metaphor repo to origin gitlab")
gitlab.PushGitRepo(dryRun, config, "gitlab", "metaphor")
progressPrinter.IncrementTracker("step-post-gitlab", 1)
// todo: keep one of the two git push functions, they're similar, but not exactly the same
//gitlab.PushGitOpsToGitLab(dryRun)
viper.Set("gitlab.metaphor-pushed", true)
viper.WriteConfig()
}
if !viper.GetBool("gitlab.registered") {
// informUser("Getting ArgoCD auth token")
// token := argocd.GetArgocdAuthToken(dryRun)
// progressPrinter.IncrementTracker("step-post-gitlab", 1)

// informUser("Detaching the registry application from softserve")
// argocd.DeleteArgocdApplicationNoCascade(dryRun, "registry", token)
// progressPrinter.IncrementTracker("step-post-gitlab", 1)

informUser("Adding the registry application registered against gitlab")
gitlab.ChangeRegistryToGitLab(dryRun)
progressPrinter.IncrementTracker("step-post-gitlab", 1)
// todo triage / force apply the contents adjusting
// todo kind: Application .repoURL:

// informUser("Waiting for argocd host to resolve")
// gitlab.AwaitHost("argocd", dryRun)
cfg := configs.ReadConfig()
config, err := clientcmd.BuildConfigFromFlags("", cfg.KubeConfigPath)
if err != nil {
panic(err.Error())
}
if !viper.GetBool("gitlab.metaphor-pushed") {
informUser("Pushing metaphor repo to origin gitlab")
gitlab.PushGitRepo(dryRun, config, "gitlab", "metaphor")
progressPrinter.IncrementTracker("step-post-gitlab", 1)
// todo: keep one of the two git push functions, they're similar, but not exactly the same
//gitlab.PushGitOpsToGitLab(dryRun)
viper.Set("gitlab.metaphor-pushed", true)
viper.WriteConfig()
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
if !viper.GetBool("gitlab.registered") {
informUser("Changing registry to Gitlab")
gitlab.ChangeRegistryToGitLab(dryRun)
progressPrinter.IncrementTracker("step-post-gitlab", 1)
// todo triage / force apply the contents adjusting
// todo kind: Application .repoURL:
viper.Set("gitlab.registered", true)
viper.WriteConfig()
argocdPodClient := clientset.CoreV1().Pods("argocd")
argocdPodName := k8s.GetPodNameByLabel(argocdPodClient, "app.kubernetes.io/name=argocd-server")
kPortForwardArgocd.Process.Signal(syscall.SIGTERM)
informUser("deleting argocd-server pod")
k8s.DeletePodByName(argocdPodClient, argocdPodName)
informUser("waiting for argocd to be ready")
waitArgoCDToBeReady(dryRun)

informUser("Port forwarding to new argocd-server pod")
if !dryRun {
time.Sleep(time.Second * 20)
var kPortForwardArgocdOutb, kPortForwardArgocdErrb bytes.Buffer
config := configs.ReadConfig()
kPortForwardArgocd := exec.Command(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "-n", "argocd", "port-forward", "svc/argocd-server", "8080:80")
kPortForwardArgocd.Stdout = &kPortForwardArgocdOutb
kPortForwardArgocd.Stderr = &kPortForwardArgocdErrb
err = kPortForwardArgocd.Start()
defer kPortForwardArgocd.Process.Signal(syscall.SIGTERM)
if err != nil {
log.Printf("Commad Execution STDOUT: %s", kPortForwardArgocdOutb.String())
log.Printf("Commad Execution STDERR: %s", kPortForwardArgocdErrb.String())
log.Panicf("error: failed to port-forward to argocd in main thread %s", err)
}
log.Println("sleeping for 40 seconds")
time.Sleep(40 * time.Second)
}

informUser("Syncing the registry application")
token := argocd.GetArgocdAuthToken(dryRun)
argocd.SyncArgocdApplication(dryRun, "registry", token)

viper.Set("gitlab.registered", true)
viper.WriteConfig()
}

sendCompleteInstallTelemetry(dryRun)
time.Sleep(time.Millisecond * 100)

Expand Down
Loading