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

Ingress Urls User level access Issue #2659

Merged
merged 8 commits into from
Nov 19, 2022
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
40 changes: 29 additions & 11 deletions api/restHandler/AppListingRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,20 @@ func (handler AppListingRestHandlerImpl) GetHostUrlsByBatch(w http.ResponseWrite
common.WriteJsonResp(w, fmt.Errorf("only one of the appId or installedAppId should be valid appId: %s installedAppId: %s", appIdParam, installedAppIdParam), nil, http.StatusBadRequest)
return
}
token := r.Header.Get("token")
if appIdParam != "" {
appId, err := strconv.Atoi(appIdParam)
if err != nil {
handler.logger.Errorw("error in parsing appId from request body", "appId", appIdParam, "err", err)
common.WriteJsonResp(w, fmt.Errorf("error in parsing appId : %s must be integer", appIdParam), nil, http.StatusBadRequest)
return
}
object := handler.enforcerUtil.GetAppRBACNameByAppId(appId)
if ok := handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, object); !ok {
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden)
return
}
}
var appDetail bean.AppDetailContainer
var appId, envId int
envId, err := strconv.Atoi(envIdParam)
Expand All @@ -595,7 +609,7 @@ func (handler AppListingRestHandlerImpl) GetHostUrlsByBatch(w http.ResponseWrite
common.WriteJsonResp(w, fmt.Errorf("error in parsing envId : %s must be integer", envIdParam), nil, http.StatusBadRequest)
return
}
appDetail, err = handler.getAppDetails(appIdParam, installedAppIdParam, envId)
appDetail, err, appId = handler.getAppDetails(appIdParam, installedAppIdParam, envId)
if err != nil {
handler.logger.Errorw("error occurred while getting app details", "appId", appIdParam, "installedAppId", installedAppIdParam, "envId", envId)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
Expand All @@ -610,11 +624,13 @@ func (handler AppListingRestHandlerImpl) GetHostUrlsByBatch(w http.ResponseWrite
return
}
//check user authorization for this app
token := r.Header.Get("token")
object := handler.enforcerUtil.GetAppRBACNameByAppId(appId)
if ok := handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, object); !ok {
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden)
return
if installedAppIdParam != "" {
object, object2 := handler.enforcerUtil.GetHelmObjectByAppNameAndEnvId(appDetail.AppName, appDetail.EnvironmentId)
ok := handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionGet, object) || handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionGet, object2)
if !ok {
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden)
return
}
}

if installedAppIdParam != "" {
Expand Down Expand Up @@ -649,23 +665,25 @@ func (handler AppListingRestHandlerImpl) GetHostUrlsByBatch(w http.ResponseWrite
common.WriteJsonResp(w, nil, result, http.StatusOK)
}

func (handler AppListingRestHandlerImpl) getAppDetails(appIdParam, installedAppIdParam string, envId int) (bean.AppDetailContainer, error) {
func (handler AppListingRestHandlerImpl) getAppDetails(appIdParam, installedAppIdParam string, envId int) (bean.AppDetailContainer, error, int) {
var appDetail bean.AppDetailContainer
if appIdParam != "" {
appId, err := strconv.Atoi(appIdParam)
if err != nil {
handler.logger.Errorw("error in parsing appId from request body", "appId", appIdParam, "err", err)
return appDetail, err
return appDetail, err, appId
}
return handler.appListingService.FetchAppDetails(appId, envId)
appDetail, err = handler.appListingService.FetchAppDetails(appId, envId)
return appDetail, err, appId
}

appId, err := strconv.Atoi(installedAppIdParam)
if err != nil {
handler.logger.Errorw("error in parsing installedAppId from request body", "installedAppId", installedAppIdParam, "err", err)
return appDetail, err
return appDetail, err, appId
}
return handler.installedAppService.FindAppDetailsForAppstoreApplication(appId, envId)
appDetail, err = handler.installedAppService.FindAppDetailsForAppstoreApplication(appId, envId)
return appDetail, err, appId
}

func (handler AppListingRestHandlerImpl) fetchResourceTree(w http.ResponseWriter, r *http.Request, appId int, envId int, appDetail bean.AppDetailContainer) bean.AppDetailContainer {
Expand Down
15 changes: 13 additions & 2 deletions util/k8s/k8sApplicationService.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,20 @@ type BatchResourceResponse struct {

func (impl *K8sApplicationServiceImpl) FilterServiceAndIngress(resourceTree map[string]interface{}, validRequests []ResourceRequestBean, appDetail bean.AppDetailContainer, appId string) []ResourceRequestBean {
noOfNodes := len(resourceTree["nodes"].([]interface{}))
resourceNodeItemss := resourceTree["nodes"].([]interface{})
for i := 0; i < noOfNodes; i++ {
resourceItem := resourceTree["nodes"].([]interface{})[i].(map[string]interface{})
kind, name, namespace := resourceItem["kind"].(string), resourceItem["name"].(string), resourceItem["namespace"].(string)
resourceItem := resourceNodeItemss[i].(map[string]interface{})
var kind, name, namespace string
if _, ok := resourceItem["kind"]; ok && resourceItem["kind"] != nil {
kind = resourceItem["kind"].(string)
}
if _, ok := resourceItem["name"]; ok && resourceItem["name"] != nil {
name = resourceItem["name"].(string)
}
if _, ok := resourceItem["namespace"]; ok && resourceItem["namespace"] != nil {
namespace = resourceItem["namespace"].(string)
}

if appId == "" {
appId = strconv.Itoa(appDetail.ClusterId) + "|" + namespace + "|" + (appDetail.AppName + "-" + appDetail.EnvironmentName)
}
Expand Down