2022-03-08 11:25:26 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
2022-03-09 10:11:27 +01:00
|
|
|
"context"
|
2022-10-12 14:56:47 +02:00
|
|
|
"crypto/tls"
|
|
|
|
|
"crypto/x509"
|
2022-03-08 11:25:26 +01:00
|
|
|
"fmt"
|
2022-10-12 14:56:47 +02:00
|
|
|
"os"
|
2022-03-08 11:25:26 +01:00
|
|
|
|
|
|
|
|
"github.com/cs3org/reva/v2/pkg/events"
|
2022-12-19 10:18:05 +01:00
|
|
|
"github.com/cs3org/reva/v2/pkg/events/stream"
|
2022-04-19 09:44:47 +02:00
|
|
|
"github.com/go-micro/plugins/v4/events/natsjs"
|
2022-08-18 08:57:27 +00:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
|
2022-10-12 14:56:47 +02:00
|
|
|
ociscrypto "github.com/owncloud/ocis/v2/ocis-pkg/crypto"
|
2022-06-20 16:49:06 +02:00
|
|
|
"github.com/owncloud/ocis/v2/services/audit/pkg/config"
|
|
|
|
|
"github.com/owncloud/ocis/v2/services/audit/pkg/config/parser"
|
|
|
|
|
"github.com/owncloud/ocis/v2/services/audit/pkg/logging"
|
|
|
|
|
svc "github.com/owncloud/ocis/v2/services/audit/pkg/service"
|
|
|
|
|
"github.com/owncloud/ocis/v2/services/audit/pkg/types"
|
2022-03-08 11:25:26 +01:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Server is the entrypoint for the server command.
|
|
|
|
|
func Server(cfg *config.Config) *cli.Command {
|
|
|
|
|
return &cli.Command{
|
|
|
|
|
Name: "server",
|
2022-08-18 10:59:35 +02:00
|
|
|
Usage: fmt.Sprintf("start the %s service without runtime (unsupervised mode)", cfg.Service.Name),
|
2022-03-08 11:25:26 +01:00
|
|
|
Category: "server",
|
|
|
|
|
Before: func(c *cli.Context) error {
|
2022-08-22 08:08:18 +02:00
|
|
|
return configlog.ReturnFatal(parser.ParseConfig(cfg))
|
2022-03-08 11:25:26 +01:00
|
|
|
},
|
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
|
logger := logging.Configure(cfg.Service.Name, cfg.Log)
|
|
|
|
|
|
2022-03-09 10:11:27 +01:00
|
|
|
ctx := cfg.Context
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
ctx = context.Background()
|
2022-03-08 11:25:26 +01:00
|
|
|
}
|
2022-03-09 10:11:27 +01:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
defer cancel()
|
2022-03-08 11:25:26 +01:00
|
|
|
|
2022-03-08 14:44:39 +01:00
|
|
|
evtsCfg := cfg.Events
|
2022-10-12 14:56:47 +02:00
|
|
|
|
2022-10-20 17:13:11 +02:00
|
|
|
var tlsConf *tls.Config
|
|
|
|
|
if evtsCfg.EnableTLS {
|
|
|
|
|
var rootCAPool *x509.CertPool
|
|
|
|
|
if evtsCfg.TLSRootCACertificate != "" {
|
|
|
|
|
rootCrtFile, err := os.Open(evtsCfg.TLSRootCACertificate)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-10-12 14:56:47 +02:00
|
|
|
|
2022-10-20 17:13:11 +02:00
|
|
|
rootCAPool, err = ociscrypto.NewCertPoolFromPEM(rootCrtFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
evtsCfg.TLSInsecure = false
|
2022-10-12 14:56:47 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-20 17:13:11 +02:00
|
|
|
tlsConf = &tls.Config{
|
2022-11-03 16:08:56 +01:00
|
|
|
MinVersion: tls.VersionTLS12,
|
2022-10-20 17:13:11 +02:00
|
|
|
InsecureSkipVerify: evtsCfg.TLSInsecure, //nolint:gosec
|
|
|
|
|
RootCAs: rootCAPool,
|
|
|
|
|
}
|
2022-10-12 14:56:47 +02:00
|
|
|
}
|
2022-12-19 10:18:05 +01:00
|
|
|
client, err := stream.Nats(
|
2022-10-12 14:56:47 +02:00
|
|
|
natsjs.TLSConfig(tlsConf),
|
2022-03-11 09:02:08 +01:00
|
|
|
natsjs.Address(evtsCfg.Endpoint),
|
|
|
|
|
natsjs.ClusterID(evtsCfg.Cluster),
|
|
|
|
|
)
|
2022-03-08 11:25:26 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-03-23 10:15:27 +01:00
|
|
|
evts, err := events.Consume(client, evtsCfg.ConsumerGroup, types.RegisteredEvents()...)
|
2022-03-08 11:25:26 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-03-08 14:44:39 +01:00
|
|
|
|
2022-03-09 10:11:27 +01:00
|
|
|
svc.AuditLoggerFromConfig(ctx, cfg.Auditlog, evts, logger)
|
2022-03-08 14:44:39 +01:00
|
|
|
return nil
|
2022-03-08 11:25:26 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|