Skip to content

Commit 8655d47

Browse files
authored
credentials/alts: Add example of authz in ALTS (#2814)
1 parent 263405c commit 8655d47

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

interop/alts/server/server.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package main
2121

2222
import (
23+
"context"
2324
"flag"
2425
"net"
2526
"strings"
@@ -29,6 +30,7 @@ import (
2930
"google.golang.org/grpc/grpclog"
3031
"google.golang.org/grpc/interop"
3132
testpb "google.golang.org/grpc/interop/grpc_testing"
33+
"google.golang.org/grpc/tap"
3234
)
3335

3436
const (
@@ -59,7 +61,25 @@ func main() {
5961
opts.HandshakerServiceAddress = *hsAddr
6062
}
6163
altsTC := alts.NewServerCreds(opts)
62-
grpcServer := grpc.NewServer(grpc.Creds(altsTC))
64+
grpcServer := grpc.NewServer(grpc.Creds(altsTC), grpc.InTapHandle(authz))
6365
testpb.RegisterTestServiceServer(grpcServer, interop.NewTestServer())
6466
grpcServer.Serve(lis)
6567
}
68+
69+
// authz shows how to access client information at the server side to perform
70+
// application-layer authorization checks.
71+
func authz(ctx context.Context, info *tap.Info) (context.Context, error) {
72+
authInfo, err := alts.AuthInfoFromContext(ctx)
73+
if err != nil {
74+
return nil, err
75+
}
76+
// Access all alts.AuthInfo data:
77+
grpclog.Infof("authInfo.ApplicationProtocol() = %v", authInfo.ApplicationProtocol())
78+
grpclog.Infof("authInfo.RecordProtocol() = %v", authInfo.RecordProtocol())
79+
grpclog.Infof("authInfo.SecurityLevel() = %v", authInfo.SecurityLevel())
80+
grpclog.Infof("authInfo.PeerServiceAccount() = %v", authInfo.PeerServiceAccount())
81+
grpclog.Infof("authInfo.LocalServiceAccount() = %v", authInfo.LocalServiceAccount())
82+
grpclog.Infof("authInfo.PeerRPCVersions() = %v", authInfo.PeerRPCVersions())
83+
grpclog.Infof("info.FullMethodName = %v", info.FullMethodName)
84+
return ctx, nil
85+
}

0 commit comments

Comments
 (0)