Tie together the database-driven session handling with the worker and add relevant CLI comands (#370)
This commit is contained in:
1 parent
fd8ceb6cc4
commit
07a7e9750a
57 files changed
+2548
-826
No files matched your search
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/host/static"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/servers"
|
||||
"github.com/hashicorp/boundary/internal/session"
|
||||
"github.com/hashicorp/boundary/internal/target"
|
||||
)
|
||||
|
||||
@@ -16,4 +17,5 @@ type (
|
||||
ServersRepoFactory func() (*servers.Repository, error)
|
||||
StaticRepoFactory func() (*static.Repository, error)
|
||||
TargetRepoFactory func() (*target.Repository, error)
|
||||
SessionRepoFactory func() (*session.Repository, error)
|
||||
)
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/servers"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/common"
|
||||
"github.com/hashicorp/boundary/internal/session"
|
||||
"github.com/hashicorp/boundary/internal/target"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/hashicorp/vault/sdk/helper/base62"
|
||||
@@ -35,7 +36,6 @@ type Controller struct {
|
||||
|
||||
// Used for testing
|
||||
workerStatusUpdateTimes *sync.Map
|
||||
jobMap *sync.Map
|
||||
|
||||
// Repo factory methods
|
||||
AuthTokenRepoFn common.AuthTokenRepoFactory
|
||||
@@ -44,6 +44,7 @@ type Controller struct {
|
||||
ServersRepoFn common.ServersRepoFactory
|
||||
StaticHostRepoFn common.StaticRepoFactory
|
||||
TargetRepoFn common.TargetRepoFactory
|
||||
SessionRepoFn common.SessionRepoFactory
|
||||
|
||||
kms *kms.Kms
|
||||
|
||||
@@ -55,7 +56,6 @@ func New(conf *Config) (*Controller, error) {
|
||||
conf: conf,
|
||||
logger: conf.Logger.Named("controller"),
|
||||
workerStatusUpdateTimes: new(sync.Map),
|
||||
jobMap: new(sync.Map),
|
||||
}
|
||||
|
||||
c.started.Store(false)
|
||||
@@ -125,6 +125,9 @@ func New(conf *Config) (*Controller, error) {
|
||||
c.TargetRepoFn = func() (*target.Repository, error) {
|
||||
return target.NewRepository(dbase, dbase, c.kms)
|
||||
}
|
||||
c.SessionRepoFn = func() (*session.Repository, error) {
|
||||
return session.NewRepository(dbase, dbase, c.kms)
|
||||
}
|
||||
|
||||
c.workerAuthCache = cache.New(0, 0)
|
||||
|
||||
|
||||
@@ -3,32 +3,23 @@ package controller
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcutil/base58"
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/globals"
|
||||
"github.com/hashicorp/boundary/internal/auth"
|
||||
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/sessions"
|
||||
"github.com/hashicorp/boundary/internal/gen/controller/api/services"
|
||||
wpbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/servers"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/accounts"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/authmethods"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/host_sets"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/targets"
|
||||
"github.com/hashicorp/boundary/internal/session"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
"github.com/hashicorp/boundary/sdk/strutil"
|
||||
"github.com/hashicorp/shared-secure-libs/configutil"
|
||||
"github.com/hashicorp/vault/sdk/helper/base62"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/authenticate"
|
||||
@@ -40,8 +31,6 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/scopes"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/users"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
type HandlerProperties struct {
|
||||
@@ -60,7 +49,6 @@ func (c *Controller) handler(props HandlerProperties) (http.Handler, error) {
|
||||
return nil, err
|
||||
}
|
||||
mux.Handle("/v1/", h)
|
||||
mux.Handle("/jobtesting", jobTestingHandler(c))
|
||||
mux.Handle("/", handleUi(c))
|
||||
|
||||
corsWrappedHandler := wrapHandlerWithCors(mux, props)
|
||||
@@ -154,7 +142,13 @@ func handleGrpcGateway(c *Controller, props HandlerProperties) (http.Handler, er
|
||||
if err := services.RegisterUserServiceHandlerServer(ctx, mux, us); err != nil {
|
||||
return nil, fmt.Errorf("failed to register user service handler: %w", err)
|
||||
}
|
||||
ts, err := targets.NewService(c.TargetRepoFn, c.IamRepoFn)
|
||||
ts, err := targets.NewService(
|
||||
c.kms,
|
||||
c.TargetRepoFn,
|
||||
c.IamRepoFn,
|
||||
c.ServersRepoFn,
|
||||
c.SessionRepoFn,
|
||||
c.StaticHostRepoFn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create target handler service: %w", err)
|
||||
}
|
||||
@@ -314,95 +308,6 @@ func wrapHandlerWithCors(h http.Handler, props HandlerProperties) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func jobTestingHandler(c *Controller) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
errorResp := func(e error) {
|
||||
w.Write([]byte(e.Error()))
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if err := r.ParseForm(); err != nil {
|
||||
errorResp(err)
|
||||
return
|
||||
}
|
||||
endpoint := r.URL.Query().Get("endpoint")
|
||||
if endpoint == "" {
|
||||
errorResp(errors.New("missing endpoint query param"))
|
||||
return
|
||||
}
|
||||
|
||||
timeout := 15 * time.Second
|
||||
var err error
|
||||
if t := r.URL.Query().Get("timeout"); t != "" {
|
||||
if timeout, err = time.ParseDuration(t); err != nil {
|
||||
errorResp(fmt.Errorf("error parsing timeout: %w", err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var workers []*pb.WorkerInfo
|
||||
repo, err := c.ServersRepoFn()
|
||||
if err != nil {
|
||||
errorResp(err)
|
||||
return
|
||||
}
|
||||
servers, err := repo.ListServers(r.Context(), servers.ServerTypeWorker)
|
||||
if err != nil {
|
||||
errorResp(err)
|
||||
return
|
||||
}
|
||||
for _, v := range servers {
|
||||
workers = append(workers, &pb.WorkerInfo{Address: v.Address})
|
||||
}
|
||||
|
||||
wrapper, err := c.kms.GetWrapper(r.Context(), scope.Global.String(), kms.KeyPurposeSessions)
|
||||
if err != nil {
|
||||
errorResp(err)
|
||||
return
|
||||
}
|
||||
jobId, err := base62.Random(10)
|
||||
if err != nil {
|
||||
errorResp(err)
|
||||
return
|
||||
}
|
||||
// TODO (jimlambrt 8/2020): this is quite correct. We need to create a
|
||||
// new session here (in the session repo) which would have a cert.
|
||||
privKey, certBytes, err := session.TestCert(wrapper, "u_1234567890", jobId)
|
||||
if err != nil {
|
||||
errorResp(err)
|
||||
return
|
||||
}
|
||||
|
||||
ret := &wpbs.GetSessionResponse{
|
||||
Session: &pb.Session{
|
||||
Id: jobId,
|
||||
ScopeId: scope.Global.String(),
|
||||
UserId: "u_1234567890",
|
||||
Type: "tcp",
|
||||
Endpoint: endpoint,
|
||||
Certificate: certBytes,
|
||||
PrivateKey: privKey,
|
||||
WorkerInfo: workers,
|
||||
ExpirationTime: ×tamppb.Timestamp{Seconds: time.Now().Add(timeout).Unix()},
|
||||
},
|
||||
}
|
||||
|
||||
marshaled, err := proto.Marshal(ret)
|
||||
if err != nil {
|
||||
errorResp(err)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := w.Write([]byte(base58.Encode(marshaled))); err != nil {
|
||||
errorResp(err)
|
||||
return
|
||||
}
|
||||
|
||||
ret.Session.PrivateKey = nil
|
||||
c.jobMap.Store(jobId, ret.GetSession())
|
||||
})
|
||||
}
|
||||
|
||||
/*
|
||||
func WrapForwardedForHandler(h http.Handler, authorizedAddrs []*sockaddr.SockAddrMarshaler, rejectNotPresent, rejectNonAuthz bool, hopSkips int) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package hosts
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/auth"
|
||||
@@ -357,6 +358,15 @@ func validateCreateRequest(req *pbs.CreateHostRequest) error {
|
||||
len(attrs.GetAddress().GetValue()) > static.MaxHostAddressLength {
|
||||
badFields["attributes.address"] = fmt.Sprintf("Address length must be between %d and %d characters.", static.MinHostAddressLength, static.MaxHostAddressLength)
|
||||
}
|
||||
_, _, err := net.SplitHostPort(attrs.GetAddress().GetValue())
|
||||
switch {
|
||||
case err == nil:
|
||||
badFields["attributes.address"] = "Address for static hosts does not support a port."
|
||||
case strings.Contains(err.Error(), "missing port in address"):
|
||||
// Bare hostname, which we want
|
||||
default:
|
||||
badFields["attributes.address"] = fmt.Sprintf("Error parsing address: %v.", err)
|
||||
}
|
||||
}
|
||||
return badFields
|
||||
})
|
||||
|
||||
@@ -4,14 +4,21 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
|
||||
"github.com/btcsuite/btcutil/base58"
|
||||
"github.com/golang/protobuf/ptypes/wrappers"
|
||||
"github.com/hashicorp/boundary/internal/auth"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/targets"
|
||||
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
|
||||
"github.com/hashicorp/boundary/internal/host"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/servers"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/common"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
|
||||
"github.com/hashicorp/boundary/internal/session"
|
||||
"github.com/hashicorp/boundary/internal/target"
|
||||
"github.com/hashicorp/boundary/internal/target/store"
|
||||
"github.com/hashicorp/boundary/internal/types/action"
|
||||
@@ -19,6 +26,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
)
|
||||
|
||||
@@ -35,19 +43,45 @@ func init() {
|
||||
|
||||
// Service handles request as described by the pbs.TargetServiceServer interface.
|
||||
type Service struct {
|
||||
repoFn common.TargetRepoFactory
|
||||
iamRepoFn common.IamRepoFactory
|
||||
repoFn common.TargetRepoFactory
|
||||
iamRepoFn common.IamRepoFactory
|
||||
serversRepoFn common.ServersRepoFactory
|
||||
sessionRepoFn common.SessionRepoFactory
|
||||
staticHostRepoFn common.StaticRepoFactory
|
||||
kmsCache *kms.Kms
|
||||
}
|
||||
|
||||
// NewService returns a target service which handles target related requests to boundary.
|
||||
func NewService(repoFn common.TargetRepoFactory, iamRepoFn common.IamRepoFactory) (Service, error) {
|
||||
func NewService(
|
||||
kmsCache *kms.Kms,
|
||||
repoFn common.TargetRepoFactory,
|
||||
iamRepoFn common.IamRepoFactory,
|
||||
serversRepoFn common.ServersRepoFactory,
|
||||
sessionRepoFn common.SessionRepoFactory,
|
||||
staticHostRepoFn common.StaticRepoFactory) (Service, error) {
|
||||
if repoFn == nil {
|
||||
return Service{}, fmt.Errorf("nil target repository provided")
|
||||
}
|
||||
if iamRepoFn == nil {
|
||||
return Service{}, fmt.Errorf("nil iam repository provided")
|
||||
}
|
||||
return Service{repoFn: repoFn, iamRepoFn: iamRepoFn}, nil
|
||||
if serversRepoFn == nil {
|
||||
return Service{}, fmt.Errorf("nil servers repository provided")
|
||||
}
|
||||
if sessionRepoFn == nil {
|
||||
return Service{}, fmt.Errorf("nil session repository provided")
|
||||
}
|
||||
if staticHostRepoFn == nil {
|
||||
return Service{}, fmt.Errorf("nil static host repository provided")
|
||||
}
|
||||
return Service{
|
||||
repoFn: repoFn,
|
||||
iamRepoFn: iamRepoFn,
|
||||
serversRepoFn: serversRepoFn,
|
||||
sessionRepoFn: sessionRepoFn,
|
||||
staticHostRepoFn: staticHostRepoFn,
|
||||
kmsCache: kmsCache,
|
||||
}, nil
|
||||
}
|
||||
|
||||
var _ pbs.TargetServiceServer = Service{}
|
||||
@@ -84,6 +118,9 @@ func (s Service) GetTarget(ctx context.Context, req *pbs.GetTargetRequest) (*pbs
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if u == nil {
|
||||
return nil, nil
|
||||
}
|
||||
u.Scope = authResults.Scope
|
||||
return &pbs.GetTargetResponse{Item: u}, nil
|
||||
}
|
||||
@@ -118,6 +155,9 @@ func (s Service) UpdateTarget(ctx context.Context, req *pbs.UpdateTargetRequest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if u == nil {
|
||||
return nil, nil
|
||||
}
|
||||
u.Scope = authResults.Scope
|
||||
return &pbs.UpdateTargetResponse{Item: u}, nil
|
||||
}
|
||||
@@ -189,6 +229,182 @@ func (s Service) RemoveTargetHostSets(ctx context.Context, req *pbs.RemoveTarget
|
||||
return &pbs.RemoveTargetHostSetsResponse{Item: u}, nil
|
||||
}
|
||||
|
||||
func (s Service) AuthorizeSession(ctx context.Context, req *pbs.AuthorizeSessionRequest) (*pbs.AuthorizeSessionResponse, error) {
|
||||
if err := validateAuthorizeRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
authResults := s.authResult(ctx, req.GetId(), action.Authorize)
|
||||
if authResults.Error != nil {
|
||||
return nil, authResults.Error
|
||||
}
|
||||
// This could happen if, say, u_recovery was used or u_anon was granted. But
|
||||
// don't allow it. It's one thing if grants give access to resources within
|
||||
// Boundary, even if those could eventually be used to provide an unintended
|
||||
// user access to a remote system. It's quite another to enable anonymous
|
||||
// access directly to a remote system.
|
||||
//
|
||||
// Note that even if u_anon or u_auth are given grants we can still validate
|
||||
// a token! So this is just checking that a valid token was provided. The
|
||||
// actual reality of this works out to excluding:
|
||||
//
|
||||
// * True anonymous access (no token provided and u_anon)
|
||||
//
|
||||
// * u_recovery access (which is fine, recovery is meant for recovering
|
||||
// system state, no real reason to allow it to then connect to systems)
|
||||
if authResults.AuthTokenId == "" {
|
||||
return nil, handlers.ForbiddenError()
|
||||
}
|
||||
|
||||
// Get the target information
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t, hostSets, err := repo.LookupTarget(ctx, req.GetId())
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if t == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Instantiate some repos
|
||||
sessionRepo, err := s.sessionRepoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
serversRepo, err := s.serversRepoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// First, fetch all available hosts. Unless one was chosen in the request,
|
||||
// we will pick one at random.
|
||||
type compoundHost struct {
|
||||
hostSetId string
|
||||
hostId string
|
||||
}
|
||||
|
||||
var chosenId compoundHost
|
||||
requestedId := req.GetHostId()
|
||||
staticHostRepo, err := s.staticHostRepoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hostIds := make([]compoundHost, 0, len(hostSets)*10)
|
||||
for _, tSet := range hostSets {
|
||||
hsId := tSet.PublicId
|
||||
switch host.SubtypeFromId(hsId) {
|
||||
case host.StaticSubtype:
|
||||
_, hosts, err := staticHostRepo.LookupSet(ctx, hsId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, host := range hosts {
|
||||
compoundId := compoundHost{hostSetId: hsId, hostId: host.PublicId}
|
||||
if host.PublicId == requestedId {
|
||||
chosenId = compoundId
|
||||
goto HOST_GATHERING_DONE
|
||||
}
|
||||
hostIds = append(hostIds, compoundId)
|
||||
}
|
||||
}
|
||||
}
|
||||
if requestedId != "" {
|
||||
// We didn't find it
|
||||
return nil, handlers.InvalidArgumentErrorf(
|
||||
"Errors in provided fields.",
|
||||
map[string]string{
|
||||
"host_id": "The requested host id is not available.",
|
||||
})
|
||||
}
|
||||
chosenId = hostIds[rand.Intn(len(hostIds))]
|
||||
|
||||
HOST_GATHERING_DONE:
|
||||
// Generate the endpoint URL
|
||||
endpointUrl := &url.URL{
|
||||
Scheme: t.GetType(),
|
||||
}
|
||||
defaultPort := t.GetDefaultPort()
|
||||
var endpointHost string
|
||||
switch host.SubtypeFromId(chosenId.hostId) {
|
||||
case host.StaticSubtype:
|
||||
h, err := staticHostRepo.LookupHost(ctx, chosenId.hostId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error looking up host: %w", err)
|
||||
}
|
||||
endpointHost = h.Address
|
||||
if endpointHost == "" {
|
||||
return nil, errors.New("host had empty address")
|
||||
}
|
||||
}
|
||||
if defaultPort != 0 {
|
||||
endpointUrl.Host = fmt.Sprintf("%s:%d", endpointHost, defaultPort)
|
||||
} else {
|
||||
endpointUrl.Host = endpointHost
|
||||
}
|
||||
|
||||
sessionComposition := session.ComposedOf{
|
||||
UserId: authResults.UserId,
|
||||
HostId: chosenId.hostId,
|
||||
TargetId: t.GetPublicId(),
|
||||
HostSetId: chosenId.hostSetId,
|
||||
AuthTokenId: authResults.AuthTokenId,
|
||||
ScopeId: authResults.Scope.Id,
|
||||
Endpoint: endpointUrl.String(),
|
||||
}
|
||||
|
||||
sess, err := session.New(sessionComposition)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wrapper, err := s.kmsCache.GetWrapper(ctx, authResults.Scope.Id, kms.KeyPurposeSessions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sess, _, privKey, err := sessionRepo.CreateSession(ctx, wrapper, sess)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var workers []*pb.WorkerInfo
|
||||
servers, err := serversRepo.ListServers(ctx, servers.ServerTypeWorker)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, v := range servers {
|
||||
workers = append(workers, &pb.WorkerInfo{Address: v.Address})
|
||||
}
|
||||
|
||||
sad := &pb.SessionAuthorizationData{
|
||||
SessionId: sess.PublicId,
|
||||
TargetId: t.GetPublicId(),
|
||||
Scope: authResults.Scope,
|
||||
CreatedTime: sess.CreateTime.GetTimestamp(),
|
||||
Type: t.GetType(),
|
||||
Certificate: sess.Certificate,
|
||||
PrivateKey: privKey,
|
||||
WorkerInfo: workers,
|
||||
}
|
||||
marshaledSad, err := proto.Marshal(sad)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ret := &pb.SessionAuthorization{
|
||||
SessionId: sess.PublicId,
|
||||
TargetId: t.GetPublicId(),
|
||||
Scope: authResults.Scope,
|
||||
CreatedTime: sess.CreateTime.GetTimestamp(),
|
||||
Type: t.GetType(),
|
||||
AuthorizationToken: base58.Encode(marshaledSad),
|
||||
}
|
||||
return &pbs.AuthorizeSessionResponse{Item: ret}, nil
|
||||
}
|
||||
|
||||
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Target, error) {
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
@@ -197,12 +413,12 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Target, error)
|
||||
u, m, err := repo.LookupTarget(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrRecordNotFound) {
|
||||
return nil, handlers.NotFoundErrorf("Target %q doesn't exist.", id)
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if u == nil {
|
||||
return nil, handlers.NotFoundErrorf("Target %q doesn't exist.", id)
|
||||
return nil, nil
|
||||
}
|
||||
return toProto(u, m)
|
||||
}
|
||||
@@ -272,7 +488,7 @@ func (s Service) updateInRepo(ctx context.Context, scopeId, id string, mask []st
|
||||
return nil, status.Errorf(codes.Internal, "Unable to update target: %v.", err)
|
||||
}
|
||||
if rowsUpdated == 0 {
|
||||
return nil, handlers.NotFoundErrorf("Target %q doesn't exist.", id)
|
||||
return nil, nil
|
||||
}
|
||||
return toProto(out, m)
|
||||
}
|
||||
@@ -490,7 +706,6 @@ func validateUpdateRequest(req *pbs.UpdateTargetRequest) error {
|
||||
badFields["name"] = "This field cannot be set to empty."
|
||||
}
|
||||
switch target.SubtypeFromType(req.GetItem().GetType()) {
|
||||
|
||||
case target.TcpSubType:
|
||||
tcpAttrs := &pb.TcpTargetAttributes{}
|
||||
if err := handlers.StructToProto(req.GetItem().GetAttributes(), tcpAttrs); err != nil {
|
||||
@@ -569,3 +784,21 @@ func validateRemoveRequest(req *pbs.RemoveTargetHostSetsRequest) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateAuthorizeRequest(req *pbs.AuthorizeSessionRequest) error {
|
||||
badFields := map[string]string{}
|
||||
if !handlers.ValidId(target.TcpTargetPrefix, req.GetId()) {
|
||||
badFields["id"] = "Incorrectly formatted identifier."
|
||||
}
|
||||
if req.GetHostId() != "" {
|
||||
switch host.SubtypeFromId(req.GetHostId()) {
|
||||
case host.StaticSubtype:
|
||||
default:
|
||||
badFields["host_id"] = "Incorrectly formatted identifier."
|
||||
}
|
||||
}
|
||||
if len(badFields) > 0 {
|
||||
return handlers.InvalidArgumentErrorf("Errors in provided fields.", badFields)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -16,9 +16,13 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/host/static"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/servers"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/targets"
|
||||
"github.com/hashicorp/boundary/internal/session"
|
||||
"github.com/hashicorp/boundary/internal/target"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/genproto/protobuf/field_mask"
|
||||
@@ -30,6 +34,26 @@ import (
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
)
|
||||
|
||||
func testService(t *testing.T, conn *gorm.DB, kms *kms.Kms, wrapper wrapping.Wrapper) (targets.Service, error) {
|
||||
rw := db.New(conn)
|
||||
repoFn := func() (*target.Repository, error) {
|
||||
return target.NewRepository(rw, rw, kms)
|
||||
}
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iam.TestRepo(t, conn, wrapper), nil
|
||||
}
|
||||
serversRepoFn := func() (*servers.Repository, error) {
|
||||
return servers.NewRepository(rw, rw, kms)
|
||||
}
|
||||
sessionRepoFn := func() (*session.Repository, error) {
|
||||
return session.NewRepository(rw, rw, kms)
|
||||
}
|
||||
staticHostRepoFn := func() (*static.Repository, error) {
|
||||
return static.NewRepository(rw, rw, kms)
|
||||
}
|
||||
return targets.NewService(kms, repoFn, iamRepoFn, serversRepoFn, sessionRepoFn, staticHostRepoFn)
|
||||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
@@ -38,13 +62,6 @@ func TestGet(t *testing.T) {
|
||||
|
||||
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iam.TestRepo(t, conn, wrapper), nil
|
||||
}
|
||||
rw := db.New(conn)
|
||||
repoFn := func() (*target.Repository, error) {
|
||||
return target.NewRepository(rw, rw, kms)
|
||||
}
|
||||
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
|
||||
hs := static.TestSets(t, conn, hc.GetPublicId(), 2)
|
||||
|
||||
@@ -100,7 +117,7 @@ func TestGet(t *testing.T) {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
s, err := targets.NewService(repoFn, iamRepoFn)
|
||||
s, err := testService(t, conn, kms, wrapper)
|
||||
require.NoError(t, err, "Couldn't create a new host set service.")
|
||||
|
||||
got, gErr := s.GetTarget(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), tc.req)
|
||||
@@ -119,13 +136,6 @@ func TestList(t *testing.T) {
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iam.TestRepo(t, conn, wrapper), nil
|
||||
}
|
||||
rw := db.New(conn)
|
||||
repoFn := func() (*target.Repository, error) {
|
||||
return target.NewRepository(rw, rw, kms)
|
||||
}
|
||||
_, projNoTar := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
|
||||
@@ -170,7 +180,7 @@ func TestList(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
s, err := targets.NewService(repoFn, iamRepoFn)
|
||||
s, err := testService(t, conn, kms, wrapper)
|
||||
require.NoError(err, "Couldn't create new host set service.")
|
||||
|
||||
got, gErr := s.ListTargets(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), &pbs.ListTargetsRequest{ScopeId: tc.scopeId})
|
||||
@@ -189,15 +199,7 @@ func TestDelete(t *testing.T) {
|
||||
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
tar := target.TestTcpTarget(t, conn, proj.GetPublicId(), "test")
|
||||
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iam.TestRepo(t, conn, wrapper), nil
|
||||
}
|
||||
rw := db.New(conn)
|
||||
repoFn := func() (*target.Repository, error) {
|
||||
return target.NewRepository(rw, rw, kms)
|
||||
}
|
||||
|
||||
s, err := targets.NewService(repoFn, iamRepoFn)
|
||||
s, err := testService(t, conn, kms, wrapper)
|
||||
require.NoError(t, err, "Couldn't create a new target service.")
|
||||
|
||||
cases := []struct {
|
||||
@@ -251,16 +253,9 @@ func TestDelete_twice(t *testing.T) {
|
||||
|
||||
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iam.TestRepo(t, conn, wrapper), nil
|
||||
}
|
||||
rw := db.New(conn)
|
||||
repoFn := func() (*target.Repository, error) {
|
||||
return target.NewRepository(rw, rw, kms)
|
||||
}
|
||||
tar := target.TestTcpTarget(t, conn, proj.GetPublicId(), "test")
|
||||
|
||||
s, err := targets.NewService(repoFn, iamRepoFn)
|
||||
s, err := testService(t, conn, kms, wrapper)
|
||||
require.NoError(t, err, "Couldn't create a new target service.")
|
||||
req := &pbs.DeleteTargetRequest{
|
||||
Id: tar.GetPublicId(),
|
||||
@@ -281,14 +276,6 @@ func TestCreate(t *testing.T) {
|
||||
|
||||
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iam.TestRepo(t, conn, wrapper), nil
|
||||
}
|
||||
rw := db.New(conn)
|
||||
repoFn := func() (*target.Repository, error) {
|
||||
return target.NewRepository(rw, rw, kms)
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
req *pbs.CreateTargetRequest
|
||||
@@ -379,7 +366,7 @@ func TestCreate(t *testing.T) {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
|
||||
s, err := targets.NewService(repoFn, iamRepoFn)
|
||||
s, err := testService(t, conn, kms, wrapper)
|
||||
require.NoError(err, "Failed to create a new host set service.")
|
||||
|
||||
got, gErr := s.CreateTarget(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), tc.req)
|
||||
@@ -409,9 +396,6 @@ func TestUpdate(t *testing.T) {
|
||||
|
||||
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iam.TestRepo(t, conn, wrapper), nil
|
||||
}
|
||||
rw := db.New(conn)
|
||||
repoFn := func() (*target.Repository, error) {
|
||||
return target.NewRepository(rw, rw, kms)
|
||||
@@ -449,7 +433,7 @@ func TestUpdate(t *testing.T) {
|
||||
Id: tar.GetPublicId(),
|
||||
}
|
||||
|
||||
tested, err := targets.NewService(repoFn, iamRepoFn)
|
||||
tested, err := testService(t, conn, kms, wrapper)
|
||||
require.NoError(t, err, "Failed to create a new host set service.")
|
||||
|
||||
cases := []struct {
|
||||
@@ -736,15 +720,6 @@ func TestUpdate(t *testing.T) {
|
||||
req := proto.Clone(toMerge).(*pbs.UpdateTargetRequest)
|
||||
proto.Merge(req, tc.req)
|
||||
|
||||
// Test some bad versions
|
||||
req.Item.Version = version + 2
|
||||
_, gErr := tested.UpdateTarget(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), req)
|
||||
require.Error(gErr)
|
||||
req.Item.Version = version - 1
|
||||
_, gErr = tested.UpdateTarget(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), req)
|
||||
require.Error(gErr)
|
||||
req.Item.Version = version
|
||||
|
||||
got, gErr := tested.UpdateTarget(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "UpdateTarget(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
|
||||
@@ -778,14 +753,7 @@ func TestAddTargetHostSets(t *testing.T) {
|
||||
|
||||
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iam.TestRepo(t, conn, wrapper), nil
|
||||
}
|
||||
rw := db.New(conn)
|
||||
repoFn := func() (*target.Repository, error) {
|
||||
return target.NewRepository(rw, rw, kms)
|
||||
}
|
||||
s, err := targets.NewService(repoFn, iamRepoFn)
|
||||
s, err := testService(t, conn, kms, wrapper)
|
||||
require.NoError(t, err, "Error when getting new target service.")
|
||||
|
||||
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
|
||||
@@ -878,14 +846,7 @@ func TestSetTargetHostSets(t *testing.T) {
|
||||
|
||||
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
|
||||
rw := db.New(conn)
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iam.TestRepo(t, conn, wrapper), nil
|
||||
}
|
||||
repoFn := func() (*target.Repository, error) {
|
||||
return target.NewRepository(rw, rw, kms)
|
||||
}
|
||||
s, err := targets.NewService(repoFn, iamRepoFn)
|
||||
s, err := testService(t, conn, kms, wrapper)
|
||||
require.NoError(t, err, "Error when getting new host set service.")
|
||||
|
||||
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
|
||||
@@ -972,14 +933,7 @@ func TestRemoveTargetHostSets(t *testing.T) {
|
||||
|
||||
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iam.TestRepo(t, conn, wrapper), nil
|
||||
}
|
||||
rw := db.New(conn)
|
||||
repoFn := func() (*target.Repository, error) {
|
||||
return target.NewRepository(rw, rw, kms)
|
||||
}
|
||||
s, err := targets.NewService(repoFn, iamRepoFn)
|
||||
s, err := testService(t, conn, kms, wrapper)
|
||||
require.NoError(t, err, "Error when getting new host set service.")
|
||||
|
||||
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
|
||||
|
||||
@@ -2,10 +2,11 @@ package workers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/sessions"
|
||||
"github.com/hashicorp/boundary/internal/gen/controller/api/resources/targets"
|
||||
pbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/common"
|
||||
@@ -17,22 +18,27 @@ import (
|
||||
)
|
||||
|
||||
type workerServiceServer struct {
|
||||
logger hclog.Logger
|
||||
repoFn common.ServersRepoFactory
|
||||
updateTimes *sync.Map
|
||||
kms *kms.Kms
|
||||
jobMap *sync.Map
|
||||
jobCancelMap *sync.Map
|
||||
logger hclog.Logger
|
||||
serversRepoFn common.ServersRepoFactory
|
||||
sessionRepoFn common.SessionRepoFactory
|
||||
updateTimes *sync.Map
|
||||
kms *kms.Kms
|
||||
jobCancelMap *sync.Map
|
||||
}
|
||||
|
||||
func NewWorkerServiceServer(logger hclog.Logger, repoFn common.ServersRepoFactory, updateTimes *sync.Map, kms *kms.Kms, jobMap *sync.Map) *workerServiceServer {
|
||||
func NewWorkerServiceServer(
|
||||
logger hclog.Logger,
|
||||
serversRepoFn common.ServersRepoFactory,
|
||||
sessionRepoFn common.SessionRepoFactory,
|
||||
updateTimes *sync.Map,
|
||||
kms *kms.Kms) *workerServiceServer {
|
||||
return &workerServiceServer{
|
||||
logger: logger,
|
||||
repoFn: repoFn,
|
||||
updateTimes: updateTimes,
|
||||
kms: kms,
|
||||
jobMap: jobMap,
|
||||
jobCancelMap: new(sync.Map),
|
||||
logger: logger,
|
||||
serversRepoFn: serversRepoFn,
|
||||
sessionRepoFn: sessionRepoFn,
|
||||
updateTimes: updateTimes,
|
||||
kms: kms,
|
||||
jobCancelMap: new(sync.Map),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +48,7 @@ var _ pbs.ServerCoordinationServiceServer = &workerServiceServer{}
|
||||
func (ws *workerServiceServer) Status(ctx context.Context, req *pbs.StatusRequest) (*pbs.StatusResponse, error) {
|
||||
ws.logger.Trace("got status request from worker", "name", req.Worker.Name, "address", req.Worker.Address, "jobs", req.GetJobs())
|
||||
ws.updateTimes.Store(req.Worker.Name, time.Now())
|
||||
repo, err := ws.repoFn()
|
||||
repo, err := ws.serversRepoFn()
|
||||
if err != nil {
|
||||
ws.logger.Error("error getting servers repo", "error", err)
|
||||
return &pbs.StatusResponse{}, status.Errorf(codes.Internal, "Error aqcuiring repo to store worker status: %v", err)
|
||||
@@ -72,15 +78,31 @@ func (ws *workerServiceServer) Status(ctx context.Context, req *pbs.StatusReques
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (ws *workerServiceServer) GetSession(ctx context.Context, req *pbs.GetSessionRequest) (*pbs.GetSessionResponse, error) {
|
||||
ws.logger.Trace("got validate session request from worker", "job_id", req.GetId())
|
||||
func (ws *workerServiceServer) LookupSession(ctx context.Context, req *pbs.LookupSessionRequest) (*pbs.LookupSessionResponse, error) {
|
||||
ws.logger.Trace("got validate session request from worker", "session_id", req.GetSessionId())
|
||||
|
||||
// Look up the job info
|
||||
storedSessionInfo, loaded := ws.jobMap.LoadAndDelete(req.GetId())
|
||||
if !loaded {
|
||||
return nil, status.Errorf(codes.PermissionDenied, "Unknown job ID: %v", req.GetId())
|
||||
sessRepo, err := ws.sessionRepoFn()
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "error getting session repo: %v", err)
|
||||
}
|
||||
|
||||
sessionInfo, _, err := sessRepo.LookupSession(ctx, req.GetSessionId())
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "error looking up session: %v", err)
|
||||
}
|
||||
if sessionInfo == nil {
|
||||
return nil, status.Error(codes.PermissionDenied, "Unknown session ID.")
|
||||
}
|
||||
|
||||
resp := &pbs.LookupSessionResponse{
|
||||
Authorization: &targets.SessionAuthorizationData{
|
||||
SessionId: sessionInfo.GetPublicId(),
|
||||
Certificate: sessionInfo.Certificate,
|
||||
},
|
||||
Version: sessionInfo.Version,
|
||||
TofuToken: base64.StdEncoding.EncodeToString(sessionInfo.TofuToken),
|
||||
Endpoint: sessionInfo.Endpoint,
|
||||
}
|
||||
sessionInfo := storedSessionInfo.(*pb.Session)
|
||||
|
||||
wrapper, err := ws.kms.GetWrapper(ctx, sessionInfo.ScopeId, kms.KeyPurposeSessions)
|
||||
if err != nil {
|
||||
@@ -89,23 +111,35 @@ func (ws *workerServiceServer) GetSession(ctx context.Context, req *pbs.GetSessi
|
||||
|
||||
// Derive the private key, which should match. Deriving on both ends allows
|
||||
// us to not store it in the DB.
|
||||
_, privKey, err := session.DeriveED25519Key(wrapper, sessionInfo.GetUserId(), req.GetId())
|
||||
_, resp.Authorization.PrivateKey, err = session.DeriveED25519Key(wrapper, sessionInfo.UserId, sessionInfo.GetPublicId())
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Error deriving session key: %v", err)
|
||||
}
|
||||
|
||||
if sessionInfo.ExpirationTime.GetSeconds() > 0 {
|
||||
timeDiff := time.Until(sessionInfo.GetExpirationTime().AsTime())
|
||||
if timeDiff < 0 {
|
||||
return nil, status.Errorf(codes.OutOfRange, "Session has already expired")
|
||||
}
|
||||
defer func() {
|
||||
time.AfterFunc(timeDiff, func() {
|
||||
ws.jobCancelMap.Store(req.GetId(), true)
|
||||
})
|
||||
}()
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (ws *workerServiceServer) ActivateSession(ctx context.Context, req *pbs.ActivateSessionRequest) (*pbs.ActivateSessionResponse, error) {
|
||||
ws.logger.Trace("got activate session request from worker", "session_id", req.GetSessionId())
|
||||
|
||||
sessRepo, err := ws.sessionRepoFn()
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "error getting session repo: %v", err)
|
||||
}
|
||||
|
||||
sessionInfo.PrivateKey = privKey
|
||||
return &pbs.GetSessionResponse{Session: sessionInfo}, nil
|
||||
sessionInfo, _, err := sessRepo.ActivateSession(
|
||||
ctx,
|
||||
req.GetSessionId(),
|
||||
req.GetVersion(),
|
||||
req.GetWorkerId(),
|
||||
resource.Worker.String(),
|
||||
[]byte(req.GetTofuToken()))
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "error looking up session: %v", err)
|
||||
}
|
||||
if sessionInfo == nil {
|
||||
return nil, status.Error(codes.PermissionDenied, "Unknown session ID.")
|
||||
}
|
||||
|
||||
return &pbs.ActivateSessionResponse{}, nil
|
||||
}
|
||||
@@ -120,7 +120,7 @@ func (c *Controller) startListeners() error {
|
||||
grpc.MaxRecvMsgSize(math.MaxInt32),
|
||||
grpc.MaxSendMsgSize(math.MaxInt32),
|
||||
)
|
||||
workerService := workers.NewWorkerServiceServer(c.logger.Named("worker-handler"), c.ServersRepoFn, c.workerStatusUpdateTimes, c.kms, c.jobMap)
|
||||
workerService := workers.NewWorkerServiceServer(c.logger.Named("worker-handler"), c.ServersRepoFn, c.SessionRepoFn, c.workerStatusUpdateTimes, c.kms)
|
||||
pbs.RegisterServerCoordinationServiceServer(workerServer, workerService)
|
||||
pbs.RegisterSessionServiceServer(workerServer, workerService)
|
||||
|
||||
|
||||
Reference in new issue
Block a user