Skip to content
Merged
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
10 changes: 10 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ func New(cfg *Config) (cmd Command, err error) {
IsHistoryDisabled: cfg.IsHistoryDisabled,
//
IsInheritEnvironmentEnabled: cfg.IsInheritEnvironmentEnabled,
//
AllowedSystemEnvKeys: cfg.AllowedSystemEnvKeys,
})
if err != nil {
return nil, err
Expand All @@ -167,6 +169,8 @@ func New(cfg *Config) (cmd Command, err error) {
Privileged: cfg.Privileged,
//
DockerHost: cfg.DockerHost,
//
AllowedSystemEnvKeys: cfg.AllowedSystemEnvKeys,
})
if err != nil {
return nil, err
Expand All @@ -186,6 +190,8 @@ func New(cfg *Config) (cmd Command, err error) {
Server: cfg.Server,
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
//
AllowedSystemEnvKeys: cfg.AllowedSystemEnvKeys,
})
if err != nil {
return nil, err
Expand All @@ -208,6 +214,8 @@ func New(cfg *Config) (cmd Command, err error) {
Platform: cfg.Platform,
Network: cfg.Network,
DisableNetwork: cfg.DisableNetwork,
//
AllowedSystemEnvKeys: cfg.AllowedSystemEnvKeys,
})
if err != nil {
return nil, err
Expand All @@ -233,6 +241,8 @@ func New(cfg *Config) (cmd Command, err error) {
//
IsIgnoreStrictHostKeyChecking: cfg.SSHIsIgnoreStrictHostKeyChecking,
KnowHostsFilePath: cfg.SSHKnowHostsFilePath,
//
AllowedSystemEnvKeys: cfg.AllowedSystemEnvKeys,
})
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Config struct {
// engine = host
IsHistoryDisabled bool
IsInheritEnvironmentEnabled bool
//
AllowedSystemEnvKeys []string

// engine = docker
Image string
Expand Down
3 changes: 3 additions & 0 deletions engine/caas/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ type Config struct {

// Custom Command Runner ID
ID string

// AllowedSystemEnvKeys is the allowed system environment keys, which will be inherited to the command
AllowedSystemEnvKeys []string
}
16 changes: 15 additions & 1 deletion engine/caas/wait.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
package caas

import "github.com/go-zoox/commands-as-a-service/entities"
import (
"os"

"github.com/go-zoox/commands-as-a-service/entities"
)

// Wait waits for the command to finish.
func (c *caas) Wait() error {
if len(c.cfg.AllowedSystemEnvKeys) != 0 {
for _, key := range c.cfg.AllowedSystemEnvKeys {
if c.cfg.Environment[key] == "" {
if value, ok := os.LookupEnv(key); ok {
c.cfg.Environment[key] = value
}
}
}
}

return c.client.Exec(&entities.Command{
ID: c.cfg.ID,
Script: c.cfg.Command,
Expand Down
3 changes: 3 additions & 0 deletions engine/dind/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ type Config struct {

// Custom Command Runner ID
ID string

// AllowedSystemEnvKeys is the allowed system environment keys, which will be inherited to the command
AllowedSystemEnvKeys []string
}
12 changes: 12 additions & 0 deletions engine/dind/create.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package dind

import (
"os"

"github.com/go-zoox/command/engine/docker"
)

// create creates a container.
func (d *dind) create() (err error) {
if len(d.cfg.AllowedSystemEnvKeys) != 0 {
for _, key := range d.cfg.AllowedSystemEnvKeys {
if d.cfg.Environment[key] == "" {
if value, ok := os.LookupEnv(key); ok {
d.cfg.Environment[key] = value
}
}
}
}

d.client, err = docker.New(&docker.Config{
ID: d.cfg.ID,
//
Expand Down
3 changes: 3 additions & 0 deletions engine/docker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ type Config struct {

// Custom Command Runner ID
ID string

// AllowedSystemEnvKeys is the allowed system environment keys, which will be inherited to the command
AllowedSystemEnvKeys []string
}
9 changes: 9 additions & 0 deletions engine/docker/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package docker
import (
"context"
"fmt"
"os"

"github.com/docker/cli/cli/streams"
"github.com/docker/docker/api/types"
Expand All @@ -23,6 +24,14 @@ func (d *docker) create() (err error) {
d.args = append(d.args, "-c", d.cfg.Command)
}

if len(d.cfg.AllowedSystemEnvKeys) != 0 {
for _, key := range d.cfg.AllowedSystemEnvKeys {
if value, ok := os.LookupEnv(key); ok {
d.env = append(d.env, fmt.Sprintf("%s=%s", key, value))
}
}
}

for k, v := range d.cfg.Environment {
d.env = append(d.env, fmt.Sprintf("%s=%s", k, v))
}
Expand Down
2 changes: 2 additions & 0 deletions engine/host/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Config struct {
IsHistoryDisabled bool
//
IsInheritEnvironmentEnabled bool
// AllowedSystemEnvKeys is the allowed system environment keys, which will be inherited to the command
AllowedSystemEnvKeys []string

// Custom Command Runner ID
ID string
Expand Down
10 changes: 8 additions & 2 deletions engine/host/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (h *host) create() error {
logger.Debugf("create command: %s %v", h.cfg.Shell, args)
h.cmd = exec.Command(h.cfg.Shell, args...)

if err := applyEnv(h.cmd, h.cfg.Environment, h.cfg.IsInheritEnvironmentEnabled); err != nil {
if err := applyEnv(h.cmd, h.cfg.Environment, h.cfg.IsInheritEnvironmentEnabled, h.cfg.AllowedSystemEnvKeys); err != nil {
return err
}

Expand All @@ -45,10 +45,16 @@ func (h *host) create() error {
return nil
}

func applyEnv(cmd *exec.Cmd, environment map[string]string, IsInheritEnvironmentEnabled bool) error {
func applyEnv(cmd *exec.Cmd, environment map[string]string, IsInheritEnvironmentEnabled bool, allowedSystemEnvKeys []string) error {
cmd.Env = append([]string{}, "TERM=xterm")
if IsInheritEnvironmentEnabled {
cmd.Env = append(cmd.Env, os.Environ()...)
} else if len(allowedSystemEnvKeys) != 0 {
for _, key := range allowedSystemEnvKeys {
if value, ok := os.LookupEnv(key); ok {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", key, value))
}
}
}

for k, v := range environment {
Expand Down
2 changes: 2 additions & 0 deletions engine/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Config struct {
//
KnowHostsFilePath string

AllowedSystemEnvKeys []string

//
ID string
}
Expand Down
10 changes: 10 additions & 0 deletions engine/ssh/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ssh

import (
"io"
"os"

sshx "golang.org/x/crypto/ssh"
)
Expand All @@ -20,6 +21,15 @@ func (s *ssh) Start() error {
return nil
}

if len(s.cfg.AllowedSystemEnvKeys) != 0 {
for _, key := range s.cfg.AllowedSystemEnvKeys {
if value, ok := os.LookupEnv(key); ok {
s.session.Setenv(key, value)
}
}

}

for k, v := range s.cfg.Environment {
s.session.Setenv(k, v)
}
Expand Down