Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
34 changes: 31 additions & 3 deletions src/cmd/cli/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ func SetupCommands(ctx context.Context, version string) {

configCmd.AddCommand(configListCmd)

configCmd.AddCommand(configResolveCmd)

RootCmd.AddCommand(configCmd)

RootCmd.AddCommand(setupComposeCommand())
Expand Down Expand Up @@ -742,9 +744,7 @@ func collectUnsetEnvVars(project *composeTypes.Project) []string {
if project == nil {
return nil // in case loading failed
}
err := compose.ValidateProjectConfig(context.TODO(), project, func(ctx context.Context) ([]string, error) {
return nil, nil // assume no config
})
err := compose.ValidateProjectConfig(context.TODO(), project, []string{})
var missingConfig compose.ErrMissingConfig
if errors.As(err, &missingConfig) {
return missingConfig
Expand Down Expand Up @@ -970,6 +970,34 @@ var configListCmd = &cobra.Command{
},
}

var configResolveCmd = &cobra.Command{
Use: "resolve",
Annotations: authNeededAnnotation,
Args: cobra.NoArgs,
Aliases: []string{"final"},
Short: "Show the final resolved config for the project",
RunE: func(cmd *cobra.Command, args []string) error {
loader := configureLoader(cmd)

provider, err := newProviderChecked(cmd.Context(), loader)
if err != nil {
return err
}

project, err := loader.LoadProject(cmd.Context())
if err != nil {
return err
}

config, err := provider.ListConfig(cmd.Context(), &defangv1.ListConfigsRequest{Project: project.Name})
if err != nil {
return err
}

return compose.PrintConfigResolutionSummary(*project, config.Names)
},
}

var debugCmd = &cobra.Command{
Use: "debug [SERVICE...]",
Annotations: authNeededAnnotation,
Expand Down
102 changes: 102 additions & 0 deletions src/pkg/cli/compose/configResolution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package compose

import (
"slices"
"sort"

"github.com/DefangLabs/defang/src/pkg/term"
)

type configOutput struct {
Service string `json:"service"`
Name string `json:"name"`
Value string `json:"value,omitempty"`
Source Source `json:"source,omitempty"`
}

type Source int

const (
SourceUnknown Source = iota
SourceComposeFile
SourceDefangConfig
SourceDefangAndComposeFile
)

var sourceNames = map[Source]string{
SourceUnknown: "unknown",
SourceComposeFile: "compose_file",
SourceDefangConfig: "defang_config",
SourceDefangAndComposeFile: "compose_file and defang_config",
}

func (s Source) String() string {
if name, ok := sourceNames[s]; ok {
return name
}
return sourceNames[SourceUnknown]
}

// determineConfigSource determines the source of an environment variable
// and returns the appropriate source type and value to display
func determineConfigSource(envKey string, envValue *string, defangConfigs map[string]string) (Source, string) {
// If the key itself is a defang config, mask it
if _, isDefangConfig := defangConfigs[envKey]; isDefangConfig {
return SourceDefangConfig, configMaskedValue
}

// If value is nil, it's from the compose file with empty value
if envValue == nil {
return SourceComposeFile, ""
}

// Check if the value contains references to defang configs
interpolatedVariables := DetectInterpolationVariables(*envValue)
if len(interpolatedVariables) > 0 {
for _, varName := range interpolatedVariables {
if _, isDefangConfig := defangConfigs[varName]; isDefangConfig {
return SourceDefangAndComposeFile, *envValue
}
}
}

// Otherwise, it's from the compose file
return SourceComposeFile, *envValue
}

const configMaskedValue = "*****"

func PrintConfigResolutionSummary(project Project, defangConfig []string) error {
configset := make(map[string]string)
for _, name := range defangConfig {
configset[name] = ""
}

projectEnvVars := []configOutput{}

for serviceName, service := range project.Services {
for envKey, envValue := range service.Environment {
source, value := determineConfigSource(envKey, envValue, configset)
projectEnvVars = append(projectEnvVars, configOutput{
Service: serviceName,
Name: envKey,
Value: value,
Source: source,
})
}
}

// Sort by Service, then by Name within each service
sort.Slice(projectEnvVars, func(i, j int) bool {
if projectEnvVars[i].Service != projectEnvVars[j].Service {
return projectEnvVars[i].Service < projectEnvVars[j].Service
}
return projectEnvVars[i].Name < projectEnvVars[j].Name
})

projectEnvVars = slices.Compact(projectEnvVars)

term.Println("\033[1mENVIRONMENT VARIABLES RESOLUTION SUMMARY:\033[0m")

return term.Table(projectEnvVars, "Service", "Name", "Value", "Source")
}
34 changes: 18 additions & 16 deletions src/pkg/cli/compose/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,21 @@ func getResourceReservations(r composeTypes.Resources) *composeTypes.Resource {
// Copied from shared/utils.ts but slightly modified to remove the negative-lookahead assertion
var interpolationRegex = regexp.MustCompile(`(?i)\$(\$)|\$(?:{([^}]+)}|([_a-z][_a-z0-9]*))|([^$]+)`) // [1] escaped dollar, [2] curly braces, [3] variable name, [4] literal

func ValidateProjectConfig(ctx context.Context, composeProject *composeTypes.Project, listConfigNamesFunc ListConfigNamesFunc) error {
func DetectInterpolationVariables(value string) []string {
var names []string
// check for variables used during interpolation
for _, match := range interpolationRegex.FindAllStringSubmatch(value, -1) {
if match[2] != "" {
names = append(names, match[2])
}
if match[3] != "" {
names = append(names, match[3])
}
}
return names
}

func ValidateProjectConfig(ctx context.Context, composeProject *composeTypes.Project, listConfigNames []string) error {
var names []string
// make list of secrets
for _, service := range composeProject.Services {
Expand All @@ -444,34 +458,22 @@ func ValidateProjectConfig(ctx context.Context, composeProject *composeTypes.Pro
names = append(names, key)
continue
}
// check for variables used during interpolation
for _, match := range interpolationRegex.FindAllStringSubmatch(*value, -1) {
if match[2] != "" {
names = append(names, match[2])
}
if match[3] != "" {
names = append(names, match[3])
}
}
detectedNames := DetectInterpolationVariables(*value)
names = append(names, detectedNames...)
}
}

if len(names) == 0 {
return nil // no secrets to check
}

configs, err := listConfigNamesFunc(ctx)
if err != nil {
return err
}

// Deduplicate (sort + uniq)
slices.Sort(names)
names = slices.Compact(names)

errMissingConfig := ErrMissingConfig{}
for _, name := range names {
if !slices.Contains(configs, name) {
if !slices.Contains(listConfigNames, name) {
errMissingConfig = append(errMissingConfig, name)
}
}
Expand Down
20 changes: 9 additions & 11 deletions src/pkg/cli/compose/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ func TestValidationAndConvert(t *testing.T) {
logs.WriteString("Error: " + err.Error() + "\n") // no coverage!
}

if err := ValidateProjectConfig(t.Context(), project, listConfigNamesFunc); err != nil {
listConfigNames, err := listConfigNamesFunc(t.Context())
if err != nil {
t.Fatal(err)
}
if err := ValidateProjectConfig(t.Context(), project, listConfigNames); err != nil {
t.Logf("Project config validation failed: %v", err)
logs.WriteString("Error: " + err.Error() + "\n")
}
Expand All @@ -82,12 +86,6 @@ func TestValidationAndConvert(t *testing.T) {
})
}

func makeListConfigNamesFunc(configs ...string) func(context.Context) ([]string, error) {
return func(context.Context) ([]string, error) {
return configs, nil
}
}

func TestValidateConfig(t *testing.T) {
ctx := t.Context()

Expand All @@ -101,7 +99,7 @@ func TestValidateConfig(t *testing.T) {
}
testProject.Services["service1"] = composeTypes.ServiceConfig{Environment: env}

if err := ValidateProjectConfig(ctx, &testProject, makeListConfigNamesFunc()); err != nil {
if err := ValidateProjectConfig(ctx, &testProject, []string{}); err != nil {
t.Fatal(err)
}
})
Expand All @@ -116,7 +114,7 @@ func TestValidateConfig(t *testing.T) {
testProject.Services["service1"] = composeTypes.ServiceConfig{Environment: env}

var missing ErrMissingConfig
if err := ValidateProjectConfig(ctx, &testProject, makeListConfigNamesFunc()); !errors.As(err, &missing) {
if err := ValidateProjectConfig(ctx, &testProject, []string{}); !errors.As(err, &missing) {
t.Fatalf("expected ErrMissingConfig, got: %v", err)
} else {
if len(missing) != 3 {
Expand All @@ -139,7 +137,7 @@ func TestValidateConfig(t *testing.T) {
}
testProject.Services["service1"] = composeTypes.ServiceConfig{Environment: env}

if err := ValidateProjectConfig(ctx, &testProject, makeListConfigNamesFunc(CONFIG_VAR)); err != nil {
if err := ValidateProjectConfig(ctx, &testProject, []string{CONFIG_VAR}); err != nil {
t.Fatal(err)
}
})
Expand All @@ -151,7 +149,7 @@ func TestValidateConfig(t *testing.T) {
testProject.Services["service1"] = composeTypes.ServiceConfig{Environment: env}

var missing ErrMissingConfig
if err := ValidateProjectConfig(ctx, &testProject, makeListConfigNamesFunc()); !errors.As(err, &missing) {
if err := ValidateProjectConfig(ctx, &testProject, []string{}); !errors.As(err, &missing) {
t.Fatalf("expected ErrMissingConfig, got: %v", err)
} else {
if len(missing) != 1 {
Expand Down
13 changes: 12 additions & 1 deletion src/pkg/cli/composeUp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,20 @@ func ComposeUp(ctx context.Context, fabric client.FabricClient, provider cliClie

// Ignore missing configs in preview mode, because we don't want to fail the preview if some configs are missing.
if upload != compose.UploadModeEstimate {
if err := compose.ValidateProjectConfig(ctx, project, listConfigNamesFunc); err != nil {
listConfigNames, err := listConfigNamesFunc(ctx)
if err != nil {
return nil, project, err
}

if err := compose.ValidateProjectConfig(ctx, project, listConfigNames); err != nil {
return nil, project, &ComposeError{err}
}

// Print config resolution summary
err = compose.PrintConfigResolutionSummary(*project, listConfigNames)
if err != nil {
return nil, project, err
}
}
}

Expand Down
Loading