Skip to content

Commit

Permalink
refactor: Refactor for better maintainability
Browse files Browse the repository at this point in the history
Signed-off-by: Anurag Rajawat <[email protected]>
  • Loading branch information
anurag-rajawat committed Sep 16, 2024
1 parent d49b0a0 commit 419fd6e
Show file tree
Hide file tree
Showing 30 changed files with 1,135 additions and 1,305 deletions.
9 changes: 2 additions & 7 deletions protobuf/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
PROTOS:="sentryflow.proto sentryflow_metrics.proto"

## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

.PHONY: help
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
Expand All @@ -30,5 +25,5 @@ clean: ## Remove the generated code

.PHONY:
go-plugins: ## Download Go plugins locally if necessary.
@test -s protoc-gen-go || GOBIN=$(LOCALBIN) go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
@test -s protoc-gen-go-grpc || GOBIN=$(LOCALBIN) go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
@test -s protoc-gen-go || go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
@test -s protoc-gen-go-grpc || go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
2 changes: 1 addition & 1 deletion protobuf/golang/sentryflow.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions protobuf/golang/sentryflow_metrics.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions protobuf/python/sentryflow_metrics_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protobuf/sentryflow.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ message APILog {
int32 responseCode = 54;
}

// APILogV2 represents an event related to an API call, including metadata,
// APIEvent represents an event related to an API call, including metadata,
// source and destination workloads, and network information.
message APIEvent {
Metadata metadata = 1;
Expand Down
2 changes: 1 addition & 1 deletion protobuf/sentryflow_metrics.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ syntax = "proto3";

package protobuf;

option go_package = "SentryFlow/protobuf";
option go_package = "github.com/5GSEC/SentryFlow/protobuf";

message APIClassifierRequest {
repeated string API = 1;
Expand Down
5 changes: 3 additions & 2 deletions sentryflow/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ COPY sentryflow /sentryflow

RUN make -C /sentryflow build

FROM gcr.io/distroless/static-debian12
#FROM gcr.io/distroless/static-debian12

FROM redhat/ubi9-minimal
COPY --from=builder /sentryflow/bin/sentryflow /

CMD ["/sentryflow"]
ENTRYPOINT ["/sentryflow"]
53 changes: 53 additions & 0 deletions sentryflow/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cmd

import (
"context"

"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/5GSEC/SentryFlow/pkg/core"
"github.com/5GSEC/SentryFlow/pkg/util"
)

var (
configFilePath string
kubeConfig string
development bool
logger *zap.SugaredLogger
)

func init() {
RootCmd.PersistentFlags().StringVar(&configFilePath, "config", "", "config file path")
RootCmd.PersistentFlags().StringVar(&kubeConfig, "kubeconfig", "", "kubeconfig file path")
RootCmd.PersistentFlags().BoolVar(&development, "development", true, "run in development mode")
initLogger(development)
}

func initLogger(development bool) {
cfg := zap.NewProductionConfig()
cfg.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
if development {
cfg = zap.NewDevelopmentConfig()
cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
cfg.EncoderConfig.TimeKey = "timestamp"
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
coreLogger, _ := cfg.Build()
logger = coreLogger.Sugar()
}

var RootCmd = &cobra.Command{
Use: "sentryflow",
Run: func(cmd *cobra.Command, args []string) {
run()
},
}

func run() {
logBuildInfo()
ctx := context.WithValue(ctrl.SetupSignalHandler(), util.LoggerCtxKey, logger)
core.Run(ctx, configFilePath, kubeConfig)
}
20 changes: 20 additions & 0 deletions sentryflow/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"runtime"
"runtime/debug"
)

func logBuildInfo() {
info, _ := debug.ReadBuildInfo()
vcsRev := ""
vcsTime := ""
for _, s := range info.Settings {
if s.Key == "vcs.revision" {
vcsRev = s.Value
} else if s.Key == "vcs.time" {
vcsTime = s.Value
}
}
logger.Infof("Git commit: %s, build time: %s, build version: %s, go os/arch: %s/%s\n", vcsRev, vcsTime, info.Main.Version, runtime.GOOS, runtime.GOARCH)
}
23 changes: 23 additions & 0 deletions sentryflow/config/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
receivers:
port: 8081
serviceMeshes:
- name: istio-sidecar
enable: true
others:
- name: "optional"
# Either gRPC or HTTP not both
grpc:
url: localhost
port: 1234
http:
url: localhost
port: 4321

exporter:
grpc:
port: 8080

debug:
enable: false
pprof:
port: 6060
29 changes: 21 additions & 8 deletions sentryflow/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,42 @@ go 1.23

require (
github.com/5GSEC/SentryFlow/protobuf v0.0.0-00010101000000-000000000000
github.com/envoyproxy/go-control-plane v0.13.0
github.com/golang/protobuf v1.5.4
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
go.opentelemetry.io/proto/otlp v1.3.1
go.uber.org/zap v1.26.0
google.golang.org/grpc v1.66.2
google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.31.1
istio.io/api v1.23.1
istio.io/client-go v1.23.1
k8s.io/apimachinery v0.31.1
k8s.io/client-go v0.31.1
sigs.k8s.io/controller-runtime v0.19.0
)

require (
github.com/cncf/xds/go v0.0.0-20240905190251-b4127c9b8d78 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand All @@ -43,8 +49,11 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand All @@ -61,11 +70,15 @@ require (
golang.org/x/term v0.24.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/time v0.6.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.31.1 // indirect
k8s.io/apiextensions-apiserver v0.31.0 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240903163716-9e1beecbcb38 // indirect
k8s.io/utils v0.0.0-20240902221715-702e33fdd3c3 // indirect
Expand Down
Loading

0 comments on commit 419fd6e

Please sign in to comment.