Skip to content

Commit

Permalink
Make docker compose a bit more robust with health check, add redactio…
Browse files Browse the repository at this point in the history
…n for sensitive env variables when printing
  • Loading branch information
NHAS committed Nov 7, 2024
1 parent 2961c81 commit ebf10d9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,10 @@ features:
dbname: (string) Which database to use
sslmode: (string) postgres sslmode
```
## Development
```sh
docker compose -f docker-compose.dev.yaml down --remove-orphans
docker compose -f docker-compose.dev.yaml --env-file .env.dev up --build --force-recreate
```
32 changes: 22 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Config struct {
PublicURL string `yaml:"public_url"`
IssuerURL string `yaml:"issuer_url"`
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
ClientSecret string `yaml:"client_secret" sensitive:"yes"`
AdminGroupClaimName string `yaml:"admin_group_claim_name"`
AdminGroup string `yaml:"admin_group_name"`
}
Expand All @@ -42,7 +42,7 @@ type Config struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Password string `yaml:"password" sensitive:"yes"`
FromEmail string `yaml:"from"`
}

Expand All @@ -60,12 +60,17 @@ type Config struct {
User string `yaml:"user"`
DBname string `yaml:"dbname"`
SSLmode string `yaml:"sslmode"`
Password string `yaml:"password"`
Password string `yaml:"password" sensitive:"yes"`
}
}

func listFields(v interface{}) []string {
var fields []string
type fieldDescription struct {
Name string
Sensitive bool
}

func listFields(v interface{}) []fieldDescription {
var fields []fieldDescription
t := reflect.TypeOf(v).Elem()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
Expand All @@ -74,10 +79,11 @@ func listFields(v interface{}) []string {
if field.Type.Kind() == reflect.Struct {
subFields := listFields(reflect.New(field.Type).Interface())
for _, subField := range subFields {
fields = append(fields, fmt.Sprintf("%s.%s", fieldName, subField))
fields = append(fields, fieldDescription{Name: fmt.Sprintf("%s.%s", fieldName, subField.Name), Sensitive: subField.Sensitive})
}
} else {
fields = append(fields, fieldName)
value, _ := field.Tag.Lookup("sensitive")
fields = append(fields, fieldDescription{Name: fieldName, Sensitive: (value == "true" || value == "yes")})
}
}
return fields
Expand Down Expand Up @@ -129,12 +135,18 @@ func LoadConfig(path string) (c Config, err error) {
fields := listFields(&c)
setSomething := false
for _, field := range fields {
envVariable := os.Getenv(field)
fmt.Printf("%s=%s\n", field, envVariable)
envVariable := os.Getenv(field.Name)

printedValue := envVariable
if field.Sensitive && envVariable != "" {
printedValue = "**********"
}

fmt.Printf("%s=%s\n", field.Name, printedValue)

if envVariable != "" {
setSomething = true
setField(&c, field, envVariable)
setField(&c, field.Name, envVariable)
}
}

Expand Down
22 changes: 15 additions & 7 deletions docker-compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@ services:
postgres:
image: postgres:13
environment:
- POSTGRES_USER=gohunt
- POSTGRES_USER=${GOHUNT_DB_USERNAME:-gohunt}
- POSTGRES_PASSWORD=gohunt
- POSTGRES_DB=gohunt
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${GOHUNT_DB_USERNAME:-gohunt}"]
interval: 5s
timeout: 5s
retries: 5
volumes:
- db-data:/var/lib/postgresql/data
- db-data-dev:/var/lib/postgresql/data

gohunt:
image: ghcr.io/nhas/gohunt:main
depends_on:
- postgres
build:
context: .
dockerfile: Dockerfile
restart: on-failure
environment:
- GOHUNT_USERNAME=${GOHUNT_USERNAME}
- GOHUNT_PASSWORD=${GOHUNT_PASSWORD}
Expand All @@ -20,14 +30,12 @@ services:
- Notification.Webhooks.Enabled=true
- Database.Host=postgres
- Database.Port=5432
- Database.User=gohunt
- Database.User=${GOHUNT_DB_USERNAME:-gohunt}
- Database.DBname=gohunt
- Database.SSLmode=disable
- Database.Password=gohunt
ports:
- 8081:8081

volumes:
db-data:
caddy-data:
caddy_config:
db-data-dev:
9 changes: 7 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ services:
image: postgres:13
restart: on-failure
environment:
- POSTGRES_USER=gohunt
- POSTGRES_USER=${GOHUNT_DB_USERNAME:-gohunt}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=gohunt
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${GOHUNT_DB_USERNAME:-gohunt}"]
interval: 5s
timeout: 5s
retries: 5
volumes:
- db-data:/var/lib/postgresql/data

Expand All @@ -25,7 +30,7 @@ services:
- Notification.Webhooks.Enabled=true
- Database.Host=postgres
- Database.Port=5432
- Database.User=gohunt
- Database.User=${GOHUNT_DB_USERNAME:-gohunt}
- Database.DBname=gohunt
- Database.SSLmode=disable
- Database.Password=${DB_PASSWORD}
Expand Down

0 comments on commit ebf10d9

Please sign in to comment.