Skip to content

Commit ec41560

Browse files
authored
Cherry pick #7965 #7945 to v1.69.x (#7996)
1 parent 3b328ba commit ec41560

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

examples/features/csm_observability/client/main.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
"google.golang.org/grpc"
3131
"google.golang.org/grpc/credentials/insecure"
3232
xdscreds "google.golang.org/grpc/credentials/xds"
33-
"google.golang.org/grpc/examples/features/proto/echo"
33+
pb "google.golang.org/grpc/examples/helloworld/helloworld"
3434
"google.golang.org/grpc/stats/opentelemetry"
3535
"google.golang.org/grpc/stats/opentelemetry/csm"
3636
_ "google.golang.org/grpc/xds" // To install the xds resolvers and balancers.
@@ -40,9 +40,12 @@ import (
4040
"go.opentelemetry.io/otel/sdk/metric"
4141
)
4242

43+
const defaultName = "world"
44+
4345
var (
4446
target = flag.String("target", "xds:///helloworld:50051", "the server address to connect to")
4547
prometheusEndpoint = flag.String("prometheus_endpoint", ":9464", "the Prometheus exporter endpoint")
48+
name = flag.String("name", defaultName, "Name to greet")
4649
)
4750

4851
func main() {
@@ -68,15 +71,15 @@ func main() {
6871
log.Fatalf("Failed to start NewClient: %v", err)
6972
}
7073
defer cc.Close()
71-
c := echo.NewEchoClient(cc)
74+
c := pb.NewGreeterClient(cc)
7275

7376
// Make an RPC every second. This should trigger telemetry to be emitted from
7477
// the client and the server.
7578
for {
7679
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
77-
r, err := c.UnaryEcho(ctx, &echo.EchoRequest{Message: "this is examples/opentelemetry"})
80+
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: *name})
7881
if err != nil {
79-
log.Printf("UnaryEcho failed: %v", err)
82+
log.Fatalf("Could not greet: %v", err)
8083
}
8184
fmt.Println(r)
8285
time.Sleep(time.Second)

examples/features/csm_observability/server/main.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ package main
2222
import (
2323
"context"
2424
"flag"
25-
"fmt"
2625
"log"
2726
"net"
2827
"net/http"
2928

3029
"google.golang.org/grpc"
3130
"google.golang.org/grpc/credentials/insecure"
3231
xdscreds "google.golang.org/grpc/credentials/xds"
33-
pb "google.golang.org/grpc/examples/features/proto/echo"
32+
pb "google.golang.org/grpc/examples/helloworld/helloworld"
3433
"google.golang.org/grpc/stats/opentelemetry"
3534
"google.golang.org/grpc/stats/opentelemetry/csm"
3635
"google.golang.org/grpc/xds"
@@ -45,13 +44,15 @@ var (
4544
prometheusEndpoint = flag.String("prometheus_endpoint", ":9464", "the Prometheus exporter endpoint")
4645
)
4746

48-
type echoServer struct {
49-
pb.UnimplementedEchoServer
47+
// server is used to implement helloworld.GreeterServer.
48+
type server struct {
49+
pb.UnimplementedGreeterServer
5050
addr string
5151
}
5252

53-
func (s *echoServer) UnaryEcho(_ context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {
54-
return &pb.EchoResponse{Message: fmt.Sprintf("%s (from %s)", req.Message, s.addr)}, nil
53+
// SayHello implements helloworld.GreeterServer
54+
func (s *server) SayHello(_ context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
55+
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
5556
}
5657

5758
func main() {
@@ -80,7 +81,7 @@ func main() {
8081
if err != nil {
8182
log.Fatalf("Failed to start xDS Server: %v", err)
8283
}
83-
pb.RegisterEchoServer(s, &echoServer{addr: ":" + *port})
84+
pb.RegisterGreeterServer(s, &server{addr: ":" + *port})
8485

8586
log.Printf("Serving on %s\n", *port)
8687

internal/xds/rbac/rbac_engine.go

+3
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ func newRPCData(ctx context.Context) (*rpcData, error) {
219219
if !ok {
220220
return nil, errors.New("missing method in incoming context")
221221
}
222+
// gRPC-Go strips :path from the headers given to the application, but RBAC should be
223+
// able to match against it.
224+
md[":path"] = []string{mn}
222225

223226
// The connection is needed in order to find the destination address and
224227
// port of the incoming RPC Call.

test/xds/xds_server_rbac_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,30 @@ func (s) TestRBACHTTPFilter(t *testing.T) {
478478
wantStatusEmptyCall: codes.PermissionDenied,
479479
wantStatusUnaryCall: codes.OK,
480480
},
481+
// This test tests an RBAC HTTP Filter which is configured to allow only
482+
// RPC's with certain paths ("UnaryCall") via the ":path" header. Only
483+
// unary calls passing through this RBAC HTTP Filter should proceed as
484+
// normal, and any others should be denied.
485+
{
486+
name: "allow-certain-path-by-header",
487+
rbacCfg: &rpb.RBAC{
488+
Rules: &v3rbacpb.RBAC{
489+
Action: v3rbacpb.RBAC_ALLOW,
490+
Policies: map[string]*v3rbacpb.Policy{
491+
"certain-path": {
492+
Permissions: []*v3rbacpb.Permission{
493+
{Rule: &v3rbacpb.Permission_Header{Header: &v3routepb.HeaderMatcher{Name: ":path", HeaderMatchSpecifier: &v3routepb.HeaderMatcher_ExactMatch{ExactMatch: "/grpc.testing.TestService/UnaryCall"}}}},
494+
},
495+
Principals: []*v3rbacpb.Principal{
496+
{Identifier: &v3rbacpb.Principal_Any{Any: true}},
497+
},
498+
},
499+
},
500+
},
501+
},
502+
wantStatusEmptyCall: codes.PermissionDenied,
503+
wantStatusUnaryCall: codes.OK,
504+
},
481505
// This test that a RBAC Config with nil rules means that every RPC is
482506
// allowed. This maps to the line "If absent, no enforcing RBAC policy
483507
// will be applied" from the RBAC Proto documentation for the Rules

0 commit comments

Comments
 (0)