Skip to content

Commit

Permalink
GCS Post Verification
Browse files Browse the repository at this point in the history
Signed-off-by: “SanjuPal01” <sanju.sanju@progress.com>
  • Loading branch information
SanjuPal01 authored and Dmaddu committed Sep 4, 2023
1 parent a84c2ce commit 8d6330a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
31 changes: 27 additions & 4 deletions components/automate-cli/cmd/chef-automate/deployedConfig.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package main

import (
"encoding/json"
"errors"
"fmt"

"github.com/chef/automate/lib/config"
"github.com/chef/automate/lib/io/fileutils"
)

var GetModeOfDeployment func() string = getModeOfDeployment

func PopulateHaCommonConfig(configPuller PullConfigs) (haDeployConfig *config.HaDeployConfig, err error) {
func PopulateHaCommonConfig(configPuller PullConfigs, fileUtils fileutils.FileUtils) (haDeployConfig *config.HaDeployConfig, err error) {
modeOfDeployment := GetModeOfDeployment()
if modeOfDeployment == EXISTING_INFRA_MODE {
existingInfraConfig, err := configPuller.fetchInfraConfig()
if err != nil {
return nil, err
}
if existingInfraConfig != nil {
return CopyExistingInfra(existingInfraConfig), nil
return CopyExistingInfra(existingInfraConfig, fileUtils)
}
} else if modeOfDeployment == AWS_MODE {
awsConfig, err := configPuller.fetchAwsConfig()
Expand Down Expand Up @@ -44,7 +47,7 @@ func CopyCertsByIP(haDeploy *[]config.CertByIP, existing []CertByIP) {
}
}

func CopyExistingInfra(existingInfraConfig *ExistingInfraConfigToml) *config.HaDeployConfig {
func CopyExistingInfra(existingInfraConfig *ExistingInfraConfigToml, fileUtils fileutils.FileUtils) (*config.HaDeployConfig, error) {

existingInfraConfigConfigInitials := existingInfraConfig.Architecture.ConfigInitials
existingInfraConfigObjectStorageConfig := existingInfraConfig.ObjectStorage.Config
Expand Down Expand Up @@ -185,7 +188,13 @@ func CopyExistingInfra(existingInfraConfig *ExistingInfraConfigToml) *config.HaD
CopyCertsByIP(haDeployConfig.Opensearch.Config.CertsByIP, existingInfraOpensearchSettings.CertsByIP)
}

return haDeployConfig
if existingInfraConfigObjectStorageConfig.Location == "gcs" {
if err := readGCSConfigFile(haDeployConfig.ObjectStorage.Config, fileUtils); err != nil {
return nil, err
}
}

return haDeployConfig, nil
}

func CopyAws(awsConfig *AwsConfigToml) *config.HaDeployConfig {
Expand Down Expand Up @@ -332,3 +341,17 @@ func CopyAws(awsConfig *AwsConfigToml) *config.HaDeployConfig {

return haDeployConfig
}

func readGCSConfigFile(cfg *config.ConfigObjectStorage, fileUtils fileutils.FileUtils) error {
gcpServiceAccount := config.GcpServiceAccount{}
filepath, err := fileUtils.ReadFile(cfg.GoogleServiceAccountFile)
if err != nil {
return fmt.Errorf("error reading Json file: %w", err)
}
err = json.Unmarshal(filepath, &gcpServiceAccount)
if err != nil {
return fmt.Errorf("error unmarshalling Json file: %w", err)
}
cfg.GcpServiceAccount = &gcpServiceAccount
return nil
}
47 changes: 44 additions & 3 deletions components/automate-cli/cmd/chef-automate/deployedConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,39 @@ import (

dc "github.com/chef/automate/api/config/deployment"
"github.com/chef/automate/lib/config"
"github.com/chef/automate/lib/io/fileutils"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

var gcsAccountFileOutput = `{
"type": "service_account",
"project_id": "automate-backup-testing",
"private_key_id": "7b1e77b442708f7af",
"private_key": "ABCD",
"client_email": "abcd@test.com",
"client_id": "1234",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/a2-buildkite%40automate-backup-testing.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}`

var googleServiceAccountResp = &config.GcpServiceAccount{
Type: "service_account",
ProjectID: "automate-backup-testing",
PrivateKeyID: "7b1e77b442708f7af",
PrivateKey: "ABCD",
ClientEmail: "abcd@test.com",
ClientID: "1234",
AuthURI: "https://accounts.google.com/o/oauth2/auth",
TokenURI: "https://oauth2.googleapis.com/token",
AuthProviderX509CertURL: "https://www.googleapis.com/oauth2/v1/certs",
ClientX509CertURL: "https://www.googleapis.com/robot/v1/metadata/x509/a2-buildkite%40automate-backup-testing.iam.gserviceaccount.com",
UniverseDomain: "googleapis.com",
}

var existingInfraConfig = &ExistingInfraConfigToml{
Architecture: ExistingInfraArchitectureToml{
ConfigInitials: ExistingInfraConfigInitialsToml{
Expand Down Expand Up @@ -437,7 +466,11 @@ func TestPopulateHaCommonConfig(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
GetModeOfDeployment = tt.getModeOfDeployment
haDeployConfig, err := PopulateHaCommonConfig(tt.mockPullConfigs)
haDeployConfig, err := PopulateHaCommonConfig(tt.mockPullConfigs, &fileutils.MockFileSystemUtils{
ReadFileFunc: func(filepath string) ([]byte, error) {
return []byte(gcsAccountFileOutput), nil
},
})
if tt.wantErr {
assert.Nil(t, haDeployConfig)
assert.Error(t, err)
Expand Down Expand Up @@ -490,8 +523,13 @@ func TestCopyCertsByIP(t *testing.T) {

func TestCopyExistingInfra(t *testing.T) {

haDeployConfig := CopyExistingInfra(existingInfraConfig)

mockFSUtil := &fileutils.MockFileSystemUtils{
ReadFileFunc: func(filepath string) ([]byte, error) {
return []byte(gcsAccountFileOutput), nil
},
}
haDeployConfig, err := CopyExistingInfra(existingInfraConfig, mockFSUtil)
assert.NoError(t, err)
assert.Equal(t, "existing-ssh-user", haDeployConfig.Architecture.ExistingInfra.SSHUser)
assert.Equal(t, "existing-architecture", haDeployConfig.Architecture.ExistingInfra.Architecture)
assert.Equal(t, "object_storage", haDeployConfig.Architecture.ExistingInfra.BackupConfig)
Expand Down Expand Up @@ -578,7 +616,10 @@ func TestCopyExistingInfra(t *testing.T) {
assert.Equal(t, "existing-endpoint", haDeployConfig.ObjectStorage.Config.Endpoint)
assert.Equal(t, "existing-region", haDeployConfig.ObjectStorage.Config.Region)
assert.Equal(t, "existing-secret-key", haDeployConfig.ObjectStorage.Config.SecretKey)
assert.Equal(t, "gcs", haDeployConfig.ObjectStorage.Config.Location)
assert.Equal(t, "googleServiceAccount.json", haDeployConfig.ObjectStorage.Config.GoogleServiceAccountFile)

assert.Equal(t, googleServiceAccountResp, haDeployConfig.ObjectStorage.Config.GcpServiceAccount)
}

func TestCopyAws(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions components/automate-cli/cmd/chef-automate/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/chef/automate/components/automate-deployment/pkg/cli"
"github.com/chef/automate/lib/config"
"github.com/chef/automate/lib/httputils"
"github.com/chef/automate/lib/io/fileutils"
"github.com/chef/automate/lib/logger"
"github.com/chef/automate/lib/platform/command"
"github.com/chef/automate/lib/reporting"
Expand Down Expand Up @@ -74,7 +75,7 @@ type verifyCmdFlow struct {

type verifyCmdDeps struct {
getAutomateHAInfraDetails func() (*AutomateHAInfraDetails, error)
PopulateHaCommonConfig func(configPuller PullConfigs) (haDeployConfig *config.HaDeployConfig, err error)
PopulateHaCommonConfig func(configPuller PullConfigs, fileUtils fileutils.FileUtils) (haDeployConfig *config.HaDeployConfig, err error)
}

func NewVerifyCmdFlow(client httputils.HTTPClient, createSystemdService verifysystemdcreate.CreateSystemdService, systemdCreateUtils verifysystemdcreate.SystemdCreateUtils, config *config.HaDeployConfig, sshutil sshutils.SSHUtil, writer *cli.Writer, deps *verifyCmdDeps) *verifyCmdFlow {
Expand Down Expand Up @@ -280,7 +281,7 @@ func (v *verifyCmdFlow) RunVerify(config string) error {
sshUtil := NewSSHUtil(sshConfig)
configPuller := NewPullConfigs(infra, sshUtil)

config, err := v.deps.PopulateHaCommonConfig(configPuller)
config, err := v.deps.PopulateHaCommonConfig(configPuller, fileutils.NewFileSystemUtils())
if err != nil {
return status.New(status.ConfigVerifyError, err.Error())
}
Expand Down
11 changes: 6 additions & 5 deletions components/automate-cli/cmd/chef-automate/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/chef/automate/components/automate-cli/pkg/verifysystemdcreate"
"github.com/chef/automate/lib/config"
"github.com/chef/automate/lib/httputils"
"github.com/chef/automate/lib/io/fileutils"
"github.com/chef/automate/lib/majorupgrade_utils"
"github.com/chef/automate/lib/reporting"
"github.com/chef/automate/lib/sshutils"
Expand Down Expand Up @@ -200,7 +201,7 @@ func TestRunVerifyCmd(t *testing.T) {
getAutomateHAInfraDetails: func() (*AutomateHAInfraDetails, error) {
return &AutomateHAInfraDetails{}, nil
},
PopulateHaCommonConfig: func(configPuller PullConfigs) (haDeployConfig *config.HaDeployConfig, err error) {
PopulateHaCommonConfig: func(configPuller PullConfigs, fileUtils fileutils.FileUtils) (haDeployConfig *config.HaDeployConfig, err error) {
return &config.HaDeployConfig{}, nil
},
},
Expand Down Expand Up @@ -254,7 +255,7 @@ func TestRunVerifyCmd(t *testing.T) {
getAutomateHAInfraDetails: func() (*AutomateHAInfraDetails, error) {
return &AutomateHAInfraDetails{}, nil
},
PopulateHaCommonConfig: func(configPuller PullConfigs) (haDeployConfig *config.HaDeployConfig, err error) {
PopulateHaCommonConfig: func(configPuller PullConfigs, fileUtils fileutils.FileUtils) (haDeployConfig *config.HaDeployConfig, err error) {
return &config.HaDeployConfig{}, nil
},
},
Expand Down Expand Up @@ -311,7 +312,7 @@ func TestRunVerifyCmd(t *testing.T) {
getAutomateHAInfraDetails: func() (*AutomateHAInfraDetails, error) {
return nil, errors.New("Unable to get automate HA infra details")
},
PopulateHaCommonConfig: func(configPuller PullConfigs) (haDeployConfig *config.HaDeployConfig, err error) {
PopulateHaCommonConfig: func(configPuller PullConfigs, fileUtils fileutils.FileUtils) (haDeployConfig *config.HaDeployConfig, err error) {
return &config.HaDeployConfig{}, nil
},
},
Expand Down Expand Up @@ -368,7 +369,7 @@ func TestRunVerifyCmd(t *testing.T) {
getAutomateHAInfraDetails: func() (*AutomateHAInfraDetails, error) {
return &AutomateHAInfraDetails{}, nil
},
PopulateHaCommonConfig: func(configPuller PullConfigs) (haDeployConfig *config.HaDeployConfig, err error) {
PopulateHaCommonConfig: func(configPuller PullConfigs, fileUtils fileutils.FileUtils) (haDeployConfig *config.HaDeployConfig, err error) {
return nil, errors.New("Failed to populate HA common config")
},
},
Expand Down Expand Up @@ -599,7 +600,7 @@ func newMockVerifyCmdFlow(mockHttputils *httputils.MockHTTPClient, cw *majorupgr
getAutomateHAInfraDetails: func() (*AutomateHAInfraDetails, error) {
return &AutomateHAInfraDetails{}, nil
},
PopulateHaCommonConfig: func(configPuller PullConfigs) (haDeployConfig *config.HaDeployConfig, err error) {
PopulateHaCommonConfig: func(configPuller PullConfigs, fileUtils fileutils.FileUtils) (haDeployConfig *config.HaDeployConfig, err error) {
return &config.HaDeployConfig{}, nil
},
}
Expand Down

0 comments on commit 8d6330a

Please sign in to comment.