Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Removed leaky abstraction #150

Merged
merged 4 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions clients/go/pkg/auditopt/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/abcxyz/lumberjack/clients/go/pkg/remote"
"github.com/abcxyz/lumberjack/clients/go/pkg/security"
"github.com/sethvargo/go-envconfig"
"google.golang.org/grpc"
"gopkg.in/yaml.v2"

alpb "github.com/abcxyz/lumberjack/clients/go/apis/v1alpha1"
Expand Down Expand Up @@ -102,20 +101,20 @@ func FromConfigFile(path string) audit.Option {
// s := grpc.NewServer(opt)
// ```
// TODO(#109): add streaming interceptor.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this still relevant to this func?

func WithInterceptorFromConfigFile(path string) (grpc.ServerOption, *audit.Client, error) {
func WithInterceptorFromConfigFile(path string) (*audit.Interceptor, error) {
fc, err := ioutil.ReadFile(path)
if err != nil {
return nil, nil, err
yolocs marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("failed to read config file: %w", err)
}
cfg := &alpb.Config{}
if err := yaml.Unmarshal(fc, cfg); err != nil {
return nil, nil, err
return nil, fmt.Errorf("failed to unmarshall file to yaml: %w", err)
}
if err := cfg.ValidateSecurityContext(); err != nil {
return nil, nil, err
return nil, err
raserva marked this conversation as resolved.
Show resolved Hide resolved
}
if err := setAndValidate(cfg); err != nil {
return nil, nil, err
return nil, err
}

interceptor := &audit.Interceptor{}
Expand All @@ -127,7 +126,7 @@ func WithInterceptorFromConfigFile(path string) (grpc.ServerOption, *audit.Clien
}
interceptor.SecurityContext = fromRawJWT
default:
return nil, nil, fmt.Errorf("no supported security context configured in config %+v", cfg)
return nil, fmt.Errorf("no supported security context configured in config %+v", cfg)
}

// Add audit rules to interceptor.
Expand All @@ -139,11 +138,11 @@ func WithInterceptorFromConfigFile(path string) (grpc.ServerOption, *audit.Clien
}
auditClient, err := audit.NewClient(auditOpt)
if err != nil {
return nil, nil, fmt.Errorf("failed to create audit client from config %+v: %w", cfg, err)
return nil, fmt.Errorf("failed to create audit client from config %+v: %w", cfg, err)
}
interceptor.Client = auditClient

return grpc.UnaryInterceptor(interceptor.UnaryInterceptor), auditClient, nil
return interceptor, nil
}

func clientFromConfig(c *audit.Client, cfg *alpb.Config) error {
Expand Down
2 changes: 1 addition & 1 deletion clients/go/pkg/auditopt/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ rules:
t.Fatal(err)
}

_, _, err := WithInterceptorFromConfigFile(path)
_, err := WithInterceptorFromConfigFile(path)
if diff := errutil.DiffSubstring(err, tc.wantErrSubstr); diff != "" {
t.Errorf("WithInterceptorFromConfigFile(path) got unexpected error substring: %v", diff)
}
Expand Down
12 changes: 7 additions & 5 deletions clients/go/test/grpc-app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,23 @@ func main() {
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
opt, c, err := auditopt.WithInterceptorFromConfigFile(auditopt.DefaultConfigFilePath)
interceptor, err := auditopt.WithInterceptorFromConfigFile(auditopt.DefaultConfigFilePath)
defer func() {
sethvargo marked this conversation as resolved.
Show resolved Hide resolved
if err := interceptor.Stop(); err != nil {
log.Fatalf("failed to stop interceptor: %v", err)
}
}()
if err != nil {
log.Fatalf("failed to setup audit interceptor: %v", err)
}
s := grpc.NewServer(opt)
s := grpc.NewServer(grpc.UnaryInterceptor(interceptor.UnaryInterceptor))
yolocs marked this conversation as resolved.
Show resolved Hide resolved
talkerpb.RegisterTalkerServer(s, &server{})
// Register the reflection service makes it easier for some clients.
reflection.Register(s)
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
if err := c.Stop(); err != nil {
capri-xiyue marked this conversation as resolved.
Show resolved Hide resolved
log.Fatalf("failed to stop audit client: %v", err)
}
}

type server struct {
Expand Down