Skip to content

Commit c93ad53

Browse files
Merge pull request #6136 from devtron-labs/secret-masking-fix
fix: Secret masking fix
2 parents ff8f917 + 8ef9681 commit c93ad53

File tree

18 files changed

+818
-171
lines changed

18 files changed

+818
-171
lines changed

Wire.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949
"github.com/devtron-labs/devtron/api/restHandler"
5050
"github.com/devtron-labs/devtron/api/restHandler/app/appInfo"
5151
appList2 "github.com/devtron-labs/devtron/api/restHandler/app/appList"
52+
configDiff2 "github.com/devtron-labs/devtron/api/restHandler/app/configDiff"
5253
pipeline3 "github.com/devtron-labs/devtron/api/restHandler/app/pipeline"
5354
pipeline2 "github.com/devtron-labs/devtron/api/restHandler/app/pipeline/configure"
5455
"github.com/devtron-labs/devtron/api/restHandler/app/pipeline/history"
@@ -61,6 +62,7 @@ import (
6162
app3 "github.com/devtron-labs/devtron/api/router/app"
6263
appInfo2 "github.com/devtron-labs/devtron/api/router/app/appInfo"
6364
"github.com/devtron-labs/devtron/api/router/app/appList"
65+
configDiff3 "github.com/devtron-labs/devtron/api/router/app/configDiff"
6466
pipeline5 "github.com/devtron-labs/devtron/api/router/app/pipeline"
6567
pipeline4 "github.com/devtron-labs/devtron/api/router/app/pipeline/configure"
6668
history2 "github.com/devtron-labs/devtron/api/router/app/pipeline/history"
@@ -702,10 +704,10 @@ func InitializeApp() (*App, error) {
702704
scopedVariable.NewScopedVariableRestHandlerImpl,
703705
wire.Bind(new(scopedVariable.ScopedVariableRestHandler), new(*scopedVariable.ScopedVariableRestHandlerImpl)),
704706

705-
router.NewDeploymentConfigurationRouter,
706-
wire.Bind(new(router.DeploymentConfigurationRouter), new(*router.DeploymentConfigurationRouterImpl)),
707-
restHandler.NewDeploymentConfigurationRestHandlerImpl,
708-
wire.Bind(new(restHandler.DeploymentConfigurationRestHandler), new(*restHandler.DeploymentConfigurationRestHandlerImpl)),
707+
configDiff3.NewDeploymentConfigurationRouter,
708+
wire.Bind(new(configDiff3.DeploymentConfigurationRouter), new(*configDiff3.DeploymentConfigurationRouterImpl)),
709+
configDiff2.NewDeploymentConfigurationRestHandlerImpl,
710+
wire.Bind(new(configDiff2.DeploymentConfigurationRestHandler), new(*configDiff2.DeploymentConfigurationRestHandlerImpl)),
709711
configDiff.NewDeploymentConfigurationServiceImpl,
710712
wire.Bind(new(configDiff.DeploymentConfigurationService), new(*configDiff.DeploymentConfigurationServiceImpl)),
711713

api/restHandler/DeploymentConfigurationRestHandler.go renamed to api/restHandler/app/configDiff/DeploymentConfigurationRestHandler.go

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package restHandler
1+
package configDiff
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"github.com/devtron-labs/devtron/api/restHandler/common"
78
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
@@ -10,6 +11,7 @@ import (
1011
"github.com/devtron-labs/devtron/pkg/configDiff/bean"
1112
util2 "github.com/devtron-labs/devtron/util"
1213
"github.com/devtron-labs/devtron/util/rbac"
14+
"github.com/gorilla/mux"
1315
"github.com/gorilla/schema"
1416
"go.uber.org/zap"
1517
"gopkg.in/go-playground/validator.v9"
@@ -20,6 +22,7 @@ import (
2022
type DeploymentConfigurationRestHandler interface {
2123
ConfigAutoComplete(w http.ResponseWriter, r *http.Request)
2224
GetConfigData(w http.ResponseWriter, r *http.Request)
25+
CompareCategoryWiseConfigData(w http.ResponseWriter, r *http.Request)
2326
}
2427
type DeploymentConfigurationRestHandlerImpl struct {
2528
logger *zap.SugaredLogger
@@ -143,3 +146,69 @@ func getConfigDataQueryParams(r *http.Request) (*bean.ConfigDataQueryParams, err
143146

144147
return &queryParams, nil
145148
}
149+
150+
func (handler *DeploymentConfigurationRestHandlerImpl) CompareCategoryWiseConfigData(w http.ResponseWriter, r *http.Request) {
151+
userId, err := handler.userAuthService.GetLoggedInUser(r)
152+
if userId == 0 || err != nil {
153+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
154+
return
155+
}
156+
vars := mux.Vars(r)
157+
configCategory := vars["resource"]
158+
159+
v := r.URL.Query()
160+
comparisonReqString := v.Get("compareConfig")
161+
var comparisonRequestDto bean.ComparisonRequestDto
162+
err = json.Unmarshal([]byte(comparisonReqString), &comparisonRequestDto)
163+
if err != nil {
164+
handler.logger.Errorw("error in unmarshalling stringified json query param", "err", err)
165+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
166+
return
167+
}
168+
err = validateComparisonRequest(configCategory, comparisonRequestDto)
169+
if err != nil {
170+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
171+
return
172+
}
173+
174+
comparisonRequestDto.UpdateUserIdInComparisonItems(userId)
175+
appName := comparisonRequestDto.GetAppName()
176+
177+
//RBAC START
178+
token := r.Header.Get(common.TokenHeaderKey)
179+
object := handler.enforcerUtil.GetAppRBACName(appName)
180+
181+
ok := handler.enforcerUtil.CheckAppRbacForAppOrJob(token, object, casbin.ActionGet)
182+
if !ok {
183+
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden)
184+
return
185+
}
186+
//RBAC END
187+
//isSuperAdmin is required to make decision if a sensitive data(as defined by super admin) needs to be redacted
188+
//or not while resolving scope variable.
189+
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionGet, "*")
190+
//userHasAdminAccess is required to mask secrets in the response after scope resolution.
191+
userHasAdminAccess := handler.checkIfUserHasAdminAccessForLeastPrivilegeEnv(token, comparisonRequestDto)
192+
193+
ctx := util2.SetSuperAdminInContext(r.Context(), isSuperAdmin)
194+
res, err := handler.deploymentConfigurationService.CompareCategoryWiseConfigData(ctx, comparisonRequestDto, userHasAdminAccess)
195+
if err != nil {
196+
handler.logger.Errorw("service err, CompareCategoryWiseConfigData ", "err", err)
197+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
198+
return
199+
}
200+
201+
common.WriteJsonResp(w, nil, res, http.StatusOK)
202+
}
203+
204+
// checkIfUserHasAdminAccessForLeastPrivilegeEnv computes if a user has admin access or not for all env,
205+
// if a user is non admin for at least one env then return false.
206+
func (handler *DeploymentConfigurationRestHandlerImpl) checkIfUserHasAdminAccessForLeastPrivilegeEnv(token string, comparisonRequestDto bean.ComparisonRequestDto) bool {
207+
for _, item := range comparisonRequestDto.ComparisonItems {
208+
userHadAdminAccess := handler.enforcer.Enforce(token, casbin.ResourceEnvironment, casbin.ActionGet, item.EnvName)
209+
if !userHadAdminAccess {
210+
return false
211+
}
212+
}
213+
return true
214+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package configDiff
2+
3+
import (
4+
"errors"
5+
"github.com/devtron-labs/devtron/pkg/configDiff/bean"
6+
)
7+
8+
var validConfigCategories = map[string]bool{bean.Secret.ToString(): true, bean.ConfigMap.ToString(): true, bean.DeploymentTemplate.ToString(): true, bean.PipelineStrategy.ToString(): true}
9+
var ErrInvalidConfigCategory = errors.New("invalid config category provided")
10+
var ErrInvalidComparisonItems = errors.New("invalid comparison items, only 2 items are supported for comparison")
11+
var ErrInvalidIndexValInComparisonItems = errors.New("invalid index values in comparison items")
12+
13+
func validateComparisonRequest(configCategory string, comparisonRequestDto bean.ComparisonRequestDto) error {
14+
if ok := validConfigCategories[configCategory]; !ok {
15+
return ErrInvalidConfigCategory
16+
}
17+
// comparison items expects exactly two items
18+
if len(comparisonRequestDto.ComparisonItems) != 2 {
19+
return ErrInvalidComparisonItems
20+
}
21+
// if index value is other than 0 or 1 then throw invalid index error
22+
if len(comparisonRequestDto.ComparisonItems) > 1 && (comparisonRequestDto.ComparisonItems[0].Index != 0 && comparisonRequestDto.ComparisonItems[1].Index != 1) {
23+
return ErrInvalidIndexValInComparisonItems
24+
}
25+
return nil
26+
}
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
package router
1+
package configDiff
22

33
import (
4-
"github.com/devtron-labs/devtron/api/restHandler"
4+
"github.com/devtron-labs/devtron/api/restHandler/app/configDiff"
55
"github.com/gorilla/mux"
66
)
77

88
type DeploymentConfigurationRouter interface {
9-
initDeploymentConfigurationRouter(configRouter *mux.Router)
9+
InitDeploymentConfigurationRouter(configRouter *mux.Router)
1010
}
1111

1212
type DeploymentConfigurationRouterImpl struct {
13-
deploymentGroupRestHandler restHandler.DeploymentConfigurationRestHandler
13+
deploymentGroupRestHandler configDiff.DeploymentConfigurationRestHandler
1414
}
1515

16-
func NewDeploymentConfigurationRouter(deploymentGroupRestHandler restHandler.DeploymentConfigurationRestHandler) *DeploymentConfigurationRouterImpl {
16+
func NewDeploymentConfigurationRouter(deploymentGroupRestHandler configDiff.DeploymentConfigurationRestHandler) *DeploymentConfigurationRouterImpl {
1717
router := &DeploymentConfigurationRouterImpl{
1818
deploymentGroupRestHandler: deploymentGroupRestHandler,
1919
}
2020
return router
2121
}
2222

23-
func (router DeploymentConfigurationRouterImpl) initDeploymentConfigurationRouter(configRouter *mux.Router) {
23+
func (router DeploymentConfigurationRouterImpl) InitDeploymentConfigurationRouter(configRouter *mux.Router) {
2424
configRouter.Path("/autocomplete").
2525
HandlerFunc(router.deploymentGroupRestHandler.ConfigAutoComplete).
2626
Methods("GET")
2727
configRouter.Path("/data").
2828
HandlerFunc(router.deploymentGroupRestHandler.GetConfigData).
2929
Methods("GET")
30+
configRouter.Path("/compare/{resource}").
31+
HandlerFunc(router.deploymentGroupRestHandler.CompareCategoryWiseConfigData).
32+
Methods("GET")
3033

3134
}

api/router/router.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/devtron-labs/devtron/api/module"
4040
"github.com/devtron-labs/devtron/api/restHandler/common"
4141
"github.com/devtron-labs/devtron/api/router/app"
42+
"github.com/devtron-labs/devtron/api/router/app/configDiff"
4243
"github.com/devtron-labs/devtron/api/server"
4344
"github.com/devtron-labs/devtron/api/team"
4445
terminal2 "github.com/devtron-labs/devtron/api/terminal"
@@ -114,7 +115,7 @@ type MuxRouter struct {
114115
rbacRoleRouter user.RbacRoleRouter
115116
scopedVariableRouter ScopedVariableRouter
116117
ciTriggerCron cron.CiTriggerCron
117-
deploymentConfigurationRouter DeploymentConfigurationRouter
118+
deploymentConfigurationRouter configDiff.DeploymentConfigurationRouter
118119
infraConfigRouter infraConfig.InfraConfigRouter
119120
argoApplicationRouter argoApplication.ArgoApplicationRouter
120121
fluxApplicationRouter fluxApplication2.FluxApplicationRouter
@@ -147,12 +148,12 @@ func NewMuxRouter(logger *zap.SugaredLogger,
147148
scopedVariableRouter ScopedVariableRouter,
148149
ciTriggerCron cron.CiTriggerCron,
149150
proxyRouter proxy.ProxyRouter,
150-
deploymentConfigurationRouter DeploymentConfigurationRouter,
151+
deploymentConfigurationRouter configDiff.DeploymentConfigurationRouter,
151152
infraConfigRouter infraConfig.InfraConfigRouter,
152153
argoApplicationRouter argoApplication.ArgoApplicationRouter,
153154
devtronResourceRouter devtronResource.DevtronResourceRouter,
154155
fluxApplicationRouter fluxApplication2.FluxApplicationRouter,
155-
) *MuxRouter {
156+
) *MuxRouter {
156157
r := &MuxRouter{
157158
Router: mux.NewRouter(),
158159
EnvironmentClusterMappingsRouter: EnvironmentClusterMappingsRouter,
@@ -298,7 +299,7 @@ func (r MuxRouter) Init() {
298299

299300
configRouter := r.Router.PathPrefix("/orchestrator/config").Subrouter()
300301
r.ConfigMapRouter.initConfigMapRouter(configRouter)
301-
r.deploymentConfigurationRouter.initDeploymentConfigurationRouter(configRouter)
302+
r.deploymentConfigurationRouter.InitDeploymentConfigurationRouter(configRouter)
302303

303304
appStoreRouter := r.Router.PathPrefix("/orchestrator/app-store").Subrouter()
304305
r.AppStoreRouter.Init(appStoreRouter)

internal/sql/repository/pipelineConfig/CdWorfkflowRepository.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type CdWorkflowRepository interface {
7979

8080
MigrateIsArtifactUploaded(wfrId int, isArtifactUploaded bool)
8181
MigrateCdArtifactLocation(wfrId int, cdArtifactLocation string)
82+
FindDeployedCdWorkflowRunnersByPipelineId(pipelineId int) ([]*CdWorkflowRunner, error)
8283
}
8384

8485
type CdWorkflowRepositoryImpl struct {
@@ -761,3 +762,19 @@ func (impl *CdWorkflowRepositoryImpl) MigrateCdArtifactLocation(wfrId int, cdArt
761762
impl.logger.Errorw("error in updating cd artifact location", "wfrId", wfrId, "err", err)
762763
}
763764
}
765+
766+
func (impl *CdWorkflowRepositoryImpl) FindDeployedCdWorkflowRunnersByPipelineId(pipelineId int) ([]*CdWorkflowRunner, error) {
767+
var runners []*CdWorkflowRunner
768+
err := impl.dbConnection.
769+
Model(&runners).
770+
Column("cd_workflow_runner.*", "CdWorkflow").
771+
Where("cd_workflow.pipeline_id = ?", pipelineId).
772+
Where("workflow_type = ? ", apiBean.CD_WORKFLOW_TYPE_DEPLOY).
773+
Order("cd_workflow_runner.id").
774+
Select()
775+
if err != nil {
776+
impl.logger.Errorw("error in finding previous co workflow runners by pipeline id ", "pipelineId", pipelineId, "err", err)
777+
return nil, err
778+
}
779+
return runners, nil
780+
}

0 commit comments

Comments
 (0)