Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
777bd9ea
Commit
777bd9ea
authored
Jan 21, 2019
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
adds cleanup job for old session tokens
parent
f040f9a4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
3 deletions
+80
-3
pkg/services/auth/auth_token.go
+5
-3
pkg/services/auth/session_cleanup.go
+38
-0
pkg/services/auth/session_cleanup_test.go
+37
-0
No files found.
pkg/services/auth/auth_token.go
View file @
777bd9ea
...
...
@@ -8,6 +8,7 @@ import (
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/serverlock"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/registry"
...
...
@@ -29,8 +30,9 @@ var (
// UserAuthTokenService are used for generating and validating user auth tokens
type
UserAuthTokenService
struct
{
SQLStore
*
sqlstore
.
SqlStore
`inject:""`
log
log
.
Logger
SQLStore
*
sqlstore
.
SqlStore
`inject:""`
ServerLockService
*
serverlock
.
ServerLockService
`inject:""`
log
log
.
Logger
}
// Init this service
...
...
@@ -239,7 +241,7 @@ func (s *UserAuthTokenService) RefreshToken(token *models.UserAuthToken, clientI
}
affected
,
_
:=
res
.
RowsAffected
()
s
.
log
.
Debug
(
"rotated"
,
"affected"
,
affected
,
"auth_token_id"
,
token
.
Id
,
"userId"
,
token
.
UserId
,
"user_agent"
,
userAgent
,
"client_ip"
,
clientIP
)
s
.
log
.
Debug
(
"rotated"
,
"affected"
,
affected
,
"auth_token_id"
,
token
.
Id
,
"userId"
,
token
.
UserId
)
if
affected
>
0
{
token
.
UnhashedToken
=
newToken
return
true
,
nil
...
...
pkg/services/auth/session_cleanup.go
0 → 100644
View file @
777bd9ea
package
auth
import
(
"context"
"time"
)
func
(
srv
*
UserAuthTokenService
)
Run
(
ctx
context
.
Context
)
error
{
ticker
:=
time
.
NewTicker
(
time
.
Hour
*
12
)
deleteSessionAfter
:=
time
.
Hour
*
24
*
7
*
30
for
{
select
{
case
<-
ticker
.
C
:
srv
.
ServerLockService
.
LockAndExecute
(
ctx
,
"delete old sessions"
,
time
.
Hour
*
12
,
func
()
{
srv
.
deleteOldSession
(
deleteSessionAfter
)
})
case
<-
ctx
.
Done
()
:
return
ctx
.
Err
()
}
}
}
func
(
srv
*
UserAuthTokenService
)
deleteOldSession
(
deleteSessionAfter
time
.
Duration
)
(
int64
,
error
)
{
sql
:=
`DELETE from user_auth_token WHERE rotated_at < ?`
deleteBefore
:=
getTime
()
.
Add
(
-
deleteSessionAfter
)
res
,
err
:=
srv
.
SQLStore
.
NewSession
()
.
Exec
(
sql
,
deleteBefore
.
Unix
())
if
err
!=
nil
{
return
0
,
err
}
affected
,
err
:=
res
.
RowsAffected
()
srv
.
log
.
Info
(
"deleted old sessions"
,
"count"
,
affected
)
return
affected
,
err
}
pkg/services/auth/session_cleanup_test.go
0 → 100644
View file @
777bd9ea
package
auth
import
(
"fmt"
"testing"
"time"
"github.com/grafana/grafana/pkg/models"
.
"github.com/smartystreets/goconvey/convey"
)
func
TestUserAuthTokenCleanup
(
t
*
testing
.
T
)
{
Convey
(
"Test user auth token cleanup"
,
t
,
func
()
{
ctx
:=
createTestContext
(
t
)
insertToken
:=
func
(
token
string
,
prev
string
,
rotatedAt
int64
)
{
ut
:=
models
.
UserAuthToken
{
AuthToken
:
token
,
PrevAuthToken
:
prev
,
RotatedAt
:
rotatedAt
,
UserAgent
:
""
,
ClientIp
:
""
}
_
,
err
:=
ctx
.
sqlstore
.
NewSession
()
.
Insert
(
&
ut
)
So
(
err
,
ShouldBeNil
)
}
// insert three old tokens that should be deleted
for
i
:=
0
;
i
<
3
;
i
++
{
insertToken
(
fmt
.
Sprintf
(
"oldA%d"
,
i
),
fmt
.
Sprintf
(
"oldB%d"
,
i
),
int64
(
i
))
}
// insert three active tokens that should not be deleted
for
i
:=
0
;
i
<
3
;
i
++
{
insertToken
(
fmt
.
Sprintf
(
"newA%d"
,
i
),
fmt
.
Sprintf
(
"newB%d"
,
i
),
getTime
()
.
Unix
())
}
affected
,
err
:=
ctx
.
tokenService
.
deleteOldSession
(
time
.
Hour
)
So
(
err
,
ShouldBeNil
)
So
(
affected
,
ShouldEqual
,
3
)
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment