forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseclog.go
More file actions
217 lines (183 loc) · 5.42 KB
/
Copy pathseclog.go
File metadata and controls
217 lines (183 loc) · 5.42 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2026 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package seclog
import (
"fmt"
"sync"
"time"
"github.com/snapcore/snapd/logger"
)
// unknown is the placeholder for empty fields in descriptions.
const unknown = "<unknown>"
// Level is the importance or severity of a log event.
// The higher the level, the more severe the event.
type Level int
// Log levels.
const (
LevelDebug Level = 1
LevelInfo Level = 2
LevelWarn Level = 3
LevelError Level = 4
LevelCritical Level = 5
)
// String returns a name for the level.
func (l Level) String() string {
switch l {
case LevelDebug:
return "DEBUG"
case LevelInfo:
return "INFO"
case LevelWarn:
return "WARN"
case LevelError:
return "ERROR"
case LevelCritical:
return "CRITICAL"
default:
return fmt.Sprintf("UNKNOWN(%d)", int(l))
}
}
// SnapdUser represents the identity of a user for security log events.
type SnapdUser struct {
ID int64 `json:"snapd-user-id"`
StoreUserName string `json:"store-user-name"`
StoreUserEmail string `json:"store-user-email"`
Expiration time.Time `json:"expiration"`
}
// String returns a colon-separated description of the user in the form
// "<ID>:<StoreUserEmail>:<StoreUserName>". Fields that are unset use
// "<unknown>" as a placeholder. A zero ID means unset.
func (u SnapdUser) String() string {
id := unknown
if u.ID != 0 {
id = fmt.Sprintf("%d", u.ID)
}
email := unknown
if u.StoreUserEmail != "" {
email = u.StoreUserEmail
}
name := unknown
if u.StoreUserName != "" {
name = u.StoreUserName
}
return id + ":" + email + ":" + name
}
// Reason codes are stable identifiers for security audit events.
const (
ReasonInvalidCredentials = "invalid-credentials"
ReasonTwoFactorRequired = "two-factor-required"
ReasonTwoFactorFailed = "two-factor-failed"
ReasonInvalidAuthData = "invalid-auth-data"
ReasonPasswordPolicy = "password-policy"
ReasonInternal = "internal"
)
// Reason describes why a security event happened.
type Reason struct {
Code string `json:"code"`
Message string `json:"message"`
}
// String returns a colon-separated representation in the form
// "<Code>:<Message>". Fields that are unset use "unknown" as a
// placeholder.
func (r Reason) String() string {
code := unknown
if r.Code != "" {
code = r.Code
}
message := unknown
if r.Message != "" {
message = r.Message
}
return code + ":" + message
}
// Event describes a structured security audit event.
//
// A Version field may be added in the future if a log aggregator or
// consumer requires explicit schema versioning.
type Event struct {
Category string `json:"category"`
Name string `json:"event"`
Level Level `json:"level"`
}
// Attr is a key-value pair attached to a security log event.
type Attr struct {
Key string
Value any
}
// SecurityLogger defines the interface for emitting structured security
// audit events. Implementations receive a fully described [Event] and
// optional [Attr] values, so new event types can be added without
// changing the interface.
type SecurityLogger interface {
LogAny(event Event, description string, attrs ...Attr)
}
var (
globalLogger SecurityLogger = NewNopLogger()
// lock guards globalLogger reads and writes.
lock sync.Mutex
)
// Setup activates the security logger, replacing any previously
// configured logger.
//
// Setup is intended to be called once during early initialization.
func Setup(l SecurityLogger) {
lock.Lock()
defer lock.Unlock()
globalLogger = l
}
// LogLoggerEnabled logs that the security logger has been enabled.
func LogLoggerEnabled() {
logger.Noticef("security logger enabled")
lock.Lock()
defer lock.Unlock()
globalLogger.LogAny(
Event{Category: "SYS", Name: "sys_logging_enabled", Level: LevelInfo},
"Security logging enabled",
)
}
// LogLoggerDisabled logs that the security logger has been disabled.
func LogLoggerDisabled() {
logger.Noticef("security logger disabled")
lock.Lock()
defer lock.Unlock()
globalLogger.LogAny(
Event{Category: "SYS", Name: "sys_logging_disabled", Level: LevelCritical},
"Security logging disabled",
)
}
// LogLoginSuccess logs a successful login using the global security logger.
func LogLoginSuccess(user SnapdUser) {
lock.Lock()
defer lock.Unlock()
globalLogger.LogAny(
Event{Category: "AUTHN", Name: "authn_login_success", Level: LevelInfo},
fmt.Sprintf("User %s login success", user.String()),
Attr{Key: "user", Value: user},
)
}
// LogLoginFailure logs a failed login attempt using the global security logger.
func LogLoginFailure(user SnapdUser, reason Reason) {
lock.Lock()
defer lock.Unlock()
globalLogger.LogAny(
Event{Category: "AUTHN", Name: "authn_login_failure", Level: LevelWarn},
fmt.Sprintf("User %s login failure: %s", user.String(), reason.String()),
Attr{Key: "user", Value: user},
Attr{Key: "error", Value: reason},
)
}