Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/mux v1.7.4
github.com/kr/pretty v0.1.0 // indirect
github.com/oklog/ulid v1.3.1
github.com/rs/zerolog v1.19.0
github.com/stretchr/testify v1.6.1
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
4 changes: 4 additions & 0 deletions internal/lockclient/cache/lru_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,13 @@ func (lru *LRUCache) deleteElementFromMap(key interface{}) error {
}
return nil
}

// printMap prints the LRU map and is concurrency safe.
func (lru *LRUCache) printMap() {
lru.mu.Lock()
for k, v := range lru.m {
fmt.Printf("Key: %v, Value: ", k)
fmt.Printf("LN: %v, RN: %v, NodeKey: %v\n", v.Left(), v.Right(), v.Key())
}
lru.mu.Unlock()
}
13 changes: 10 additions & 3 deletions internal/lockclient/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package lockclient

import "github.com/SystemBuilders/LocKey/internal/lockservice"
import (
"github.com/SystemBuilders/LocKey/internal/lockclient/session"
"github.com/SystemBuilders/LocKey/internal/lockservice"
)

// Client describes a client that can be used to interact with
// the Lockey lockservice. The client can start the lockservice
Expand All @@ -17,14 +20,18 @@ type Client interface {
// to do so. Starting the service should be a non-blocking call
// and return as soon as the server is started and setup.
StartService(Config) error
// Connect allows the user process to establish a connection
// with the client. This returns an ID of the session that
// results from the connection.
Connect() session.Session
// Acquire can be used to acquire a lock on Lockey. This
// implementation interacts with the underlying server and
// provides the service.
Acquire(lockservice.Descriptors) error
Acquire(lockservice.Object, session.Session) error
// Release can be used to release a lock on Lockey. This
// implementation interacts with the underlying server and
// provides the service.
Release(lockservice.Descriptors) error
Release(lockservice.Object, session.Session) error
}

// Config describes the configuration for the lockservice to run on.
Expand Down
13 changes: 13 additions & 0 deletions internal/lockclient/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package lockclient

// Error provides constant error strings to the driver functions.
type Error string

func (e Error) Error() string { return string(e) }

// Constant errors.
// Rule of thumb, all errors start with a small letter and end with no full stop.
const (
ErrSessionNonExistent = Error("the session related to this process doesn't exist")
ErrSessionExpired = Error("session expired")
)
64 changes: 64 additions & 0 deletions internal/lockclient/id/id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package id

import (
"fmt"
"log"
"math/rand"
"sync"
"time"

"github.com/oklog/ulid"
)

// ID describes a general identifier. An ID has to be unique application-wide.
// IDs must not be re-used.
type ID interface {
fmt.Stringer
Bytes() []byte
}

var _ ID = (*id)(nil)

type id ulid.ULID

var (
lock sync.Mutex
randSource = rand.New(rand.NewSource(time.Now().UnixNano()))
entropy = ulid.Monotonic(randSource, 0)
)

// Create creates a globally unique ID. This function is safe for concurrent
// use.
func Create() ID {
lock.Lock()
defer lock.Unlock()

genID, err := ulid.New(ulid.Now(), entropy)
if err != nil {
// For this to happen, the random module would have to fail. Since we
// use Go's pseudo RNG, which just jumps around a few numbers, instead
// of using crypto/rand, and we also made this function safe for
// concurrent use, this is nearly impossible to happen. However, with
// the current version of oklog/ulid v1.3.1, this will also break after
// 2121-04-11 11:53:25.01172576 UTC.
log.Fatal(fmt.Errorf("new ulid: %w", err))
}
return id(genID)
}

// Parse parses an ID from a byte slice.
func Parse(idBytes []byte) (ID, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't understand what this function does exactly ? Why are there two version of an ID - string and bytes?

Also, it doesn't seem to be used elsewhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's recommended to pass the ID in type ID but sometimes we do need its string form. Thus bytes.

parsed, err := ulid.Parse(string(idBytes))
if err != nil {
return nil, fmt.Errorf("parse: %w", err)
}
return id(parsed), nil
}

func (id id) String() string {
return ulid.ULID(id).String()
}

func (id id) Bytes() []byte {
return []byte(id.String())
}
18 changes: 18 additions & 0 deletions internal/lockclient/session/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package session

import "github.com/SystemBuilders/LocKey/internal/lockclient/id"

// Session captures all necessary parameters necessary to
// describe a session with the lockservice in the lockclient.
type Session interface {
// SessionID is the unique ID that represents this session.
// This will be used in every transaction for validating the user.
SessionID() id.ID
// ClientID is the ID of the client that will be created when
// the client is created. This acts as a second layer check along
// with the sessionID.
ClientID() id.ID
// ProcessID the unique ID assigned for the process by the client.
// This will be the third layer check in the security mechanism.
ProcessID() id.ID
}
38 changes: 38 additions & 0 deletions internal/lockclient/session/simple_session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package session

import (
"github.com/SystemBuilders/LocKey/internal/lockclient/id"
)

var _ Session = (*SimpleSession)(nil)

// SimpleSession implements a session.
type SimpleSession struct {
sessionID id.ID
clientID id.ID
processID id.ID
}

// SessionID returns the sessionID of the SimpleSession.
func (s *SimpleSession) SessionID() id.ID {
return s.sessionID
}

// ClientID returns the clientID of the SimpleSession.
func (s *SimpleSession) ClientID() id.ID {
return s.clientID
}

// ProcessID returns the processID of the SimpleSession
func (s *SimpleSession) ProcessID() id.ID {
return s.processID
}

// NewSession returns a new instance of a session with the given parameters.
func NewSession(sessionID, clientID, processID id.ID) Session {
return &SimpleSession{
sessionID: sessionID,
clientID: clientID,
processID: processID,
}
}
Loading