v1.16.2
This commit is contained in:
53
pkg/cloudbroker/session/filter.go
Normal file
53
pkg/cloudbroker/session/filter.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package session
|
||||
|
||||
// FilterByID returns ListSession with specified ID.
|
||||
func (lsession ListSession) FilterByID(id string) ListSession {
|
||||
predicate := func(rsession ItemSession) bool {
|
||||
return rsession.PublicSessionID == id
|
||||
}
|
||||
|
||||
return lsession.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByName returns ListSession with specified User.
|
||||
func (lsession ListSession) FilterByUser(user string) ListSession {
|
||||
predicate := func(rsession ItemSession) bool {
|
||||
return rsession.User == user
|
||||
}
|
||||
|
||||
return lsession.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByObjStatus returns ListSession with specified IP.
|
||||
func (lsession ListSession) FilterByIP(ip string) ListSession {
|
||||
predicate := func(rsession ItemSession) bool {
|
||||
return rsession.IP == ip
|
||||
}
|
||||
|
||||
return lsession.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterFunc allows filtering ListSession based on user-specified predicate.
|
||||
func (lsession ListSession) FilterFunc(predicate func(session ItemSession) bool) ListSession {
|
||||
var result ListSession
|
||||
|
||||
for _, item := range lsession.Data {
|
||||
if predicate(item) {
|
||||
result.Data = append(result.Data, item)
|
||||
}
|
||||
}
|
||||
|
||||
result.EntryCount = uint64(len(result.Data))
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// FindOne returns first found ItemSession
|
||||
// If none was found, returns an empty struct.
|
||||
func (lsession ListSession) FindOne() ItemSession {
|
||||
if len(lsession.Data) == 0 {
|
||||
return ItemSession{}
|
||||
}
|
||||
|
||||
return lsession.Data[0]
|
||||
}
|
||||
54
pkg/cloudbroker/session/filter_test.go
Normal file
54
pkg/cloudbroker/session/filter_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package session
|
||||
|
||||
import "testing"
|
||||
|
||||
var sessions = ListSession{
|
||||
Data: []ItemSession{
|
||||
{
|
||||
PublicSessionID: "06674bdf-13f1-4f78-a40a-242fd76e4cd1",
|
||||
IP: "10.244.0.243",
|
||||
User: "someuser",
|
||||
},
|
||||
{
|
||||
PublicSessionID: "06674bdf-13f1-4f78-a40a-242fd76e4cd5",
|
||||
IP: "10.244.0.242",
|
||||
User: "someuser2",
|
||||
},
|
||||
{
|
||||
PublicSessionID: "06674bdf-13f1-4f78-a40a-242fd76e4cd7",
|
||||
IP: "10.244.0.243",
|
||||
User: "someuser2",
|
||||
},
|
||||
},
|
||||
EntryCount: 3,
|
||||
}
|
||||
|
||||
func TestFilterByID(t *testing.T) {
|
||||
actual := sessions.FilterByID("06674bdf-13f1-4f78-a40a-242fd76e4cd1").FindOne()
|
||||
|
||||
if actual.PublicSessionID != "06674bdf-13f1-4f78-a40a-242fd76e4cd1" {
|
||||
t.Fatal("expected ID 1, found: ", actual.PublicSessionID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByUser(t *testing.T) {
|
||||
actual := sessions.FilterByUser("someuser").FindOne()
|
||||
|
||||
if actual.User != "someuser" {
|
||||
t.Fatal("expected User 'someuser', found: ", actual.User)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByIP(t *testing.T) {
|
||||
actual := sessions.FilterByIP("10.244.0.243")
|
||||
|
||||
if len(actual.Data) != 2 {
|
||||
t.Fatal("expected 2 found, actual: ", len(actual.Data))
|
||||
}
|
||||
|
||||
for _, item := range actual.Data {
|
||||
if item.IP != "10.244.0.243" {
|
||||
t.Fatal("expected IP '10.244.0.243', found: ", item.IP)
|
||||
}
|
||||
}
|
||||
}
|
||||
46
pkg/cloudbroker/session/get.go
Normal file
46
pkg/cloudbroker/session/get.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
||||
)
|
||||
|
||||
// GetRequest struct to get Session parameters
|
||||
type GetRequest struct {
|
||||
// Storage endpoint provider ID
|
||||
// Required: true
|
||||
PublicSessionID string `url:"public_session_id" json:"public_session_id" validate:"required"`
|
||||
}
|
||||
|
||||
// Get gets Session parameters as a RecordSession struct
|
||||
func (s Session) Get(ctx context.Context, req GetRequest) (*RecordSession, error) {
|
||||
res, err := s.GetRaw(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := RecordSession{}
|
||||
|
||||
err = json.Unmarshal(res, &info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
// GetRaw gets Session parameters as an array of bytes
|
||||
func (s Session) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
|
||||
err := validators.ValidateRequest(req)
|
||||
if err != nil {
|
||||
return nil, validators.ValidationErrors(validators.GetErrors(err))
|
||||
}
|
||||
|
||||
url := "/cloudbroker/session/get"
|
||||
|
||||
res, err := s.client.DecortApiCall(ctx, http.MethodGet, url, req)
|
||||
return res, err
|
||||
}
|
||||
10
pkg/cloudbroker/session/ids.go
Normal file
10
pkg/cloudbroker/session/ids.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package session
|
||||
|
||||
// IDs gets array of PublicSessionIDs from ListSession struct
|
||||
func (ls ListSession) IDs() []string {
|
||||
res := make([]string, 0, len(ls.Data))
|
||||
for _, s := range ls.Data {
|
||||
res = append(res, s.PublicSessionID)
|
||||
}
|
||||
return res
|
||||
}
|
||||
79
pkg/cloudbroker/session/list.go
Normal file
79
pkg/cloudbroker/session/list.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
||||
)
|
||||
|
||||
type ListRequest struct {
|
||||
// Filter by username
|
||||
User string `url:"user,omitempty" json:"user,omitempty"`
|
||||
|
||||
// Filter by authorization system (bvs, decs3o, password)
|
||||
AuthorizeSystem string `url:"authorize_system,omitempty" json:"authorize_system,omitempty"`
|
||||
|
||||
// Filter by public session ID (UUID)
|
||||
PublicSessionID string `url:"public_session_id,omitempty" json:"public_session_id,omitempty"`
|
||||
|
||||
// Filter by sub from JWT
|
||||
Sub string `url:"sub,omitempty" json:"sub,omitempty"`
|
||||
|
||||
// Filter by sid from JWT
|
||||
SID uint64 `url:"sid,omitempty" json:"sid,omitempty"`
|
||||
|
||||
// Filter sessions with login_time >= timestamp (Unix timestamp)
|
||||
TimestampAt uint64 `url:"timestamp_at,omitempty" json:"timestamp_at,omitempty"`
|
||||
|
||||
// Filter sessions with login_time <= timestamp (Unix timestamp)
|
||||
TimestampTo uint64 `url:"timestamp_to,omitempty" json:"timestamp_to,omitempty"`
|
||||
|
||||
// Filter sessions active at this timestamp (login_time <= T < logout_time/expires_at)
|
||||
ActiveAt uint64 `url:"active_at,omitempty" json:"active_at,omitempty"`
|
||||
|
||||
// Page size
|
||||
// Required: false
|
||||
Size uint64 `url:"size,omitempty" json:"size,omitempty"`
|
||||
|
||||
// Page number
|
||||
// Required: false
|
||||
Page uint64 `url:"page,omitempty" json:"page,omitempty"`
|
||||
|
||||
// Sort by one of supported fields, format +|-(field)
|
||||
// Required: false
|
||||
SortBy string `url:"sortBy,omitempty" json:"sortBy,omitempty"`
|
||||
}
|
||||
|
||||
// List gets list of sessions as a ListSession struct
|
||||
func (s Session) List(ctx context.Context, req ListRequest) (*ListSession, error) {
|
||||
|
||||
res, err := s.ListRaw(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := ListSession{}
|
||||
|
||||
err = json.Unmarshal(res, &list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &list, nil
|
||||
}
|
||||
|
||||
// ListRaw gets list of sessions as an array of bytes
|
||||
func (s Session) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {
|
||||
|
||||
err := validators.ValidateRequest(req)
|
||||
if err != nil {
|
||||
return nil, validators.ValidationErrors(validators.GetErrors(err))
|
||||
}
|
||||
|
||||
url := "/cloudbroker/session/list"
|
||||
|
||||
res, err := s.client.DecortApiCall(ctx, http.MethodGet, url, req)
|
||||
return res, err
|
||||
}
|
||||
85
pkg/cloudbroker/session/models.go
Normal file
85
pkg/cloudbroker/session/models.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package session
|
||||
|
||||
// Main information about a session
|
||||
type RecordSession struct {
|
||||
// Public session ID
|
||||
PublicSessionID string `json:"public_session_id"`
|
||||
|
||||
// Username
|
||||
User string `json:"user"`
|
||||
|
||||
// Subject from JWT
|
||||
Sub string `json:"sub"`
|
||||
|
||||
// Session ID from JWT
|
||||
SID string `json:"sid"`
|
||||
|
||||
// Authorization system
|
||||
AuthorizeSystem string `json:"authorize_system"`
|
||||
|
||||
// Client IP address
|
||||
IP string `json:"ip"`
|
||||
|
||||
// Client User-Agent
|
||||
UserAgent string `json:"user_agent"`
|
||||
|
||||
// First login time
|
||||
LoginTime string `json:"login_time"`
|
||||
|
||||
// Last activity time
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
|
||||
// Logout time
|
||||
LogoutTime string `json:"logout_time"`
|
||||
|
||||
// Session expiration time
|
||||
Expires string `json:"expires"`
|
||||
|
||||
// OAuth provider type
|
||||
OAuthType string `json:"oauth_type"`
|
||||
}
|
||||
|
||||
// List of sessions
|
||||
type ListSession struct {
|
||||
// Data
|
||||
Data []ItemSession `json:"data"`
|
||||
|
||||
// Entry count
|
||||
EntryCount uint64 `json:"entryCount"`
|
||||
}
|
||||
|
||||
// A session item
|
||||
type ItemSession struct {
|
||||
// Public session ID
|
||||
PublicSessionID string `json:"public_session_id"`
|
||||
|
||||
// Username
|
||||
User string `json:"user"`
|
||||
|
||||
// Subject from JWT
|
||||
Sub string `json:"sub"`
|
||||
|
||||
// Session ID from JWT
|
||||
SID string `json:"sid"`
|
||||
|
||||
// Authorization system
|
||||
AuthorizeSystem string `json:"authorize_system"`
|
||||
|
||||
// Client IP address
|
||||
IP string `json:"ip"`
|
||||
|
||||
// Client User-Agent
|
||||
UserAgent string `json:"user_agent"`
|
||||
|
||||
// First login time
|
||||
LoginTime string `json:"login_time"`
|
||||
|
||||
// Last activity time
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
|
||||
// Logout time
|
||||
LogoutTime string `json:"logout_time"`
|
||||
|
||||
// Session expiration time
|
||||
Expires string `json:"expires"`
|
||||
}
|
||||
18
pkg/cloudbroker/session/session.go
Normal file
18
pkg/cloudbroker/session/session.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// Operator actions for handling interventions on a session
|
||||
package session
|
||||
|
||||
import (
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/interfaces"
|
||||
)
|
||||
|
||||
// Structure for creating request to session
|
||||
type Session struct {
|
||||
client interfaces.Caller
|
||||
}
|
||||
|
||||
// Builder for Session endpoints
|
||||
func New(client interfaces.Caller) *Session {
|
||||
return &Session{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user