-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3288 from kobergj/BringAuditToMonorepo
Bring audit to monorepo
- Loading branch information
Showing
22 changed files
with
644 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/owncloud/ocis/audit/pkg/command" | ||
"github.com/owncloud/ocis/audit/pkg/config" | ||
) | ||
|
||
func main() { | ||
if err := command.Execute(config.DefaultConfig()); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package command | ||
|
||
import ( | ||
"github.com/owncloud/ocis/audit/pkg/config" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// Health is the entrypoint for the health command. | ||
func Health(cfg *config.Config) *cli.Command { | ||
return &cli.Command{ | ||
Name: "health", | ||
Usage: "Check health status", | ||
Action: func(c *cli.Context) error { | ||
// Not implemented | ||
return nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/owncloud/ocis/audit/pkg/config" | ||
"github.com/owncloud/ocis/ocis-pkg/clihelper" | ||
ociscfg "github.com/owncloud/ocis/ocis-pkg/config" | ||
"github.com/thejerf/suture/v4" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// GetCommands provides all commands for this service | ||
func GetCommands(cfg *config.Config) cli.Commands { | ||
return []*cli.Command{ | ||
// start this service | ||
Server(cfg), | ||
|
||
// interaction with this service | ||
|
||
// infos about this service | ||
Health(cfg), | ||
Version(cfg), | ||
} | ||
} | ||
|
||
// Execute is the entry point for the audit command. | ||
func Execute(cfg *config.Config) error { | ||
app := clihelper.DefaultApp(&cli.App{ | ||
Name: "audit", | ||
Usage: "starts audit service", | ||
Commands: GetCommands(cfg), | ||
}) | ||
|
||
cli.HelpFlag = &cli.BoolFlag{ | ||
Name: "help,h", | ||
Usage: "Show the help", | ||
} | ||
|
||
return app.Run(os.Args) | ||
} | ||
|
||
// SutureService allows for the audit command to be embedded and supervised by a suture supervisor tree. | ||
type SutureService struct { | ||
cfg *config.Config | ||
} | ||
|
||
// NewSutureService creates a new audit.SutureService | ||
func NewSutureService(cfg *ociscfg.Config) suture.Service { | ||
cfg.Settings.Commons = cfg.Commons | ||
return SutureService{ | ||
cfg: cfg.Audit, | ||
} | ||
} | ||
|
||
func (s SutureService) Serve(ctx context.Context) error { | ||
s.cfg.Context = ctx | ||
if err := Execute(s.cfg); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/asim/go-micro/plugins/events/nats/v4" | ||
"github.com/cs3org/reva/v2/pkg/events" | ||
"github.com/cs3org/reva/v2/pkg/events/server" | ||
"github.com/owncloud/ocis/audit/pkg/config" | ||
"github.com/owncloud/ocis/audit/pkg/config/parser" | ||
"github.com/owncloud/ocis/audit/pkg/logging" | ||
svc "github.com/owncloud/ocis/audit/pkg/service" | ||
"github.com/owncloud/ocis/audit/pkg/types" | ||
"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", | ||
Usage: fmt.Sprintf("start %s extension without runtime (unsupervised mode)", cfg.Service.Name), | ||
Category: "server", | ||
Before: func(c *cli.Context) error { | ||
return parser.ParseConfig(cfg) | ||
}, | ||
Action: func(c *cli.Context) error { | ||
logger := logging.Configure(cfg.Service.Name, cfg.Log) | ||
|
||
ctx := cfg.Context | ||
if ctx == nil { | ||
ctx = context.Background() | ||
} | ||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
|
||
evtsCfg := cfg.Events | ||
client, err := server.NewNatsStream(nats.Address(evtsCfg.Endpoint), nats.ClusterID(evtsCfg.Cluster)) | ||
if err != nil { | ||
return err | ||
} | ||
evts, err := events.Consume(client, evtsCfg.ConsumerGroup, types.RegisteredEvents()...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
svc.AuditLoggerFromConfig(ctx, cfg.Auditlog, evts, logger) | ||
return nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package command | ||
|
||
import ( | ||
"github.com/owncloud/ocis/audit/pkg/config" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// Version prints the service versions of all running instances. | ||
func Version(cfg *config.Config) *cli.Command { | ||
return &cli.Command{ | ||
Name: "version", | ||
Usage: "print the version of this binary and the running extension instances", | ||
Category: "info", | ||
Action: func(c *cli.Context) error { | ||
// not implemented | ||
return nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/owncloud/ocis/ocis-pkg/shared" | ||
) | ||
|
||
// Config combines all available configuration parts. | ||
type Config struct { | ||
*shared.Commons | ||
|
||
Service Service | ||
|
||
Log *Log `ocisConfig:"log"` | ||
Debug Debug `ocisConfig:"debug"` | ||
|
||
Events Events `ocisConfig:"events"` | ||
Auditlog Auditlog `ocisConfig:"auditlog"` | ||
|
||
Context context.Context | ||
} | ||
|
||
// Events combines the configuration options for the event bus. | ||
type Events struct { | ||
Endpoint string `ocisConfig:"events_endpoint" env:"AUDIT_EVENTS_ENDPOINT" desc:"the address of the streaming service"` | ||
Cluster string `ocisConfig:"events_cluster" env:"AUDIT_EVENTS_CLUSTER" desc:"the clusterID of the streaming service. Mandatory when using nats"` | ||
ConsumerGroup string `ocisConfig:"events_group" env:"AUDIT_EVENTS_GROUP" desc:"the customergroup of the service. One group will only get one vopy of an event"` | ||
} | ||
|
||
// Auditlog holds audit log information | ||
type Auditlog struct { | ||
LogToConsole bool `ocisConfig:"log_to_console" env:"AUDIT_LOG_TO_CONSOLE" desc:"logs to Stdout if true"` | ||
LogToFile bool `ocisConfig:"log_to_file" env:"AUDIT_LOG_TO_FILE" desc:"logs to file if true"` | ||
FilePath string `ocisConfig:"filepath" env:"AUDIT_FILEPATH" desc:"filepath to the logfile. Mandatory if LogToFile is true"` | ||
Format string `ocisConfig:"format" env:"AUDIT_FORMAT" desc:"log format. using json is advised"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package config | ||
|
||
// Debug defines the available debug configuration. | ||
type Debug struct { | ||
Addr string `ocisConfig:"addr" env:"AUDIT_DEBUG_ADDR"` | ||
Token string `ocisConfig:"token" env:"AUDIT_DEBUG_TOKEN"` | ||
Pprof bool `ocisConfig:"pprof" env:"AUDIT_DEBUG_PPROF"` | ||
Zpages bool `ocisConfig:"zpages" env:"AUDIT_DEBUG_ZPAGES"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package config | ||
|
||
func DefaultConfig() *Config { | ||
return &Config{ | ||
Service: Service{ | ||
Name: "audit", | ||
}, | ||
Events: Events{ | ||
Endpoint: "127.0.0.1:9233", | ||
Cluster: "test-cluster", | ||
ConsumerGroup: "audit", | ||
}, | ||
Auditlog: Auditlog{ | ||
LogToConsole: true, | ||
Format: "json", | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package config | ||
|
||
// Log defines the available log configuration. | ||
type Log struct { | ||
Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;AUDIT_LOG_LEVEL"` | ||
Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;AUDIT_LOG_PRETTY"` | ||
Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;AUDIT_LOG_COLOR"` | ||
File string `mapstructure:"file" env:"OCIS_LOG_FILE;AUDIT_LOG_FILE"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package parser | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/owncloud/ocis/audit/pkg/config" | ||
ociscfg "github.com/owncloud/ocis/ocis-pkg/config" | ||
|
||
"github.com/owncloud/ocis/ocis-pkg/config/envdecode" | ||
) | ||
|
||
// ParseConfig loads accounts configuration from known paths. | ||
func ParseConfig(cfg *config.Config) error { | ||
_, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// provide with defaults for shared logging, since we need a valid destination address for BindEnv. | ||
if cfg.Log == nil && cfg.Commons != nil && cfg.Commons.Log != nil { | ||
cfg.Log = &config.Log{ | ||
Level: cfg.Commons.Log.Level, | ||
Pretty: cfg.Commons.Log.Pretty, | ||
Color: cfg.Commons.Log.Color, | ||
File: cfg.Commons.Log.File, | ||
} | ||
} else if cfg.Log == nil { | ||
cfg.Log = &config.Log{} | ||
} | ||
|
||
// load all env variables relevant to the config in the current context. | ||
if err := envdecode.Decode(cfg); err != nil { | ||
// no environment variable set for this config is an expected "error" | ||
if !errors.Is(err, envdecode.ErrNoTargetFieldsAreSet) { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package config | ||
|
||
// Service defines the available service configuration. | ||
type Service struct { | ||
Name string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package logging | ||
|
||
import ( | ||
"github.com/owncloud/ocis/audit/pkg/config" | ||
"github.com/owncloud/ocis/ocis-pkg/log" | ||
) | ||
|
||
// LoggerFromConfig initializes a service-specific logger instance. | ||
func Configure(name string, cfg *config.Log) log.Logger { | ||
return log.NewLogger( | ||
log.Name(name), | ||
log.Level(cfg.Level), | ||
log.Pretty(cfg.Pretty), | ||
log.Color(cfg.Color), | ||
log.File(cfg.File), | ||
) | ||
} |
Oops, something went wrong.