Skip to content

Commit

Permalink
fix: allow setting up env from multiple connections at once. Example:…
Browse files Browse the repository at this point in the history
… needing to setup EKS kubeconfig & the aws connection. (#1277)

* fix: setting up environment from connection

* allow setting up env from multiple connections at once. Example:
  needing to setup EKS kubeconfig & the aws connection.

* chore: setup git connection and shell for use in playbook exec action

* fix: use Find() instead of .First() to avoid db error which cancels the
current transaction.
  • Loading branch information
adityathebe authored Jan 20, 2025
1 parent 18052c6 commit 66fcb64
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
58 changes: 38 additions & 20 deletions connection/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,26 @@ aws_secret_access_key = {{.SecretKey.ValueStatic}}
// SetupConnections creates the necessary credential files and injects env vars
// into the cmd
func SetupConnection(ctx context.Context, connections ExecConnections, cmd *osExec.Cmd) (func() error, error) {
var cleaner = func() error {
return nil
}
var cleaners []func() error

if lo.FromPtr(connections.FromConfigItem) != "" {
var scraperNamespace string
var scraperSpec map[string]any

{
var configItem models.ConfigItem
if err := ctx.DB().Where("id = ?", *connections.FromConfigItem).First(&configItem).Error; err != nil {
if err := ctx.DB().Where("id = ?", *connections.FromConfigItem).Find(&configItem).Error; err != nil {
return nil, fmt.Errorf("failed to get config (%s): %w", *connections.FromConfigItem, err)
} else if configItem.ID.String() != *connections.FromConfigItem {
return nil, fmt.Errorf("cannot setup connection from config %s. not found", *connections.FromConfigItem)
}

var scrapeConfig models.ConfigScraper
if err := ctx.DB().Where("id = ?", lo.FromPtr(configItem.ScraperID)).First(&scrapeConfig).Error; err != nil {
if err := ctx.DB().Where("id = ?", lo.FromPtr(configItem.ScraperID)).Find(&scrapeConfig).Error; err != nil {
return nil, fmt.Errorf("failed to get scrapeconfig (%s): %w", lo.FromPtr(configItem.ScraperID), err)
} else if scrapeConfig.ID.String() != lo.FromPtr(configItem.ScraperID) {
return nil, fmt.Errorf("cannot setup connection from config %s. scraper %s not found", *connections.FromConfigItem,
lo.FromPtr(configItem.ScraperID))
}
scraperNamespace = scrapeConfig.Namespace

Expand Down Expand Up @@ -125,17 +128,19 @@ func SetupConnection(ctx context.Context, connections ExecConnections, cmd *osEx
}
}

configPath, err := saveConfig(kubernetesConfigTemplate, connections.Kubernetes)
if err != nil {
return nil, fmt.Errorf("failed to store kubernetes credentials: %w", err)
}
if filepath.IsAbs(connections.Kubernetes.KubeConfig.ValueStatic) {
cmd.Env = append(cmd.Env, fmt.Sprintf("KUBECONFIG=%s", connections.Kubernetes.KubeConfig.ValueStatic))
} else {
configPath, err := saveConfig(kubernetesConfigTemplate, connections.Kubernetes)
if err != nil {
return nil, fmt.Errorf("failed to store kubernetes credentials: %w", err)
}
cleaners = append(cleaners, func() error {
return os.RemoveAll(filepath.Dir(configPath))
})

cleaner = func() error {
return os.RemoveAll(filepath.Dir(configPath))
cmd.Env = append(cmd.Env, fmt.Sprintf("KUBECONFIG=%s", configPath))
}

cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("KUBECONFIG=%s", configPath))
}

if connections.AWS != nil {
Expand All @@ -148,11 +153,10 @@ func SetupConnection(ctx context.Context, connections ExecConnections, cmd *osEx
return nil, fmt.Errorf("failed to store AWS credentials: %w", err)
}

cleaner = func() error {
cleaners = append(cleaners, func() error {
return os.RemoveAll(filepath.Dir(configPath))
}
})

cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "AWS_EC2_METADATA_DISABLED=true") // https://github.com/aws/aws-cli/issues/5262#issuecomment-705832151
cmd.Env = append(cmd.Env, fmt.Sprintf("AWS_SHARED_CREDENTIALS_FILE=%s", configPath))
if connections.AWS.Region != "" {
Expand Down Expand Up @@ -182,9 +186,9 @@ func SetupConnection(ctx context.Context, connections ExecConnections, cmd *osEx
return nil, fmt.Errorf("failed to store gcloud credentials: %w", err)
}

cleaner = func() error {
cleaners = append(cleaners, func() error {
return os.RemoveAll(filepath.Dir(configPath))
}
})

// to configure gcloud CLI to use the service account specified in GOOGLE_APPLICATION_CREDENTIALS,
// we need to explicitly activate it
Expand All @@ -193,9 +197,23 @@ func SetupConnection(ctx context.Context, connections ExecConnections, cmd *osEx
return nil, fmt.Errorf("failed to activate GCP service account: %w", err)
}

cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("GOOGLE_APPLICATION_CREDENTIALS=%s", configPath))
}

var cleaner = func() error {
var errorList []error
for _, c := range cleaners {
if err := c(); err != nil {
errorList = append(errorList, err)
}
}

if len(errorList) > 0 {
return fmt.Errorf("failed to cleanup: %v", errorList)
}

return nil
}

return cleaner, nil
}
17 changes: 17 additions & 0 deletions connection/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/flanksource/commons/logger"
"github.com/flanksource/commons/utils"
"github.com/flanksource/duty/context"
"github.com/samber/lo"

Expand Down Expand Up @@ -168,6 +169,22 @@ type GitConnection struct {
Destination *string `yaml:"destination,omitempty" json:"destination,omitempty"`
}

func (git GitConnection) GetURL() types.EnvVar {
return types.EnvVar{ValueStatic: git.URL}
}

func (git GitConnection) GetUsername() types.EnvVar {
return utils.Deref(git.Username)
}

func (git GitConnection) GetPassword() types.EnvVar {
return utils.Deref(git.Password)
}

func (git GitConnection) GetCertificate() types.EnvVar {
return utils.Deref(git.Certificate)
}

func (c *GitConnection) HydrateConnection(ctx context.Context) error {
ctx.Logger.V(9).Infof("Hydrating GitConnection %s", logger.Pretty(*c))

Expand Down
2 changes: 1 addition & 1 deletion shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func runCmd(ctx context.Context, cmd *commandContext) (*ExecDetails, error) {
"stdout", result.Stdout,
"extra", result.Extra,
"exit-code", result.ExitCode,
).Code(fmt.Sprintf("exited with %d", result.ExitCode)).Errorf(result.Error.Error())
).Code(fmt.Sprintf("exited with %d", result.ExitCode)).Errorf("%v", result.Error.Error())
}

return &result, nil
Expand Down

0 comments on commit 66fcb64

Please sign in to comment.