echo framework, downgrade privileges at which moment?

Viewed 86

I use echo framework for creation of my API server. It is started by systemd and needs root by default, in order to aquire ports below 1024. For security I like to downgrade privileges of my go program after the listening port has been aquired by echo framework.

I know how to downgrade, but I can not find a suitable event/callback for this? The problem is, that echo.Start() and echo.StartAutoTLS() do not come back. I can create a parallel thread and try to find some status value of my echo session telling me that the port was opened, but I can not find such status indication either.

How can I make sure that I get some code executed after the port is aquired (and know for sure)?

Until now I run a parallel go thread just before server creation and wait 5 seconds to do the downgrade then. It works so far, but this is hacky and I don't like it :-(

1 Answers

You can use e.ListenerAddr() to check if the port is open. It will return nil until the port is open.

func degradePrivileges(e *echo.Echo, userName string) { 
    for { adr := e.ListenerAddr() if adr != nil { 
        degradeMe(userName) break 
    } 
    time.Sleep(100 * time.Millisecond) } 
} 
Related