Files
threshold/internal/session/connection.go
T
hashicorp-copywrite[bot]andJohan Brandhorst-Satzkorn 29da0bcb92 [COMPLIANCE] License changes (#3567)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* Update copyright file headers to BUS-1.1

* Rerun make gen

This will pick up the last of the license changes

* Revert "Rerun make gen"

This reverts commit dbe0968bffd92745d8663c6940debae098395a6b.

* Remove copywrite from generation script

The current version of copywrite is not sophisticated enough
to only apply the copywrite changes to certain subdirectories,
so to avoid accidentally relicensing our api and sdk modules,
we will stop running the copywrite script until such time
that it can do the right thing.

* License protobuf API as MPL

The protobuf API files are used to generate the SDK,
since we want the SDK to be MPL, the protobuf files
must also be MPL. This extends to all protobuf files
whose sources end up in the SDK directory, including:

- internal/proto/controller/api
- internal/proto/controller/custom_options
- internal/proto/plugin

It also includes the generated service directory in
internal/gen/controller/api/services, since they are
generated from the protobuf sources used for the SDK.

* Change website license file name

This makes the naming scheme consistent with the rest
of the repository.

* Add note on licensing to README

* Set copywrite license to BUSL

* Revert "Add note on licensing to README"

This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe.

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
2023-08-11 10:31:53 -07:00

187 lines
6.4 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package session
import (
"context"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/db/timestamp"
"github.com/hashicorp/boundary/internal/errors"
"google.golang.org/protobuf/types/known/timestamppb"
)
const (
defaultConnectionTableName = "session_connection"
)
// Connection contains information about session's connection to a target
type Connection struct {
// PublicId is used to access the connection via an API
PublicId string `json:"public_id,omitempty" gorm:"primary_key"`
// SessionId of the connection
SessionId string `json:"session_id,omitempty" gorm:"default:null"`
// ClientTcpAddress of the connection
ClientTcpAddress string `json:"client_tcp_address,omitempty" gorm:"default:null"`
// ClientTcpPort of the connection
ClientTcpPort uint32 `json:"client_tcp_port,omitempty" gorm:"default:null"`
// UserClientIp is the user's client IP
UserClientIp string `json:"user_client_ip,omitempty" gorm:"default:null"`
// EndpointTcpAddress of the connection
EndpointTcpAddress string `json:"endpoint_tcp_address,omitempty" gorm:"default:null"`
// EndpointTcpPort of the connection
EndpointTcpPort uint32 `json:"endpoint_tcp_port,omitempty" gorm:"default:null"`
// BytesUp of the connection
BytesUp int64 `json:"bytes_up,omitempty" gorm:"default:null"`
// BytesDown of the connection
BytesDown int64 `json:"bytes_down,omitempty" gorm:"default:null"`
// ClosedReason of the connection
ClosedReason string `json:"closed_reason,omitempty" gorm:"default:null"`
// CreateTime from the RDBMS
CreateTime *timestamp.Timestamp `json:"create_time,omitempty" gorm:"default:current_timestamp"`
// UpdateTime from the RDBMS
UpdateTime *timestamp.Timestamp `json:"update_time,omitempty" gorm:"default:current_timestamp"`
// Version of the connection
Version uint32 `json:"version,omitempty" gorm:"default:null"`
tableName string `gorm:"-"`
}
func (c *Connection) GetPublicId() string {
return c.PublicId
}
var (
_ Cloneable = (*Connection)(nil)
_ db.VetForWriter = (*Connection)(nil)
)
// NewConnection creates a new in memory connection. No options
// are currently supported.
func NewConnection(ctx context.Context, sessionID, clientTcpAddress string, clientTcpPort uint32, endpointTcpAddr string, endpointTcpPort uint32, userClientIp string, _ ...Option) (*Connection, error) {
const op = "session.NewConnection"
c := Connection{
SessionId: sessionID,
ClientTcpAddress: clientTcpAddress,
ClientTcpPort: clientTcpPort,
EndpointTcpAddress: endpointTcpAddr,
EndpointTcpPort: endpointTcpPort,
UserClientIp: userClientIp,
}
if err := c.validateNewConnection(ctx); err != nil {
return nil, errors.Wrap(ctx, err, op)
}
return &c, nil
}
// AllocConnection will allocate a Connection.
func AllocConnection() Connection {
return Connection{}
}
// Clone creates a clone of the Connection.
func (c *Connection) Clone() any {
clone := &Connection{
PublicId: c.PublicId,
SessionId: c.SessionId,
ClientTcpAddress: c.ClientTcpAddress,
ClientTcpPort: c.ClientTcpPort,
UserClientIp: c.UserClientIp,
EndpointTcpAddress: c.EndpointTcpAddress,
EndpointTcpPort: c.EndpointTcpPort,
BytesUp: c.BytesUp,
BytesDown: c.BytesDown,
ClosedReason: c.ClosedReason,
Version: c.Version,
}
if c.CreateTime != nil {
clone.CreateTime = &timestamp.Timestamp{
Timestamp: &timestamppb.Timestamp{
Seconds: c.CreateTime.Timestamp.Seconds,
Nanos: c.CreateTime.Timestamp.Nanos,
},
}
}
if c.UpdateTime != nil {
clone.UpdateTime = &timestamp.Timestamp{
Timestamp: &timestamppb.Timestamp{
Seconds: c.UpdateTime.Timestamp.Seconds,
Nanos: c.UpdateTime.Timestamp.Nanos,
},
}
}
return clone
}
// VetForWrite implements db.VetForWrite() interface and validates the connection
// before it's written.
func (c *Connection) VetForWrite(ctx context.Context, _ db.Reader, opType db.OpType, opt ...db.Option) error {
const op = "session.(Connection).VetForWrite"
opts := db.GetOpts(opt...)
if c.PublicId == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing public id")
}
switch opType {
case db.CreateOp:
if err := c.validateNewConnection(ctx); err != nil {
return errors.Wrap(ctx, err, op)
}
case db.UpdateOp:
switch {
case contains(opts.WithFieldMaskPaths, "PublicId"):
return errors.New(ctx, errors.InvalidParameter, op, "public id is immutable")
case contains(opts.WithFieldMaskPaths, "SessionId"):
return errors.New(ctx, errors.InvalidParameter, op, "session id is immutable")
case contains(opts.WithFieldMaskPaths, "CreateTime"):
return errors.New(ctx, errors.InvalidParameter, op, "create time is immutable")
case contains(opts.WithFieldMaskPaths, "UpdateTime"):
return errors.New(ctx, errors.InvalidParameter, op, "update time is immutable")
case contains(opts.WithFieldMaskPaths, "ClosedReason"):
if _, err := convertToClosedReason(ctx, c.ClosedReason); err != nil {
return errors.Wrap(ctx, err, op)
}
}
}
return nil
}
// TableName returns the tablename to override the default gorm table name
func (c *Connection) TableName() string {
if c.tableName != "" {
return c.tableName
}
return defaultConnectionTableName
}
// SetTableName sets the tablename and satisfies the ReplayableMessage
// interface. If the caller attempts to set the name to "" the name will be
// reset to the default name.
func (c *Connection) SetTableName(n string) {
c.tableName = n
}
// validateNewConnection checks everything but the connection's PublicId
func (c *Connection) validateNewConnection(ctx context.Context) error {
const op = "session.(Connection).validateNewConnection"
if c.SessionId == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing session id")
}
if c.ClientTcpAddress == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing client address")
}
if c.ClientTcpPort == 0 {
return errors.New(ctx, errors.InvalidParameter, op, "missing client port")
}
if c.EndpointTcpAddress == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing endpoint address")
}
if c.EndpointTcpPort == 0 {
return errors.New(ctx, errors.InvalidParameter, op, "missing endpoint port")
}
if c.UserClientIp == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing user client ip")
}
return nil
}