mirror of
https://github.com/netbirdio/netbird.git
synced 2024-12-24 23:59:25 +01:00
feature: increase key usage after successful peer registration (#93)
This commit is contained in:
parent
90ef1e939b
commit
6869b48905
@ -277,6 +277,7 @@ func (manager *AccountManager) AddPeer(setupKey string, peerKey string) (*Peer,
|
|||||||
}
|
}
|
||||||
|
|
||||||
account.Peers[newPeer.Key] = newPeer
|
account.Peers[newPeer.Key] = newPeer
|
||||||
|
account.SetupKeys[sk.Key] = sk.IncrementUsage()
|
||||||
err = manager.Store.SaveAccount(account)
|
err = manager.Store.SaveAccount(account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.Internal, "failed adding peer")
|
return nil, status.Errorf(codes.Internal, "failed adding peer")
|
||||||
|
@ -25,6 +25,8 @@ type SetupKeyResponse struct {
|
|||||||
Type server.SetupKeyType
|
Type server.SetupKeyType
|
||||||
Valid bool
|
Valid bool
|
||||||
Revoked bool
|
Revoked bool
|
||||||
|
UsedTimes int
|
||||||
|
LastUsed time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetupKeyRequest is a request sent by client. This object contains fields that can be modified
|
// SetupKeyRequest is a request sent by client. This object contains fields that can be modified
|
||||||
@ -50,6 +52,11 @@ func (h *SetupKeys) CreateKey(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !(req.Type == server.SetupKeyReusable || req.Type == server.SetupKeyOneOff) {
|
||||||
|
http.Error(w, "unknown setup key type "+string(req.Type), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
setupKey, err := h.accountManager.AddSetupKey(accountId, req.Name, req.Type, req.ExpiresIn.Duration)
|
setupKey, err := h.accountManager.AddSetupKey(accountId, req.Name, req.Type, req.ExpiresIn.Duration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errStatus, ok := status.FromError(err)
|
errStatus, ok := status.FromError(err)
|
||||||
@ -173,5 +180,7 @@ func toResponseBody(key *server.SetupKey) *SetupKeyResponse {
|
|||||||
Type: key.Type,
|
Type: key.Type,
|
||||||
Valid: key.IsValid(),
|
Valid: key.IsValid(),
|
||||||
Revoked: key.Revoked,
|
Revoked: key.Revoked,
|
||||||
|
UsedTimes: key.UsedTimes,
|
||||||
|
LastUsed: key.LastUsed,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,8 @@ type SetupKey struct {
|
|||||||
Revoked bool
|
Revoked bool
|
||||||
// UsedTimes indicates how many times the key was used
|
// UsedTimes indicates how many times the key was used
|
||||||
UsedTimes int
|
UsedTimes int
|
||||||
|
// LastUsed last time the key was used for peer registration
|
||||||
|
LastUsed time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
//Copy copies SetupKey to a new object
|
//Copy copies SetupKey to a new object
|
||||||
@ -51,6 +53,14 @@ func (key *SetupKey) Copy() *SetupKey {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//IncrementUsage makes a copy of a key, increments the UsedTimes by 1 and sets LastUsed to now
|
||||||
|
func (key *SetupKey) IncrementUsage() *SetupKey {
|
||||||
|
c := key.Copy()
|
||||||
|
c.UsedTimes = c.UsedTimes + 1
|
||||||
|
c.LastUsed = time.Now()
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
// IsValid is true if the key was not revoked, is not expired and used not more than it was supposed to
|
// IsValid is true if the key was not revoked, is not expired and used not more than it was supposed to
|
||||||
func (key *SetupKey) IsValid() bool {
|
func (key *SetupKey) IsValid() bool {
|
||||||
expired := time.Now().After(key.ExpiresAt)
|
expired := time.Now().After(key.ExpiresAt)
|
||||||
|
Loading…
Reference in New Issue
Block a user