yoake/cmd/server/server.go

100 lines
2.4 KiB
Go
Raw Permalink Normal View History

2022-11-07 04:45:02 -06:00
package main
import (
"flag"
"log"
2022-11-11 22:58:05 -06:00
"os"
2022-11-19 11:04:03 -06:00
"strconv"
2022-11-07 04:45:02 -06:00
2022-11-15 10:31:27 -06:00
"github.com/eternal-flame-AD/go-apparmor/apparmor"
"github.com/eternal-flame-AD/go-apparmor/apparmor/magic"
2022-11-07 04:45:02 -06:00
"github.com/eternal-flame-AD/yoake/config"
2022-11-11 16:15:22 -06:00
"github.com/eternal-flame-AD/yoake/internal/comm"
"github.com/eternal-flame-AD/yoake/internal/db"
2022-11-19 11:04:03 -06:00
"github.com/eternal-flame-AD/yoake/internal/filestore"
2022-11-07 04:45:02 -06:00
"github.com/eternal-flame-AD/yoake/server"
"github.com/eternal-flame-AD/yoake/server/vault"
"github.com/eternal-flame-AD/yoake/server/webroot"
2022-11-19 11:04:03 -06:00
"github.com/spf13/afero"
2022-11-07 04:45:02 -06:00
)
var (
flagConfig = flag.String("c", "config.yml", "config file")
)
2022-11-19 11:04:03 -06:00
func writePid(fs filestore.FS) error {
pid := []byte(strconv.Itoa(os.Getpid()))
return afero.WriteFile(fs, "yoake.pid", pid, 0644)
}
2022-11-07 04:45:02 -06:00
func init() {
flag.Parse()
config.ParseConfig(*flagConfig)
2022-11-19 11:04:03 -06:00
fs := filestore.NewFS(config.Config().FS.BasePath)
if err := writePid(fs); err != nil {
log.Panicf("failed to write pid: %v", err)
}
2022-11-11 16:15:22 -06:00
db, err := db.New(config.Config())
if err != nil {
log.Panicf("failed to initialize database: %v", err)
}
2022-11-19 11:04:03 -06:00
comm := comm.InitCommunicator(db)
2022-11-07 04:45:02 -06:00
conf := config.Config()
for host, handler := range conf.Hosts {
switch handler {
case "vault":
vault.Init(host)
case "webroot":
2022-11-19 11:04:03 -06:00
webroot.Init(host, comm, db, fs)
2022-11-07 04:45:02 -06:00
default:
log.Panicf("unknown handler for %s: %s", host, handler)
}
}
}
2022-11-11 22:58:05 -06:00
2022-11-07 04:45:02 -06:00
func main() {
listen := config.Config().Listen
2022-11-15 10:31:27 -06:00
Server := server.New()
2022-11-07 04:45:02 -06:00
if listen.Ssl.Use {
2022-11-11 22:58:05 -06:00
var sslCertBytes, sslKeyBytes []byte
2022-11-15 10:31:27 -06:00
readCerts := func() {
2022-11-11 22:58:05 -06:00
var err error
sslCertBytes, err = os.ReadFile(listen.Ssl.Cert)
if err != nil {
log.Panicf("failed to read ssl cert: %v", err)
}
sslKeyBytes, err = os.ReadFile(listen.Ssl.Key)
if err != nil {
log.Panicf("failed to read ssl key: %v", err)
}
2022-11-15 10:31:27 -06:00
}
magic, err := magic.Generate(nil)
if err != nil {
log.Panicf("failed to generate apparmor magic token: %v", err)
}
2022-11-11 22:58:05 -06:00
if listen.AppArmor.SSL != "" {
2022-11-15 10:31:27 -06:00
if err := apparmor.WithHat(listen.AppArmor.SSL, func() uint64 { return magic }, readCerts); err != nil {
log.Panicf("failed to read ssl cert/key with apparmor hat: %v", err)
}
2022-11-11 22:58:05 -06:00
// defensive programming, try read ssl key
if _, err := os.ReadFile(listen.Ssl.Key); err == nil {
log.Panicf("AppArmor profile set for SSL but I could still read %v!", listen.Ssl.Key)
}
2022-11-15 10:31:27 -06:00
} else {
readCerts()
2022-11-11 22:58:05 -06:00
}
2022-11-15 10:31:27 -06:00
log.Fatalln(Server.StartTLS(listen.Addr, sslCertBytes, sslKeyBytes))
2022-11-07 04:45:02 -06:00
} else {
2022-11-15 10:31:27 -06:00
log.Fatalln(Server.Start(listen.Addr))
2022-11-07 04:45:02 -06:00
}
}