Skip to content

Commit

Permalink
Rename session.SessionID to session.ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Waldz committed Sep 26, 2018
1 parent cd5c59d commit caaebbd
Show file tree
Hide file tree
Showing 19 changed files with 42 additions and 42 deletions.
4 changes: 2 additions & 2 deletions client/stats/sender.go
Expand Up @@ -33,7 +33,7 @@ const statsSenderLogPrefix = "[session-stats-sender] "
// RemoteStatsSender sends session stats to remote API server with a fixed sendInterval.
// Extra one send will be done on session disconnect.
type RemoteStatsSender struct {
sessionID session.SessionID
sessionID session.ID
providerID identity.Identity
consumerCountry string

Expand All @@ -46,7 +46,7 @@ type RemoteStatsSender struct {
}

// NewRemoteStatsSender function creates new session stats sender by given options
func NewRemoteStatsSender(statsKeeper SessionStatsKeeper, mysteriumClient server.Client, sessionID session.SessionID, providerID identity.Identity, signer identity.Signer, consumerCountry string, interval time.Duration) *RemoteStatsSender {
func NewRemoteStatsSender(statsKeeper SessionStatsKeeper, mysteriumClient server.Client, sessionID session.ID, providerID identity.Identity, signer identity.Signer, consumerCountry string, interval time.Duration) *RemoteStatsSender {
return &RemoteStatsSender{
sessionID: sessionID,
providerID: providerID,
Expand Down
6 changes: 3 additions & 3 deletions core/connection/manager.go
Expand Up @@ -221,7 +221,7 @@ func (manager *connectionManager) startOpenvpnClient(vpnSession session.Session,
return openvpnClient, nil
}

func (manager *connectionManager) waitForConnectedState(stateChannel <-chan openvpn.State, sessionID session.SessionID) error {
func (manager *connectionManager) waitForConnectedState(stateChannel <-chan openvpn.State, sessionID session.ID) error {
for {
select {
case state, more := <-stateChannel:
Expand All @@ -242,7 +242,7 @@ func (manager *connectionManager) waitForConnectedState(stateChannel <-chan open
}
}

func (manager *connectionManager) consumeOpenvpnStates(stateChannel <-chan openvpn.State, sessionID session.SessionID) {
func (manager *connectionManager) consumeOpenvpnStates(stateChannel <-chan openvpn.State, sessionID session.ID) {
for state := range stateChannel {
manager.onStateChanged(state, sessionID)
}
Expand All @@ -254,7 +254,7 @@ func (manager *connectionManager) consumeOpenvpnStates(stateChannel <-chan openv
log.Debug(managerLogPrefix, "State updater stopped")
}

func (manager *connectionManager) onStateChanged(state openvpn.State, sessionID session.SessionID) {
func (manager *connectionManager) onStateChanged(state openvpn.State, sessionID session.ID) {
manager.mutex.Lock()
defer manager.mutex.Unlock()

Expand Down
4 changes: 2 additions & 2 deletions core/connection/status.go
Expand Up @@ -38,14 +38,14 @@ const (
// ConnectionStatus holds connection state and session id of the connnection
type ConnectionStatus struct {
State State
SessionID session.SessionID
SessionID session.ID
}

func statusConnecting() ConnectionStatus {
return ConnectionStatus{Connecting, ""}
}

func statusConnected(sessionID session.SessionID) ConnectionStatus {
func statusConnected(sessionID session.ID) ConnectionStatus {
return ConnectionStatus{Connected, sessionID}
}

Expand Down
2 changes: 1 addition & 1 deletion server/interface.go
Expand Up @@ -33,5 +33,5 @@ type Client interface {
UnregisterProposal(proposal dto_discovery.ServiceProposal, signer identity.Signer) (err error)
PingProposal(proposal dto_discovery.ServiceProposal, signer identity.Signer) (err error)

SendSessionStats(sessionId session.SessionID, sessionStats dto.SessionStats, signer identity.Signer) (err error)
SendSessionStats(sessionId session.ID, sessionStats dto.SessionStats, signer identity.Signer) (err error)
}
2 changes: 1 addition & 1 deletion server/mysterium_api.go
Expand Up @@ -154,7 +154,7 @@ func (mApi *mysteriumAPI) FindProposals(providerID string) ([]dto_discovery.Serv
}

// SendSessionStats sends session statistics
func (mApi *mysteriumAPI) SendSessionStats(sessionID session.SessionID, sessionStats dto.SessionStats, signer identity.Signer) error {
func (mApi *mysteriumAPI) SendSessionStats(sessionID session.ID, sessionStats dto.SessionStats, signer identity.Signer) error {
path := fmt.Sprintf("sessions/%s/stats", sessionID)
req, err := requests.NewSignedPostRequest(mApi.discoveryAPIAddress, path, sessionStats, signer)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion server/mysterium_api_fake.go
Expand Up @@ -96,7 +96,7 @@ func (client *ClientFake) FindProposals(providerID string) (proposals []dto_disc
}

// SendSessionStats heartbeats that session is still active + session upload and download amounts
func (client *ClientFake) SendSessionStats(sessionId session.SessionID, sessionStats dto.SessionStats, signer identity.Signer) (err error) {
func (client *ClientFake) SendSessionStats(sessionId session.ID, sessionStats dto.SessionStats, signer identity.Signer) (err error) {
log.Info(mysteriumAPILogPrefix, "session stats sent: ", sessionId)

return nil
Expand Down
12 changes: 6 additions & 6 deletions services/openvpn/session/client_map.go
Expand Up @@ -27,19 +27,19 @@ import (
// SessionMap defines map of current sessions
type SessionMap interface {
Add(session.Session)
Find(session.SessionID) (session.Session, bool)
Remove(session.SessionID)
Find(session.ID) (session.Session, bool)
Remove(session.ID)
}

// clientMap extends current sessions with client id metadata from Openvpn
type clientMap struct {
sessions SessionMap
sessionClientIDs map[session.SessionID]int
sessionClientIDs map[session.ID]int
sessionMapLock sync.Mutex
}

// FindClientSession returns OpenVPN session instance by given session id
func (cm *clientMap) FindClientSession(clientID int, id session.SessionID) (session.Session, bool, error) {
func (cm *clientMap) FindClientSession(clientID int, id session.ID) (session.Session, bool, error) {
sessionInstance, sessionExist := cm.sessions.Find(id)
if !sessionExist {
return session.Session{}, false, errors.New("no underlying session exists, possible break-in attempt")
Expand All @@ -54,7 +54,7 @@ func (cm *clientMap) FindClientSession(clientID int, id session.SessionID) (sess
}

// UpdateClientSession updates OpenVPN session with clientID, returns false on clientID conflict
func (cm *clientMap) UpdateClientSession(clientID int, id session.SessionID) bool {
func (cm *clientMap) UpdateClientSession(clientID int, id session.ID) bool {
cm.sessionMapLock.Lock()
defer cm.sessionMapLock.Unlock()

Expand All @@ -67,7 +67,7 @@ func (cm *clientMap) UpdateClientSession(clientID int, id session.SessionID) boo
}

// RemoveSession removes given session from underlying session managers
func (cm *clientMap) RemoveSession(id session.SessionID) error {
func (cm *clientMap) RemoveSession(id session.ID) error {
cm.sessionMapLock.Lock()
defer cm.sessionMapLock.Unlock()

Expand Down
2 changes: 1 addition & 1 deletion services/openvpn/session/credentials_provider.go
Expand Up @@ -23,7 +23,7 @@ import (
)

// SignatureCredentialsProvider returns session id as username and id signed with given signer as password
func SignatureCredentialsProvider(sessionID session.SessionID, signer identity.Signer) func() (username string, password string, err error) {
func SignatureCredentialsProvider(sessionID session.ID, signer identity.Signer) func() (username string, password string, err error) {
return func() (username string, password string, err error) {
signature, err := signer.Sign([]byte(SignaturePrefix + sessionID))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion services/openvpn/session/manager_auth_middleware_test.go
Expand Up @@ -30,7 +30,7 @@ import (
var (
identityToExtract = identity.FromAddress("deadbeef")
validator = mockValidatorWithSession(identityToExtract, session.Session{
ID: session.SessionID("Boop!"),
ID: session.ID("Boop!"),
Config: "config_string",
ConsumerID: identityToExtract,
})
Expand Down
6 changes: 3 additions & 3 deletions services/openvpn/session/validator.go
Expand Up @@ -38,7 +38,7 @@ func NewValidator(sessionMap SessionMap, extractor identity.Extractor) *Validato
return &Validator{
clientMap: &clientMap{
sessions: sessionMap,
sessionClientIDs: make(map[session.SessionID]int),
sessionClientIDs: make(map[session.ID]int),
sessionMapLock: sync.Mutex{},
},
identityExtractor: extractor,
Expand All @@ -48,7 +48,7 @@ func NewValidator(sessionMap SessionMap, extractor identity.Extractor) *Validato
// Validate provides glue code for openvpn management interface to validate incoming client login request,
// it expects session id as username, and session signature signed by client as password
func (v *Validator) Validate(clientID int, sessionString, signatureString string) (bool, error) {
sessionID := session.SessionID(sessionString)
sessionID := session.ID(sessionString)
currentSession, found, err := v.clientMap.FindClientSession(clientID, sessionID)

if err != nil {
Expand All @@ -69,7 +69,7 @@ func (v *Validator) Validate(clientID int, sessionString, signatureString string

// Cleanup removes session from underlying session managers
func (v *Validator) Cleanup(sessionString string) error {
sessionID := session.SessionID(sessionString)
sessionID := session.ID(sessionString)

return v.clientMap.RemoveSession(sessionID)
}
4 changes: 2 additions & 2 deletions services/openvpn/session/validator_mocks.go
Expand Up @@ -65,11 +65,11 @@ type mockSessions struct {
func (sessions *mockSessions) Add(sessionInstance session.Session) {
}

func (sessions *mockSessions) Find(session.SessionID) (session.Session, bool) {
func (sessions *mockSessions) Find(session.ID) (session.Session, bool) {
return sessions.OnFindReturnSession, sessions.OnFindReturnSuccess
}

func (sessions *mockSessions) Remove(session.SessionID) {
func (sessions *mockSessions) Remove(session.ID) {
sessions.OnFindReturnSession = session.Session{}
sessions.OnFindReturnSuccess = false
}
2 changes: 1 addition & 1 deletion services/openvpn/session/validator_test.go
Expand Up @@ -32,7 +32,7 @@ const (
var (
identityExisting = identity.FromAddress("deadbeef")
sessionExisting = session.Session{
ID: session.SessionID(sessionExistingString),
ID: session.ID(sessionExistingString),
Config: "config_string",
ConsumerID: identityExisting,
}
Expand Down
2 changes: 1 addition & 1 deletion session/create_dto.go
Expand Up @@ -39,6 +39,6 @@ type CreateResponse struct {

// SessionDto structure represents session information data within session creation response (session id and configuration options for underlaying service type)
type SessionDto struct {
ID SessionID `json:"id"`
ID ID `json:"id"`
Config json.RawMessage `json:"config"`
}
6 changes: 3 additions & 3 deletions session/dto.go
Expand Up @@ -19,12 +19,12 @@ package session

import "github.com/mysteriumnetwork/node/identity"

// SessionID represents session id type
type SessionID string
// ID represents session id type
type ID string

// Session structure holds all required information about current session between service consumer and provider
type Session struct {
ID SessionID
ID ID
Config ServiceConfiguration
ConsumerID identity.Identity
}
Expand Down
6 changes: 3 additions & 3 deletions session/generator.go
Expand Up @@ -19,7 +19,7 @@ package session

import "github.com/satori/go.uuid"

// GenerateUUID method returns SessionID based on random UUID
func GenerateUUID() SessionID {
return SessionID(uuid.NewV4().String())
// GenerateUUID method returns ID based on random UUID
func GenerateUUID() ID {
return ID(uuid.NewV4().String())
}
2 changes: 1 addition & 1 deletion session/manager.go
Expand Up @@ -27,7 +27,7 @@ import (
type ServiceConfigProvider func() (ServiceConfiguration, error)

// IDGenerator defines method for session id generation
type IDGenerator func() SessionID
type IDGenerator func() ID

// SaveCallback stores newly started sessions
type SaveCallback func(Session)
Expand Down
4 changes: 2 additions & 2 deletions session/manager_test.go
Expand Up @@ -25,7 +25,7 @@ import (
)

var (
expectedID = SessionID("mocked-id")
expectedID = ID("mocked-id")
expectedSession = Session{
ID: expectedID,
Config: mockedVPNConfig,
Expand All @@ -46,7 +46,7 @@ func saveSession(sessionInstance Session) {

func TestManager_Create(t *testing.T) {
manager := NewManager(
func() SessionID {
func() ID {
return expectedID
},
mockedConfigProvider,
Expand Down
8 changes: 4 additions & 4 deletions session/storage_memory.go
Expand Up @@ -24,14 +24,14 @@ import (
// NewStorageMemory initiates new session storage
func NewStorageMemory() *StorageMemory {
return &StorageMemory{
sessionMap: make(map[SessionID]Session),
sessionMap: make(map[ID]Session),
lock: sync.Mutex{},
}
}

// StorageMemory maintains a map of session id -> session
type StorageMemory struct {
sessionMap map[SessionID]Session
sessionMap map[ID]Session
lock sync.Mutex
}

Expand All @@ -44,13 +44,13 @@ func (storage *StorageMemory) Add(sessionInstance Session) {
}

// Find returns underlying session instance
func (storage *StorageMemory) Find(id SessionID) (Session, bool) {
func (storage *StorageMemory) Find(id ID) (Session, bool) {
sessionInstance, found := storage.sessionMap[id]
return sessionInstance, found
}

// Remove removes given session from underlying storage
func (storage *StorageMemory) Remove(id SessionID) {
func (storage *StorageMemory) Remove(id ID) {
storage.lock.Lock()
defer storage.lock.Unlock()

Expand Down
8 changes: 4 additions & 4 deletions session/storage_memory_test.go
Expand Up @@ -25,7 +25,7 @@ import (

var (
sessionExisting = Session{
ID: SessionID("mocked-id"),
ID: ID("mocked-id"),
}
)

Expand All @@ -40,15 +40,15 @@ func TestStorage_FindSession_Existing(t *testing.T) {
func TestStorage_FindSession_Unknown(t *testing.T) {
storage := mockStorage(sessionExisting)

sessionInstance, found := storage.Find(SessionID("unknown-id"))
sessionInstance, found := storage.Find(ID("unknown-id"))
assert.False(t, found)
assert.Exactly(t, Session{}, sessionInstance)
}

func TestStorage_Add(t *testing.T) {
storage := mockStorage(sessionExisting)
sessionNew := Session{
ID: SessionID("new-id"),
ID: ID("new-id"),
}

storage.Add(sessionNew)
Expand All @@ -65,7 +65,7 @@ func TestStorage_Remove(t *testing.T) {

func mockStorage(sessionInstance Session) *StorageMemory {
return &StorageMemory{
sessionMap: map[SessionID]Session{
sessionMap: map[ID]Session{
sessionInstance.ID: sessionInstance,
},
}
Expand Down

0 comments on commit caaebbd

Please sign in to comment.