Skip to content

Commit 07e3044

Browse files
committed
tsidp-server.go: read ENV vars using Go
Remove the script used to read environment variables in docker and instead read the variables using plain Go. This simplifies the container build and make it possible to use tools like Ko to build the container. Signed-off-by: Rodrigo Schio <[email protected]>
1 parent 335dc10 commit 07e3044

File tree

3 files changed

+48
-69
lines changed

3 files changed

+48
-69
lines changed

Dockerfile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ RUN addgroup -g 1001 -S app && \
3535
# Copy the binary from builder
3636
COPY --from=builder /app/tsidp-server /tsidp-server
3737

38-
# Copy the entrypoint script
39-
COPY scripts/docker/run.sh /run.sh
40-
RUN chmod +x /run.sh
41-
4238
USER app:app
4339

44-
# Run the binary through the entrypoint script
45-
ENTRYPOINT ["/run.sh"]
40+
ENTRYPOINT ["/tsidp-server"]

scripts/docker/run.sh

Lines changed: 0 additions & 53 deletions
This file was deleted.

tsidp-server.go

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"net/http"
2020
"os"
2121
"os/signal"
22+
"strconv"
2223
"strings"
2324
"time"
2425

@@ -37,20 +38,20 @@ import (
3738
// Command line flags
3839
// Migrated from legacy/tsidp.go:64-73
3940
var (
40-
flagPort = flag.Int("port", 443, "port to listen on")
41-
flagLocalPort = flag.Int("local-port", -1, "allow requests from localhost")
42-
flagUseLocalTailscaled = flag.Bool("use-local-tailscaled", false, "use local tailscaled instead of tsnet")
43-
flagFunnel = flag.Bool("funnel", false, "use Tailscale Funnel to make tsidp available on the public internet")
44-
flagHostname = flag.String("hostname", "idp", "tsnet hostname to use instead of idp")
45-
flagDir = flag.String("dir", "", "tsnet state directory; a default one will be created if not provided")
46-
flagEnableSTS = flag.Bool("enable-sts", false, "enable OIDC STS token exchange support")
41+
flagPort = flag.Int("port", envInt("TSIDP_PORT", 443), "port to listen on")
42+
flagLocalPort = flag.Int("local-port", envInt("TSIDP_LOCAL_PORT", -1), "allow requests from localhost")
43+
flagUseLocalTailscaled = flag.Bool("use-local-tailscaled", envBool("TSIDP_USE_LOCAL_TAILSCALED", false), "use local tailscaled instead of tsnet")
44+
flagFunnel = flag.Bool("funnel", envBool("TSIDP_USE_FUNNEL", false), "use Tailscale Funnel to make tsidp available on the public internet")
45+
flagHostname = flag.String("hostname", envString("TS_HOSTNAME", "idp"), "tsnet hostname to use instead of idp")
46+
flagDir = flag.String("dir", envString("TS_STATE_DIR", ""), "tsnet state directory; a default one will be created if not provided")
47+
flagEnableSTS = flag.Bool("enable-sts", envBool("TSIDP_ENABLE_STS", false), "enable OIDC STS token exchange support")
4748

4849
// application logging levels
49-
flagLogLevel = flag.String("log", "info", "log levels: debug, info, warn, error")
50+
flagLogLevel = flag.String("log", envString("TSIDP_LOG", "info"), "log levels: debug, info, warn, error")
5051

5152
// extended debugging information
52-
flagDebugAllRequests = flag.Bool("debug-all-requests", false, "capture and print all HTTP requests and responses")
53-
flagDebugTSNet = flag.Bool("debug-tsnet", false, "enable tsnet.Server logging")
53+
flagDebugAllRequests = flag.Bool("debug-all-requests", envBool("TSIDP_DEBUG_ALL_REQUESTS", false), "capture and print all HTTP requests and responses")
54+
flagDebugTSNet = flag.Bool("debug-tsnet", envBool("TSIDP_DEBUG_TSNET", false), "enable tsnet.Server logging")
5455
)
5556

5657
// main initializes and starts the tsidp server
@@ -345,3 +346,39 @@ func (rw *responseWrapper) Write(b []byte) (int, error) {
345346
// Write to the original response writer
346347
return rw.ResponseWriter.Write(b)
347348
}
349+
350+
func envString(key, defaultVal string) string {
351+
str := os.Getenv(key)
352+
if str == "" {
353+
return defaultVal
354+
}
355+
return str
356+
}
357+
358+
func envBool(key string, defaultVal bool) bool {
359+
str := os.Getenv(key)
360+
if str == "" {
361+
return defaultVal
362+
}
363+
364+
v, err := strconv.ParseBool(str)
365+
if err != nil {
366+
slog.Error("invalid value for bool env", "key", key)
367+
os.Exit(1)
368+
}
369+
return v
370+
}
371+
372+
func envInt(key string, defaultVal int) int {
373+
str := os.Getenv(key)
374+
if str == "" {
375+
return defaultVal
376+
}
377+
378+
v, err := strconv.Atoi(str)
379+
if err != nil {
380+
slog.Error("invalid value for int env", "key", key)
381+
os.Exit(1)
382+
}
383+
return v
384+
}

0 commit comments

Comments
 (0)