Tie together the database-driven session handling with the worker and add relevant CLI comands (#370)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-09-19 00:18:17 -04:00
1 parent fd8ceb6cc4
commit 07a7e9750a
57 files changed
+2548 -826

No files matched your search

+13 -4
View File
@@ -3,6 +3,7 @@ package session
import (
"context"
"crypto/ed25519"
"crypto/subtle"
"errors"
"fmt"
"strings"
@@ -45,10 +46,10 @@ func (r *Repository) CreateSession(ctx context.Context, sessionWrapper wrapping.
return nil, nil, nil, fmt.Errorf("create session: scope id is empty: %w", db.ErrInvalidParameter)
}
if newSession.ServerId != "" {
return nil, nil, nil, fmt.Errorf("create session: server id must empty: %w", db.ErrInvalidParameter)
return nil, nil, nil, fmt.Errorf("create session: server id must be empty: %w", db.ErrInvalidParameter)
}
if newSession.ServerType != "" {
return nil, nil, nil, fmt.Errorf("create session: server type must empty: %w", db.ErrInvalidParameter)
return nil, nil, nil, fmt.Errorf("create session: server type must be empty: %w", db.ErrInvalidParameter)
}
if newSession.CtTofuToken != nil {
return nil, nil, nil, fmt.Errorf("create session: ct must be empty: %w", db.ErrInvalidParameter)
@@ -391,7 +392,7 @@ func (r *Repository) CloseConnections(ctx context.Context, closeWith []CloseWith
// was cancelled or terminated.
func (r *Repository) ActivateSession(ctx context.Context, sessionId string, sessionVersion uint32, serverId, serverType string, tofuToken []byte) (*Session, []*State, error) {
if sessionId == "" {
return nil, nil, fmt.Errorf("activate session: missing session id %w", db.ErrInvalidParameter)
return nil, nil, fmt.Errorf("activate session: missing session id: %w", db.ErrInvalidParameter)
}
if sessionVersion == 0 {
return nil, nil, fmt.Errorf("activate session: version cannot be zero: %w", db.ErrInvalidParameter)
@@ -402,6 +403,10 @@ func (r *Repository) ActivateSession(ctx context.Context, sessionId string, sess
if serverType == "" {
return nil, nil, fmt.Errorf("activate session: missing server type: %w", db.ErrInvalidParameter)
}
if len(tofuToken) == 0 {
return nil, nil, fmt.Errorf("activate session: missing tofu token: %w", db.ErrInvalidParameter)
}
updatedSession := AllocSession()
updatedSession.PublicId = sessionId
var returnedStates []*State
@@ -420,12 +425,16 @@ func (r *Repository) ActivateSession(ctx context.Context, sessionId string, sess
foundSession := AllocSession()
foundSession.PublicId = sessionId
if err := reader.LookupById(ctx, &foundSession); err != nil {
return fmt.Errorf("lookup session: failed %w for %s", err, sessionId)
return fmt.Errorf("lookup session: failed for %s: %w", sessionId, err)
}
databaseWrapper, err := r.kms.GetWrapper(ctx, foundSession.ScopeId, kms.KeyPurposeDatabase)
if err != nil {
return fmt.Errorf("unable to get database wrapper: %w", err)
}
if len(foundSession.TofuToken) > 0 && subtle.ConstantTimeCompare(foundSession.TofuToken, tofuToken) != 1 {
return fmt.Errorf("tofu token mismatch")
}
updatedSession.TofuToken = tofuToken
updatedSession.ServerId = serverId
updatedSession.ServerType = serverType
@@ -243,6 +243,7 @@ func TestRepository_CreateSession(t *testing.T) {
HostSetId: tt.args.composedOf.HostSetId,
AuthTokenId: tt.args.composedOf.AuthTokenId,
ScopeId: tt.args.composedOf.ScopeId,
Endpoint: "tcp://127.0.0.1:22",
}
ses, st, privKey, err := repo.CreateSession(context.Background(), wrapper, s)
if tt.wantErr {
+13
View File
@@ -36,6 +36,10 @@ type ComposedOf struct {
AuthTokenId string
// ScopeId of the session
ScopeId string
// Endpoint. This is generated by the target, but is not stored in the
// warehouse as the worker may need to e.g. resolve DNS. This is to round
// trip the information to the worker when it validates a session.
Endpoint string
}
// Session contains information about a user's session with a target
@@ -76,6 +80,8 @@ type Session struct {
UpdateTime *timestamp.Timestamp `json:"update_time,omitempty" gorm:"default:current_timestamp"`
// Version for the session
Version uint32 `json:"version,omitempty" gorm:"default:null"`
// Endpoint
Endpoint string `json:"-" gorm:"default:null"`
// key_id is the key ID that was used for the encryption operation. It can be
// used to identify a specific version of the key needed to decrypt the value,
@@ -105,6 +111,7 @@ func New(c ComposedOf, opt ...Option) (*Session, error) {
HostSetId: c.HostSetId,
AuthTokenId: c.AuthTokenId,
ScopeId: c.ScopeId,
Endpoint: c.Endpoint,
ExpirationTime: opts.withExpirationTime,
}
if err := s.validateNewSession("new session:"); err != nil {
@@ -132,6 +139,7 @@ func (s *Session) Clone() interface{} {
ScopeId: s.ScopeId,
TerminationReason: s.TerminationReason,
Version: s.Version,
Endpoint: s.Endpoint,
}
if s.TofuToken != nil {
clone.TofuToken = make([]byte, len(s.TofuToken))
@@ -207,6 +215,8 @@ func (s *Session) VetForWrite(ctx context.Context, r db.Reader, opType db.OpType
return fmt.Errorf("session vet for write: create time is immutable: %w", db.ErrInvalidParameter)
case contains(opts.WithFieldMaskPaths, "UpdateTime"):
return fmt.Errorf("session vet for write: update time is immutable: %w", db.ErrInvalidParameter)
case contains(opts.WithFieldMaskPaths, "Endpoint"):
return fmt.Errorf("session vet for write: endpoint is immutable: %w", db.ErrInvalidParameter)
case contains(opts.WithFieldMaskPaths, "TerminationReason"):
if _, err := convertToReason(s.TerminationReason); err != nil {
return fmt.Errorf("session vet for write: termination reason '%s' is invalid: %w", s.TerminationReason, db.ErrInvalidParameter)
@@ -251,6 +261,9 @@ func (s *Session) validateNewSession(errorPrefix string) error {
if s.ScopeId == "" {
return fmt.Errorf("%s missing scope id: %w", errorPrefix, db.ErrInvalidParameter)
}
if s.Endpoint == "" {
return fmt.Errorf("%s missing endpoint: %w", errorPrefix, db.ErrInvalidParameter)
}
if s.TerminationReason != "" {
return fmt.Errorf("%s termination reason must be empty: %w", errorPrefix, db.ErrInvalidParameter)
}
+1
View File
@@ -51,6 +51,7 @@ func TestSession_Create(t *testing.T) {
HostSetId: composedOf.HostSetId,
AuthTokenId: composedOf.AuthTokenId,
ScopeId: composedOf.ScopeId,
Endpoint: "tcp://127.0.0.1:22",
},
create: true,
},
+1
View File
@@ -133,6 +133,7 @@ func TestSessionParams(t *testing.T, conn *gorm.DB, wrapper wrapping.Wrapper, ia
HostSetId: sets[0].PublicId,
AuthTokenId: at.PublicId,
ScopeId: tcpTarget.ScopeId,
Endpoint: "tcp://127.0.0.1:22",
}
}