4
0

restapi.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package httpservers
  2. import (
  3. "context"
  4. "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
  5. log "github.com/sirupsen/logrus"
  6. "google.golang.org/grpc"
  7. "google.golang.org/grpc/metadata"
  8. "google.golang.org/protobuf/encoding/protojson"
  9. "net/http"
  10. gw "github.com/OliveTin/OliveTin/gen/grpc"
  11. config "github.com/OliveTin/OliveTin/internal/config"
  12. cors "github.com/OliveTin/OliveTin/internal/cors"
  13. )
  14. var (
  15. cfg *config.Config
  16. )
  17. func parseHttpHeaderForAuth(req *http.Request) (string, string) {
  18. username, ok := req.Header[cfg.AuthHttpHeaderUsername]
  19. if !ok {
  20. log.Warnf("Config has AuthHttpHeaderUsername set to %v, but it was not found", cfg.AuthHttpHeaderUsername)
  21. return "", ""
  22. }
  23. if cfg.AuthHttpHeaderUserGroup != "" {
  24. usergroup, ok := req.Header[cfg.AuthHttpHeaderUserGroup]
  25. if ok {
  26. log.Debugf("HTTP Header Auth found a username and usergroup")
  27. return username[0], usergroup[0]
  28. } else {
  29. log.Warnf("Config has AuthHttpHeaderUserGroup set to %v, but it was not found", cfg.AuthHttpHeaderUserGroup)
  30. }
  31. }
  32. log.Debugf("HTTP Header Auth found a username, but usergroup is not being used")
  33. return username[0], ""
  34. }
  35. func parseRequestMetadata(ctx context.Context, req *http.Request) metadata.MD {
  36. username := ""
  37. usergroup := ""
  38. if cfg.AuthJwtCookieName != "" {
  39. username, usergroup = parseJwtCookie(req)
  40. }
  41. if cfg.AuthHttpHeaderUsername != "" {
  42. username, usergroup = parseHttpHeaderForAuth(req)
  43. }
  44. if len(cfg.AuthOAuth2Providers) > 0 {
  45. username, usergroup = parseOAuth2Cookie(req)
  46. }
  47. md := metadata.New(map[string]string{
  48. "username": username,
  49. "usergroup": usergroup,
  50. })
  51. log.Tracef("api request metadata: %+v", md)
  52. return md
  53. }
  54. func SetGlobalRestConfig(config *config.Config) {
  55. cfg = config
  56. }
  57. func startRestAPIServer(globalConfig *config.Config) error {
  58. cfg = globalConfig
  59. log.WithFields(log.Fields{
  60. "address": cfg.ListenAddressRestActions,
  61. }).Info("Starting REST API")
  62. mux := newMux()
  63. return http.ListenAndServe(cfg.ListenAddressRestActions, cors.AllowCors(mux))
  64. }
  65. func newMux() *runtime.ServeMux {
  66. // The MarshalOptions set some important compatibility settings for the webui. See below.
  67. mux := runtime.NewServeMux(
  68. runtime.WithMetadata(parseRequestMetadata),
  69. runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.HTTPBodyMarshaler{
  70. Marshaler: &runtime.JSONPb{
  71. MarshalOptions: protojson.MarshalOptions{
  72. UseProtoNames: false, // eg: canExec for js instead of can_exec from protobuf
  73. EmitUnpopulated: true, // Emit empty fields so that javascript does not get "undefined" when accessing fields with empty values.
  74. },
  75. },
  76. }),
  77. )
  78. ctx := context.Background()
  79. opts := []grpc.DialOption{grpc.WithInsecure()}
  80. err := gw.RegisterOliveTinApiServiceHandlerFromEndpoint(ctx, mux, cfg.ListenAddressGrpcActions, opts)
  81. if err != nil {
  82. log.Panicf("Could not register REST API Handler %v", err)
  83. }
  84. return mux
  85. }