Fix the problem with environment as secret

We needed to filter the environment coming from a env file, but declared
as secet in `secret-vars` label

fix #17
This commit is contained in:
2022-05-23 12:11:23 +02:00
parent 5fc8c06d8b
commit 114fab4870
2 changed files with 39 additions and 8 deletions

View File

@@ -573,6 +573,15 @@ func prepareEnvFromFiles(name string, s *types.ServiceConfig, container *helm.Co
secretsFiles = strings.Split(v, ",")
}
var secretVars []string
if v, ok := s.Labels[helm.LABEL_SECRETVARS]; ok {
secretVars = strings.Split(v, ",")
}
for i, s := range secretVars {
secretVars[i] = strings.TrimSpace(s)
}
// manage environment files (env_file in compose)
for _, envfile := range s.EnvFile {
f := PathToName(envfile)
@@ -594,7 +603,7 @@ func prepareEnvFromFiles(name string, s *types.ServiceConfig, container *helm.Co
}
envfile = filepath.Join(compose.GetCurrentDir(), envfile)
if err := store.AddEnvFile(envfile); err != nil {
if err := store.AddEnvFile(envfile, secretVars); err != nil {
logger.ActivateColors = true
logger.Red(err.Error())
logger.ActivateColors = false

View File

@@ -9,11 +9,14 @@ import (
// InlineConfig is made to represent a configMap or a secret
type InlineConfig interface {
AddEnvFile(filename string) error
AddEnvFile(filename string, filter []string) error
AddEnv(key, val string) error
Metadata() *Metadata
}
var _ InlineConfig = (*ConfigMap)(nil)
var _ InlineConfig = (*Secret)(nil)
// ConfigMap is made to represent a configMap with data.
type ConfigMap struct {
*K8sBase `yaml:",inline"`
@@ -42,7 +45,7 @@ func (c *ConfigMap) Metadata() *Metadata {
}
// AddEnvFile adds an environment file to the configMap.
func (c *ConfigMap) AddEnvFile(file string) error {
func (c *ConfigMap) AddEnvFile(file string, filter []string) error {
content, err := ioutil.ReadFile(file)
if err != nil {
return err
@@ -51,8 +54,8 @@ func (c *ConfigMap) AddEnvFile(file string) error {
lines := strings.Split(string(content), "\n")
for _, l := range lines {
//Check if the line is a comment
isComment := strings.HasPrefix(l, "#")
l = strings.TrimSpace(l)
isComment := strings.HasPrefix(l, "#")
if len(l) == 0 || isComment {
continue
}
@@ -60,8 +63,17 @@ func (c *ConfigMap) AddEnvFile(file string) error {
if len(parts) < 2 {
return errors.New("The environment file " + file + " is not valid")
}
var skip bool
for _, filterEnv := range filter {
if parts[0] == filterEnv {
skip = true
}
}
if !skip {
c.Data[parts[0]] = parts[1]
}
}
return nil
}
@@ -93,7 +105,7 @@ func NewSecret(name, path string) *Secret {
}
// AddEnvFile adds an environment file to the secret.
func (s *Secret) AddEnvFile(file string) error {
func (s *Secret) AddEnvFile(file string, filter []string) error {
content, err := ioutil.ReadFile(file)
if err != nil {
return err
@@ -102,15 +114,25 @@ func (s *Secret) AddEnvFile(file string) error {
lines := strings.Split(string(content), "\n")
for _, l := range lines {
l = strings.TrimSpace(l)
if len(l) == 0 {
isComment := strings.HasPrefix(l, "#")
if len(l) == 0 || isComment {
continue
}
parts := strings.SplitN(l, "=", 2)
if len(parts) < 2 {
return errors.New("The environment file " + file + " is not valid")
}
var skip bool
for _, filterEnv := range filter {
if parts[0] == filterEnv {
skip = true
}
}
if !skip {
s.Data[parts[0]] = fmt.Sprintf(`{{ "%s" | b64enc }}`, parts[1])
}
}
return nil