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

Add cert rotate command for HA Service #7494

Merged
merged 9 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
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
160 changes: 160 additions & 0 deletions components/automate-cli/cmd/chef-automate/certRotate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"time"

"github.com/chef/automate/components/automate-cli/pkg/status"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

var certFlags = struct {
privateCert string
publicCert string
}{}

var sshFlag = struct {
automate bool
chefserver bool
}{}

var certRotateCmd = &cobra.Command{
Use: "cert-rotate",
Short: "Chef Automate rotate cert",
Long: "Chef Automate CLI command to rotate certificates",
RunE: certRotate,
}

func init() {
RootCmd.AddCommand(certRotateCmd)

certRotateCmd.PersistentFlags().BoolVar(&sshFlag.automate, "automate", false, "Automate Certificate Rotation")
certRotateCmd.PersistentFlags().BoolVar(&sshFlag.automate, "a2", false, "Automate Certificate Rotation")
certRotateCmd.PersistentFlags().BoolVar(&sshFlag.chefserver, "chefserver", false, "Chef Infra Server Certificate Rotation")
certRotateCmd.PersistentFlags().BoolVar(&sshFlag.chefserver, "cs", false, "Chef Infra Server Certificate Rotation")

certRotateCmd.PersistentFlags().StringVar(&certFlags.privateCert, "private-cert", "", "Private certificate")
certRotateCmd.PersistentFlags().StringVar(&certFlags.publicCert, "public-cert", "", "Public certificate")
}

func certRotate(cmd *cobra.Command, args []string) error {
privateCertPath := certFlags.privateCert
publicCertPath := certFlags.publicCert
fileName := "cert-rotate.toml"
timestamp := time.Now().Format("20060102150405")

if privateCertPath == "" || publicCertPath == "" {
return errors.New("Please provide public and private cert paths")
}
privateCert, err := ioutil.ReadFile(privateCertPath) // nosemgrep
if err != nil {
return status.Wrap(
err,
status.FileAccessError,
fmt.Sprintf("failed reading data from file: %s", err.Error()),
)
}

publicCert, err := ioutil.ReadFile(publicCertPath) // nosemgrep
if err != nil {
return status.Wrap(
err,
status.FileAccessError,
fmt.Sprintf("failed reading data from file: %s", err.Error()),
)
}

f, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
}

if isA2HARBFileExist() {

infra, err := getAutomateHAInfraDetails()
if err != nil {
return err
}
sshUser := infra.Outputs.SSHUser.Value
sskKeyFile := infra.Outputs.SSHKeyFile.Value
sshPort := infra.Outputs.SSHPort.Value

if sshFlag.automate {
config := fmt.Sprintf(`
Sahiba3108 marked this conversation as resolved.
Show resolved Hide resolved
[[load_balancer.v1.sys.frontend_tls]]
cert = """%v"""
key = """%v"""
[[global.v1.frontend_tls]]
cert = """%v"""
key = """%v"""
`, string(publicCert), string(privateCert), string(publicCert), string(privateCert))

_, err = f.Write([]byte(config))
if err != nil {
log.Fatal(err)
Sahiba3108 marked this conversation as resolved.
Show resolved Hide resolved
}
f.Close()
automateIps := infra.Outputs.AutomatePrivateIps.Value
if len(automateIps) == 0 {
return errors.New("No Automate Ips found")
}

const remoteService string = "automate"
scriptCommands := fmt.Sprintf(FRONTEND_COMMANDS, remoteService+timestamp, dateFormat)
for i := 0; i < len(automateIps); i++ {
err := copyFileToRemote(sskKeyFile, fileName, sshUser, automateIps[i], remoteService+timestamp)
if err != nil {
writer.Errorf("%v", err)
return err
}
output, err := ConnectAndExecuteCommandOnRemote(sshUser, sshPort, sskKeyFile, automateIps[i], scriptCommands)
if err != nil {
writer.Errorf("%v", err)
return err
}
writer.Printf(output)
}

} else if sshFlag.chefserver {
config := fmt.Sprintf(`
[[load_balancer.v1.sys.frontend_tls]]
Sahiba3108 marked this conversation as resolved.
Show resolved Hide resolved
cert = """%v"""
key = """%v"""
[[global.v1.frontend_tls]]
cert = """%v"""
key = """%v"""
`, string(publicCert), string(privateCert), string(publicCert), string(privateCert))

_, err = f.Write([]byte(config))
if err != nil {
log.Fatal(err)
}
f.Close()
chefserverIps := infra.Outputs.ChefServerPrivateIps.Value
if len(chefserverIps) == 0 {
return errors.New("No Chef Server Ips found")
}

const remoteService string = "chefserver"
scriptCommands := fmt.Sprintf(FRONTEND_COMMANDS, remoteService+timestamp, dateFormat)
for i := 0; i < len(chefserverIps); i++ {
err := copyFileToRemote(sskKeyFile, fileName, sshUser, chefserverIps[i], remoteService+timestamp)
if err != nil {
writer.Errorf("%v", err)
return err
}
output, err := ConnectAndExecuteCommandOnRemote(sshUser, sshPort, sskKeyFile, chefserverIps[i], scriptCommands)
if err != nil {
writer.Errorf("%v", err)
return err
}
writer.Printf(output)
}
}
}
return nil
}
2 changes: 1 addition & 1 deletion components/automate-cli/cmd/chef-automate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func runSetCommand(cmd *cobra.Command, args []string) error {

func copyFileToRemote(sshKeyFile string, tomlFilePath string, sshUser string, hostIP string, destFileName string) error {
cmd := "scp"
exec_args := []string{"-o StrictHostKeyChecking=off", "-i", sshKeyFile, "-r", tomlFilePath, sshUser + "@" + hostIP + ":/tmp/" + destFileName}
exec_args := []string{"-o StrictHostKeyChecking=no", "-i", sshKeyFile, "-r", tomlFilePath, sshUser + "@" + hostIP + ":/tmp/" + destFileName}
Sahiba3108 marked this conversation as resolved.
Show resolved Hide resolved
if err := exec.Command(cmd, exec_args...).Run(); err != nil {
writer.Print("Failed to copy TOML file to remote\n")
return err
Expand Down
4 changes: 3 additions & 1 deletion terraform/a2ha-terraform/modules/aws/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,6 @@ output "aws_cluster_id" {
output "ssh_user" {
value = var.aws_ssh_user
}

output "ssh_port" {
value = var.aws_ssh_port
}
3 changes: 3 additions & 0 deletions terraform/a2ha-terraform/modules/vsphere/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ output "opensearch_public_ips" {
output "ssh_user" {
value = var.aws_ssh_user
}
output "ssh_port" {
value = var.aws_ssh_port
}