forked from suzuki-shunsuke/go-graylog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.go
114 lines (100 loc) · 2.97 KB
/
logic.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
105
106
107
108
109
110
111
112
113
114
package logic
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/suzuki-shunsuke/go-graylog"
"github.com/suzuki-shunsuke/go-graylog/mockserver/store"
"github.com/suzuki-shunsuke/go-graylog/mockserver/store/plain"
)
// Logic represents a mock of the Graylog API.
// This is embedded to mockserver.Server.
type Logic struct {
authEnabled bool
streamRules map[string]map[string]graylog.StreamRule
store store.Store
logger *log.Logger
}
// Logger returns a logger.
// This logger is logrus.Logger .
// https://github.com/sirupsen/logrus
// You can change the Logic's logger configuration freely.
//
// lgc := logic.NewLogic(nil)
// logger := lgc.Logger()
// logger.SetFormatter(&log.JSONFormatter{})
// logger.SetLevel(log.WarnLevel)
func (lgc *Logic) Logger() *log.Logger {
return lgc.logger
}
// NewLogic returns new Server.
// The argument `store` is the store which the server uses.
// If `store` is nil, the default plain store is used and data is not persisted.
func NewLogic(store store.Store) (*Logic, error) {
if store == nil {
store = plain.NewStore("")
}
lgc := &Logic{
// indexSetStats: map[string]graylog.IndexSetStats{},
streamRules: map[string]map[string]graylog.StreamRule{},
store: store,
logger: log.New(),
// By default the authentication is enabled
authEnabled: true,
}
// By Default logLevel is warn,
// because debug and info logs are often noisy at unit tests.
lgc.logger.SetLevel(log.WarnLevel)
err := lgc.InitData()
return lgc, err
}
// SetStore sets a store to the mock server.
func (lgc *Logic) SetStore(store store.Store) {
lgc.store = store
}
// Save writes Mock Server's data in a file for persistence.
func (lgc *Logic) Save() error {
return lgc.store.Save()
}
// Load reads Mock Server's data from a file.
func (lgc *Logic) Load() error {
return lgc.store.Load()
}
// SetAuth sets whether the authentication and authentication are enabled.
// Disable the authentication.
//
// lgc.SetAuth(false)
//
// Enable the authentication.
//
// lgc.SetAuth(true)
func (lgc *Logic) SetAuth(authEnabled bool) {
lgc.authEnabled = authEnabled
}
// Auth returns whether the authentication and authentication are enabled.
func (lgc *Logic) Auth() bool {
return lgc.authEnabled
}
// Authorize authorizes a user.
// If the user doesn't have the permission, an error is returned.
//
// // whether the user has the permission to read all roles
// if sc, err := lgc.Authorize(user, "roles:read", ""); err != nil {
// fmt.Println(sc, err) // 403, "authorization failure"
// }
//
// // whether the user has the permission to read the role "foo"
// sc, err := lgc.Authorize(admin, "roles:read", "foo")
// fmt.Println(sc, err) // 200, nil
func (lgc *Logic) Authorize(user *graylog.User, scope string, args ...string) (int, error) {
if user == nil {
return 200, nil
}
ok, err := lgc.store.Authorize(user, scope, args...)
if err != nil {
return 500, err
}
if ok {
return 200, nil
}
return 403, fmt.Errorf("authorization failure")
}