Skip to content
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
3 changes: 2 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,8 @@ func (api *Api) Application(w http.ResponseWriter, r *http.Request, u *db.User)
app.AddReport(model.AuditReportProfiling, &model.Widget{Profiling: &model.Profiling{ApplicationId: app.Id}, Width: "100%"})
app.AddReport(model.AuditReportTracing, &model.Widget{Tracing: &model.Tracing{ApplicationId: app.Id}, Width: "100%"})

utils.WriteJson(w, api.WithContext(u, project, cacheStatus, world, views.Application(project, world, app)))
view := api.filterAppMapForUser(u, projectId, world, views.Application(project, world, app))
utils.WriteJson(w, api.WithContext(u, project, cacheStatus, world, view))
}

func (api *Api) Incidents(w http.ResponseWriter, r *http.Request, u *db.User) {
Expand Down
34 changes: 34 additions & 0 deletions api/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"fmt"

"github.com/coroot/coroot/api/views/application"
"github.com/coroot/coroot/api/views/overview"
"github.com/coroot/coroot/cache"
"github.com/coroot/coroot/db"
Expand Down Expand Up @@ -325,6 +326,39 @@ func (api *Api) filterOverviewForUser(u *db.User, projectId string, w *model.Wor
return ov
}

// filterAppMapForUser drops the neighbour cards (clients / dependencies) of
// an application map that the user may not view. The Application handler
// only checks the application itself, so a namespace-scoped handoff user
// still saw every application its database talks to, by name, namespace
// and status: the Kubernetes API, the operators, the Coroot agent. The
// Overview map is filtered the same way (filterOverviewForUser).
func (api *Api) filterAppMapForUser(u *db.User, projectId string, w *model.World, v *application.View) *application.View {
if v == nil || v.AppMap == nil || u == nil || w == nil {
return v
}
keep := func(apps []*application.Application) []*application.Application {
out := make([]*application.Application, 0, len(apps))
for _, a := range apps {
if a == nil {
continue
}
var allowed bool
if app := w.GetApplication(a.Id); app != nil {
allowed = api.canViewApplication(u, projectId, app)
} else {
allowed = api.IsAllowed(u, rbac.Actions.Project(projectId).Application(a.Category, a.Id.Namespace, a.Id.Kind, a.Id.Name).View())
}
if allowed {
out = append(out, a)
}
}
return out
}
v.AppMap.Clients = keep(v.AppMap.Clients)
v.AppMap.Dependencies = keep(v.AppMap.Dependencies)
return v
}

func (api *Api) renderIncidents(u *db.User, projectId string, w *model.World) map[model.ApplicationCategory]int {
res := map[model.ApplicationCategory]int{}
if w == nil {
Expand Down
43 changes: 43 additions & 0 deletions api/rbac_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"testing"

"github.com/coroot/coroot/api/views/application"
"github.com/coroot/coroot/api/views/overview"
"github.com/coroot/coroot/db"
"github.com/coroot/coroot/model"
Expand Down Expand Up @@ -81,3 +82,45 @@ func TestRenderSearchFiltersByApplicationPermission(t *testing.T) {
assert.Len(t, search.Applications, 2)
assert.False(t, api.hasRestrictedAppAccess(viewer, projectId))
}

func TestFilterAppMapForUserDropsForeignNeighbours(t *testing.T) {
projectId := "proj1"
roleName := rbac.RoleName("kubero-twenty-production")
roles := staticRoles{
rbac.NewRole(roleName,
rbac.NewPermission(rbac.ScopeApplication, rbac.ActionView, rbac.Object{
"project_id": projectId,
"application_namespace": "twenty-production",
"application_kind": "*",
"application_name": "*",
}),
),
}
api := &Api{roles: roles}
user := &db.User{Email: "kubero+u1+twenty-production@handoff.local", Roles: []rbac.RoleName{roleName}}

web := model.NewApplication(model.NewApplicationId(projectId, "twenty-production", model.ApplicationKindDeployment, "twenty-kuberoapp-web"))
agent := model.NewApplication(model.NewApplicationId(projectId, "coroot", model.ApplicationKindDeployment, "coroot-cluster-agent"))
operator := model.NewApplication(model.NewApplicationId(projectId, "operators", model.ApplicationKindDeployment, "cloudnative-pg"))
// The Kubernetes API as Coroot sees it: no namespace.
k3s := model.NewApplication(model.NewApplicationId(projectId, "", model.ApplicationKindExternalService, "k3s"))
w := &model.World{Applications: map[model.ApplicationId]*model.Application{
web.Id: web, agent.Id: agent, operator.Id: operator, k3s.Id: k3s,
}}
view := func() *application.View {
return &application.View{AppMap: &application.AppMap{
Clients: []*application.Application{{Id: agent.Id}, {Id: web.Id}, {Id: operator.Id}},
Dependencies: []*application.Application{{Id: k3s.Id}},
}}
}

v := api.filterAppMapForUser(user, projectId, w, view())
require.Len(t, v.AppMap.Clients, 1)
assert.Equal(t, web.Id, v.AppMap.Clients[0].Id)
assert.Empty(t, v.AppMap.Dependencies)

// No user (auth off): everything stays.
v = api.filterAppMapForUser(nil, projectId, w, view())
assert.Len(t, v.AppMap.Clients, 3)
assert.Len(t, v.AppMap.Dependencies, 1)
}
Loading