interceptor.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package api
  2. import (
  3. "fmt"
  4. "net"
  5. "github.com/Sirupsen/logrus"
  6. "github.com/asaskevich/govalidator"
  7. "github.com/cad/ovpm"
  8. "github.com/cad/ovpm/permset"
  9. gcontext "golang.org/x/net/context"
  10. "google.golang.org/grpc"
  11. "google.golang.org/grpc/metadata"
  12. )
  13. // AuthUnaryInterceptor is a interceptor function.
  14. //
  15. // See https://godoc.org/google.golang.org/grpc#UnaryServerInterceptor.
  16. func AuthUnaryInterceptor(ctx gcontext.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  17. var enableAuthCheck bool
  18. md, ok := metadata.FromIncomingContext(ctx)
  19. if !ok {
  20. return nil, fmt.Errorf("Expected 2 metadata items in context; got %v", md)
  21. }
  22. // We enable auth check if we find a non-loopback
  23. // or invalid IP in the headers coming from the grpc-gateway.
  24. for _, userAgentIP := range md["x-forwarded-for"] {
  25. // Check if the remote user IP addr is a proper IP addr.
  26. if !govalidator.IsIP(userAgentIP) {
  27. enableAuthCheck = true
  28. logrus.Debugf("grpc request user agent ip can not be fetched from x-forwarded-for metadata, enabling auth check module '%s'", userAgentIP)
  29. break
  30. }
  31. // Check if the remote user IP addr is a loopback IP addr.
  32. if ip := net.ParseIP(userAgentIP); !ip.IsLoopback() {
  33. enableAuthCheck = true
  34. logrus.Debugf("grpc request user agent ips include non-loopback ip, enabling auth check module '%s'", userAgentIP)
  35. break
  36. }
  37. // TODO(cad): We assume gRPC endpoints are for cli only therefore
  38. // we are listening only on looback IP.
  39. //
  40. // But if we decide use gRPC endpoints publicly, we need to add
  41. // extra checks against gRPC remote peer IP to test if the request
  42. // is coming from a remote peer IP or also from a loopback ip.
  43. }
  44. if !enableAuthCheck {
  45. logrus.Debugf("rpc: auth-check not enabled: %s", md["x-forwarded-for"])
  46. ctx = NewUsernameContext(ctx, "root")
  47. permissions := permset.New(ovpm.AdminPerms()...)
  48. ctx = permset.NewContext(ctx, permissions)
  49. }
  50. if enableAuthCheck {
  51. switch info.FullMethod {
  52. // AuthService methods
  53. case "/pb.AuthService/Status":
  54. return authRequired(ctx, req, handler)
  55. // UserService methods
  56. case "/pb.UserService/List":
  57. return authRequired(ctx, req, handler)
  58. case "/pb.UserService/Create":
  59. return authRequired(ctx, req, handler)
  60. case "/pb.UserService/Update":
  61. return authRequired(ctx, req, handler)
  62. case "/pb.UserService/Delete":
  63. return authRequired(ctx, req, handler)
  64. case "/pb.UserService/Renew":
  65. return authRequired(ctx, req, handler)
  66. case "/pb.UserService/GenConfig":
  67. return authRequired(ctx, req, handler)
  68. // VPNService methods
  69. case "/pb.VPNService/Status":
  70. return authRequired(ctx, req, handler)
  71. case "/pb.VPNService/Init":
  72. return authRequired(ctx, req, handler)
  73. case "/pb.VPNService/Update":
  74. return authRequired(ctx, req, handler)
  75. case "/pb.VPNService/Restart":
  76. return authRequired(ctx, req, handler)
  77. // NetworkService methods
  78. case "/pb.NetworkService/Create":
  79. return authRequired(ctx, req, handler)
  80. case "/pb.NetworkService/List":
  81. return authRequired(ctx, req, handler)
  82. case "/pb.NetworkService/Delete":
  83. return authRequired(ctx, req, handler)
  84. case "/pb.NetworkService/GetAllTypes":
  85. return authRequired(ctx, req, handler)
  86. case "/pb.NetworkService/GetAssociatedUsers":
  87. return authRequired(ctx, req, handler)
  88. case "/pb.NetworkService/Associate":
  89. return authRequired(ctx, req, handler)
  90. case "/pb.NetworkService/Dissociate":
  91. return authRequired(ctx, req, handler)
  92. default:
  93. logrus.Debugln("rpc: auth is not required for this endpoint: '%s'", info.FullMethod)
  94. }
  95. }
  96. return handler(ctx, req)
  97. }