This commit is contained in:
jamesread 2021-10-16 07:38:47 +01:00
parent da2ea12f30
commit 03d7250cf6
4 changed files with 62 additions and 65 deletions

View File

@ -1,29 +1,29 @@
package acl
import (
"context"
config "github.com/jamesread/OliveTin/internal/config"
log "github.com/sirupsen/logrus"
"context"
)
type User struct {
Username string;
Username string
}
func IsAllowedExec(cfg *config.Config, user *User, action *config.ActionButton) bool {
canExec := cfg.DefaultPermissions.Exec
log.WithFields(log.Fields{
"User": user.Username,
"Action": action.Title,
"CanExec": canExec,
"User": user.Username,
"Action": action.Title,
"CanExec": canExec,
}).Debug("isAllowedExec Permission Default")
for _, permissionEntry := range action.Permissions {
if isUserInGroup(user, permissionEntry.Usergroup) {
log.WithFields(log.Fields{
"User": user.Username,
"Action": action.Title,
"User": user.Username,
"Action": action.Title,
"CanExec": permissionEntry.Exec,
}).Debug("isAllowedExec Permission Entry")
@ -32,30 +32,30 @@ func IsAllowedExec(cfg *config.Config, user *User, action *config.ActionButton)
}
log.WithFields(log.Fields{
"User": user.Username,
"Action": action.Title,
"User": user.Username,
"Action": action.Title,
"CanExec": canExec,
}).Debug("isAllowedExec Final Result")
return canExec;
return canExec
}
func IsAllowedView(cfg *config.Config, user *User, action *config.ActionButton) bool {
canView := cfg.DefaultPermissions.View
log.WithFields(log.Fields{
"User": user.Username,
"Action": action.Title,
"CanView": canView,
"User": user.Username,
"Action": action.Title,
"CanView": canView,
}).Debug("isAllowedView Permission Default")
for idx, permissionEntry := range action.Permissions {
if isUserInGroup(user, permissionEntry.Usergroup) {
log.WithFields(log.Fields{
"User": user.Username,
"Action": action.Title,
"User": user.Username,
"Action": action.Title,
"CanView": permissionEntry.View,
"Index": idx,
"Index": idx,
}).Debug("isAllowedView Permission Entry")
canView = permissionEntry.View
@ -63,22 +63,20 @@ func IsAllowedView(cfg *config.Config, user *User, action *config.ActionButton)
}
log.WithFields(log.Fields{
"User": user.Username,
"Action": action.Title,
"User": user.Username,
"Action": action.Title,
"CanView": canView,
}).Debug("isAllowedView Final Result")
return canView;
return canView
}
func isUserInGroup(user *User, usergroup string) bool {
return true;
return true
}
func UserFromContext(ctx context.Context) *User {
return &User {
return &User{
Username: "Guest",
}
}

View File

@ -4,12 +4,12 @@ import ()
// ActionButton represents a button that is shown in the webui.
type ActionButton struct {
ID string
Title string
Icon string
Shell string
CSS map[string]string `mapstructure:"omitempty"`
Timeout int
ID string
Title string
Icon string
Shell string
CSS map[string]string `mapstructure:"omitempty"`
Timeout int
Permissions []PermissionsEntry
}
@ -24,8 +24,8 @@ type Entity struct {
type PermissionsEntry struct {
Usergroup string
View bool
Exec bool
View bool
Exec bool
}
type DefaultPermissions struct {
@ -34,7 +34,7 @@ type DefaultPermissions struct {
}
type UserGroup struct {
Name string
Name string
Members []string
}
@ -51,8 +51,8 @@ type Config struct {
ActionButtons []ActionButton `mapstructure:"actions"`
Entities []Entity `mapstructure:"entities"`
CheckForUpdates bool
Usergroups []UserGroup
DefaultPermissions DefaultPermissions
Usergroups []UserGroup
DefaultPermissions DefaultPermissions
}
// DefaultConfig gets a new Config structure with sensible default values.

View File

@ -2,9 +2,9 @@ package executor
import (
pb "github.com/jamesread/OliveTin/gen/grpc"
acl "github.com/jamesread/OliveTin/internal/acl"
config "github.com/jamesread/OliveTin/internal/config"
log "github.com/sirupsen/logrus"
acl "github.com/jamesread/OliveTin/internal/acl"
"context"
"errors"
@ -13,12 +13,12 @@ import (
)
type InternalLogEntry struct {
Datetime string
Content string
Stdout string
Stderr string
TimedOut bool
ExitCode int32
Datetime string
Content string
Stdout string
Stderr string
TimedOut bool
ExitCode int32
ActionTitle string
}
@ -34,23 +34,23 @@ func (e *Executor) ExecAction(cfg *config.Config, user *acl.User, actualAction *
res := execAction(cfg, actualAction)
e.Logs = append(e.Logs, *res);
e.Logs = append(e.Logs, *res)
return &pb.StartActionResponse{
LogEntry: &pb.LogEntry {
LogEntry: &pb.LogEntry{
ActionTitle: actualAction.Title,
TimedOut: res.TimedOut,
Stderr: res.Stderr,
Stdout: res.Stdout,
ExitCode: res.ExitCode,
TimedOut: res.TimedOut,
Stderr: res.Stderr,
Stdout: res.Stdout,
ExitCode: res.ExitCode,
},
};
}
}
func execAction(cfg *config.Config, actualAction *config.ActionButton) *InternalLogEntry {
res := &InternalLogEntry {
Datetime: time.Now().Format("2006-01-02 15:04:05"),
TimedOut: false,
res := &InternalLogEntry{
Datetime: time.Now().Format("2006-01-02 15:04:05"),
TimedOut: false,
ActionTitle: actualAction.Title,
}

View File

@ -9,14 +9,14 @@ import (
"google.golang.org/grpc"
"net"
acl "github.com/jamesread/OliveTin/internal/acl"
config "github.com/jamesread/OliveTin/internal/config"
executor "github.com/jamesread/OliveTin/internal/executor"
acl "github.com/jamesread/OliveTin/internal/acl"
)
var (
cfg *config.Config
ex = executor.Executor{}
ex = executor.Executor{}
)
type oliveTinAPI struct {
@ -37,8 +37,7 @@ func (api *oliveTinAPI) StartAction(ctx ctx.Context, req *pb.StartActionRequest)
user := acl.UserFromContext(ctx)
if !acl.IsAllowedExec(cfg, user, actualAction) {
return &pb.StartActionResponse{
}, nil
return &pb.StartActionResponse{}, nil
}
@ -56,9 +55,9 @@ func (api *oliveTinAPI) GetButtons(ctx ctx.Context, req *pb.GetButtonsRequest) (
}
btn := pb.ActionButton{
Id: fmt.Sprintf("%x", md5.Sum([]byte(action.Title))),
Title: action.Title,
Icon: lookupHTMLIcon(action.Icon),
Id: fmt.Sprintf("%x", md5.Sum([]byte(action.Title))),
Title: action.Title,
Icon: lookupHTMLIcon(action.Icon),
CanExec: acl.IsAllowedExec(cfg, user, &action),
}
@ -75,18 +74,18 @@ func (api *oliveTinAPI) GetButtons(ctx ctx.Context, req *pb.GetButtonsRequest) (
}
func (api *oliveTinAPI) GetLogs(ctx ctx.Context, req *pb.GetLogsRequest) (*pb.GetLogsResponse, error) {
ret := &pb.GetLogsResponse{};
ret := &pb.GetLogsResponse{}
// TODO Limit to 10 entries or something to prevent browser lag.
for _, logEntry := range ex.Logs {
ret.Logs = append(ret.Logs, &pb.LogEntry{
ActionTitle: logEntry.ActionTitle,
Datetime: logEntry.Datetime,
Stdout: logEntry.Stdout,
Stderr: logEntry.Stderr,
TimedOut: logEntry.TimedOut,
ExitCode: logEntry.ExitCode,
Datetime: logEntry.Datetime,
Stdout: logEntry.Stdout,
Stderr: logEntry.Stderr,
TimedOut: logEntry.TimedOut,
ExitCode: logEntry.ExitCode,
})
}