compose/pkg/compose/plugins.go
Yohta Kimura 43922d55b0 Fix rawsetenv nil-pointer guard for inherit-from-shell env vars
When a service declares an env var without a value (e.g. `- KEY` or
`KEY:`), MappingWithEquals stores it as a nil *string. The previous
condition `existing != nil && ...` skipped the warning for this case,
allowing silent overwrites. Change to `existing == nil || ...` so the
warning fires for both nil (shell-inherit) and value-mismatch cases.

Add e2e tests for both list-style (`- KEY`) and map-style (`KEY:`)
YAML forms to lock in the behavior.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Yohta Kimura <38206553+rajyan@users.noreply.github.com>
2026-06-23 15:01:33 +02:00

331 lines
9.1 KiB
Go

/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package compose
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"github.com/compose-spec/compose-go/v2/types"
"github.com/containerd/errdefs"
"github.com/docker/cli/cli-plugins/manager"
"github.com/docker/cli/cli/config"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/docker/compose/v5/pkg/api"
)
type JsonMessage struct {
Type string `json:"type"`
Message string `json:"message"`
}
const (
ErrorType = "error"
InfoType = "info"
SetEnvType = "setenv"
RawSetEnvType = "rawsetenv"
DebugType = "debug"
providerMetadataDirectory = "compose/providers"
)
type pluginVariables struct {
prefixed types.Mapping
raw types.Mapping
}
var mux sync.Mutex
func (s *composeService) runPlugin(ctx context.Context, project *types.Project, service types.ServiceConfig, command string) error {
provider := *service.Provider
plugin, err := s.getPluginBinaryPath(provider.Type)
if err != nil {
return err
}
cmd, err := s.setupPluginCommand(ctx, project, service, plugin, command)
if err != nil {
return err
}
if cmd == nil {
return nil
}
variables, err := s.executePlugin(cmd, command, service)
if err != nil {
return err
}
if command == "stop" {
return nil
}
mux.Lock()
defer mux.Unlock()
for name, s := range project.Services {
if _, ok := s.DependsOn[service.Name]; ok {
prefix := strings.ToUpper(service.Name) + "_"
for key, val := range variables.prefixed {
s.Environment[prefix+key] = &val
}
for key, val := range variables.raw {
if existing, ok := s.Environment[key]; ok && (existing == nil || *existing != val) {
logrus.Warnf("provider %q overrides environment variable %q in service %q", service.Name, key, name)
}
s.Environment[key] = &val
}
project.Services[name] = s
}
}
return nil
}
func (s *composeService) executePlugin(cmd *exec.Cmd, command string, service types.ServiceConfig) (pluginVariables, error) { //nolint:gocyclo
var action string
switch command {
case "up":
s.events.On(creatingEvent(service.Name))
action = "create"
case "down":
s.events.On(removingEvent(service.Name))
action = "remove"
case "stop":
s.events.On(stoppingEvent(service.Name))
action = "stop"
default:
return pluginVariables{}, fmt.Errorf("unsupported plugin command: %s", command)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return pluginVariables{}, err
}
err = cmd.Start()
if err != nil {
return pluginVariables{}, err
}
decoder := json.NewDecoder(stdout)
defer func() { _ = stdout.Close() }()
variables := pluginVariables{
prefixed: types.Mapping{},
raw: types.Mapping{},
}
for {
var msg JsonMessage
err = decoder.Decode(&msg)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return pluginVariables{}, err
}
switch msg.Type {
case ErrorType:
s.events.On(newEvent(service.Name, api.Error, firstLine(msg.Message)))
return pluginVariables{}, errors.New(msg.Message)
case InfoType:
s.events.On(newEvent(service.Name, api.Working, firstLine(msg.Message)))
case SetEnvType:
key, val, found := strings.Cut(msg.Message, "=")
if !found {
return pluginVariables{}, fmt.Errorf("invalid response from plugin: %s", msg.Message)
}
variables.prefixed[key] = val
case RawSetEnvType:
key, val, found := strings.Cut(msg.Message, "=")
if !found {
return pluginVariables{}, fmt.Errorf("invalid response from plugin: %s", msg.Message)
}
variables.raw[key] = val
case DebugType:
logrus.Debugf("%s: %s", service.Name, msg.Message)
default:
return pluginVariables{}, fmt.Errorf("invalid response from plugin: %s", msg.Type)
}
}
err = cmd.Wait()
if err != nil {
s.events.On(errorEvent(service.Name, err.Error()))
return pluginVariables{}, fmt.Errorf("failed to %s service provider: %s", action, err.Error())
}
switch command {
case "up":
s.events.On(createdEvent(service.Name))
case "down":
s.events.On(removedEvent(service.Name))
case "stop":
s.events.On(stoppedEvent(service.Name))
}
return variables, nil
}
func (s *composeService) getPluginBinaryPath(provider string) (path string, err error) {
if provider == "compose" {
return "", errors.New("'compose' is not a valid provider type")
}
plugin, err := manager.GetPlugin(provider, s.dockerCli, &cobra.Command{})
if err == nil {
path = plugin.Path
}
if errdefs.IsNotFound(err) {
path, err = exec.LookPath(executable(provider))
}
return path, err
}
func (s *composeService) setupPluginCommand(ctx context.Context, project *types.Project, service types.ServiceConfig, path, command string) (*exec.Cmd, error) {
cmdOptionsMetadata := s.getPluginMetadata(path, service.Provider.Type, project)
var currentCommandMetadata CommandMetadata
switch command {
case "up":
currentCommandMetadata = cmdOptionsMetadata.Up
case "down":
currentCommandMetadata = cmdOptionsMetadata.Down
case "stop":
if cmdOptionsMetadata.Stop == nil {
return nil, nil
}
currentCommandMetadata = *cmdOptionsMetadata.Stop
}
provider := *service.Provider
commandMetadataIsEmpty := cmdOptionsMetadata.IsEmpty()
if err := currentCommandMetadata.CheckRequiredParameters(provider); !commandMetadataIsEmpty && err != nil {
return nil, err
}
args := []string{"compose", fmt.Sprintf("--project-name=%s", project.Name), command}
for k, v := range provider.Options {
for _, value := range v {
if _, ok := currentCommandMetadata.GetParameter(k); commandMetadataIsEmpty || ok {
args = append(args, fmt.Sprintf("--%s=%s", k, value))
}
}
}
args = append(args, service.Name)
cmd := exec.CommandContext(ctx, path, args...)
err := s.prepareShellOut(ctx, project.Environment, cmd)
if err != nil {
return nil, err
}
return cmd, nil
}
func (s *composeService) getPluginMetadata(path, command string, project *types.Project) ProviderMetadata {
cmd := exec.Command(path, "compose", "metadata")
err := s.prepareShellOut(context.Background(), project.Environment, cmd)
if err != nil {
logrus.Debugf("failed to prepare plugin metadata command: %v", err)
return ProviderMetadata{}
}
stdout := &bytes.Buffer{}
cmd.Stdout = stdout
if err := cmd.Run(); err != nil {
logrus.Debugf("failed to start plugin metadata command: %v", err)
return ProviderMetadata{}
}
var metadata ProviderMetadata
if err := json.Unmarshal(stdout.Bytes(), &metadata); err != nil {
output, _ := io.ReadAll(stdout)
logrus.Debugf("failed to decode plugin metadata: %v - %s", err, output)
return ProviderMetadata{}
}
// Save metadata into docker home directory to be used by Docker LSP tool
// Just log the error as it's not a critical error for the main flow
metadataDir := filepath.Join(config.Dir(), providerMetadataDirectory)
if err := os.MkdirAll(metadataDir, 0o700); err == nil {
metadataFilePath := filepath.Join(metadataDir, command+".json")
if err := os.WriteFile(metadataFilePath, stdout.Bytes(), 0o600); err != nil {
logrus.Debugf("failed to save plugin metadata: %v", err)
}
} else {
logrus.Debugf("failed to create plugin metadata directory: %v", err)
}
return metadata
}
type ProviderMetadata struct {
Description string `json:"description"`
Up CommandMetadata `json:"up"`
Down CommandMetadata `json:"down"`
Stop *CommandMetadata `json:"stop,omitempty"`
}
func (p ProviderMetadata) IsEmpty() bool {
return p.Description == "" && p.Up.Parameters == nil && p.Down.Parameters == nil
}
type CommandMetadata struct {
Parameters []ParameterMetadata `json:"parameters"`
}
type ParameterMetadata struct {
Name string `json:"name"`
Description string `json:"description"`
Required bool `json:"required"`
Type string `json:"type"`
Default string `json:"default,omitempty"`
}
func (c CommandMetadata) GetParameter(paramName string) (ParameterMetadata, bool) {
for _, p := range c.Parameters {
if p.Name == paramName {
return p, true
}
}
return ParameterMetadata{}, false
}
func (c CommandMetadata) CheckRequiredParameters(provider types.ServiceProviderConfig) error {
for _, p := range c.Parameters {
if p.Required {
if _, ok := provider.Options[p.Name]; !ok {
return fmt.Errorf("required parameter %q is missing from provider %q definition", p.Name, provider.Type)
}
}
}
return nil
}
// firstLine returns the first line of s, stripping any trailing newlines.
func firstLine(s string) string {
s = strings.TrimRight(s, "\n")
if before, _, ok := strings.Cut(s, "\n"); ok {
return before
}
return s
}