This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for calling an external policy agent to control aut…
…horizing field level access for GraphQL requests. Fiexes #33
- Loading branch information
Showing
16 changed files
with
1,085 additions
and
24 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
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
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,77 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net" | ||
|
||
"github.com/golang/glog" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials" | ||
"google.golang.org/grpc/testdata" | ||
|
||
gw "github.com/chirino/graphql-gw/internal/gateway/policyagent/proto" | ||
) | ||
|
||
var ( | ||
tls = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP") | ||
certFile = flag.String("cert_file", "", "The TLS cert file") | ||
keyFile = flag.String("key_file", "", "The TLS key file") | ||
grpcPort = flag.Int("grpc-port", 10000, "The server port") | ||
) | ||
|
||
func run() error { | ||
var opts []grpc.ServerOption | ||
if *tls { | ||
if *certFile == "" { | ||
*certFile = testdata.Path("server1.pem") | ||
} | ||
if *keyFile == "" { | ||
*keyFile = testdata.Path("server1.key") | ||
} | ||
creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile) | ||
if err != nil { | ||
log.Fatalf("Failed to generate credentials %v", err) | ||
} | ||
opts = []grpc.ServerOption{grpc.Creds(creds)} | ||
} | ||
|
||
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", *grpcPort)) | ||
if err != nil { | ||
log.Fatalf("failed to listen: %v", err) | ||
} | ||
|
||
grpcServer := grpc.NewServer(opts...) | ||
gw.RegisterPolicyAgentServer(grpcServer, &server{}) | ||
|
||
log.Println("GRPC service available at:", lis.Addr()) | ||
return grpcServer.Serve(lis) | ||
|
||
} | ||
|
||
type server struct { | ||
} | ||
|
||
func (s *server) Check(ctx context.Context, req *gw.CheckRequest) (*gw.CheckResponse, error) { | ||
resp := gw.CheckResponse{} | ||
for i, f := range req.Graphql.GetFields() { | ||
if (i % 2) == 1 { | ||
resp.Fields = append(resp.Fields, &gw.GraphQLFieldResponse{ | ||
Path: f.Path, | ||
Error: "You are not allowed to access odd fields", | ||
}) | ||
} | ||
} | ||
return &resp, nil | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
defer glog.Flush() | ||
|
||
if err := run(); err != nil { | ||
glog.Fatal(err) | ||
} | ||
} |
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
Oops, something went wrong.