|
| 1 | +package scopedVariable |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "github.com/devtron-labs/devtron/api/restHandler/common" |
| 8 | + "github.com/devtron-labs/devtron/pkg/bean" |
| 9 | + "github.com/devtron-labs/devtron/pkg/pipeline" |
| 10 | + "github.com/devtron-labs/devtron/pkg/user" |
| 11 | + "github.com/devtron-labs/devtron/pkg/user/casbin" |
| 12 | + "github.com/devtron-labs/devtron/pkg/variables" |
| 13 | + "github.com/devtron-labs/devtron/pkg/variables/models" |
| 14 | + "github.com/devtron-labs/devtron/pkg/variables/utils" |
| 15 | + "github.com/devtron-labs/devtron/util" |
| 16 | + "github.com/devtron-labs/devtron/util/rbac" |
| 17 | + "go.uber.org/zap" |
| 18 | + "gopkg.in/go-playground/validator.v9" |
| 19 | + "net/http" |
| 20 | + "strconv" |
| 21 | +) |
| 22 | + |
| 23 | +type ScopedVariableRestHandler interface { |
| 24 | + CreateVariables(w http.ResponseWriter, r *http.Request) |
| 25 | + GetScopedVariables(w http.ResponseWriter, r *http.Request) |
| 26 | + GetJsonForVariables(w http.ResponseWriter, r *http.Request) |
| 27 | +} |
| 28 | + |
| 29 | +type ScopedVariableRestHandlerImpl struct { |
| 30 | + logger *zap.SugaredLogger |
| 31 | + userAuthService user.UserService |
| 32 | + validator *validator.Validate |
| 33 | + pipelineBuilder pipeline.PipelineBuilder |
| 34 | + enforcerUtil rbac.EnforcerUtil |
| 35 | + enforcer casbin.Enforcer |
| 36 | + scopedVariableService variables.ScopedVariableService |
| 37 | +} |
| 38 | +type JsonResponse struct { |
| 39 | + Manifest *models.ScopedVariableManifest `json:"manifest"` |
| 40 | + JsonSchema string `json:"jsonSchema"` |
| 41 | +} |
| 42 | + |
| 43 | +func NewScopedVariableRestHandlerImpl(logger *zap.SugaredLogger, userAuthService user.UserService, validator *validator.Validate, pipelineBuilder pipeline.PipelineBuilder, enforcerUtil rbac.EnforcerUtil, enforcer casbin.Enforcer, scopedVariableService variables.ScopedVariableService) *ScopedVariableRestHandlerImpl { |
| 44 | + return &ScopedVariableRestHandlerImpl{ |
| 45 | + logger: logger, |
| 46 | + userAuthService: userAuthService, |
| 47 | + validator: validator, |
| 48 | + pipelineBuilder: pipelineBuilder, |
| 49 | + enforcerUtil: enforcerUtil, |
| 50 | + enforcer: enforcer, |
| 51 | + scopedVariableService: scopedVariableService, |
| 52 | + } |
| 53 | +} |
| 54 | +func (handler *ScopedVariableRestHandlerImpl) CreateVariables(w http.ResponseWriter, r *http.Request) { |
| 55 | + decoder := json.NewDecoder(r.Body) |
| 56 | + userId, err := handler.userAuthService.GetLoggedInUser(r) |
| 57 | + if userId == 0 || err != nil { |
| 58 | + common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) |
| 59 | + return |
| 60 | + } |
| 61 | + request := models.VariableRequest{} |
| 62 | + decoder.UseNumber() |
| 63 | + err = decoder.Decode(&request) |
| 64 | + if err != nil { |
| 65 | + handler.logger.Errorw("request err, Save", "error", err, "request", request) |
| 66 | + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) |
| 67 | + return |
| 68 | + } |
| 69 | + request.UserId = userId |
| 70 | + |
| 71 | + // validate request |
| 72 | + err = handler.validator.Struct(request) |
| 73 | + if err != nil { |
| 74 | + handler.logger.Errorw("struct validation err in CreateVariables", "err", err, "request", request) |
| 75 | + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + payload := utils.ManifestToPayload(request.Manifest, userId) |
| 80 | + |
| 81 | + // not logging bean object as it contains sensitive data |
| 82 | + handler.logger.Infow("request payload received for variables") |
| 83 | + |
| 84 | + // RBAC enforcer applying |
| 85 | + isSuperAdmin, err := handler.userAuthService.IsSuperAdmin(int(userId)) |
| 86 | + if !isSuperAdmin || err != nil { |
| 87 | + if err != nil { |
| 88 | + handler.logger.Errorw("request err, CheckSuperAdmin", "err", err, "isSuperAdmin", isSuperAdmin) |
| 89 | + } |
| 90 | + common.WriteJsonResp(w, err, "Unauthorized User", http.StatusForbidden) |
| 91 | + return |
| 92 | + } |
| 93 | + //RBAC enforcer Ends |
| 94 | + err = handler.scopedVariableService.CreateVariables(payload) |
| 95 | + if err != nil { |
| 96 | + if errors.As(err, &models.ValidationError{}) { |
| 97 | + common.WriteJsonResp(w, err, nil, http.StatusNotAcceptable) |
| 98 | + } else { |
| 99 | + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) |
| 100 | + } |
| 101 | + return |
| 102 | + } |
| 103 | + common.WriteJsonResp(w, nil, nil, http.StatusOK) |
| 104 | +} |
| 105 | +func (handler *ScopedVariableRestHandlerImpl) GetScopedVariables(w http.ResponseWriter, r *http.Request) { |
| 106 | + userId, err := handler.userAuthService.GetLoggedInUser(r) |
| 107 | + if err != nil { |
| 108 | + common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) |
| 109 | + return |
| 110 | + } |
| 111 | + isSuperAdmin, err := handler.userAuthService.IsSuperAdmin(int(userId)) |
| 112 | + |
| 113 | + if err != nil { |
| 114 | + handler.logger.Errorw("request err, CheckSuperAdmin", "err", err, "isSuperAdmin", isSuperAdmin) |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + appIdQueryParam := r.URL.Query().Get("appId") |
| 119 | + var appId int |
| 120 | + appId, err = strconv.Atoi(appIdQueryParam) |
| 121 | + if err != nil { |
| 122 | + common.WriteJsonResp(w, err, "invalid appId", http.StatusBadRequest) |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + var scope models.Scope |
| 127 | + scopeQueryParam := r.URL.Query().Get("scope") |
| 128 | + if scopeQueryParam != "" { |
| 129 | + if err := json.Unmarshal([]byte(scopeQueryParam), &scope); err != nil { |
| 130 | + http.Error(w, "Invalid JSON format for 'scope' parameter", http.StatusBadRequest) |
| 131 | + return |
| 132 | + } |
| 133 | + } |
| 134 | + if scope.AppId != 0 && scope.AppId != appId { |
| 135 | + http.Error(w, "scope.AppId provided in scope is not equal to appId", http.StatusBadRequest) |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + token := r.Header.Get("token") |
| 140 | + var app *bean.CreateAppDTO |
| 141 | + app, err = handler.pipelineBuilder.GetApp(appId) |
| 142 | + if err != nil { |
| 143 | + handler.logger.Errorw("service err, GetScopedVariables", "err", err, "payload", scope.AppId, scope.EnvId, scope.ClusterId) |
| 144 | + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) |
| 145 | + return |
| 146 | + } |
| 147 | + handler.logger.Infow("request payload, GetScopedVariables", "payload", scope.AppId, scope.EnvId, scope.ClusterId) |
| 148 | + resourceName := handler.enforcerUtil.GetAppRBACName(app.AppName) |
| 149 | + if ok := handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, resourceName); !ok { |
| 150 | + common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden) |
| 151 | + return |
| 152 | + } |
| 153 | + var scopedVariableData []*models.ScopedVariableData |
| 154 | + |
| 155 | + scopedVariableData, err = handler.scopedVariableService.GetScopedVariables(scope, nil, isSuperAdmin) |
| 156 | + if err != nil { |
| 157 | + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) |
| 158 | + return |
| 159 | + } |
| 160 | + common.WriteJsonResp(w, nil, scopedVariableData, http.StatusOK) |
| 161 | + |
| 162 | +} |
| 163 | +func (handler *ScopedVariableRestHandlerImpl) GetJsonForVariables(w http.ResponseWriter, r *http.Request) { |
| 164 | + userId, err := handler.userAuthService.GetLoggedInUser(r) |
| 165 | + if userId == 0 || err != nil { |
| 166 | + common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) |
| 167 | + return |
| 168 | + } |
| 169 | + // not logging bean object as it contains sensitive data |
| 170 | + handler.logger.Infow("request payload received for variables") |
| 171 | + |
| 172 | + // RBAC enforcer applying |
| 173 | + isSuperAdmin, err := handler.userAuthService.IsSuperAdmin(int(userId)) |
| 174 | + if !isSuperAdmin || err != nil { |
| 175 | + if err != nil { |
| 176 | + handler.logger.Errorw("request err, CheckSuperAdmin", "err", err, "isSuperAdmin", isSuperAdmin) |
| 177 | + } |
| 178 | + common.WriteJsonResp(w, err, "Unauthorized User", http.StatusForbidden) |
| 179 | + return |
| 180 | + } |
| 181 | + //RBAC enforcer Ends |
| 182 | + //var payload *repository.Payload |
| 183 | + |
| 184 | + payload, err := handler.scopedVariableService.GetJsonForVariables() |
| 185 | + if err != nil { |
| 186 | + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) |
| 187 | + return |
| 188 | + } |
| 189 | + |
| 190 | + schema, err := util.GetSchemaFromType(models.ScopedVariableManifest{}) |
| 191 | + if err != nil { |
| 192 | + common.WriteJsonResp(w, err, "schema cannot be generated for manifest type", http.StatusInternalServerError) |
| 193 | + return |
| 194 | + } |
| 195 | + |
| 196 | + jsonResponse := JsonResponse{ |
| 197 | + JsonSchema: schema, |
| 198 | + } |
| 199 | + |
| 200 | + if payload != nil { |
| 201 | + manifest := utils.PayloadToManifest(*payload) |
| 202 | + jsonResponse.Manifest = &manifest |
| 203 | + } |
| 204 | + common.WriteJsonResp(w, nil, jsonResponse, http.StatusOK) |
| 205 | +} |
0 commit comments