|
| 1 | +package restHandler |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/devtron-labs/devtron/api/restHandler/common" |
| 6 | + "github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin" |
| 7 | + "github.com/devtron-labs/devtron/pkg/auth/user" |
| 8 | + "github.com/devtron-labs/devtron/pkg/configDiff" |
| 9 | + "github.com/devtron-labs/devtron/pkg/configDiff/bean" |
| 10 | + "github.com/devtron-labs/devtron/util/rbac" |
| 11 | + "github.com/gorilla/schema" |
| 12 | + "go.uber.org/zap" |
| 13 | + "gopkg.in/go-playground/validator.v9" |
| 14 | + "net/http" |
| 15 | +) |
| 16 | + |
| 17 | +type DeploymentConfigurationRestHandler interface { |
| 18 | + ConfigAutoComplete(w http.ResponseWriter, r *http.Request) |
| 19 | + GetConfigData(w http.ResponseWriter, r *http.Request) |
| 20 | +} |
| 21 | +type DeploymentConfigurationRestHandlerImpl struct { |
| 22 | + logger *zap.SugaredLogger |
| 23 | + userAuthService user.UserService |
| 24 | + validator *validator.Validate |
| 25 | + enforcerUtil rbac.EnforcerUtil |
| 26 | + deploymentConfigurationService configDiff.DeploymentConfigurationService |
| 27 | + enforcer casbin.Enforcer |
| 28 | +} |
| 29 | + |
| 30 | +func NewDeploymentConfigurationRestHandlerImpl(logger *zap.SugaredLogger, |
| 31 | + userAuthService user.UserService, |
| 32 | + enforcerUtil rbac.EnforcerUtil, |
| 33 | + deploymentConfigurationService configDiff.DeploymentConfigurationService, |
| 34 | + enforcer casbin.Enforcer, |
| 35 | +) *DeploymentConfigurationRestHandlerImpl { |
| 36 | + return &DeploymentConfigurationRestHandlerImpl{ |
| 37 | + logger: logger, |
| 38 | + userAuthService: userAuthService, |
| 39 | + enforcerUtil: enforcerUtil, |
| 40 | + deploymentConfigurationService: deploymentConfigurationService, |
| 41 | + enforcer: enforcer, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func (handler *DeploymentConfigurationRestHandlerImpl) ConfigAutoComplete(w http.ResponseWriter, r *http.Request) { |
| 46 | + userId, err := handler.userAuthService.GetLoggedInUser(r) |
| 47 | + if userId == 0 || err != nil { |
| 48 | + common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) |
| 49 | + return |
| 50 | + } |
| 51 | + appId, err := common.ExtractIntQueryParam(w, r, "appId", 0) |
| 52 | + if err != nil { |
| 53 | + return |
| 54 | + } |
| 55 | + envId, err := common.ExtractIntQueryParam(w, r, "envId", 0) |
| 56 | + if err != nil { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + //RBAC START |
| 61 | + token := r.Header.Get(common.TokenHeaderKey) |
| 62 | + object := handler.enforcerUtil.GetAppRBACNameByAppId(appId) |
| 63 | + ok := handler.enforcerUtil.CheckAppRbacForAppOrJob(token, object, casbin.ActionGet) |
| 64 | + if !ok { |
| 65 | + common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden) |
| 66 | + return |
| 67 | + } |
| 68 | + //RBAC END |
| 69 | + |
| 70 | + res, err := handler.deploymentConfigurationService.ConfigAutoComplete(appId, envId) |
| 71 | + if err != nil { |
| 72 | + handler.logger.Errorw("service err, ConfigAutoComplete ", "appId", appId, "envId", envId, "err", err) |
| 73 | + common.WriteJsonResp(w, err, nil, http.StatusInternalServerError) |
| 74 | + return |
| 75 | + } |
| 76 | + common.WriteJsonResp(w, err, res, http.StatusOK) |
| 77 | +} |
| 78 | + |
| 79 | +func (handler *DeploymentConfigurationRestHandlerImpl) GetConfigData(w http.ResponseWriter, r *http.Request) { |
| 80 | + userId, err := handler.userAuthService.GetLoggedInUser(r) |
| 81 | + if userId == 0 || err != nil { |
| 82 | + common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) |
| 83 | + return |
| 84 | + } |
| 85 | + configDataQueryParams, err := getConfigDataQueryParams(r) |
| 86 | + if err != nil { |
| 87 | + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + //RBAC START |
| 92 | + token := r.Header.Get(common.TokenHeaderKey) |
| 93 | + object := handler.enforcerUtil.GetAppRBACName(configDataQueryParams.AppName) |
| 94 | + ok := handler.enforcerUtil.CheckAppRbacForAppOrJob(token, object, casbin.ActionGet) |
| 95 | + if !ok { |
| 96 | + common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden) |
| 97 | + return |
| 98 | + } |
| 99 | + //RBAC END |
| 100 | + |
| 101 | + res, err := handler.deploymentConfigurationService.GetAllConfigData(r.Context(), configDataQueryParams) |
| 102 | + if err != nil { |
| 103 | + handler.logger.Errorw("service err, GetAllConfigData ", "err", err) |
| 104 | + common.WriteJsonResp(w, err, nil, http.StatusInternalServerError) |
| 105 | + return |
| 106 | + } |
| 107 | + res.IsAppAdmin = handler.enforceForAppAndEnv(configDataQueryParams.AppName, configDataQueryParams.EnvName, token, casbin.ActionUpdate) |
| 108 | + |
| 109 | + common.WriteJsonResp(w, nil, res, http.StatusOK) |
| 110 | +} |
| 111 | + |
| 112 | +func (handler *DeploymentConfigurationRestHandlerImpl) enforceForAppAndEnv(appName, envName string, token string, action string) bool { |
| 113 | + object := handler.enforcerUtil.GetAppRBACNameByAppName(appName) |
| 114 | + if ok := handler.enforcer.Enforce(token, casbin.ResourceApplications, action, object); !ok { |
| 115 | + return false |
| 116 | + } |
| 117 | + |
| 118 | + object = handler.enforcerUtil.GetEnvRBACNameByAppAndEnvName(appName, envName) |
| 119 | + if ok := handler.enforcer.Enforce(token, casbin.ResourceEnvironment, action, object); !ok { |
| 120 | + return false |
| 121 | + } |
| 122 | + return true |
| 123 | +} |
| 124 | +func getConfigDataQueryParams(r *http.Request) (*bean.ConfigDataQueryParams, error) { |
| 125 | + v := r.URL.Query() |
| 126 | + var decoder = schema.NewDecoder() |
| 127 | + decoder.IgnoreUnknownKeys(true) |
| 128 | + queryParams := bean.ConfigDataQueryParams{} |
| 129 | + err := decoder.Decode(&queryParams, v) |
| 130 | + if err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + |
| 134 | + return &queryParams, nil |
| 135 | +} |
0 commit comments