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

fix: Ea rbac fixes #5813

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pkg/appStore/bean/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
apiBean "github.com/devtron-labs/devtron/api/bean/gitOps"
openapi "github.com/devtron-labs/devtron/api/helm-app/openapiClient"
bean3 "github.com/devtron-labs/devtron/api/helm-app/service/bean"
"github.com/devtron-labs/devtron/pkg/cluster/repository/bean"
bean2 "github.com/devtron-labs/devtron/pkg/deployment/common/bean"
"github.com/devtron-labs/devtron/util/gitUtil"
Expand Down Expand Up @@ -120,6 +121,19 @@ type InstallAppVersionDTO struct {
DisplayName string `json:"-"` // used only for external apps
}

func (chart *InstallAppVersionDTO) GetAppIdentifierString() string {
displayName := chart.DisplayName
if len(displayName) == 0 {
displayName = chart.AppName
}
appIdentifier := &bean3.AppIdentifier{
ClusterId: chart.ClusterId,
Namespace: chart.Namespace,
ReleaseName: displayName,
}
return appIdentifier.GetUniqueAppNameIdentifier()
}

// UpdateDeploymentAppType updates deploymentAppType to InstallAppVersionDTO
func (chart *InstallAppVersionDTO) UpdateDeploymentAppType(deploymentAppType string) {
if chart == nil {
Expand Down
33 changes: 33 additions & 0 deletions pkg/appStore/installedApp/repository/InstalledAppRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type InstalledAppRepository interface {
GetInstalledAppVersionByClusterIds(clusterIds []int) ([]*InstalledAppVersions, error) //unused
GetInstalledAppVersionByClusterIdsV2(clusterIds []int) ([]*InstalledAppVersions, error)
GetInstalledApplicationByClusterIdAndNamespaceAndAppName(clusterId int, namespace string, appName string) (*InstalledApps, error)
GetInstalledApplicationByClusterIdAndNamespaceAndAppIdentifier(clusterId int, namespace string, appIdentifier string, appName string) (*InstalledApps, error)
GetAppAndEnvDetailsForDeploymentAppTypeInstalledApps(deploymentAppType string, clusterIds []int) ([]*InstalledApps, error)
GetDeploymentSuccessfulStatusCountForTelemetry() (int, error)
GetGitOpsInstalledAppsWhereArgoAppDeletedIsTrue(installedAppId int, envId int) (InstalledApps, error)
Expand Down Expand Up @@ -672,6 +673,38 @@ func (impl InstalledAppRepositoryImpl) GetInstalledAppVersionByClusterIdsV2(clus
return installedAppVersions, err
}

func (impl InstalledAppRepositoryImpl) GetInstalledApplicationByClusterIdAndNamespaceAndAppIdentifier(clusterId int, namespace string, appIdentifier string, appName string) (*InstalledApps, error) {
var installedApps []*InstalledApps
err := impl.dbConnection.Model(&installedApps).
Column("installed_apps.*", "App", "Environment", "App.Team").
Where("environment.cluster_id = ?", clusterId).
Where("environment.namespace = ?", namespace).
Where("app.app_name = ? OR app.display_name = ?", appName, appName).
Where("installed_apps.active = ?", true).
Where("app.active = ?", true).
Where("environment.active = ?", true).
Select()
// extract app which has matching display name and app name
for _, installedApp := range installedApps {
appObj := installedApp.App
if appObj.DisplayName == appName && appObj.AppName == appIdentifier {
return installedApp, nil
}
}
// if not found any matching app in above case, then return app with only app name
for _, installedApp := range installedApps {
appObj := installedApp.App
if appObj.DisplayName == "" && appObj.AppName == appName {
return installedApp, nil
}
}
if err == nil {
err = pg.ErrNoRows
}

return &InstalledApps{}, err
}

func (impl InstalledAppRepositoryImpl) GetInstalledApplicationByClusterIdAndNamespaceAndAppName(clusterId int, namespace string, appName string) (*InstalledApps, error) {
model := &InstalledApps{}
err := impl.dbConnection.Model(model).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ func (impl *AppStoreDeploymentDBServiceImpl) AppStoreDeployOperationDB(installRe
appCreateRequest.AppType = helper.ExternalChartStoreApp
appCreateRequest.DisplayName = installRequest.DisplayName
}
if globalUtil.IsBaseStack() || globalUtil.IsHelmApp(installRequest.AppOfferingMode) {
appCreateRequest.DisplayName = installRequest.AppName
appCreateRequest.AppName = installRequest.GetAppIdentifierString()
}
appCreateRequest, err = impl.createAppForAppStore(appCreateRequest, tx, getAppInstallationMode(installRequest.AppOfferingMode))
if err != nil {
impl.logger.Errorw("error while creating app", "error", err)
Expand Down Expand Up @@ -603,6 +607,7 @@ func (impl *AppStoreDeploymentDBServiceImpl) createAppForAppStore(createRequest
TeamId: createRequest.TeamId,
AppType: helper.ChartStoreApp,
AppOfferingMode: appInstallationMode,
DisplayName: createRequest.DisplayName,
}
if createRequest.AppType == helper.ExternalChartStoreApp {
//when linking ext helm app to chart store, there can be a case that two (or more) external apps can have same name, in diff namespaces or diff
Expand Down
44 changes: 37 additions & 7 deletions util/rbac/EnforcerUtilHelm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package rbac

import (
"fmt"
"github.com/devtron-labs/devtron/api/helm-app/service/bean"
"github.com/devtron-labs/devtron/internal/sql/repository/app"
repository2 "github.com/devtron-labs/devtron/pkg/appStore/installedApp/repository"
"github.com/devtron-labs/devtron/pkg/cluster/repository"
Expand Down Expand Up @@ -78,8 +79,7 @@ func (impl EnforcerUtilHelmImpl) GetHelmObjectByTeamIdAndClusterId(teamId int, c

func (impl EnforcerUtilHelmImpl) GetHelmObjectByClusterIdNamespaceAndAppName(clusterId int, namespace string, appName string) (string, string) {

installedApp, installedAppErr := impl.InstalledAppRepository.GetInstalledApplicationByClusterIdAndNamespaceAndAppName(clusterId, namespace, appName)

installedApp, installedAppErr := impl.getInstalledApp(clusterId, namespace, appName)
if installedAppErr != nil && installedAppErr != pg.ErrNoRows {
impl.logger.Errorw("error on fetching data for rbac object from installed app repository", "err", installedAppErr)
return "", ""
Expand All @@ -93,19 +93,18 @@ func (impl EnforcerUtilHelmImpl) GetHelmObjectByClusterIdNamespaceAndAppName(clu

if installedApp == nil || installedAppErr == pg.ErrNoRows {
// for cli apps which are not yet linked

app, err := impl.appRepository.FindAppAndProjectByAppName(appName)
app, err := impl.getAppObject(clusterId, namespace, appName)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in fetching app details", "err", err)
return "", ""
}

if app.TeamId == 0 {
// case if project is not assigned to cli app
return fmt.Sprintf("%s/%s__%s/%s", team.UNASSIGNED_PROJECT, cluster.ClusterName, namespace, appName), ""
return fmt.Sprintf("%s/%s__%s/%s", team.UNASSIGNED_PROJECT, cluster.ClusterName, namespace, appName), fmt.Sprintf("%s/%s/%s", team.UNASSIGNED_PROJECT, namespace, appName)
} else {
// case if project is assigned
return fmt.Sprintf("%s/%s__%s/%s", app.Team.Name, cluster.ClusterName, namespace, appName), ""
return fmt.Sprintf("%s/%s__%s/%s", app.Team.Name, cluster.ClusterName, namespace, appName), fmt.Sprintf("%s/%s/%s", app.Team.Name, namespace, appName)
}

}
Expand All @@ -118,7 +117,7 @@ func (impl EnforcerUtilHelmImpl) GetHelmObjectByClusterIdNamespaceAndAppName(clu
} else {
if installedApp.EnvironmentId == 0 {
// for apps in EA mode, initally env can be 0.
return fmt.Sprintf("%s/%s__%s/%s", installedApp.App.Team.Name, cluster.ClusterName, namespace, appName), ""
return fmt.Sprintf("%s/%s__%s/%s", installedApp.App.Team.Name, cluster.ClusterName, namespace, appName), fmt.Sprintf("%s/%s/%s", installedApp.App.Team.Name, namespace, appName)
}
// for apps which are assigned to a project and have env ID
rbacOne := fmt.Sprintf("%s/%s/%s", installedApp.App.Team.Name, installedApp.Environment.EnvironmentIdentifier, appName)
Expand All @@ -131,6 +130,37 @@ func (impl EnforcerUtilHelmImpl) GetHelmObjectByClusterIdNamespaceAndAppName(clu

}

func (impl EnforcerUtilHelmImpl) getAppObject(clusterId int, namespace string, appName string) (*app.App, error) {
appIdentifier := &bean.AppIdentifier{
ClusterId: clusterId,
Namespace: namespace,
ReleaseName: appName,
}
appNameIdentifier := appIdentifier.GetUniqueAppNameIdentifier()
appObj, err := impl.appRepository.FindAppAndProjectByAppName(appNameIdentifier)
if appObj == nil || err == pg.ErrNoRows {
impl.logger.Warnw("appObj not found, going to find app using display name ", "appIdentifier", appNameIdentifier, "appName", appName)
appObj, err = impl.appRepository.FindAppAndProjectByAppName(appName)
}
return appObj, err
}

func (impl EnforcerUtilHelmImpl) getInstalledApp(clusterId int, namespace string, appName string) (*repository2.InstalledApps, error) {
appIdentifier := &bean.AppIdentifier{
ClusterId: clusterId,
Namespace: namespace,
ReleaseName: appName,
}
appNameIdentifier := appIdentifier.GetUniqueAppNameIdentifier()
//installedApp, installedAppErr := impl.InstalledAppRepository.GetInstalledApplicationByClusterIdAndNamespaceAndAppName(clusterId, namespace, appNameIdentifier)
//if installedApp == nil || installedAppErr == pg.ErrNoRows {
// impl.logger.Warnw("installed app not found, going to find app using display name ", "appIdentifier", appNameIdentifier, "appName", appName)
// installedApp, installedAppErr = impl.InstalledAppRepository.GetInstalledApplicationByClusterIdAndNamespaceAndAppName(clusterId, namespace, appName)
//}
return impl.InstalledAppRepository.GetInstalledApplicationByClusterIdAndNamespaceAndAppIdentifier(clusterId, namespace, appNameIdentifier, appName)
//return installedApp, installedAppErr
}

func (impl EnforcerUtilHelmImpl) GetAppRBACNameByInstalledAppId(installedAppVersionId int) (string, string) {

InstalledApp, err := impl.InstalledAppRepository.GetInstalledApp(installedAppVersionId)
Expand Down
Loading