Skip to content
Open
25 changes: 25 additions & 0 deletions admin/commands/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,31 @@ func ParseDisableCollectors(collectors []string) []string {
return disableCollectors
}

// ValidateEnvironmentVariableNames validates environment variable names.
func ValidateEnvironmentVariableNames(varNames []string) ([]string, error) {
if len(varNames) == 0 {
return nil, nil
}

result := make([]string, 0, len(varNames))
validNamePattern := regexp.MustCompile(`^[A-Z_][A-Z0-9_]*$`)

for _, name := range varNames {
name = strings.TrimSpace(name)
if name == "" {
return nil, fmt.Errorf("environment variable name cannot be empty")
}

if !validNamePattern.MatchString(name) {
return nil, fmt.Errorf("invalid environment variable name: %s (must match [A-Z_][A-Z0-9_]*)", name)
}

result = append(result, name)
}

return result, nil
}

// ReadFile reads file from filepath if filepath is not empty.
func ReadFile(filePath string) (string, error) {
if filePath == "" {
Expand Down
26 changes: 16 additions & 10 deletions admin/commands/management/add_mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type AddMongoDBCommand struct {
StatsCollections []string `help:"Collections for collstats & indexstats"`
CollectionsLimit int32 `name:"max-collections-limit" default:"-1" help:"Disable collstats, dbstats, topmetrics and indexstats if there are more than <n> collections. 0: No limit. Default is -1, which let PMM automatically set this value"`
ExposeExporter bool `name:"expose-exporter" help:"Optionally expose the address of the exporter publicly on 0.0.0.0"`
AgentEnvVars []string `name:"agent-env-vars" help:"Comma-separated list of environment variable names to pass to the exporter (values are read from the current environment), e.g. 'VAR1,VAR2'"`

AddCommonFlags
flags.MetricsModeFlags
Expand Down Expand Up @@ -125,6 +126,10 @@ func (cmd *AddMongoDBCommand) GetCredentials() error {
// RunCmd runs the command for AddMongoDBCommand.
func (cmd *AddMongoDBCommand) RunCmd() (commands.Result, error) {
customLabels := commands.ParseKeyValuePair(cmd.CustomLabels)
agentVarNames, err := commands.ValidateEnvironmentVariableNames(cmd.AgentEnvVars)
if err != nil {
return nil, err
}

tlsCertificateKey, err := commands.ReadFile(cmd.TLSCertificateKeyFile)
if err != nil {
Expand Down Expand Up @@ -179,16 +184,17 @@ func (cmd *AddMongoDBCommand) RunCmd() (commands.Result, error) {
QANMongodbProfiler: cmd.QuerySource == MongodbQuerySourceProfiler,
QANMongodbMongolog: cmd.QuerySource == MongodbQuerySourceMongolog,

CustomLabels: customLabels,
SkipConnectionCheck: cmd.SkipConnectionCheck,
MaxQueryLength: cmd.MaxQueryLength,
TLS: cmd.TLS,
TLSSkipVerify: cmd.TLSSkipVerify,
TLSCertificateKey: tlsCertificateKey,
TLSCertificateKeyFilePassword: cmd.TLSCertificateKeyFilePassword,
TLSCa: tlsCa,
AuthenticationMechanism: cmd.AuthenticationMechanism,
AuthenticationDatabase: cmd.AuthenticationDatabase,
CustomLabels: customLabels,
SharedEnvironmentVariableNames: agentVarNames,
SkipConnectionCheck: cmd.SkipConnectionCheck,
MaxQueryLength: cmd.MaxQueryLength,
TLS: cmd.TLS,
TLSSkipVerify: cmd.TLSSkipVerify,
TLSCertificateKey: tlsCertificateKey,
TLSCertificateKeyFilePassword: cmd.TLSCertificateKeyFilePassword,
TLSCa: tlsCa,
AuthenticationMechanism: cmd.AuthenticationMechanism,
AuthenticationDatabase: cmd.AuthenticationDatabase,

MetricsMode: cmd.MetricsModeFlags.MetricsMode.EnumValue(),

Expand Down
11 changes: 11 additions & 0 deletions agent/agents/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,17 @@ func (s *Supervisor) processParams(agentID string, agentProcess *agentv1.SetStat
}
processParams.Env = append(processParams.Env, env...)

for _, varName := range agentProcess.EnvVariableNames {
value, exists := os.LookupEnv(varName)
if !exists {
s.l.Warnf("Environment variable %s not found in pmm-agent environment for agent %s", varName, agentID)
continue
}

processParams.Env = append(processParams.Env, fmt.Sprintf("%s=%s", varName, value))
s.l.Debugf("Resolved environment variable %s for agent %s", varName, agentID)
}

return &processParams, nil
}

Expand Down
4 changes: 2 additions & 2 deletions api-tests/management/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,9 @@ func TestAddMongoDB(t *testing.T) {
},
},
}
addProxySQLOK, err := client.Default.ManagementService.AddService(params)
addMongoDBOK, err := client.Default.ManagementService.AddService(params)
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "Socket and address cannot be specified together.")
assert.Nil(t, addProxySQLOK)
assert.Nil(t, addMongoDBOK)
})

t.Run("Socket", func(t *testing.T) {
Expand Down
Loading
Loading