Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add peer login expiration #682

Merged
merged 8 commits into from
Feb 13, 2023
Merged
Changes from 1 commit
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
Next Next commit
Add peer login expiration
braginini committed Feb 10, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit f9a99e140e1958836a0a7bffa92f0eb5ab7af9c9
5 changes: 5 additions & 0 deletions management/server/grpcserver.go
Original file line number Diff line number Diff line change
@@ -354,6 +354,11 @@ func (s *GRPCServer) Login(ctx context.Context, req *proto.EncryptedMessage) (*p
}
}

expired, left := peer.LoginExpired(24 * time.Hour)
if peer.UserID != "" && expired {
return nil, status.Errorf(codes.PermissionDenied, "peer login has expired %v ago. Please login once more", left)
}

var sshKey []byte
if loginReq.GetPeerKeys() != nil {
sshKey = loginReq.GetPeerKeys().GetSshPubKey()
43 changes: 30 additions & 13 deletions management/server/peer.go
Original file line number Diff line number Diff line change
@@ -58,27 +58,44 @@ type Peer struct {
UserID string
// SSHKey is a public SSH key of the peer
SSHKey string
// SSHEnabled indicated whether SSH server is enabled on the peer
// SSHEnabled indicates whether SSH server is enabled on the peer
SSHEnabled bool
// LoginExpirationEnabled indicates whether peer's login expiration is enabled and once expired the peer has to re-login.
// Works with LastLogin
LoginExpirationEnabled bool
// LastLogin the time when peer performed last login operation
LastLogin time.Time
}

// Copy copies Peer object
func (p *Peer) Copy() *Peer {
return &Peer{
ID: p.ID,
Key: p.Key,
SetupKey: p.SetupKey,
IP: p.IP,
Meta: p.Meta,
Name: p.Name,
Status: p.Status,
UserID: p.UserID,
SSHKey: p.SSHKey,
SSHEnabled: p.SSHEnabled,
DNSLabel: p.DNSLabel,
ID: p.ID,
Key: p.Key,
SetupKey: p.SetupKey,
IP: p.IP,
Meta: p.Meta,
Name: p.Name,
Status: p.Status,
UserID: p.UserID,
SSHKey: p.SSHKey,
SSHEnabled: p.SSHEnabled,
DNSLabel: p.DNSLabel,
LoginExpirationEnabled: p.LoginExpirationEnabled,
LastLogin: p.LastLogin,
}
}

// LoginExpired indicates whether peer's login has expired or not.
// If Peer.LastLogin plus the expiresIn duration has happened already then login has expired.
// Return true if login has expired, false otherwise and time left to expiration (negative when expired).
func (p *Peer) LoginExpired(expiresIn time.Duration) (bool, time.Duration) {
expiresAt := p.LastLogin.Add(expiresIn)
now := time.Now()
left := expiresAt.Sub(now)
return p.LoginExpirationEnabled && (left <= 0), left
}

// FQDN returns peers FQDN combined of the peer's DNS label and the system's DNS domain
func (p *Peer) FQDN(dnsDomain string) string {
if dnsDomain == "" {
@@ -100,7 +117,7 @@ func (p *PeerStatus) Copy() *PeerStatus {
}
}

// GetPeer looks up peer by its public WireGuard key
// GetPeerByKey looks up peer by its public WireGuard key
func (am *DefaultAccountManager) GetPeerByKey(peerPubKey string) (*Peer, error) {

account, err := am.Store.GetAccountByPeerPubKey(peerPubKey)
46 changes: 46 additions & 0 deletions management/server/peer_test.go
Original file line number Diff line number Diff line change
@@ -3,11 +3,57 @@ package server
import (
"github.com/stretchr/testify/assert"
"testing"
"time"

"github.com/rs/xid"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)

func TestPeer_LoginExpired(t *testing.T) {

tt := []struct {
name string
expirationEnbaled bool
lastLogin time.Time
expiresIn time.Duration
expected bool
}{
{
name: "Peer Login Expiration Disabled. Peer Login Should Not Expire",
expirationEnbaled: false,
lastLogin: time.Now().Add(-25 * time.Hour),
expiresIn: time.Hour,
expected: false,
},
{
name: "Peer Login Should Expire",
expirationEnbaled: true,
lastLogin: time.Now().Add(-25 * time.Hour),
expiresIn: time.Hour,
expected: true,
},
{
name: "Peer Login Should Not Expire",
expirationEnbaled: true,
lastLogin: time.Now(),
expiresIn: time.Hour,
expected: false,
},
}

for _, c := range tt {
t.Run(c.name, func(t *testing.T) {
peer := &Peer{
LoginExpirationEnabled: c.expirationEnbaled,
LastLogin: c.lastLogin,
}

expired, _ := peer.LoginExpired(c.expiresIn)
assert.Equal(t, expired, c.expected)
})
}
}

func TestAccountManager_GetNetworkMap(t *testing.T) {
manager, err := createManager(t)
if err != nil {