Skip to content

Commit

Permalink
address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
gertd committed Nov 15, 2024
1 parent 04d25f0 commit 57526a7
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 200 deletions.
15 changes: 6 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/aserto-dev/topaz

go 1.23.3

replace github.com/aserto-dev/service-host => ./internal/pkg/service/builder

replace github.com/adrg/xdg => ./internal/pkg/xdg

require (
Expand All @@ -27,16 +25,18 @@ require (
github.com/aserto-dev/openapi-directory v0.31.2
github.com/aserto-dev/runtime v0.69.1
github.com/aserto-dev/self-decision-logger v0.0.8
github.com/aserto-dev/service-host v0.0.0-00010101000000-000000000000
github.com/cli/browser v1.3.0
github.com/docker/docker v27.1.1+incompatible
github.com/docker/go-connections v0.5.0
github.com/fatih/color v1.18.0
github.com/fullstorydev/grpcurl v1.9.1
github.com/gdamore/tcell/v2 v2.7.4
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a
github.com/google/uuid v1.6.0
github.com/google/wire v0.6.0
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0
github.com/itchyny/gojq v0.12.16
github.com/lestrrat-go/jwx/v2 v2.1.2
Expand All @@ -50,9 +50,12 @@ require (
github.com/open-policy-agent/opa v0.69.0
github.com/opencontainers/image-spec v1.1.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.20.5
github.com/rivo/tview v0.0.0-20241103174730-c76f7879f592
github.com/rs/cors v1.11.1
github.com/rs/zerolog v1.33.0
github.com/samber/lo v1.47.0
github.com/slok/go-http-metrics v0.13.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -98,7 +101,6 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gdamore/encoding v1.0.1 // indirect
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -110,9 +112,7 @@ require (
github.com/google/cel-go v0.21.0 // indirect
github.com/google/subcommands v1.2.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand Down Expand Up @@ -153,20 +153,17 @@ require (
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.60.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rs/cors v1.11.1 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/slok/go-http-metrics v0.13.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ go 1.23.3

use (
.
./internal/pkg/service/builder
./internal/pkg/xdg
)
46 changes: 0 additions & 46 deletions internal/pkg/service/builder/go.mod

This file was deleted.

90 changes: 0 additions & 90 deletions internal/pkg/service/builder/go.sum

This file was deleted.

2 changes: 1 addition & 1 deletion internal/pkg/service/builder/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type GRPCRegistrations func(server *grpc.Server)

type HandlerRegistrations func(ctx context.Context, mux *runtime.ServeMux, grpcEndpoint string, opts []grpc.DialOption) error

type Server struct {
type Service struct {
Config *API
Server *grpc.Server
Listener net.Listener
Expand Down
24 changes: 7 additions & 17 deletions internal/pkg/service/builder/service_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"

"github.com/aserto-dev/go-aserto"
"github.com/samber/lo"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/pkg/errors"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (f *ServiceFactory) CreateService(
grpcOpts *GRPCOptions,
gatewayOpts *GatewayOptions,
cleanup ...func(),
) (*Server, error) {
) (*Service, error) {
grpcServer, err := prepareGrpcServer(&config.GRPC.Certs, grpcOpts.ServerOptions)
if err != nil {
return nil, err
Expand All @@ -77,7 +78,7 @@ func (f *ServiceFactory) CreateService(
}
}

return &Server{
return &Service{
Config: config,
Server: grpcServer,
Listener: listener,
Expand Down Expand Up @@ -109,7 +110,7 @@ func (f *ServiceFactory) prepareGateway(config *API, gatewayOpts *GatewayOptions
runtimeMux := f.gatewayMux(config.Gateway.AllowedHeaders, gatewayOpts.ErrorHandler)

opts := []grpc.DialOption{}
if TLS(&config.GRPC.Certs) {
if config.GRPC.Certs.HasCert() {
tlsCreds, err := config.GRPC.Certs.ClientCredentials(true)
if err != nil {
return Gateway{}, errors.Wrapf(err, "failed to get TLS credentials")
Expand Down Expand Up @@ -141,9 +142,7 @@ func (f *ServiceFactory) prepareGateway(config *API, gatewayOpts *GatewayOptions
IdleTimeout: config.Gateway.IdleTimeout,
}

if NoTLS(&config.Gateway.Certs) {
config.Gateway.HTTP = true
}
config.Gateway.HTTP = !config.Gateway.Certs.HasCert()

if !config.Gateway.HTTP {
tlsServerConfig, err := config.Gateway.Certs.ServerConfig()
Expand All @@ -161,7 +160,7 @@ func (f *ServiceFactory) prepareGateway(config *API, gatewayOpts *GatewayOptions
func (f *ServiceFactory) gatewayMux(allowedHeaders []string, errorHandler runtime.ErrorHandlerFunc) *runtime.ServeMux {
opts := []runtime.ServeMuxOption{
runtime.WithIncomingHeaderMatcher(func(key string) (string, bool) {
if contains(allowedHeaders, key) {
if lo.Contains(allowedHeaders, key) {
return key, true
}
return runtime.DefaultHeaderMatcher(key)
Expand Down Expand Up @@ -236,7 +235,7 @@ func httpResponseModifier(ctx context.Context, w http.ResponseWriter, p proto.Me
// prepareGrpcServer provides a new grpc server with the provided grpc.ServerOptions using the provided certificates.
func prepareGrpcServer(certCfg *aserto.TLSConfig, opts []grpc.ServerOption) (*grpc.Server, error) {
// NoTLS path.
if NoTLS(certCfg) {
if !certCfg.HasCert() {
opts = append(opts, grpc.Creds(insecure.NewCredentials()))
return grpc.NewServer(opts...), nil
}
Expand Down Expand Up @@ -266,15 +265,6 @@ func fieldsMaskHandler(h http.Handler) http.Handler {
})
}

func contains[T comparable](slice []T, item T) bool {
for i := range slice {
if slice[i] == item {
return true
}
}
return false
}

type key int

var pathPatternKey key
Expand Down
19 changes: 9 additions & 10 deletions internal/pkg/service/builder/service_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type ServiceManager struct {
logger *zerolog.Logger
errGroup *errgroup.Group

Servers map[string]*Server
Servers map[string]*Service
DependencyMap map[string][]string
HealthServer *Health
MetricServer *http.Server
Expand All @@ -42,7 +42,7 @@ func NewServiceManager(logger *zerolog.Logger) *ServiceManager {
return &ServiceManager{
Context: ctx,
logger: &serviceLogger,
Servers: make(map[string]*Server),
Servers: make(map[string]*Service),
DependencyMap: make(map[string][]string),
errGroup: errGroup,
shutdownTimeout: 30,
Expand All @@ -54,7 +54,7 @@ func (s *ServiceManager) WithShutdownTimeout(seconds int) *ServiceManager {
return s
}

func (s *ServiceManager) AddGRPCServer(server *Server) error {
func (s *ServiceManager) AddGRPCServer(server *Service) error {
s.Servers[server.Config.GRPC.ListenAddress] = server
return nil
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func (s *ServiceManager) SetupMetricsServer(address string, certCfg *aserto.TLSC

s.logger.Info().Msgf("Starting %s metric server", address)

if NoTLS(certCfg) {
if !certCfg.HasCert() {
s.errGroup.Go(metric.ListenAndServe)
} else {
s.errGroup.Go(func() error {
Expand Down Expand Up @@ -163,14 +163,13 @@ func (s *ServiceManager) StartServers(ctx context.Context) error {
if httpServer.Server != nil {
s.errGroup.Go(func() error {
s.logger.Info().Msgf("Starting %s gateway server", httpServer.Server.Addr)
if NoTLS(httpServer.Certs) {
err := httpServer.Server.ListenAndServe()
if httpServer.Certs.HasCert() {
err := httpServer.Server.ListenAndServeTLS(httpServer.Certs.Cert, httpServer.Certs.Key)
if err != nil {
return err
}
}
if TLS(httpServer.Certs) {
err := httpServer.Server.ListenAndServeTLS(httpServer.Certs.Cert, httpServer.Certs.Key)
} else {
err := httpServer.Server.ListenAndServe()
if err != nil {
return err
}
Expand All @@ -190,7 +189,7 @@ func (s *ServiceManager) logDetails(address string, element interface{}) {
ref := reflect.ValueOf(element).Elem()
typeOfT := ref.Type()

for i := 0; i < ref.NumField(); i++ {
for i := range ref.NumField() {
f := ref.Field(i)
s.logger.Debug().Str("address", address).Msgf("%s = %v\n", typeOfT.Field(i).Name, f.Interface())
}
Expand Down
11 changes: 0 additions & 11 deletions internal/pkg/service/builder/tls.go

This file was deleted.

Loading

0 comments on commit 57526a7

Please sign in to comment.