Skip to content
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

misc: Release candidate v0.24.0 #6157

Merged
merged 12 commits into from
Dec 4, 2024
94 changes: 72 additions & 22 deletions api/cluster/ClusterRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ import (
"context"
"encoding/json"
"errors"
bean2 "github.com/devtron-labs/devtron/pkg/cluster/bean"
"github.com/devtron-labs/devtron/pkg/cluster/environment"
"github.com/devtron-labs/devtron/pkg/cluster/rbac"
"net/http"
"strconv"
"strings"
"time"

"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
Expand Down Expand Up @@ -69,8 +73,8 @@ type ClusterRestHandlerImpl struct {
enforcer casbin.Enforcer
deleteService delete2.DeleteService
argoUserService argo.ArgoUserService
environmentService cluster.EnvironmentService
clusterRbacService cluster.ClusterRbacService
environmentService environment.EnvironmentService
clusterRbacService rbac.ClusterRbacService
}

func NewClusterRestHandlerImpl(clusterService cluster.ClusterService,
Expand All @@ -82,8 +86,8 @@ func NewClusterRestHandlerImpl(clusterService cluster.ClusterService,
enforcer casbin.Enforcer,
deleteService delete2.DeleteService,
argoUserService argo.ArgoUserService,
environmentService cluster.EnvironmentService,
clusterRbacService cluster.ClusterRbacService) *ClusterRestHandlerImpl {
environmentService environment.EnvironmentService,
clusterRbacService rbac.ClusterRbacService) *ClusterRestHandlerImpl {
return &ClusterRestHandlerImpl{
clusterService: clusterService,
clusterNoteService: clusterNoteService,
Expand All @@ -107,7 +111,7 @@ func (impl ClusterRestHandlerImpl) SaveClusters(w http.ResponseWriter, r *http.R
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}
beans := []*cluster.ClusterBean{}
beans := []*bean2.ClusterBean{}
err = decoder.Decode(&beans)
if err != nil {
impl.logger.Errorw("request err, Save", "error", err, "payload", beans)
Expand Down Expand Up @@ -178,7 +182,7 @@ func (impl ClusterRestHandlerImpl) Save(w http.ResponseWriter, r *http.Request)
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}
bean := new(cluster.ClusterBean)
bean := new(bean2.ClusterBean)
err = decoder.Decode(bean)
if err != nil {
impl.logger.Errorw("request err, Save", "error", err, "payload", bean)
Expand Down Expand Up @@ -247,7 +251,7 @@ func (impl ClusterRestHandlerImpl) ValidateKubeconfig(w http.ResponseWriter, r *
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}
bean := &cluster.Kubeconfig{}
bean := &bean2.Kubeconfig{}
err = decoder.Decode(bean)
if err != nil {
impl.logger.Errorw("request err, Validate", "error", err, "payload", bean)
Expand Down Expand Up @@ -309,7 +313,7 @@ func (impl ClusterRestHandlerImpl) FindAll(w http.ResponseWriter, r *http.Reques
}

// RBAC enforcer applying
var result []*cluster.ClusterBean
var result []*bean2.ClusterBean
for _, item := range clusterList {
if ok := impl.enforcer.Enforce(token, casbin.ResourceCluster, casbin.ActionGet, item.ClusterName); ok {
result = append(result, item)
Expand Down Expand Up @@ -374,7 +378,7 @@ func (impl ClusterRestHandlerImpl) FindNoteByClusterId(w http.ResponseWriter, r
}
// RBAC enforcer applying
token := r.Header.Get("token")
authenticated, err := impl.clusterRbacService.CheckAuthorization(bean.ClusterName, bean.ClusterId, token, userId, false)
authenticated, err := impl.clusterRbacService.CheckAuthorization(bean.ClusterName, bean.ClusterId, token, userId, true)
if err != nil {
impl.logger.Errorw("error in checking rbac for cluster", "err", err, "clusterId", bean.ClusterId)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
Expand All @@ -397,7 +401,7 @@ func (impl ClusterRestHandlerImpl) Update(w http.ResponseWriter, r *http.Request
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}
var bean cluster.ClusterBean
var bean bean2.ClusterBean
err = decoder.Decode(&bean)
if err != nil {
impl.logger.Errorw("request err, Update", "error", err, "payload", bean)
Expand Down Expand Up @@ -458,7 +462,7 @@ func (impl ClusterRestHandlerImpl) UpdateClusterDescription(w http.ResponseWrite
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}
var bean cluster.ClusterBean
var bean bean2.ClusterBean
err = decoder.Decode(&bean)
if err != nil {
impl.logger.Errorw("request err, UpdateClusterDescription", "error", err, "payload", bean)
Expand All @@ -474,7 +478,8 @@ func (impl ClusterRestHandlerImpl) UpdateClusterDescription(w http.ResponseWrite
return
}
// RBAC enforcer applying
if ok := impl.enforcer.Enforce(token, casbin.ResourceCluster, casbin.ActionUpdate, clusterDescription.ClusterName); !ok {
authenticated := impl.clusterRbacService.CheckAuthorisationForAllK8sPermissions(token, clusterDescription.ClusterName, casbin.ActionUpdate)
if !authenticated {
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
return
}
Expand Down Expand Up @@ -518,7 +523,8 @@ func (impl ClusterRestHandlerImpl) UpdateClusterNote(w http.ResponseWriter, r *h
return
}
// RBAC enforcer applying
if ok := impl.enforcer.Enforce(token, casbin.ResourceCluster, casbin.ActionUpdate, clusterDescription.ClusterName); !ok {
authenticated := impl.clusterRbacService.CheckAuthorisationForAllK8sPermissions(token, clusterDescription.ClusterName, casbin.ActionUpdate)
if !authenticated {
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
return
}
Expand All @@ -544,7 +550,7 @@ func (impl ClusterRestHandlerImpl) FindAllForAutoComplete(w http.ResponseWriter,
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
var result []cluster.ClusterBean
var result []bean2.ClusterBean
v := r.URL.Query()
authEnabled := true
auth := v.Get("auth")
Expand Down Expand Up @@ -573,7 +579,7 @@ func (impl ClusterRestHandlerImpl) FindAllForAutoComplete(w http.ResponseWriter,
//RBAC enforcer Ends

if len(result) == 0 {
result = make([]cluster.ClusterBean, 0)
result = make([]bean2.ClusterBean, 0)
}
common.WriteJsonResp(w, err, result, http.StatusOK)
}
Expand All @@ -586,7 +592,7 @@ func (impl ClusterRestHandlerImpl) DeleteCluster(w http.ResponseWriter, r *http.
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}
var bean cluster.ClusterBean
var bean bean2.ClusterBean
err = decoder.Decode(&bean)
if err != nil {
impl.logger.Errorw("request err, Delete", "error", err, "payload", bean)
Expand Down Expand Up @@ -619,17 +625,61 @@ func (impl ClusterRestHandlerImpl) DeleteCluster(w http.ResponseWriter, r *http.

func (impl ClusterRestHandlerImpl) GetAllClusterNamespaces(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("token")
userId, err := impl.userService.GetLoggedInUser(r)
if userId == 0 || err != nil {
impl.logger.Errorw("err, GetAllClusterNamespaces", "error", err, "userId", userId)
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}
clusterNamespaces := impl.clusterService.GetAllClusterNamespaces()

// RBAC enforcer applying
for clusterName, _ := range clusterNamespaces {
if ok := impl.enforcer.Enforce(token, casbin.ResourceCluster, casbin.ActionGet, clusterName); !ok {
delete(clusterNamespaces, clusterName)
}
filteredClusterNamespaces, err := impl.HandleRbacForClusterNamespace(userId, token, clusterNamespaces)
if err != nil {
impl.logger.Errorw("error in GetAllClusterNamespaces", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
//RBAC enforcer Ends

common.WriteJsonResp(w, nil, clusterNamespaces, http.StatusOK)
common.WriteJsonResp(w, nil, filteredClusterNamespaces, http.StatusOK)
}

func (impl ClusterRestHandlerImpl) HandleRbacForClusterNamespace(userId int32, token string, clusterNamespaces map[string][]string) (map[string][]string, error) {
filteredClusterNamespaces := make(map[string][]string)
if ok := impl.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionGet, "*"); ok {
return clusterNamespaces, nil
}
roles, err := impl.clusterService.FetchRolesFromGroup(userId)
if err != nil {
impl.logger.Errorw("error on fetching user roles for cluster list", "err", err)
return nil, err
}

clusterAndNameSpaceVsAllowedMap := make(map[string]bool, len(roles))
clusterNameVsAllAllowedMap := make(map[string]bool, len(roles))
for _, role := range roles {
clusterAndNameSpaceVsAllowedMap[strings.ToLower(role.Cluster+"_"+role.Namespace)] = true
if role.Namespace == "" {
clusterNameVsAllAllowedMap[role.Cluster] = true
} else {
clusterNameVsAllAllowedMap[role.Cluster] = false
}
}

for clusterName, allNamespaces := range clusterNamespaces {
if val, exist := clusterNameVsAllAllowedMap[clusterName]; val {
filteredClusterNamespaces[clusterName] = allNamespaces
} else if exist {
for _, namespace := range allNamespaces {
if val2, exist2 := clusterAndNameSpaceVsAllowedMap[strings.ToLower(clusterName+"_"+namespace)]; exist2 && val2 {
filteredClusterNamespaces[clusterName] = append(filteredClusterNamespaces[clusterName], namespace)
}
}
}
}
return filteredClusterNamespaces, nil

}

func (impl ClusterRestHandlerImpl) GetClusterNamespaces(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -688,7 +738,7 @@ func (impl ClusterRestHandlerImpl) FindAllForClusterPermission(w http.ResponseWr
if len(clusterList) == 0 {
// assumption is that if list is empty, then it can happen only in case of Unauthorized (but not sending Unauthorized for super-admin user)
if isActionUserSuperAdmin {
clusterList = make([]cluster.ClusterBean, 0)
clusterList = make([]bean2.ClusterBean, 0)
} else {
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
return
Expand Down
14 changes: 9 additions & 5 deletions api/cluster/EnvironmentRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ package cluster
import (
"context"
"encoding/json"
bean2 "github.com/devtron-labs/devtron/pkg/cluster/repository/bean"
bean3 "github.com/devtron-labs/devtron/pkg/cluster/bean"
request "github.com/devtron-labs/devtron/pkg/cluster/environment"
bean2 "github.com/devtron-labs/devtron/pkg/cluster/environment/bean"
"github.com/devtron-labs/devtron/pkg/cluster/environment/read"
"net/http"
"strconv"
"strings"
Expand All @@ -35,7 +38,6 @@ import (
"github.com/devtron-labs/devtron/api/bean"

"github.com/devtron-labs/devtron/api/restHandler/common"
request "github.com/devtron-labs/devtron/pkg/cluster"
delete2 "github.com/devtron-labs/devtron/pkg/delete"
"github.com/gorilla/mux"
"github.com/pkg/errors"
Expand All @@ -61,6 +63,7 @@ type EnvironmentRestHandler interface {

type EnvironmentRestHandlerImpl struct {
environmentClusterMappingsService request.EnvironmentService
environmentReadService read.EnvironmentReadService
k8sCommonService k8s.K8sCommonService
logger *zap.SugaredLogger
userService user.UserService
Expand All @@ -76,7 +79,7 @@ type ClusterReachableResponse struct {
ClusterName string `json:"clusterName"`
}

func NewEnvironmentRestHandlerImpl(svc request.EnvironmentService, logger *zap.SugaredLogger, userService user.UserService, validator *validator.Validate, enforcer casbin.Enforcer, deleteService delete2.DeleteService, k8sUtil *k8s2.K8sServiceImpl, k8sCommonService k8s.K8sCommonService) *EnvironmentRestHandlerImpl {
func NewEnvironmentRestHandlerImpl(svc request.EnvironmentService, environmentReadService read.EnvironmentReadService, logger *zap.SugaredLogger, userService user.UserService, validator *validator.Validate, enforcer casbin.Enforcer, deleteService delete2.DeleteService, k8sUtil *k8s2.K8sServiceImpl, k8sCommonService k8s.K8sCommonService) *EnvironmentRestHandlerImpl {
cfg := &bean.Config{}
err := env.Parse(cfg)
if err != nil {
Expand All @@ -86,6 +89,7 @@ func NewEnvironmentRestHandlerImpl(svc request.EnvironmentService, logger *zap.S
logger.Infow("evironment rest handler initialized", "ignoreAuthCheckValue", cfg.IgnoreAuthCheck)
return &EnvironmentRestHandlerImpl{
environmentClusterMappingsService: svc,
environmentReadService: environmentReadService,
logger: logger,
userService: userService,
validator: validator,
Expand Down Expand Up @@ -160,7 +164,7 @@ func (impl EnvironmentRestHandlerImpl) Get(w http.ResponseWriter, r *http.Reques
}

func (impl EnvironmentRestHandlerImpl) GetAll(w http.ResponseWriter, r *http.Request) {
environments, err := impl.environmentClusterMappingsService.GetAll()
environments, err := impl.environmentReadService.GetAll()
if err != nil {
impl.logger.Errorw("service err, GetAll", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
Expand Down Expand Up @@ -516,6 +520,6 @@ func (impl EnvironmentRestHandlerImpl) GetEnvironmentConnection(w http.ResponseW
//updating the cluster connection error to db
mapObj := &sync.Map{}
mapObj.Store(clusterBean.Id, err)
impl.environmentClusterMappingsService.HandleErrorInClusterConnections([]*request.ClusterBean{clusterBean}, mapObj, true)
impl.environmentClusterMappingsService.HandleErrorInClusterConnections([]*bean3.ClusterBean{clusterBean}, mapObj, true)
common.WriteJsonResp(w, nil, responseObj, http.StatusOK)
}
37 changes: 25 additions & 12 deletions api/cluster/wire_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ package cluster

import (
"github.com/devtron-labs/devtron/pkg/cluster"
"github.com/devtron-labs/devtron/pkg/cluster/environment"
read2 "github.com/devtron-labs/devtron/pkg/cluster/environment/read"
repository3 "github.com/devtron-labs/devtron/pkg/cluster/environment/repository"
"github.com/devtron-labs/devtron/pkg/cluster/rbac"
"github.com/devtron-labs/devtron/pkg/cluster/read"
"github.com/devtron-labs/devtron/pkg/cluster/repository"
"github.com/devtron-labs/devtron/pkg/genericNotes"
repository2 "github.com/devtron-labs/devtron/pkg/genericNotes/repository"
Expand All @@ -32,9 +37,11 @@ var ClusterWireSet = wire.NewSet(
cluster.NewClusterServiceImpl,
cluster.NewClusterServiceImplExtended,
wire.Bind(new(cluster.ClusterService), new(*cluster.ClusterServiceImplExtended)),
read.NewClusterReadServiceImpl,
wire.Bind(new(read.ClusterReadService), new(*read.ClusterReadServiceImpl)),

cluster.NewClusterRbacServiceImpl,
wire.Bind(new(cluster.ClusterRbacService), new(*cluster.ClusterRbacServiceImpl)),
rbac.NewClusterRbacServiceImpl,
wire.Bind(new(rbac.ClusterRbacService), new(*rbac.ClusterRbacServiceImpl)),

repository.NewClusterDescriptionRepositoryImpl,
wire.Bind(new(repository.ClusterDescriptionRepository), new(*repository.ClusterDescriptionRepositoryImpl)),
Expand All @@ -54,10 +61,12 @@ var ClusterWireSet = wire.NewSet(
NewClusterRouterImpl,
wire.Bind(new(ClusterRouter), new(*ClusterRouterImpl)),

repository.NewEnvironmentRepositoryImpl,
wire.Bind(new(repository.EnvironmentRepository), new(*repository.EnvironmentRepositoryImpl)),
cluster.NewEnvironmentServiceImpl,
wire.Bind(new(cluster.EnvironmentService), new(*cluster.EnvironmentServiceImpl)),
repository3.NewEnvironmentRepositoryImpl,
wire.Bind(new(repository3.EnvironmentRepository), new(*repository3.EnvironmentRepositoryImpl)),
environment.NewEnvironmentServiceImpl,
wire.Bind(new(environment.EnvironmentService), new(*environment.EnvironmentServiceImpl)),
read2.NewEnvironmentReadServiceImpl,
wire.Bind(new(read2.EnvironmentReadService), new(*read2.EnvironmentReadServiceImpl)),
NewEnvironmentRestHandlerImpl,
wire.Bind(new(EnvironmentRestHandler), new(*EnvironmentRestHandlerImpl)),
NewEnvironmentRouterImpl,
Expand All @@ -68,10 +77,12 @@ var ClusterWireSet = wire.NewSet(
var ClusterWireSetEa = wire.NewSet(
repository.NewClusterRepositoryImpl,
wire.Bind(new(repository.ClusterRepository), new(*repository.ClusterRepositoryImpl)),
cluster.NewClusterRbacServiceImpl,
wire.Bind(new(cluster.ClusterRbacService), new(*cluster.ClusterRbacServiceImpl)),
rbac.NewClusterRbacServiceImpl,
wire.Bind(new(rbac.ClusterRbacService), new(*rbac.ClusterRbacServiceImpl)),
cluster.NewClusterServiceImpl,
wire.Bind(new(cluster.ClusterService), new(*cluster.ClusterServiceImpl)),
read.NewClusterReadServiceImpl,
wire.Bind(new(read.ClusterReadService), new(*read.ClusterReadServiceImpl)),

repository.NewClusterDescriptionRepositoryImpl,
wire.Bind(new(repository.ClusterDescriptionRepository), new(*repository.ClusterDescriptionRepositoryImpl)),
Expand All @@ -90,10 +101,12 @@ var ClusterWireSetEa = wire.NewSet(
wire.Bind(new(ClusterRestHandler), new(*ClusterRestHandlerImpl)),
NewClusterRouterImpl,
wire.Bind(new(ClusterRouter), new(*ClusterRouterImpl)),
repository.NewEnvironmentRepositoryImpl,
wire.Bind(new(repository.EnvironmentRepository), new(*repository.EnvironmentRepositoryImpl)),
cluster.NewEnvironmentServiceImpl,
wire.Bind(new(cluster.EnvironmentService), new(*cluster.EnvironmentServiceImpl)),
repository3.NewEnvironmentRepositoryImpl,
wire.Bind(new(repository3.EnvironmentRepository), new(*repository3.EnvironmentRepositoryImpl)),
environment.NewEnvironmentServiceImpl,
wire.Bind(new(environment.EnvironmentService), new(*environment.EnvironmentServiceImpl)),
read2.NewEnvironmentReadServiceImpl,
wire.Bind(new(read2.EnvironmentReadService), new(*read2.EnvironmentReadServiceImpl)),
NewEnvironmentRestHandlerImpl,
wire.Bind(new(EnvironmentRestHandler), new(*EnvironmentRestHandlerImpl)),
NewEnvironmentRouterImpl,
Expand Down
8 changes: 5 additions & 3 deletions api/helm-app/service/HelmAppService.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/devtron-labs/devtron/api/helm-app/service/read"
"github.com/devtron-labs/devtron/internal/constants"
repository2 "github.com/devtron-labs/devtron/internal/sql/repository/dockerRegistry"
bean2 "github.com/devtron-labs/devtron/pkg/cluster/bean"
"github.com/devtron-labs/devtron/pkg/cluster/environment"
"github.com/go-pg/pg"
"net/http"
"reflect"
Expand Down Expand Up @@ -102,7 +104,7 @@ type HelmAppServiceImpl struct {
serverDataStore *serverDataStore.ServerDataStore
serverEnvConfig *serverEnvConfig.ServerEnvConfig
appStoreApplicationVersionRepository appStoreDiscoverRepository.AppStoreApplicationVersionRepository
environmentService cluster.EnvironmentService
environmentService environment.EnvironmentService
pipelineRepository pipelineConfig.PipelineRepository
installedAppRepository repository.InstalledAppRepository
appRepository app.AppRepository
Expand All @@ -116,7 +118,7 @@ func NewHelmAppServiceImpl(Logger *zap.SugaredLogger, clusterService cluster.Clu
helmAppClient gRPC.HelmAppClient, pump connector.Pump, enforcerUtil rbac.EnforcerUtilHelm,
serverDataStore *serverDataStore.ServerDataStore, serverEnvConfig *serverEnvConfig.ServerEnvConfig,
appStoreApplicationVersionRepository appStoreDiscoverRepository.AppStoreApplicationVersionRepository,
environmentService cluster.EnvironmentService, pipelineRepository pipelineConfig.PipelineRepository,
environmentService environment.EnvironmentService, pipelineRepository pipelineConfig.PipelineRepository,
installedAppRepository repository.InstalledAppRepository, appRepository app.AppRepository,
clusterRepository clusterRepository.ClusterRepository, K8sUtil *k8s.K8sServiceImpl,
helmReleaseConfig *HelmReleaseConfig,
Expand Down Expand Up @@ -584,7 +586,7 @@ func (impl *HelmAppServiceImpl) DeleteApplication(ctx context.Context, app *helm
return response, nil
}

func (impl *HelmAppServiceImpl) checkIfNsExists(namespace string, clusterBean *cluster.ClusterBean) (bool, error) {
func (impl *HelmAppServiceImpl) checkIfNsExists(namespace string, clusterBean *bean2.ClusterBean) (bool, error) {

config := clusterBean.GetClusterConfig()
v12Client, err := impl.K8sUtil.GetCoreV1Client(config)
Expand Down
Loading
Loading