Skip to content

Commit

Permalink
feat: refactoring authorisation checks and support of super-admin in …
Browse files Browse the repository at this point in the history
…permissions groups. (#4433)

* refactored directories

* refactoring in enforcer code, removed enforceByEmail/InBatch usages

* removed isSuperAdmin method calls

* updated missed auth check

* replaced userService.GetById methods by lighter calls

* replaced getEmailFromToken by lighter calls

* removed remaining 2 usages of enforceByEmail

* self registration roles service renaming

* updated authenticator version for userVerifier changes

* added sql file for creating user group

* test: Origin/self register auth change (#4479)

* global-authorisation and resfactoring

* rbacfor deletematerial

* script number change

* role group superAdmin support

* script change

* sql script delete

* super-admin role group

* chart group duplication fix

* api-token

* check user roles for super admin group

* updated authenticator version

---------

Co-authored-by: Shivam Nagar <[email protected]>
Co-authored-by: Shivam-nagar23 <[email protected]>
  • Loading branch information
3 people authored Jan 8, 2024
1 parent aa6e9be commit 35b084b
Show file tree
Hide file tree
Showing 184 changed files with 1,833 additions and 1,894 deletions.
9 changes: 5 additions & 4 deletions App.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@ import (
"context"
"crypto/tls"
"fmt"
"github.com/devtron-labs/devtron/api/util"
"github.com/devtron-labs/devtron/client/telemetry"
"github.com/devtron-labs/devtron/otel"
"log"
"net/http"
"os"
"time"

"github.com/devtron-labs/devtron/api/util"
"github.com/devtron-labs/devtron/client/telemetry"
"github.com/devtron-labs/devtron/otel"
"github.com/devtron-labs/devtron/pkg/auth/user"

"github.com/casbin/casbin"
authMiddleware "github.com/devtron-labs/authenticator/middleware"
pubsub "github.com/devtron-labs/common-lib/pubsub-lib"
"github.com/devtron-labs/devtron/api/router"
"github.com/devtron-labs/devtron/api/sse"
"github.com/devtron-labs/devtron/internal/middleware"
"github.com/devtron-labs/devtron/pkg/user"
"github.com/go-pg/pg"
_ "github.com/lib/pq"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
Expand Down
4 changes: 2 additions & 2 deletions Wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
appStoreDeployment "github.com/devtron-labs/devtron/api/appStore/deployment"
appStoreDiscover "github.com/devtron-labs/devtron/api/appStore/discover"
appStoreValues "github.com/devtron-labs/devtron/api/appStore/values"
"github.com/devtron-labs/devtron/api/auth/sso"
"github.com/devtron-labs/devtron/api/auth/user"
chartRepo "github.com/devtron-labs/devtron/api/chartRepo"
"github.com/devtron-labs/devtron/api/cluster"
"github.com/devtron-labs/devtron/api/connector"
Expand All @@ -46,10 +48,8 @@ import (
"github.com/devtron-labs/devtron/api/router/pubsub"
"github.com/devtron-labs/devtron/api/server"
"github.com/devtron-labs/devtron/api/sse"
"github.com/devtron-labs/devtron/api/sso"
"github.com/devtron-labs/devtron/api/team"
"github.com/devtron-labs/devtron/api/terminal"
"github.com/devtron-labs/devtron/api/user"
util5 "github.com/devtron-labs/devtron/api/util"
webhookHelm "github.com/devtron-labs/devtron/api/webhook/helm"
"github.com/devtron-labs/devtron/client/argocdServer"
Expand Down
9 changes: 5 additions & 4 deletions api/apiToken/ApiTokenRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ package apiToken

import (
"encoding/json"
"net/http"
"strconv"

openapi "github.com/devtron-labs/devtron/api/openapi/openapiClient"
"github.com/devtron-labs/devtron/api/restHandler/common"
"github.com/devtron-labs/devtron/pkg/apiToken"
"github.com/devtron-labs/devtron/pkg/user"
"github.com/devtron-labs/devtron/pkg/user/casbin"
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
"github.com/devtron-labs/devtron/pkg/auth/user"
"github.com/gorilla/mux"
"github.com/juju/errors"
"go.uber.org/zap"
"gopkg.in/go-playground/validator.v9"
"net/http"
"strconv"
)

type ApiTokenRestHandler interface {
Expand Down
7 changes: 4 additions & 3 deletions api/appStore/AppStoreStatusTimelineRestHandler.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package appStore

import (
"net/http"
"strconv"

"github.com/devtron-labs/devtron/api/restHandler/common"
"github.com/devtron-labs/devtron/pkg/app/status"
"github.com/devtron-labs/devtron/pkg/user/casbin"
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
"github.com/devtron-labs/devtron/util/rbac"
"github.com/gorilla/mux"
"go.uber.org/zap"
"net/http"
"strconv"
)

type AppStoreStatusTimelineRestHandler interface {
Expand Down
23 changes: 9 additions & 14 deletions api/appStore/InstalledAppRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"

bean2 "github.com/devtron-labs/devtron/api/bean"
client "github.com/devtron-labs/devtron/api/helm-app"
openapi "github.com/devtron-labs/devtron/api/helm-app/openapiClient"
Expand All @@ -35,10 +40,10 @@ import (
appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
"github.com/devtron-labs/devtron/pkg/appStore/deployment/repository"
"github.com/devtron-labs/devtron/pkg/appStore/deployment/service"
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
"github.com/devtron-labs/devtron/pkg/auth/user"
"github.com/devtron-labs/devtron/pkg/cluster"
application2 "github.com/devtron-labs/devtron/pkg/k8s/application"
"github.com/devtron-labs/devtron/pkg/user"
"github.com/devtron-labs/devtron/pkg/user/casbin"
"github.com/devtron-labs/devtron/util"
"github.com/devtron-labs/devtron/util/argo"
"github.com/devtron-labs/devtron/util/rbac"
Expand All @@ -47,10 +52,6 @@ import (
"github.com/gorilla/mux"
"go.uber.org/zap"
"gopkg.in/go-playground/validator.v9"
"net/http"
"strconv"
"strings"
"time"
)

type InstalledAppRestHandler interface {
Expand Down Expand Up @@ -159,12 +160,6 @@ func (handler InstalledAppRestHandlerImpl) GetAllInstalledApp(w http.ResponseWri
}
v := r.URL.Query()
token := r.Header.Get("token")
userEmailId, err := handler.userAuthService.GetEmailFromToken(token)
if err != nil {
handler.Logger.Errorw("error in getting user emailId from token", "userId", userId, "err", err)
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}
var envIds []int
envsQueryParam := v.Get("envIds")
if envsQueryParam != "" {
Expand Down Expand Up @@ -290,8 +285,8 @@ func (handler InstalledAppRestHandlerImpl) GetAllInstalledApp(w http.ResponseWri

}
start := time.Now()
resultObjectMap1 := handler.enforcer.EnforceByEmailInBatch(userEmailId, casbin.ResourceHelmApp, casbin.ActionGet, objectArray1)
resultObjectMap2 := handler.enforcer.EnforceByEmailInBatch(userEmailId, casbin.ResourceHelmApp, casbin.ActionGet, objectArray2)
resultObjectMap1 := handler.enforcer.EnforceInBatch(token, casbin.ResourceHelmApp, casbin.ActionGet, objectArray1)
resultObjectMap2 := handler.enforcer.EnforceInBatch(token, casbin.ResourceHelmApp, casbin.ActionGet, objectArray2)
middleware.AppListingDuration.WithLabelValues("enforceByEmailInBatch", "helm").Observe(time.Since(start).Seconds())
authorizedAppIdSet := make(map[string]bool)
//O(n) time loop , at max we will only iterate through all the apps
Expand Down
15 changes: 8 additions & 7 deletions api/appStore/chartProvider/ChartProviderRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package chartProvider

import (
"encoding/json"
"net/http"
"strconv"

"github.com/devtron-labs/devtron/api/restHandler/common"
"github.com/devtron-labs/devtron/pkg/appStore/chartProvider"
"github.com/devtron-labs/devtron/pkg/user"
"github.com/devtron-labs/devtron/pkg/user/casbin"
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
"github.com/devtron-labs/devtron/pkg/auth/user"
"go.uber.org/zap"
"gopkg.in/go-playground/validator.v9"
"net/http"
"strconv"
)

type ChartProviderRestHandler interface {
Expand All @@ -38,9 +39,9 @@ type ChartProviderRestHandler interface {
type ChartProviderRestHandlerImpl struct {
Logger *zap.SugaredLogger
chartProviderService chartProvider.ChartProviderService
validator *validator.Validate
userAuthService user.UserService
enforcer casbin.Enforcer
validator *validator.Validate
userAuthService user.UserService
enforcer casbin.Enforcer
}

func NewChartProviderRestHandlerImpl(Logger *zap.SugaredLogger, userAuthService user.UserService, validator *validator.Validate, chartProviderService chartProvider.ChartProviderService,
Expand Down
19 changes: 10 additions & 9 deletions api/appStore/deployment/AppStoreDeploymentRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"

client "github.com/devtron-labs/devtron/api/helm-app"
openapi "github.com/devtron-labs/devtron/api/helm-app/openapiClient"
"github.com/devtron-labs/devtron/api/restHandler/common"
Expand All @@ -30,19 +35,15 @@ import (
appStoreDeploymentCommon "github.com/devtron-labs/devtron/pkg/appStore/deployment/common"
"github.com/devtron-labs/devtron/pkg/appStore/deployment/service"
"github.com/devtron-labs/devtron/pkg/attributes"
"github.com/devtron-labs/devtron/pkg/user"
"github.com/devtron-labs/devtron/pkg/user/casbin"
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
"github.com/devtron-labs/devtron/pkg/auth/user"
util2 "github.com/devtron-labs/devtron/util"
"github.com/devtron-labs/devtron/util/argo"
"github.com/devtron-labs/devtron/util/rbac"
"github.com/go-pg/pg"
"github.com/gorilla/mux"
"go.uber.org/zap"
"gopkg.in/go-playground/validator.v9"
"net/http"
"strconv"
"strings"
"time"
)

const HELM_APP_UPDATE_COUNTER = "HelmAppUpdateCounter"
Expand All @@ -58,9 +59,9 @@ type AppStoreDeploymentRestHandler interface {
}

type AppStoreDeploymentRestHandlerImpl struct {
Logger *zap.SugaredLogger
userAuthService user.UserService
enforcer casbin.Enforcer
Logger *zap.SugaredLogger
userAuthService user.UserService
enforcer casbin.Enforcer
enforcerUtil rbac.EnforcerUtil
enforcerUtilHelm rbac.EnforcerUtilHelm
appStoreDeploymentService service.AppStoreDeploymentService
Expand Down
17 changes: 9 additions & 8 deletions api/appStore/deployment/CommonDeploymentRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"

"github.com/devtron-labs/common-lib/utils/k8sObjectsUtil"
client "github.com/devtron-labs/devtron/api/helm-app"
openapi2 "github.com/devtron-labs/devtron/api/openapi/openapiClient"
Expand All @@ -29,18 +33,15 @@ import (
appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
appStoreDeploymentCommon "github.com/devtron-labs/devtron/pkg/appStore/deployment/common"
"github.com/devtron-labs/devtron/pkg/appStore/deployment/service"
"github.com/devtron-labs/devtron/pkg/user"
"github.com/devtron-labs/devtron/pkg/user/casbin"
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
"github.com/devtron-labs/devtron/pkg/auth/user"
util2 "github.com/devtron-labs/devtron/util"
"github.com/devtron-labs/devtron/util/argo"
"github.com/devtron-labs/devtron/util/rbac"
"github.com/gorilla/mux"
"go.opentelemetry.io/otel"
"go.uber.org/zap"
"gopkg.in/go-playground/validator.v9"
"net/http"
"strconv"
"time"
)

type CommonDeploymentRestHandler interface {
Expand All @@ -50,9 +51,9 @@ type CommonDeploymentRestHandler interface {
}

type CommonDeploymentRestHandlerImpl struct {
Logger *zap.SugaredLogger
userAuthService user.UserService
enforcer casbin.Enforcer
Logger *zap.SugaredLogger
userAuthService user.UserService
enforcer casbin.Enforcer
enforcerUtil rbac.EnforcerUtil
enforcerUtilHelm rbac.EnforcerUtilHelm
appStoreDeploymentService service.AppStoreDeploymentService
Expand Down
11 changes: 6 additions & 5 deletions api/appStore/discover/AppStoreRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
package appStoreDiscover

import (
"net/http"
"strconv"
"strings"

"github.com/devtron-labs/devtron/api/restHandler/common"
appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
"github.com/devtron-labs/devtron/pkg/appStore/discover/service"
"github.com/devtron-labs/devtron/pkg/user"
"github.com/devtron-labs/devtron/pkg/user/casbin"
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
"github.com/devtron-labs/devtron/pkg/auth/user"
"github.com/gorilla/mux"
"go.uber.org/zap"
"net/http"
"strconv"
"strings"
)

type AppStoreRestHandler interface {
Expand Down
7 changes: 4 additions & 3 deletions api/appStore/values/AppStoreValuesRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package appStoreValues

import (
"encoding/json"
"net/http"
"strconv"

"github.com/devtron-labs/devtron/api/restHandler/common"
appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
"github.com/devtron-labs/devtron/pkg/appStore/values/service"
"github.com/devtron-labs/devtron/pkg/user"
"github.com/devtron-labs/devtron/pkg/auth/user"
"github.com/gorilla/mux"
"go.uber.org/zap"
"net/http"
"strconv"
)

type AppStoreValuesRestHandler interface {
Expand Down
11 changes: 6 additions & 5 deletions api/sso/SsoLoginHandler.go → api/auth/sso/SsoLoginHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ package sso
import (
"encoding/json"
"errors"
"net/http"
"strconv"

"github.com/devtron-labs/devtron/api/bean"
"github.com/devtron-labs/devtron/api/restHandler/common"
"github.com/devtron-labs/devtron/pkg/sso"
"github.com/devtron-labs/devtron/pkg/user"
"github.com/devtron-labs/devtron/pkg/user/casbin"
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
"github.com/devtron-labs/devtron/pkg/auth/sso"
"github.com/devtron-labs/devtron/pkg/auth/user"
"github.com/gorilla/mux"
"go.uber.org/zap"
"gopkg.in/go-playground/validator.v9"
"net/http"
"strconv"
)

type SsoLoginRestHandler interface {
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions api/sso/wire_sso.go → api/auth/sso/wire_sso.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package sso

import (
"github.com/devtron-labs/devtron/pkg/sso"
sso2 "github.com/devtron-labs/devtron/pkg/auth/sso"
"github.com/google/wire"
)

//depends on sql,user,K8sUtil, logger, enforcer,

var SsoConfigWireSet = wire.NewSet(
sso.NewSSOLoginServiceImpl,
wire.Bind(new(sso.SSOLoginService), new(*sso.SSOLoginServiceImpl)),
sso.NewSSOLoginRepositoryImpl,
wire.Bind(new(sso.SSOLoginRepository), new(*sso.SSOLoginRepositoryImpl)),
sso2.NewSSOLoginServiceImpl,
wire.Bind(new(sso2.SSOLoginService), new(*sso2.SSOLoginServiceImpl)),
sso2.NewSSOLoginRepositoryImpl,
wire.Bind(new(sso2.SSOLoginRepository), new(*sso2.SSOLoginRepositoryImpl)),

NewSsoLoginRouterImpl,
wire.Bind(new(SsoLoginRouter), new(*SsoLoginRouterImpl)),
Expand Down
Loading

0 comments on commit 35b084b

Please sign in to comment.