-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
- Loading branch information
1 parent
19d3da1
commit d3023bd
Showing
13 changed files
with
470 additions
and
393 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
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,27 @@ | ||
package authz | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
authv3 "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3" | ||
"github.com/kyverno/kyverno-envoy-plugin/pkg/server" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func NewGrpcServer(network, addr string) server.ServerFunc { | ||
return func(ctx context.Context) error { | ||
// create a server | ||
s := grpc.NewServer() | ||
// setup our authorization service | ||
svc := &service{} | ||
// register our authorization service | ||
authv3.RegisterAuthorizationServer(s, svc) | ||
// create a listener | ||
l, err := net.Listen(network, addr) | ||
if err != nil { | ||
return err | ||
} | ||
return server.RunGrpc(ctx, s, l) | ||
} | ||
} |
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,27 @@ | ||
package authz | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/kyverno/kyverno-envoy-plugin/pkg/server" | ||
"github.com/kyverno/kyverno-envoy-plugin/pkg/server/handlers" | ||
) | ||
|
||
func NewHttpServer(addr string) server.ServerFunc { | ||
return func(ctx context.Context) error { | ||
// create mux | ||
mux := http.NewServeMux() | ||
// register health check | ||
mux.Handle("/livez", handlers.Healthy(handlers.True)) | ||
// register ready check | ||
mux.Handle("/readyz", handlers.Ready(handlers.True)) | ||
// create server | ||
s := &http.Server{ | ||
Addr: addr, | ||
Handler: mux, | ||
} | ||
// run server | ||
return server.RunHttp(ctx, s, "", "") | ||
} | ||
} |
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,43 @@ | ||
package authz | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
authv3 "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3" | ||
typev3 "github.com/envoyproxy/go-control-plane/envoy/type/v3" | ||
"google.golang.org/genproto/googleapis/rpc/status" | ||
"google.golang.org/grpc/codes" | ||
) | ||
|
||
type service struct{} | ||
|
||
func (s *service) Check(ctx context.Context, req *authv3.CheckRequest) (*authv3.CheckResponse, error) { | ||
if r := allow(); r != nil { | ||
return r, nil | ||
} | ||
return deny("foo"), nil | ||
} | ||
|
||
func allow() *authv3.CheckResponse { | ||
return &authv3.CheckResponse{ | ||
Status: &status.Status{Code: int32(codes.OK)}, | ||
HttpResponse: &authv3.CheckResponse_OkResponse{}, | ||
DynamicMetadata: nil, | ||
} | ||
} | ||
|
||
func deny(denialReason string) *authv3.CheckResponse { | ||
return &authv3.CheckResponse{ | ||
Status: &status.Status{ | ||
Code: int32(codes.PermissionDenied), | ||
}, | ||
HttpResponse: &authv3.CheckResponse_DeniedResponse{ | ||
DeniedResponse: &authv3.DeniedHttpResponse{ | ||
Status: &typev3.HttpStatus{Code: typev3.StatusCode_Forbidden}, | ||
Body: fmt.Sprintf("Request denied by Kyverno JSON engine. Reason: %s", denialReason), | ||
}, | ||
}, | ||
DynamicMetadata: 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
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
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,32 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
|
||
"google.golang.org/grpc" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
) | ||
|
||
func RunGrpc(ctx context.Context, server *grpc.Server, listener net.Listener) error { | ||
defer fmt.Println("GRPC Server stopped") | ||
// create a wait group | ||
var group wait.Group | ||
// wait all tasks in the group are over | ||
defer group.Wait() | ||
// create a cancellable context | ||
ctx, cancel := context.WithCancel(ctx) | ||
// cancel context at the end | ||
defer cancel() | ||
// shutdown server when context is cancelled | ||
group.StartWithContext(ctx, func(ctx context.Context) { | ||
// wait context cancelled | ||
<-ctx.Done() | ||
fmt.Println("GRPC Server shutting down...") | ||
// gracefully shutdown server | ||
server.GracefulStop() | ||
}) | ||
fmt.Printf("GRPC Server starting at %s...\n", listener.Addr()) | ||
return server.Serve(listener) | ||
} |
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
Oops, something went wrong.