Skip to content

Commit

Permalink
Merge pull request #680 from cvaroqui/main
Browse files Browse the repository at this point in the history
RBAC
  • Loading branch information
cvaroqui authored Jan 24, 2025
2 parents a2585be + 09e51d2 commit c0b1eae
Show file tree
Hide file tree
Showing 105 changed files with 176 additions and 111 deletions.
12 changes: 8 additions & 4 deletions core/clusterdump/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,25 @@ func (s *Data) WithSelector(selector string) *Data {
}

// WithNamespace purges the dataset from objects not matching the namespace
func (s *Data) WithNamespace(namespace string) *Data {
if namespace == "" {
func (s *Data) WithNamespace(namespaces ...string) *Data {
if len(namespaces) == 0 {
return s
}
allowedNamespaces := make(map[string]any)
for _, namespace := range namespaces {
allowedNamespaces[namespace] = nil
}
for nodename, nodeData := range s.Cluster.Node {
for ps := range nodeData.Instance {
p, _ := naming.ParsePath(ps)
if p.Namespace != namespace {
if _, ok := allowedNamespaces[p.Namespace]; !ok {
delete(s.Cluster.Node[nodename].Instance, ps)
}
}
}
for ps := range s.Cluster.Object {
p, _ := naming.ParsePath(ps)
if p.Namespace != namespace {
if _, ok := allowedNamespaces[p.Namespace]; !ok {
delete(s.Cluster.Object, ps)
}
}
Expand Down
4 changes: 1 addition & 3 deletions daemon/daemonapi/get_daemon_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,8 @@ func (a *DaemonAPI) getPeerDaemonEvents(ctx echo.Context, nodename string, param

// getLocalDaemonEvents handles streaming local daemon events based on provided filters, selectors, and parameters.
func (a *DaemonAPI) getLocalDaemonEvents(ctx echo.Context, params api.GetDaemonEventsParams) error {
if v, err := assertRole(ctx, rbac.RoleGuest, rbac.RoleOperator, rbac.RoleAdmin, rbac.RoleRoot, rbac.RoleJoin, rbac.RoleLeave); err != nil {
if v, err := assertRole(ctx, rbac.RoleGuest, rbac.RoleOperator, rbac.RoleAdmin, rbac.RoleRoot, rbac.RoleJoin, rbac.RoleLeave); !v {
return err
} else if !v {
return nil
}
var (
handlerName = "GetDaemonEvents"
Expand Down
18 changes: 17 additions & 1 deletion daemon/daemonapi/get_daemon_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/labstack/echo/v4"

"github.com/opensvc/om3/daemon/api"
"github.com/opensvc/om3/daemon/rbac"
)

type (
Expand All @@ -30,7 +31,8 @@ var (
//
// Serve 2s cached data.
func (a *DaemonAPI) GetDaemonStatus(ctx echo.Context, params api.GetDaemonStatusParams) error {
if v, err := assertRoot(ctx); !v {
// Require at least "guest" on any namespace.
if v, err := assertRole(ctx, rbac.RoleGuest, rbac.RoleOperator, rbac.RoleAdmin, rbac.RoleRoot, rbac.RoleJoin, rbac.RoleLeave); !v {
return err
}
now := time.Now()
Expand All @@ -42,11 +44,25 @@ func (a *DaemonAPI) GetDaemonStatus(ctx echo.Context, params api.GetDaemonStatus
subRefreshed.Unlock()

status := a.Daemondata.ClusterData()

// Explicit object selector filtering
if params.Selector != nil {
status = status.WithSelector(*params.Selector)
}

// Explicit namespace filtering
if params.Namespace != nil {
status = status.WithNamespace(*params.Namespace)
}

// RBAC namespace filtering
userGrants := grantsFromContext(ctx)
if !userGrants.HasRole(rbac.RoleRoot) {
// If the user has no "root" grant, filter out all objects from namespaces
// he has no role for. The guest:ns1 grant is sufficient to see all
// objects in ns1.
status = status.WithNamespace(userGrants.Namespaces()...)
}

return ctx.JSON(http.StatusOK, status)
}
6 changes: 5 additions & 1 deletion daemon/daemonapi/get_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/opensvc/om3/core/instance"
"github.com/opensvc/om3/core/naming"
"github.com/opensvc/om3/daemon/api"
"github.com/opensvc/om3/daemon/rbac"
)

func (a *DaemonAPI) GetInstances(ctx echo.Context, params api.GetInstancesParams) error {
Expand All @@ -24,14 +25,17 @@ func (a *DaemonAPI) GetInstances(ctx echo.Context, params api.GetInstancesParams
}
configs := instance.ConfigData.GetAll()
l := make(api.InstanceItems, 0)
hasRoot := grantsFromContext(ctx).HasRole(rbac.RoleRoot)
userGrants := grantsFromContext(ctx)

for _, config := range configs {
if !meta.HasPath(config.Path.String()) {
continue
}
if !meta.HasNode(config.Node) {
continue
}
if _, err := assertGuest(ctx, config.Path.Namespace); err != nil {
if !hasRoot && !userGrants.Has(rbac.RoleGuest, config.Path.Namespace) {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_instance_config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (a *DaemonAPI) GetInstanceConfigFile(ctx echo.Context, nodename, namespace string, kind naming.Kind, name string) error {
if _, err := assertGuest(ctx, namespace); err != nil {
if v, err := assertGuest(ctx, namespace); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_instance_resource_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func (a *DaemonAPI) GetInstanceResourceInfo(ctx echo.Context, nodename, namespace string, kind naming.Kind, name string) error {
if _, err := assertGuest(ctx, namespace); err != nil {
if v, err := assertGuest(ctx, namespace); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_instance_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func (a *DaemonAPI) GetInstanceSchedule(ctx echo.Context, nodename, namespace string, kind naming.Kind, name string) error {
if _, err := assertGuest(ctx, namespace); err != nil {
if v, err := assertGuest(ctx, namespace); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_instances_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func (a *DaemonAPI) GetInstanceLogs(ctx echo.Context, nodename string, namespace string, kind naming.Kind, name string, params api.GetInstanceLogsParams) error {
if _, err := assertGuest(ctx, namespace); err != nil {
if v, err := assertGuest(ctx, namespace); !v {
return err
}
p, err := naming.NewPath(namespace, kind, name)
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// GetNetworks returns network status list.
func (a *DaemonAPI) GetNetworks(ctx echo.Context, params api.GetNetworksParams) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
var items api.NetworkItems
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_network_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func GetClusterIPs() clusterip.L {

// GetNetworkIP returns network status list.
func (a *DaemonAPI) GetNetworkIP(ctx echo.Context, params api.GetNetworkIPParams) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
n, err := object.NewNode(object.WithVolatile(true))
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func (a *DaemonAPI) GetNodes(ctx echo.Context, params api.GetNodesParams) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
meta := Meta{
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func (a *DaemonAPI) GetNodeCapabilities(ctx echo.Context, nodename string) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func (a *DaemonAPI) GetNodeConfig(ctx echo.Context, nodename string, params api.GetNodeConfigParams) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (a *DaemonAPI) GetNodeConfigFile(ctx echo.Context, nodename string) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_config_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func (a *DaemonAPI) GetNodeConfigGet(ctx echo.Context, nodename string, params api.GetNodeConfigGetParams) error {
//log := LogHandler(ctx, "GetNodeConfigGet")

if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_drbd_allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func init() {
}

func (a *DaemonAPI) GetNodeDRBDAllocation(ctx echo.Context, nodename string) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_drbd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (a *DaemonAPI) GetNodeDRBDConfig(ctx echo.Context, nodename string, params api.GetNodeDRBDConfigParams) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
log := LogHandler(ctx, "GetNodeDRBDConfig")
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func (a *DaemonAPI) GetNodeDriver(ctx echo.Context, nodename api.InPathNodeName) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

// GetNodeLogs feeds publications in rss format.
func (a *DaemonAPI) GetNodeLogs(ctx echo.Context, nodename string, params api.GetNodeLogsParams) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if nodename == a.localhost || nodename == "localhost" {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func (a *DaemonAPI) GetNodePing(ctx echo.Context, nodename api.InPathNodeName) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func (a *DaemonAPI) GetNodeSchedule(ctx echo.Context, nodename string) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_ssh_hostkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (a *DaemonAPI) GetNodeSSHHostkeys(ctx echo.Context, nodename string) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

func (a *DaemonAPI) GetNodeSSHKey(ctx echo.Context, nodename string) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_system_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (a *DaemonAPI) GetNodeSystemDisk(ctx echo.Context, nodename api.InPathNodeName) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_system_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (a *DaemonAPI) GetNodeSystemGroup(ctx echo.Context, nodename api.InPathNodeName) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_system_hardware.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (a *DaemonAPI) GetNodeSystemHardware(ctx echo.Context, nodename api.InPathNodeName) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_system_ipaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (a *DaemonAPI) GetNodeSystemIPAddress(ctx echo.Context, nodename api.InPathNodeName) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_system_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (a *DaemonAPI) GetNodeSystemPackage(ctx echo.Context, nodename api.InPathNodeName) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_system_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (a *DaemonAPI) GetNodeSystemPatch(ctx echo.Context, nodename api.InPathNodeName) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_system_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (a *DaemonAPI) GetNodeSystemProperty(ctx echo.Context, nodename api.InPathNodeName) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_system_san_initiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (a *DaemonAPI) GetNodeSystemSANInitiator(ctx echo.Context, nodename api.InPathNodeName) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_system_san_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (a *DaemonAPI) GetNodeSystemSANPath(ctx echo.Context, nodename api.InPathNodeName) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_node_system_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (a *DaemonAPI) GetNodeSystemUser(ctx echo.Context, nodename api.InPathNodeName) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
if a.localhost == nodename {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemonapi/get_nodes_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (a *DaemonAPI) GetNodesInfo(ctx echo.Context) error {
if _, err := assertRoot(ctx); err != nil {
if v, err := assertRoot(ctx); !v {
return err
}
log := LogHandler(ctx, "GetNodesInfo")
Expand Down
Loading

0 comments on commit c0b1eae

Please sign in to comment.