Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
Server listen addr from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
nacx committed Feb 9, 2024
1 parent 768910b commit 1782814
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 56 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
configFile = &internal.LocalConfigFile{}
logging = internal.NewLogSystem(log.New(), &configFile.Config)
authz = server.NewExtAuthZFilter(&configFile.Config)
authzServer = server.New(authz.Register)
authzServer = server.New(&configFile.Config, authz.Register)
)

g := run.Group{Logger: internal.Logger(internal.Default)}
Expand Down
3 changes: 3 additions & 0 deletions e2e/mock/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Force run of the e2e tests
E2E_TEST_OPTS ?= -count=1


.PHONY: e2e
e2e: e2e-pre
Expand Down
2 changes: 2 additions & 0 deletions e2e/mock/authz-config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"listen_address": "0.0.0.0",
"listen_port": 10004,
"log_level": "debug",
"chains": [
{
Expand Down
6 changes: 3 additions & 3 deletions internal/server/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ func (e *ExtAuthZFilter) Check(ctx context.Context, req *envoy.CheckRequest) (re
if !ok {
return deny(codes.PermissionDenied, fmt.Sprintf("%s[%d] filter denied the request", c.Name, i)), nil
}

// Use the first filter chain that matches
return allow, nil
}

// Return OK if the chain matched and all filters allowed the request
return allow, nil
}

if e.cfg.AllowUnmatchedRequests {
Expand Down
40 changes: 11 additions & 29 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/tetratelabs/telemetry"
"google.golang.org/grpc"

configv1 "github.com/tetrateio/authservice-go/config/gen/go/v1"
"github.com/tetrateio/authservice-go/internal"
)

Expand All @@ -33,18 +34,16 @@ type RegisterGrpc interface {
}

var (
_ run.Initializer = (*Server)(nil)
_ run.Config = (*Server)(nil)
_ run.PreRunner = (*Server)(nil)
_ run.Service = (*Server)(nil)
_ run.PreRunner = (*Server)(nil)
_ run.Service = (*Server)(nil)
)

var ErrInvalidAddress = errors.New("invalid address")

// Server that runs as a unit in a run.Group.
type Server struct {
log telemetry.Logger
addr string
log telemetry.Logger
cfg *configv1.Config

server *grpc.Server
registerHandlers []func(s *grpc.Server)
Expand All @@ -55,42 +54,25 @@ type Server struct {
}

// New creates a new dual gRPC server.
func New(registerHandlers ...func(s *grpc.Server)) *Server {
func New(cfg *configv1.Config, registerHandlers ...func(s *grpc.Server)) *Server {
return &Server{
log: internal.Logger(internal.Server),
cfg: cfg,
registerHandlers: registerHandlers,
}
}

// Name returns the name of the unit in the run.Group.
func (s *Server) Name() string { return "gRPC Server" }

// FlagSet returns the flags used to customize the server.
func (s *Server) FlagSet() *run.FlagSet {
flags := run.NewFlagSet("gRPC Server flags")
flags.StringVar(&s.addr, "listen-address", ":10004", "listen address")
return flags
}

// Validate the server configuration.
func (s *Server) Validate() error {
if _, _, err := net.SplitHostPort(s.addr); err != nil {
return fmt.Errorf("%w: %w", ErrInvalidAddress, err)
}
return nil
}

// Initialize the server.
func (s *Server) Initialize() {
// PreRun registers the server registerHandlers
func (s *Server) PreRun() error {
if s.Listen == nil {
s.Listen = func() (net.Listener, error) {
return net.Listen("tcp", s.addr)
return net.Listen("tcp", fmt.Sprintf("%s:%d", s.cfg.ListenAddress, s.cfg.ListenPort))
}
}
}

// PreRun registers the server registerHandlers
func (s *Server) PreRun() error {
logMiddleware := NewLogMiddleware()

// Initialize the gRPC server
Expand All @@ -112,7 +94,7 @@ func (s *Server) Serve() error {
if err != nil {
return err
}
s.log.Info("starting gRPC server", "addr", s.addr)
s.log.Info("starting gRPC server", "addr", l.Addr())
return s.server.Serve(l)
}

Expand Down
24 changes: 1 addition & 23 deletions internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,12 @@ import (
"google.golang.org/grpc/test/bufconn"
)

func TestValidate(t *testing.T) {
tests := []struct {
name string
addr string
err error
}{
{"empty", "", ErrInvalidAddress},
{"no-port", "localhost", ErrInvalidAddress},
{"invalid", "::9090", ErrInvalidAddress},
{"ipv4", "1.2.3.4:9090", nil},
{"ipv6", "[::1]:9090", nil},
{"hostname", "localhost:9090", nil},
{"any-addr", ":9090", nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.ErrorIs(t, (&Server{addr: tt.addr}).Validate(), tt.err)
})
}
}

func TestServer(t *testing.T) {
var (
g = run.Group{Logger: telemetry.NoopLogger()}
irq = test.NewIRQService(func() {})
l = bufconn.Listen(1024)
s = New(func(s *grpc.Server) {
s = New(nil, func(s *grpc.Server) {
testgrpc.RegisterTestServiceServer(s, interop.NewTestServer())
})
)
Expand Down

0 comments on commit 1782814

Please sign in to comment.