Skip to content

feat: Restrict access on terminal for all non Super Admin Users #5178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 10, 2024
Merged
32 changes: 31 additions & 1 deletion api/k8s/application/k8sApplicationRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ type K8sApplicationRestHandlerImpl struct {
helmAppService client.HelmAppService
userService user.UserService
k8sCommonService k8s.K8sCommonService
terminalEnvVariables *util.TerminalEnvVariables
}

func NewK8sApplicationRestHandlerImpl(logger *zap.SugaredLogger, k8sApplicationService application2.K8sApplicationService, pump connector.Pump, terminalSessionHandler terminal.TerminalSessionHandler, enforcer casbin.Enforcer, enforcerUtilHelm rbac.EnforcerUtilHelm, enforcerUtil rbac.EnforcerUtil, helmAppService client.HelmAppService, userService user.UserService, k8sCommonService k8s.K8sCommonService, validator *validator.Validate) *K8sApplicationRestHandlerImpl {
func NewK8sApplicationRestHandlerImpl(logger *zap.SugaredLogger, k8sApplicationService application2.K8sApplicationService, pump connector.Pump, terminalSessionHandler terminal.TerminalSessionHandler, enforcer casbin.Enforcer, enforcerUtilHelm rbac.EnforcerUtilHelm, enforcerUtil rbac.EnforcerUtil, helmAppService client.HelmAppService, userService user.UserService, k8sCommonService k8s.K8sCommonService, validator *validator.Validate, envVariables *util.EnvironmentVariables) *K8sApplicationRestHandlerImpl {
return &K8sApplicationRestHandlerImpl{
logger: logger,
k8sApplicationService: k8sApplicationService,
Expand All @@ -104,6 +105,7 @@ func NewK8sApplicationRestHandlerImpl(logger *zap.SugaredLogger, k8sApplicationS
helmAppService: helmAppService,
userService: userService,
k8sCommonService: k8sCommonService,
terminalEnvVariables: envVariables.TerminalEnvVariables,
}
}

Expand Down Expand Up @@ -820,6 +822,17 @@ func (handler *K8sApplicationRestHandlerImpl) requestValidationAndRBAC(w http.Re
}
}

func (handler *K8sApplicationRestHandlerImpl) restrictTerminalAccessForNonSuperUsers(w http.ResponseWriter, token string) bool {
// if RESTRICT_TERMINAL_ACCESS_FOR_NON_SUPER_USER is set to true, only super admins can access terminal/ephemeral containers
if handler.terminalEnvVariables.RestrictTerminalAccessForNonSuperUser {
if isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionGet, "*"); !isSuperAdmin {
common.WriteJsonResp(w, errors.New("unauthorized, only super-admins can access terminal"), nil, http.StatusForbidden)
return true
}
}
return false
}

func (handler *K8sApplicationRestHandlerImpl) GetTerminalSession(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("token")
userId, err := handler.userService.GetLoggedInUser(r)
Expand All @@ -836,6 +849,11 @@ func (handler *K8sApplicationRestHandlerImpl) GetTerminalSession(w http.Response
return
}
request.ExternalArgoApplicationName = vars.Get("externalArgoApplicationName")
// check for super admin
restricted := handler.restrictTerminalAccessForNonSuperUsers(w, token)
if restricted {
return
}
if resourceRequestBean.AppIdentifier != nil {
// RBAC enforcer applying For Helm App
rbacObject, rbacObject2 := handler.enforcerUtilHelm.GetHelmObjectByClusterIdNamespaceAndAppName(resourceRequestBean.AppIdentifier.ClusterId, resourceRequestBean.AppIdentifier.Namespace, resourceRequestBean.AppIdentifier.ReleaseName)
Expand Down Expand Up @@ -1013,6 +1031,7 @@ func (handler *K8sApplicationRestHandlerImpl) verifyRbacForCluster(token string,
}

func (handler *K8sApplicationRestHandlerImpl) CreateEphemeralContainer(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("token")
userId, err := handler.userService.GetLoggedInUser(r)
if userId == 0 || err != nil {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
Expand All @@ -1034,6 +1053,11 @@ func (handler *K8sApplicationRestHandlerImpl) CreateEphemeralContainer(w http.Re
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
// check for super admin
restricted := handler.restrictTerminalAccessForNonSuperUsers(w, token)
if restricted {
return
}
//rbac applied in below function
resourceRequestBean := handler.handleEphemeralRBAC(request.PodName, request.Namespace, w, r)
if resourceRequestBean == nil {
Expand All @@ -1057,6 +1081,7 @@ func (handler *K8sApplicationRestHandlerImpl) CreateEphemeralContainer(w http.Re
}

func (handler *K8sApplicationRestHandlerImpl) DeleteEphemeralContainer(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("token")
userId, err := handler.userService.GetLoggedInUser(r)
if userId == 0 || err != nil {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
Expand All @@ -1078,6 +1103,11 @@ func (handler *K8sApplicationRestHandlerImpl) DeleteEphemeralContainer(w http.Re
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
// check for super admin
restricted := handler.restrictTerminalAccessForNonSuperUsers(w, token)
if restricted {
return
}
//rbac applied in below function
resourceRequestBean := handler.handleEphemeralRBAC(request.PodName, request.Namespace, w, r)
if resourceRequestBean == nil {
Expand Down
1 change: 1 addition & 0 deletions env_gen.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
| REQ_CI_MEM | 3G | |
| RESOURCE_LIST_FOR_REPLICAS | Deployment,Rollout,StatefulSet,ReplicaSet | |
| RESOURCE_LIST_FOR_REPLICAS_BATCH_SIZE | 5 | |
| RESTRICT_TERMINAL_ACCESS_FOR_NON_SUPER_USER | false | |
| REVISION_HISTORY_LIMIT_DEVTRON_APP | 1 | |
| REVISION_HISTORY_LIMIT_EXTERNAL_HELM_APP | 0 | |
| REVISION_HISTORY_LIMIT_HELM_APP | 1 | |
Expand Down
6 changes: 6 additions & 0 deletions util/GlobalConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type EnvironmentVariables struct {
GlobalEnvVariables *GlobalEnvVariables
DevtronSecretConfig *DevtronSecretConfig
DeploymentServiceTypeConfig *DeploymentServiceTypeConfig
TerminalEnvVariables *TerminalEnvVariables
}

type DeploymentServiceTypeConfig struct {
Expand All @@ -43,11 +44,16 @@ type DevtronSecretConfig struct {
DevtronDexSecretNamespace string `env:"DEVTRON_DEX_SECRET_NAMESPACE" envDefault:"devtroncd"`
}

type TerminalEnvVariables struct {
RestrictTerminalAccessForNonSuperUser bool `env:"RESTRICT_TERMINAL_ACCESS_FOR_NON_SUPER_USER" envDefault:"false"`
}

func GetEnvironmentVariables() (*EnvironmentVariables, error) {
cfg := &EnvironmentVariables{
GlobalEnvVariables: &GlobalEnvVariables{},
DevtronSecretConfig: &DevtronSecretConfig{},
DeploymentServiceTypeConfig: &DeploymentServiceTypeConfig{},
TerminalEnvVariables: &TerminalEnvVariables{},
}
err := env.Parse(cfg)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading