|
| 1 | +/* |
| 2 | +Copyright 2022 The Numaproj Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package routes |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "net/http" |
| 23 | + |
| 24 | + "github.com/gin-gonic/gin" |
| 25 | + "github.com/numaproj/numaflow/pkg/shared/logging" |
| 26 | + v1 "github.com/numaproj/numaflow/server/apis/v1" |
| 27 | + "github.com/numaproj/numaflow/server/authn" |
| 28 | + "github.com/numaproj/numaflow/server/authz" |
| 29 | + "github.com/numaproj/numaflow/server/common" |
| 30 | +) |
| 31 | + |
| 32 | +// authMiddleware is the middleware for AuthN/AuthZ. |
| 33 | +// it ensures the user is authenticated and authorized |
| 34 | +// to execute the requested action before sending the request to the api handler. |
| 35 | +func authMiddleware(ctx context.Context, authorizer authz.Authorizer, dexAuthenticator authn.Authenticator, localUsersAuthenticator authn.Authenticator, authRouteMap authz.RouteMap) gin.HandlerFunc { |
| 36 | + |
| 37 | + return func(c *gin.Context) { |
| 38 | + |
| 39 | + log := logging.FromContext(ctx) |
| 40 | + var userInfo *authn.UserInfo |
| 41 | + |
| 42 | + loginType, err := c.Cookie(common.LoginCookieName) |
| 43 | + if err != nil { |
| 44 | + errMsg := fmt.Sprintf("Failed to get login type: %v", err) |
| 45 | + c.JSON(http.StatusUnauthorized, v1.NewNumaflowAPIResponse(&errMsg, nil)) |
| 46 | + c.Abort() |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + // Authenticate the user based on the login type. |
| 51 | + switch loginType { |
| 52 | + case "dex": |
| 53 | + userInfo, err = dexAuthenticator.Authenticate(c) |
| 54 | + case "local": |
| 55 | + userInfo, err = localUsersAuthenticator.Authenticate(c) |
| 56 | + default: |
| 57 | + errMsg := fmt.Sprintf("unidentified login type received: %v", loginType) |
| 58 | + c.JSON(http.StatusUnauthorized, v1.NewNumaflowAPIResponse(&errMsg, nil)) |
| 59 | + c.Abort() |
| 60 | + return |
| 61 | + } |
| 62 | + if err != nil { |
| 63 | + errMsg := fmt.Sprintf("Failed to authenticate user: %v", err) |
| 64 | + c.JSON(http.StatusUnauthorized, v1.NewNumaflowAPIResponse(&errMsg, nil)) |
| 65 | + c.Abort() |
| 66 | + return |
| 67 | + } |
| 68 | + // Check if the route requires authorization. |
| 69 | + if authRouteMap.GetRouteFromContext(c) != nil && authRouteMap.GetRouteFromContext(c).RequiresAuthZ { |
| 70 | + // Check if the user is authorized to execute the requested action. |
| 71 | + isAuthorized := authorizer.Authorize(c, userInfo) |
| 72 | + if isAuthorized { |
| 73 | + // If the user is authorized, continue the request. |
| 74 | + c.Next() |
| 75 | + } else { |
| 76 | + // If the user is not authorized, return an error. |
| 77 | + errMsg := "user is not authorized to execute the requested action" |
| 78 | + c.JSON(http.StatusForbidden, v1.NewNumaflowAPIResponse(&errMsg, nil)) |
| 79 | + c.Abort() |
| 80 | + } |
| 81 | + } else if authRouteMap.GetRouteFromContext(c) != nil && !authRouteMap.GetRouteFromContext(c).RequiresAuthZ { |
| 82 | + // If the route does not require AuthZ, skip the AuthZ check. |
| 83 | + c.Next() |
| 84 | + } else { |
| 85 | + // If the route is not present in the route map, return an error. |
| 86 | + log.Errorw("route not present in routeMap", "route", authz.GetRouteMapKey(c)) |
| 87 | + errMsg := "Invalid route" |
| 88 | + c.JSON(http.StatusForbidden, v1.NewNumaflowAPIResponse(&errMsg, nil)) |
| 89 | + c.Abort() |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments