Add config

This commit is contained in:
2021-12-04 03:25:09 +01:00
parent d53235d8ef
commit 377962440c
13 changed files with 326 additions and 128 deletions

View File

@@ -15,6 +15,7 @@ import (
"time"
"gitea.benny.dog/torjus/ezshare/certs"
"gitea.benny.dog/torjus/ezshare/config"
"gitea.benny.dog/torjus/ezshare/pb"
"gitea.benny.dog/torjus/ezshare/server"
"gitea.benny.dog/torjus/ezshare/store"
@@ -26,6 +27,12 @@ import (
func main() {
app := cli.App{
Name: "ezshare",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "Path to config-file.",
},
},
Commands: []*cli.Command{
{
Name: "serve",
@@ -79,15 +86,21 @@ func main() {
},
},
{
Name: "gencerts",
Usage: "Generate certificates",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "out-dir",
Usage: "Directory where certificates will be stored.",
Name: "cert",
Usage: "Certificate commands",
Subcommands: []*cli.Command{
{
Name: "gen-all",
Usage: "Generate CA, Server and Client certificates",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "out-dir",
Usage: "Directory where certificates will be stored.",
},
},
Action: ActionGencerts,
},
},
Action: ActionGencerts,
},
},
}
@@ -100,6 +113,25 @@ func main() {
}
func ActionServe(c *cli.Context) error {
cfg, err := getConfig(c)
if err != nil {
return err
}
// Read certificates
srvCertBytes, err := cfg.Server.GRPC.Certs.GetCertBytes()
if err != nil {
return err
}
srvKeyBytes, err := cfg.Server.GRPC.Certs.GetKeyBytes()
if err != nil {
return err
}
caCertBytes, err := cfg.Server.GRPC.CACerts.GetCertBytes()
if err != nil {
return err
}
fileStore := store.NewMemoryFileStore()
// Setup shutdown-handling
rootCtx, rootCancel := signal.NotifyContext(context.Background(), os.Interrupt)
@@ -115,7 +147,7 @@ func ActionServe(c *cli.Context) error {
// Start grpc server
go func() {
grpcAddr := ":50051"
grpcAddr := cfg.Server.GRPC.ListenAddr
if c.IsSet("grpc-addr") {
grpcAddr = c.String("grpc-addr")
}
@@ -130,14 +162,13 @@ func ActionServe(c *cli.Context) error {
log.Printf("Unable to setup grpc listener: %s\n", err)
rootCancel()
}
srvCert, err := tls.X509KeyPair(certs.SrvCert, certs.SrvKey)
srvCert, err := tls.X509KeyPair(srvCertBytes, srvKeyBytes)
if err != nil {
log.Printf("%d %d", len(certs.SrvCert), len(certs.SrvKey))
log.Printf("Unable load server certs: %s\n", err)
rootCancel()
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(certs.CACert) {
if !certPool.AppendCertsFromPEM(caCertBytes) {
log.Println("Unable to load CA cert")
rootCancel()
}
@@ -242,8 +273,17 @@ func ActionClientGet(c *cli.Context) error {
}
func ActionClientUpload(c *cli.Context) error {
addr := c.String("addr")
clientCreds, err := getClientCreds()
cfg, err := getConfig(c)
if err != nil {
return err
}
addr := cfg.Client.DefaultServer
if c.IsSet("addr") {
addr = c.String("addr")
}
clientCreds, err := cfg.Client.Creds()
if err != nil {
return err
}
@@ -279,22 +319,22 @@ func ActionClientUpload(c *cli.Context) error {
}
func ActionGencerts(c *cli.Context) error {
return certs.GenCerts()
outDir := "."
if c.IsSet("out-dir") {
outDir = c.String("out-dir")
}
return certs.GenAllCerts(outDir)
}
func getClientCreds() (credentials.TransportCredentials, error) {
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(certs.CACert) {
return nil, fmt.Errorf("unable to load ca cert")
func getConfig(c *cli.Context) (*config.Config, error) {
if c.IsSet("config") {
cfgPath := c.String("config")
return config.FromFile(cfgPath)
}
clientCert, err := tls.X509KeyPair(certs.ClientCert, certs.ClientKey)
if err != nil {
return nil, fmt.Errorf("unable to load client cert: %s", err)
cfg, err := config.FromDefaultLocations()
if err == nil {
fmt.Printf("Config loaded from %s\n", cfg.Location())
fmt.Printf("Config: %+v\n", cfg)
}
config := &tls.Config{
Certificates: []tls.Certificate{clientCert},
RootCAs: certPool,
}
return credentials.NewTLS(config), nil
return cfg, err
}