2022-12-06 12:28:55 -08:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2022-04-29 15:55:27 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2020-08-26 13:58:46 -04:00
|
|
|
package target
|
|
|
|
|
|
|
|
|
|
import (
|
2021-09-29 13:13:49 -04:00
|
|
|
"context"
|
2023-02-21 21:31:22 +00:00
|
|
|
goerrs "errors"
|
2020-08-26 13:58:46 -04:00
|
|
|
"fmt"
|
|
|
|
|
|
2023-11-15 19:55:46 -05:00
|
|
|
"github.com/hashicorp/boundary/globals"
|
2024-02-01 14:03:04 -05:00
|
|
|
"github.com/hashicorp/boundary/internal/alias/target"
|
2022-04-29 15:55:27 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/boundary"
|
2020-09-01 15:05:46 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/db/timestamp"
|
2021-01-22 13:46:20 -08:00
|
|
|
"github.com/hashicorp/boundary/internal/errors"
|
2020-08-26 13:58:46 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/oplog"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/target/store"
|
2023-10-23 15:53:29 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/types/resource"
|
2020-08-26 13:58:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Target is a commmon interface for all target subtypes
|
|
|
|
|
type Target interface {
|
|
|
|
|
GetPublicId() string
|
2022-07-20 16:24:46 -07:00
|
|
|
GetProjectId() string
|
2020-08-26 13:58:46 -04:00
|
|
|
GetDefaultPort() uint32
|
2023-04-21 06:50:53 -04:00
|
|
|
GetDefaultClientPort() uint32
|
2020-08-26 13:58:46 -04:00
|
|
|
GetName() string
|
|
|
|
|
GetDescription() string
|
|
|
|
|
GetVersion() uint32
|
2023-11-15 19:55:46 -05:00
|
|
|
GetType() globals.Subtype
|
2023-10-23 15:53:29 -07:00
|
|
|
GetResourceType() resource.Type
|
2020-09-01 15:05:46 -07:00
|
|
|
GetCreateTime() *timestamp.Timestamp
|
|
|
|
|
GetUpdateTime() *timestamp.Timestamp
|
2020-09-19 11:32:38 -04:00
|
|
|
GetSessionMaxSeconds() uint32
|
2020-09-20 15:53:49 -04:00
|
|
|
GetSessionConnectionLimit() int32
|
2021-01-21 16:27:20 -05:00
|
|
|
GetWorkerFilter() string
|
2022-11-30 15:23:37 -05:00
|
|
|
GetEgressWorkerFilter() string
|
|
|
|
|
GetIngressWorkerFilter() string
|
2022-12-06 12:28:55 -08:00
|
|
|
GetAddress() string
|
2024-02-01 14:03:04 -05:00
|
|
|
GetAliases() []*target.Alias
|
2023-03-30 15:19:30 -04:00
|
|
|
GetHostSources() []HostSource
|
|
|
|
|
GetCredentialSources() []CredentialSource
|
2023-05-03 15:54:33 -07:00
|
|
|
GetStorageBucketId() string
|
|
|
|
|
GetEnableSessionRecording() bool
|
2021-09-29 13:13:49 -04:00
|
|
|
Clone() Target
|
|
|
|
|
SetPublicId(context.Context, string) error
|
2022-07-20 16:24:46 -07:00
|
|
|
SetProjectId(string)
|
2021-09-29 13:13:49 -04:00
|
|
|
SetName(string)
|
|
|
|
|
SetDescription(string)
|
|
|
|
|
SetVersion(uint32)
|
|
|
|
|
SetDefaultPort(uint32)
|
2023-04-21 06:50:53 -04:00
|
|
|
SetDefaultClientPort(uint32)
|
2021-09-29 13:13:49 -04:00
|
|
|
SetCreateTime(*timestamp.Timestamp)
|
|
|
|
|
SetUpdateTime(*timestamp.Timestamp)
|
|
|
|
|
SetSessionMaxSeconds(uint32)
|
|
|
|
|
SetSessionConnectionLimit(int32)
|
|
|
|
|
SetWorkerFilter(string)
|
2022-11-30 15:23:37 -05:00
|
|
|
SetEgressWorkerFilter(string)
|
|
|
|
|
SetIngressWorkerFilter(string)
|
2022-12-06 12:28:55 -08:00
|
|
|
SetAddress(string)
|
2024-02-01 14:03:04 -05:00
|
|
|
SetAliases([]*target.Alias)
|
2023-03-30 15:19:30 -04:00
|
|
|
SetHostSources([]HostSource)
|
|
|
|
|
SetCredentialSources([]CredentialSource)
|
2023-05-03 15:54:33 -07:00
|
|
|
SetStorageBucketId(string)
|
|
|
|
|
SetEnableSessionRecording(bool)
|
2021-09-29 13:13:49 -04:00
|
|
|
Oplog(op oplog.OpType) oplog.Metadata
|
2020-08-26 13:58:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
targetsViewDefaultTable = "target_all_subtypes"
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-21 21:31:22 +00:00
|
|
|
var (
|
|
|
|
|
_ boundary.AuthzProtectedEntity = (*targetView)(nil)
|
|
|
|
|
|
|
|
|
|
errTargetSubtypeNotFound = goerrs.New("target subtype not found")
|
|
|
|
|
)
|
2022-04-29 15:55:27 -04:00
|
|
|
|
2020-08-26 13:58:46 -04:00
|
|
|
// targetView provides a common way to return targets regardless of their
|
|
|
|
|
// underlying type.
|
|
|
|
|
type targetView struct {
|
|
|
|
|
*store.TargetView
|
2022-12-06 12:28:55 -08:00
|
|
|
// Network address assigned to the Target.
|
2023-03-30 15:19:30 -04:00
|
|
|
Address string `json:"address,omitempty" gorm:"-"`
|
|
|
|
|
tableName string `gorm:"-"`
|
|
|
|
|
HostSource []HostSource `gorm:"-"`
|
|
|
|
|
CredentialSources []CredentialSource `gorm:"-"`
|
2024-02-01 14:03:04 -05:00
|
|
|
Aliases []*target.Alias `gorm:"-"`
|
2020-08-26 13:58:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// allocTargetView will allocate a target view
|
|
|
|
|
func allocTargetView() targetView {
|
|
|
|
|
return targetView{
|
|
|
|
|
TargetView: &store.TargetView{},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableName provides an overridden gorm table name for targets.
|
|
|
|
|
func (t *targetView) TableName() string {
|
|
|
|
|
if t.tableName != "" {
|
|
|
|
|
return t.tableName
|
|
|
|
|
}
|
|
|
|
|
return targetsViewDefaultTable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetTableName sets the table name for the resource. If the caller attempts to
|
|
|
|
|
// set the name to "" the name will be reset to the default name.
|
|
|
|
|
func (t *targetView) SetTableName(n string) {
|
|
|
|
|
switch n {
|
|
|
|
|
case "":
|
|
|
|
|
t.tableName = targetsViewDefaultTable
|
|
|
|
|
default:
|
|
|
|
|
t.tableName = n
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 15:53:29 -07:00
|
|
|
// GetResourceType returns the resource type of the Target
|
|
|
|
|
func (t *targetView) GetResourceType() resource.Type {
|
|
|
|
|
return resource.Target
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-30 15:19:30 -04:00
|
|
|
func (t *targetView) SetHostSources(hs []HostSource) {
|
|
|
|
|
t.HostSource = hs
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *targetView) SetCredentialSources(cs []CredentialSource) {
|
|
|
|
|
t.CredentialSources = cs
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 15:55:27 -04:00
|
|
|
// GetPublicId satisfies boundary.AuthzProtectedEntity
|
|
|
|
|
func (t targetView) GetPublicId() string {
|
|
|
|
|
return t.PublicId
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-20 16:24:46 -07:00
|
|
|
// GetProjectId satisfies boundary.AuthzProtectedEntity
|
|
|
|
|
func (t targetView) GetProjectId() string {
|
|
|
|
|
return t.ProjectId
|
2022-04-29 15:55:27 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-30 15:19:30 -04:00
|
|
|
func (t *targetView) GetHostSources() []HostSource {
|
|
|
|
|
return t.HostSource
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *targetView) GetCredentialSources() []CredentialSource {
|
|
|
|
|
return t.CredentialSources
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 15:55:27 -04:00
|
|
|
// GetUserId satisfies boundary.AuthzProtectedEntity; targets are not associated
|
|
|
|
|
// with a user ID so this always returns an empty string
|
|
|
|
|
func (t targetView) GetUserId() string {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-15 19:55:46 -05:00
|
|
|
func (t *targetView) Subtype() globals.Subtype {
|
|
|
|
|
return globals.Subtype(t.Type)
|
2021-09-29 13:13:49 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-17 15:36:45 -04:00
|
|
|
// targetSubtype converts the target view to the concrete subtype
|
2022-12-06 12:28:55 -08:00
|
|
|
func (t *targetView) targetSubtype(ctx context.Context, address string) (Target, error) {
|
2021-05-17 15:36:45 -04:00
|
|
|
const op = "target.targetView.targetSubtype"
|
2021-09-29 13:13:49 -04:00
|
|
|
|
|
|
|
|
alloc, ok := subtypeRegistry.allocFunc(t.Subtype())
|
|
|
|
|
if !ok {
|
2023-02-21 21:31:22 +00:00
|
|
|
return nil, errors.Wrap(ctx,
|
|
|
|
|
errTargetSubtypeNotFound,
|
|
|
|
|
op,
|
|
|
|
|
errors.WithCode(errors.InvalidParameter),
|
|
|
|
|
errors.WithMsg(fmt.Sprintf("%s is an unknown target subtype of %s", t.PublicId, t.Type)),
|
|
|
|
|
)
|
2021-09-29 13:13:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tt := alloc()
|
|
|
|
|
if err := tt.SetPublicId(ctx, t.PublicId); err != nil {
|
|
|
|
|
return nil, err
|
2020-08-26 13:58:46 -04:00
|
|
|
}
|
2021-09-29 13:13:49 -04:00
|
|
|
tt.SetVersion(t.Version)
|
2022-07-20 16:24:46 -07:00
|
|
|
tt.SetProjectId(t.ProjectId)
|
2021-09-29 13:13:49 -04:00
|
|
|
tt.SetName(t.Name)
|
|
|
|
|
tt.SetDescription(t.Description)
|
|
|
|
|
tt.SetDefaultPort(t.DefaultPort)
|
2023-04-21 06:50:53 -04:00
|
|
|
tt.SetDefaultClientPort(t.DefaultClientPort)
|
2021-09-29 13:13:49 -04:00
|
|
|
tt.SetCreateTime(t.CreateTime)
|
|
|
|
|
tt.SetUpdateTime(t.UpdateTime)
|
|
|
|
|
tt.SetSessionMaxSeconds(t.SessionMaxSeconds)
|
|
|
|
|
tt.SetSessionConnectionLimit(t.SessionConnectionLimit)
|
|
|
|
|
tt.SetWorkerFilter(t.WorkerFilter)
|
2022-11-30 15:23:37 -05:00
|
|
|
tt.SetEgressWorkerFilter(t.EgressWorkerFilter)
|
|
|
|
|
tt.SetIngressWorkerFilter(t.IngressWorkerFilter)
|
2022-12-06 12:28:55 -08:00
|
|
|
tt.SetAddress(address)
|
2023-03-30 15:19:30 -04:00
|
|
|
tt.SetHostSources(t.HostSource)
|
|
|
|
|
tt.SetCredentialSources(t.CredentialSources)
|
2023-05-03 15:54:33 -07:00
|
|
|
tt.SetEnableSessionRecording(t.EnableSessionRecording)
|
|
|
|
|
tt.SetStorageBucketId(t.StorageBucketId)
|
2021-09-29 13:13:49 -04:00
|
|
|
return tt, nil
|
2020-08-26 13:58:46 -04:00
|
|
|
}
|
2023-10-24 09:40:46 -07:00
|
|
|
|
|
|
|
|
type deletedTarget struct {
|
|
|
|
|
PublicId string `gorm:"primary_key"`
|
|
|
|
|
DeleteTime *timestamp.Timestamp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableName returns the tablename to override the default gorm table name
|
|
|
|
|
func (s *deletedTarget) TableName() string {
|
|
|
|
|
return "target_all_subtypes_deleted_view"
|
|
|
|
|
}
|