Prevent running as root user
All checks were successful
Go Test / test (push) Successful in 1m5s

This commit is contained in:
Fred Boniface 2024-04-16 19:20:19 +01:00
parent 9ef9429511
commit ff98adf1a6
2 changed files with 24 additions and 2 deletions

View File

@ -5,4 +5,5 @@ RUN go build .
FROM scratch FROM scratch
COPY --from=builder /source/timetable-mgr /bin/timetable-mgr COPY --from=builder /source/timetable-mgr /bin/timetable-mgr
USER nobody
CMD [ "/bin/timetable-mgr" ] CMD [ "/bin/timetable-mgr" ]

25
main.go
View File

@ -19,9 +19,19 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
const (
bold = "\033[1m"
redB = "\033[1;31m"
blue = "\033[32m" //Actually green!
cyan = "\033[36m"
reset = "\033[0m"
)
func init() { func init() {
printStartupBanner() printStartupBanner()
fmt.Printf("Version %s \n\n", helpers.Version) fmt.Printf("%sVersion %s \n\n%s", bold+blue, helpers.Version, reset)
checkRunAsRoot()
} }
func main() { func main() {
@ -110,5 +120,16 @@ func printStartupBanner() {
|___/ |___/
` `
fmt.Println(art) fmt.Println(cyan + art + reset)
}
func checkRunAsRoot() {
uid := os.Getuid()
if uid != 0 {
return
}
fmt.Println(redB + "This program must not be run as the root user" + reset)
fmt.Println("")
os.Exit(1)
} }