2022-03-17 07:53:20 +01:00
|
|
|
// Copyright Project Harbor Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-17 05:11:04 +02:00
|
|
|
"errors"
|
2022-03-17 07:53:20 +01:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-11-24 11:07:42 +01:00
|
|
|
"github.com/beego/beego/v2/server/web/session"
|
2022-07-20 05:33:08 +02:00
|
|
|
|
2022-03-17 07:53:20 +01:00
|
|
|
"github.com/goharbor/harbor/src/lib/cache"
|
|
|
|
"github.com/goharbor/harbor/src/lib/cache/redis"
|
2022-11-15 04:30:01 +01:00
|
|
|
"github.com/goharbor/harbor/src/lib/config"
|
2022-03-17 07:53:20 +01:00
|
|
|
"github.com/goharbor/harbor/src/lib/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// HarborProviderName is the harbor session provider name
|
|
|
|
HarborProviderName = "harbor"
|
|
|
|
)
|
|
|
|
|
|
|
|
var harborpder = &Provider{}
|
|
|
|
|
2022-07-01 08:52:47 +02:00
|
|
|
// Store redis session store
|
|
|
|
type Store struct {
|
2022-03-17 07:53:20 +01:00
|
|
|
c cache.Cache
|
|
|
|
sid string
|
|
|
|
lock sync.RWMutex
|
|
|
|
values map[interface{}]interface{}
|
|
|
|
maxlifetime int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set value in redis session
|
2022-11-24 11:07:42 +01:00
|
|
|
func (rs *Store) Set(ctx context.Context, key, value interface{}) error {
|
2022-03-17 07:53:20 +01:00
|
|
|
rs.lock.Lock()
|
|
|
|
defer rs.lock.Unlock()
|
|
|
|
rs.values[key] = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get value in redis session
|
2022-11-24 11:07:42 +01:00
|
|
|
func (rs *Store) Get(ctx context.Context, key interface{}) interface{} {
|
2022-03-17 07:53:20 +01:00
|
|
|
rs.lock.RLock()
|
|
|
|
defer rs.lock.RUnlock()
|
|
|
|
if v, ok := rs.values[key]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete value in redis session
|
2022-11-24 11:07:42 +01:00
|
|
|
func (rs *Store) Delete(ctx context.Context, key interface{}) error {
|
2022-03-17 07:53:20 +01:00
|
|
|
rs.lock.Lock()
|
|
|
|
defer rs.lock.Unlock()
|
|
|
|
delete(rs.values, key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush clear all values in redis session
|
2022-11-24 11:07:42 +01:00
|
|
|
func (rs *Store) Flush(ctx context.Context) error {
|
2022-03-17 07:53:20 +01:00
|
|
|
rs.lock.Lock()
|
|
|
|
defer rs.lock.Unlock()
|
|
|
|
rs.values = make(map[interface{}]interface{})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionID get redis session id
|
2022-11-24 11:07:42 +01:00
|
|
|
func (rs *Store) SessionID(ctx context.Context) string {
|
2022-03-17 07:53:20 +01:00
|
|
|
return rs.sid
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionRelease save session values to redis
|
2022-11-24 11:07:42 +01:00
|
|
|
func (rs *Store) SessionRelease(ctx context.Context, w http.ResponseWriter) {
|
2022-03-17 07:53:20 +01:00
|
|
|
b, err := session.EncodeGob(rs.values)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-24 11:07:42 +01:00
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.TODO()
|
|
|
|
}
|
2022-11-15 04:30:01 +01:00
|
|
|
maxlifetime := time.Duration(systemSessionTimeout(ctx, rs.maxlifetime))
|
2022-03-17 07:53:20 +01:00
|
|
|
if rdb, ok := rs.c.(*redis.Cache); ok {
|
2022-11-15 04:30:01 +01:00
|
|
|
cmd := rdb.Client.Set(ctx, rs.sid, string(b), maxlifetime)
|
2022-03-17 07:53:20 +01:00
|
|
|
if cmd.Err() != nil {
|
|
|
|
log.Debugf("release session error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provider redis session provider
|
|
|
|
type Provider struct {
|
|
|
|
maxlifetime int64
|
|
|
|
c cache.Cache
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionInit init redis session
|
2022-11-24 11:07:42 +01:00
|
|
|
func (rp *Provider) SessionInit(ctx context.Context, maxlifetime int64, url string) (err error) {
|
2022-03-17 07:53:20 +01:00
|
|
|
rp.maxlifetime = maxlifetime * int64(time.Second)
|
|
|
|
rp.c, err = redis.New(cache.Options{Address: url, Codec: codec})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-24 11:07:42 +01:00
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.TODO()
|
|
|
|
}
|
|
|
|
return rp.c.Ping(ctx)
|
2022-03-17 07:53:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SessionRead read redis session by sid
|
2022-11-24 11:07:42 +01:00
|
|
|
func (rp *Provider) SessionRead(ctx context.Context, sid string) (session.Store, error) {
|
2022-03-17 07:53:20 +01:00
|
|
|
kv := make(map[interface{}]interface{})
|
2022-11-24 11:07:42 +01:00
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.TODO()
|
|
|
|
}
|
|
|
|
err := rp.c.Fetch(ctx, sid, &kv)
|
2023-08-17 05:11:04 +02:00
|
|
|
if err != nil && !errors.Is(err, cache.ErrNotFound) {
|
2022-03-17 07:53:20 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-01 08:52:47 +02:00
|
|
|
rs := &Store{c: rp.c, sid: sid, values: kv, maxlifetime: rp.maxlifetime}
|
2022-03-17 07:53:20 +01:00
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionExist check redis session exist by sid
|
2022-11-24 11:07:42 +01:00
|
|
|
func (rp *Provider) SessionExist(ctx context.Context, sid string) (bool, error) {
|
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.TODO()
|
|
|
|
}
|
|
|
|
return rp.c.Contains(ctx, sid), nil
|
2022-03-17 07:53:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SessionRegenerate generate new sid for redis session
|
2022-11-24 11:07:42 +01:00
|
|
|
func (rp *Provider) SessionRegenerate(ctx context.Context, oldsid, sid string) (session.Store, error) {
|
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.TODO()
|
|
|
|
}
|
2022-11-15 04:30:01 +01:00
|
|
|
maxlifetime := time.Duration(systemSessionTimeout(ctx, rp.maxlifetime))
|
2022-11-24 11:07:42 +01:00
|
|
|
if isExist, _ := rp.SessionExist(ctx, oldsid); !isExist {
|
|
|
|
err := rp.c.Save(ctx, sid, "", time.Duration(rp.maxlifetime))
|
2022-06-07 11:00:36 +02:00
|
|
|
if err != nil {
|
2022-08-03 07:51:26 +02:00
|
|
|
log.Debugf("failed to save sid=%s, where oldsid=%s, error: %s", sid, oldsid, err)
|
2022-06-07 11:00:36 +02:00
|
|
|
}
|
2022-03-17 07:53:20 +01:00
|
|
|
} else {
|
|
|
|
if rdb, ok := rp.c.(*redis.Cache); ok {
|
|
|
|
// redis has rename command
|
|
|
|
rdb.Rename(ctx, oldsid, sid)
|
2022-11-15 04:30:01 +01:00
|
|
|
rdb.Expire(ctx, sid, maxlifetime)
|
2022-03-17 07:53:20 +01:00
|
|
|
} else {
|
|
|
|
kv := make(map[interface{}]interface{})
|
|
|
|
err := rp.c.Fetch(ctx, sid, &kv)
|
2023-08-17 05:11:04 +02:00
|
|
|
if err != nil && !errors.Is(err, cache.ErrNotFound) {
|
2022-03-17 07:53:20 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-07 11:00:36 +02:00
|
|
|
err = rp.c.Delete(ctx, oldsid)
|
|
|
|
if err != nil {
|
2022-08-03 07:51:26 +02:00
|
|
|
log.Debugf("failed to delete oldsid=%s, error: %s", oldsid, err)
|
2022-06-07 11:00:36 +02:00
|
|
|
}
|
2022-11-15 04:30:01 +01:00
|
|
|
err = rp.c.Save(ctx, sid, kv, maxlifetime)
|
2022-06-07 11:00:36 +02:00
|
|
|
if err != nil {
|
2022-08-03 07:51:26 +02:00
|
|
|
log.Debugf("failed to save sid=%s, error: %s", sid, err)
|
2022-06-07 11:00:36 +02:00
|
|
|
}
|
2022-03-17 07:53:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 11:07:42 +01:00
|
|
|
return rp.SessionRead(ctx, sid)
|
2022-03-17 07:53:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SessionDestroy delete redis session by id
|
2022-11-24 11:07:42 +01:00
|
|
|
func (rp *Provider) SessionDestroy(ctx context.Context, sid string) error {
|
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.TODO()
|
|
|
|
}
|
|
|
|
return rp.c.Delete(ctx, sid)
|
2022-03-17 07:53:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SessionGC Implement method, no used.
|
2022-11-24 11:07:42 +01:00
|
|
|
func (rp *Provider) SessionGC(ctx context.Context) {
|
2022-03-17 07:53:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SessionAll return all activeSession
|
2022-11-24 11:07:42 +01:00
|
|
|
func (rp *Provider) SessionAll(ctx context.Context) int {
|
2022-03-17 07:53:20 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-11-15 04:30:01 +01:00
|
|
|
// systemSessionTimeout return the system session timeout set by user.
|
|
|
|
func systemSessionTimeout(ctx context.Context, beegoTimeout int64) int64 {
|
|
|
|
// read from system config if it is meaningful to support change session timeout in runtime for user.
|
|
|
|
// otherwise, use parameters beegoTimeout which set from beego.
|
|
|
|
timeout := beegoTimeout
|
|
|
|
if sysTimeout := config.SessionTimeout(ctx); sysTimeout > 0 {
|
|
|
|
timeout = sysTimeout * int64(time.Minute)
|
|
|
|
}
|
|
|
|
|
|
|
|
return timeout
|
|
|
|
}
|
|
|
|
|
2022-03-17 07:53:20 +01:00
|
|
|
func init() {
|
|
|
|
session.Register(HarborProviderName, harborpder)
|
|
|
|
}
|