Skip to content

Commit

Permalink
feat: Enhance environment variable handling and workspace initialization
Browse files Browse the repository at this point in the history
- Introduced `environToMap` function to convert OS environment variables to a map.
- Improved error handling for invalid environment variable formats.
- Push these env vars into the child workspaces
  • Loading branch information
noamsdahan committed Aug 27, 2023
1 parent 728e387 commit 17b3a23
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package common

import (
"context"

cnappgoat "github.com/ermetic-research/CNAPPgoat"
"github.com/ermetic-research/CNAPPgoat/cmd/commands/common"
"github.com/pulumi/pulumi/sdk/v3/go/auto"
Expand Down
37 changes: 34 additions & 3 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/pulumi/pulumi/sdk/v3/go/auto/optup"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/sirupsen/logrus"
"os"
"strings"
)

type Engine struct {
Expand Down Expand Up @@ -97,9 +99,14 @@ func (e *Engine) InitializeScenarioWorkspace(ctx context.Context, scenario *Scen

wd := auto.WorkDir(scenarioWorkDir)
secretsProvider := auto.SecretsProvider("passphrase")
ws, err := auto.NewLocalWorkspace(ctx, wd, ph, secretsProvider, auto.EnvVars(map[string]string{
"PULUMI_CONFIG_PASSPHRASE": "cnappgoat12345!",
}))

envMap, err := environToMap()
if err != nil {
return nil, err
}
envMap["PULUMI_CONFIG_PASSPHRASE"] = "cnappgoat12345!"
logrus.WithField("envMap", envMap).Debug("envMap")
ws, err := auto.NewLocalWorkspace(ctx, wd, ph, secretsProvider, auto.EnvVars(envMap))
if err != nil {
return nil, fmt.Errorf("failed to create new local workspace: %w", err)
}
Expand Down Expand Up @@ -349,3 +356,27 @@ func (e *Engine) refresh(ctx context.Context, stack auto.Stack, force bool, scen
func getScenarioStackName(scenario *Scenario) string {
return "cnappgoat_" + scenario.ScenarioParams.ID
}

func environToMap() (map[string]string, error) {
envs := os.Environ()
envMap := make(map[string]string, len(envs))

for _, env := range envs {
parts := strings.SplitN(env, "=", 2) // Split only on the first '='
if len(parts) != 2 {
return nil, fmt.Errorf("invalid environment variable format: %s", env)
}

key := parts[0]
value := parts[1]

if key == "" {
continue
//return nil, fmt.Errorf("empty environment variable key found for value: %s", value)
}

envMap[key] = value
}

return envMap, nil
}

0 comments on commit 17b3a23

Please sign in to comment.