Skip to content

Commit

Permalink
Protect nginx vars from being interpreted as env vars
Browse files Browse the repository at this point in the history
Fixes #465.
  • Loading branch information
Alex Guerra authored and aguerra committed Mar 26, 2018
1 parent ec1026d commit d0a38a1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed
- `.teresaignore` behavior (to work like `.gitignore`)
- Protect nginx vars from being interpreted as env vars

## [0.17.0] - 2018-03-20
### Changed
Expand Down
32 changes: 26 additions & 6 deletions pkg/server/spec/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package spec
import (
"fmt"
"strconv"
"strings"

"github.com/luizalabs/teresa/pkg/server/app"
"github.com/luizalabs/teresa/pkg/server/storage"
Expand All @@ -12,7 +13,7 @@ const (
nginxConfTmplDir = "/etc/nginx/template/"
nginxConfDir = "/etc/nginx/"
nginxConfFile = "nginx.conf"
nginxArgTmpl = "envsubst < %s%s > %s%s && nginx -g 'daemon off;'"
nginxArgTmpl = "envsubst '%s' < %s%s > %s%s && nginx -g 'daemon off;'"
nginxBackendTmpl = "http://localhost:%d"
)

Expand Down Expand Up @@ -88,9 +89,13 @@ func newNginxVolumeMount() *VolumeMounts {
}

func newNginxContainer(image string) *Container {
args := fmt.Sprintf(nginxArgTmpl, nginxConfTmplDir, nginxConfFile, nginxConfDir, nginxConfFile)
port := strconv.Itoa(DefaultPort)
backend := fmt.Sprintf(nginxBackendTmpl, secondaryPort)
env := map[string]string{
"NGINX_PORT": port,
"NGINX_BACKEND": backend,
}
args := newNginxContainerArgs(env)

return &Container{
Name: "nginx",
Expand All @@ -101,16 +106,31 @@ func newNginxContainer(image string) *Container {
Name: "nginx",
ContainerPort: int32(DefaultPort),
}},
Env: map[string]string{
"NGINX_PORT": port,
"NGINX_BACKEND": backend,
},
Env: env,
VolumeMounts: []*VolumeMounts{
newNginxVolumeMount(),
},
}
}

func newNginxContainerArgs(env map[string]string) string {
tmp := make([]string, len(env))
var i int
for key, _ := range env {
tmp[i] = fmt.Sprintf("$%s", key)
i++
}
args := fmt.Sprintf(
nginxArgTmpl,
strings.Join(tmp, " "),
nginxConfTmplDir,
nginxConfFile,
nginxConfDir,
nginxConfFile,
)
return args
}

func newAppContainer(name, image string, envVars map[string]string, port int, secrets []string) *Container {
return &Container{
Name: name,
Expand Down
25 changes: 25 additions & 0 deletions pkg/server/spec/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package spec

import (
"fmt"
"testing"
)

func TestNewNginxContainerArgs(t *testing.T) {
env := map[string]string{"key1": "value1", "key2": "value2"}
keys := "$key1 $key2"
want := fmt.Sprintf(
nginxArgTmpl,
keys,
nginxConfTmplDir,
nginxConfFile,
nginxConfDir,
nginxConfFile,
)

got := newNginxContainerArgs(env)

if got != want {
t.Errorf("got %s; want %s", got, want)
}
}

0 comments on commit d0a38a1

Please sign in to comment.