-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatekeeper.go
104 lines (87 loc) · 2.94 KB
/
gatekeeper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package gatekeeper
import (
"context"
"fmt"
"github.com/agrim123/gatekeeper/internal/constants"
"github.com/agrim123/gatekeeper/internal/guard"
"github.com/agrim123/gatekeeper/internal/runtime"
"github.com/agrim123/gatekeeper/internal/store"
"github.com/agrim123/gatekeeper/internal/utils"
"github.com/agrim123/gatekeeper/pkg/authentication"
"github.com/agrim123/gatekeeper/pkg/authorization"
"github.com/agrim123/gatekeeper/pkg/logger"
"github.com/agrim123/gatekeeper/pkg/notifier"
storePkg "github.com/agrim123/gatekeeper/pkg/store"
)
type GateKeeper struct {
ctx context.Context
store *store.StoreStruct
runtime *runtime.Runtime
guard *guard.Guard
notifyRequester func(string)
}
// NewGatekeeper returns new instance of gatekeeper with default modules
func NewGatekeeper(ctx context.Context) *GateKeeper {
// Initializes the staging path for containers
// filesystem.CreateDir(constants.RootStagingPath)
ctx = utils.AttachExecutingUserToCtx(ctx)
g := &GateKeeper{
ctx: ctx,
runtime: runtime.NewRuntime(ctx),
notifyRequester: notifier.AttachFallbackNotifier(notifier.NewDefaultNotifier()),
guard: guard.NewGuard(ctx),
store: store.Store,
}
return g
}
// WithNotifier updates the notifier module
func (g *GateKeeper) WithNotifier(customNotifier notifier.Notifier) *GateKeeper {
g.notifyRequester = notifier.AttachFallbackNotifier(customNotifier)
return g
}
// WithAuthorizationModule updates the guard's authorization module
func (g *GateKeeper) WithAuthorizationModule(authorizationModule authorization.Module) *GateKeeper {
g.guard = g.guard.WithAuthorizationModule(authorizationModule)
return g
}
// WithAuthenticationModule updates the guard's authentication module
func (g *GateKeeper) WithAuthenticationModule(authenticationModule authentication.Module) *GateKeeper {
g.guard = g.guard.WithAuthenticationModule(authenticationModule)
return g
}
func (g *GateKeeper) AllowedCommands() map[string][]string {
return g.store.GetAllowedCommandsForUser(utils.GetExecutingUser())
}
func (g *GateKeeper) Whoami() *storePkg.Whoami {
return &storePkg.Whoami{
Username: utils.GetExecutingUser(),
Groups: store.Store.Users[utils.GetExecutingUser()].Groups,
AllowedCommands: g.AllowedCommands(),
}
}
// Run runs the command given to the gatekeeper
// It then delegates different tasks
func (g *GateKeeper) Run(plan, option string) {
g.guard.Verify(plan, option)
err := g.runtime.Execute(plan, option)
if err != nil {
g.notifyRequester(
fmt.Sprintf(
"Plan `%s %s` executed by `%s` failed. Error: %s",
logger.Underline(plan),
logger.Underline(option),
logger.Bold(g.ctx.Value(constants.UserContextKey).(string)),
err.Error(),
),
)
} else {
g.notifyRequester(
fmt.Sprintf(
"Plan `%s %s` executed by `%s` successfully!",
logger.Underline(plan),
logger.Underline(option),
logger.Bold(g.ctx.Value(constants.UserContextKey).(string)),
),
)
}
}