Skip to content

Commit 03f6a3d

Browse files
committed
fix: scratch env vars and args parsing
1 parent 5e848a5 commit 03f6a3d

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ ENV POLICY="/policies/traefik.json"
3030
ENV LISTEN_ADDR=":2375"
3131
ENV DOCKER_SOCKET_PATH="/var/run/docker.sock"
3232

33-
CMD ["-policy=$POLICY", "-listen-addr=$LISTEN_ADDR", "-socket-path=$DOCKER_SOCKET_PATH"]
34-
3533
LABEL org.opencontainers.image.description="Docker Socket Proxy"
3634
LABEL org.opencontainers.image.url="https://github.com/andrmr/docker-socket-proxy"
3735
LABEL org.opencontainers.image.source="https://github.com/andrmr/docker-socket-proxy.git"

cmd/proxy/main.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"flag"
7+
"fmt"
78
"log/slog"
89
"net/http"
910
"os"
@@ -23,19 +24,45 @@ const (
2324
shutdownTimeout = 10 * time.Second
2425
)
2526

27+
func getEnv(key, fallback string) string {
28+
if value, ok := os.LookupEnv(key); ok {
29+
return value
30+
}
31+
return fallback
32+
}
33+
2634
func main() {
27-
policyPath := flag.String("policy", "policy.json", "Path to the authorization policy JSON file")
28-
listenAddr := flag.String("listen-addr", ":2375", "Address to listen on")
29-
socketPath := flag.String("socket-path", "/var/run/docker.sock", "Path to the Docker Unix socket")
35+
showUsage := func() {
36+
fmt.Fprintf(os.Stderr, "Docker Socket Proxy using JSON policy for access control.\n\n")
37+
fmt.Fprintf(os.Stderr, "Usage:\n")
38+
fmt.Fprintf(os.Stderr, " docker-socket-proxy [flags]\n\n")
39+
fmt.Fprintf(os.Stderr, "Configuration can be provided via flags or environment variables.\n")
40+
fmt.Fprintf(os.Stderr, "Example:\n")
41+
fmt.Fprintf(os.Stderr, "DOCKER_SOCKET_PATH=/run/docker.sock docker-socket-proxy -listen-addr :2376\n")
42+
}
43+
44+
policyPath := flag.String("policy", getEnv("POLICY", "policy.json"), "Path to the authorization policy JSON file")
45+
listenAddr := flag.String("listen-addr", getEnv("LISTEN_ADDR", ":2375"), "Address to listen on")
46+
socketPath := flag.String(
47+
"socket-path",
48+
getEnv("DOCKER_SOCKET_PATH", "/var/run/docker.sock"),
49+
"Path to the Docker Unix socket",
50+
)
3051
flag.Parse()
3152

3253
logger := initLogger()
3354

3455
logger.Info("starting docker socket proxy", "listen", *listenAddr, "socket", *socketPath, "policy", *policyPath)
3556

57+
if _, err := os.Stat(*policyPath); os.IsNotExist(err) {
58+
fmt.Fprintf(os.Stderr, "Error: Policy file not found: %s\n\n", *policyPath)
59+
showUsage()
60+
os.Exit(1)
61+
}
62+
3663
pol, err := auth.LoadPolicy(*policyPath)
3764
if err != nil {
38-
logger.Error("failed to load policy", "err", err)
65+
fmt.Fprintf(os.Stderr, "Error: Failed to load policy: %v\n", err)
3966
os.Exit(1)
4067
}
4168

0 commit comments

Comments
 (0)