mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 04:07:31 +00:00
feat: add activity logs on user actions with authentication, resource… (#6748)
* feat: add activity logs on user actions with authentication, resourceAPI and sessionAPI * feat: add activity logs on user actions with authentication, resourceAPI and sessionAPI * feat: add activity logs on user actions with authentication, resourceAPI and sessionAPI * feat: add activity logs on user actions with authentication, resourceAPI and sessionAPI * feat: add activity logs on user actions with authentication, resourceAPI and sessionAPI * fix: add unit tests to info package for context changes * fix: add activity_interceptor.go suggestion Co-authored-by: Tim Möhlmann <tim+github@zitadel.com> * fix: refactoring and fixes through PR review * fix: add auth service to lists of resourceAPIs --------- Co-authored-by: Tim Möhlmann <tim+github@zitadel.com> Co-authored-by: Fabi <fabienne@zitadel.com>
This commit is contained in:
43
internal/api/info/info.go
Normal file
43
internal/api/info/info.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package info
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type activityInfoKey struct{}
|
||||
|
||||
type ActivityInfo struct {
|
||||
Method string
|
||||
Path string
|
||||
RequestMethod string
|
||||
}
|
||||
|
||||
func (a *ActivityInfo) IntoContext(ctx context.Context) context.Context {
|
||||
return context.WithValue(ctx, activityInfoKey{}, a)
|
||||
}
|
||||
|
||||
func ActivityInfoFromContext(ctx context.Context) *ActivityInfo {
|
||||
m := ctx.Value(activityInfoKey{})
|
||||
if m == nil {
|
||||
return &ActivityInfo{}
|
||||
}
|
||||
ai, ok := m.(*ActivityInfo)
|
||||
if !ok {
|
||||
return &ActivityInfo{}
|
||||
}
|
||||
return ai
|
||||
}
|
||||
|
||||
func (a *ActivityInfo) SetMethod(method string) *ActivityInfo {
|
||||
a.Method = method
|
||||
return a
|
||||
}
|
||||
func (a *ActivityInfo) SetPath(path string) *ActivityInfo {
|
||||
a.Path = path
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *ActivityInfo) SetRequestMethod(method string) *ActivityInfo {
|
||||
a.RequestMethod = method
|
||||
return a
|
||||
}
|
117
internal/api/info/info_test.go
Normal file
117
internal/api/info/info_test.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package info
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_ActivityInfo(t *testing.T) {
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
ok bool
|
||||
path string
|
||||
method string
|
||||
requestMethod string
|
||||
}
|
||||
type want struct {
|
||||
ok bool
|
||||
path string
|
||||
method string
|
||||
requestMethod string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want want
|
||||
}{
|
||||
{
|
||||
"already set",
|
||||
args{
|
||||
ctx: ctxWithActivityInfo(context.Background(), "set", "set", "set"),
|
||||
ok: false,
|
||||
},
|
||||
want{
|
||||
ok: true,
|
||||
path: "set",
|
||||
method: "set",
|
||||
requestMethod: "set",
|
||||
},
|
||||
},
|
||||
{
|
||||
"not set, empty",
|
||||
args{
|
||||
ctx: context.Background(),
|
||||
ok: false,
|
||||
},
|
||||
want{
|
||||
ok: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
"set empty",
|
||||
args{
|
||||
ctx: context.Background(),
|
||||
ok: true,
|
||||
},
|
||||
want{
|
||||
ok: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
"set",
|
||||
args{
|
||||
ctx: context.Background(),
|
||||
ok: true,
|
||||
path: "set",
|
||||
method: "set",
|
||||
requestMethod: "set",
|
||||
},
|
||||
want{
|
||||
ok: true,
|
||||
path: "set",
|
||||
method: "set",
|
||||
requestMethod: "set",
|
||||
},
|
||||
},
|
||||
{
|
||||
"reset",
|
||||
args{
|
||||
ctx: ctxWithActivityInfo(context.Background(), "set", "set", "set"),
|
||||
ok: true,
|
||||
path: "set2",
|
||||
method: "set2",
|
||||
requestMethod: "set2",
|
||||
},
|
||||
want{
|
||||
ok: true,
|
||||
path: "set2",
|
||||
method: "set2",
|
||||
requestMethod: "set2",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ai := &ActivityInfo{}
|
||||
ai.SetMethod(tt.args.method).SetPath(tt.args.path).SetRequestMethod(tt.args.requestMethod)
|
||||
if tt.args.ok {
|
||||
tt.args.ctx = ai.IntoContext(tt.args.ctx)
|
||||
}
|
||||
|
||||
res := ActivityInfoFromContext(tt.args.ctx)
|
||||
if tt.want.ok {
|
||||
assert.NotNil(t, res)
|
||||
}
|
||||
assert.Equal(t, tt.want.path, res.Path)
|
||||
assert.Equal(t, tt.want.method, res.Method)
|
||||
assert.Equal(t, tt.want.requestMethod, res.RequestMethod)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func ctxWithActivityInfo(ctx context.Context, method, path, requestMethod string) context.Context {
|
||||
ai := &ActivityInfo{}
|
||||
return ai.SetPath(path).SetRequestMethod(requestMethod).SetMethod(method).IntoContext(ctx)
|
||||
}
|
Reference in New Issue
Block a user