Add some target fields and move some things around (#3879)

In preparation for putting proxy code in the api package this creates some necessary fields in the target output and it pre-moves credential parsing and proxy handshake code to their new places. This will make the diff for the actual proxy code move quite a bit smaller.

Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
This commit is contained in:
Jeff MitchellandJohan Brandhorst-Satzkorn authored and GitHub committed 2023-10-22 20:48:54 -04:00
1 parent 82b3f202c0
commit fb4a5a98dc
27 files changed
+733 -444

No files matched your search

+57
View File
@@ -5,10 +5,17 @@ package targets
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/scopes"
targetspb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/targets"
"github.com/mr-tron/base58"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
type SessionAuthorizationResult struct {
@@ -24,6 +31,56 @@ func (n SessionAuthorizationResult) GetResponse() *api.Response {
return n.response
}
func (n SessionAuthorizationResult) GetSessionAuthorization() (*SessionAuthorization, error) {
result, ok := n.GetItem().(*SessionAuthorization)
if !ok {
return nil, fmt.Errorf("unable to interpret session authorization result as session authorization data")
}
return result, nil
}
func (n SessionAuthorization) GetSessionAuthorizationData() (*SessionAuthorizationData, error) {
if n.AuthorizationToken == "" {
return nil, fmt.Errorf("authorization token is empty")
}
marshaled, err := base58.FastBase58Decoding(n.AuthorizationToken)
if err != nil {
return nil, fmt.Errorf("unable to base58-decode authorization token: %w", err)
}
if len(marshaled) == 0 {
return nil, errors.New("zero-length authorization information after decoding")
}
// Marshal using protojson and unmarshal using json, rather than statically
// copying
d := new(targetspb.SessionAuthorizationData)
if err := proto.Unmarshal(marshaled, d); err != nil {
return nil, fmt.Errorf("unable to unmarshal authorization data: %w", err)
}
jsBytes, err := protojson.MarshalOptions{
UseProtoNames: true,
EmitUnpopulated: false,
}.Marshal(d)
if err != nil {
return nil, fmt.Errorf("error marshaling decoded proto as json: %w", err)
}
ret := &SessionAuthorizationData{
Scope: &scopes.ScopeInfo{},
}
if err := json.Unmarshal(jsBytes, ret); err != nil {
return nil, fmt.Errorf("error unmashaling protojson bytes: %w", err)
}
ret.WorkerInfo = make([]*WorkerInfo, len(d.WorkerInfo))
for i, w := range d.WorkerInfo {
ret.WorkerInfo[i] = &WorkerInfo{
Address: w.Address,
}
}
return ret, nil
}
func (c *Client) AuthorizeSession(ctx context.Context, targetId string, opt ...Option) (*SessionAuthorizationResult, error) {
opts, apiOpts := getOpts(opt...)
+2
View File
@@ -21,5 +21,7 @@ type SessionAuthorization struct {
Type string `json:"type,omitempty"`
AuthorizationToken string `json:"authorization_token,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
EndpointPort uint32 `json:"endpoint_port,omitempty"`
Expiration time.Time `json:"expiration,omitempty"`
Credentials []*SessionCredential `json:"credentials,omitempty"`
}
@@ -0,0 +1,28 @@
// Code generated by "make api"; DO NOT EDIT.
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package targets
import (
"time"
"github.com/hashicorp/boundary/api/scopes"
)
type SessionAuthorizationData struct {
SessionId string `json:"session_id,omitempty"`
TargetId string `json:"target_id,omitempty"`
Scope *scopes.ScopeInfo `json:"scope,omitempty"`
CreatedTime time.Time `json:"created_time,omitempty"`
Type string `json:"type,omitempty"`
ConnectionLimit int32 `json:"connection_limit,omitempty"`
EndpointPort uint32 `json:"endpoint_port,omitempty"`
Expiration time.Time `json:"expiration,omitempty"`
Certificate []byte `json:"certificate,omitempty"`
PrivateKey []byte `json:"private_key,omitempty"`
HostId string `json:"host_id,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
WorkerInfo []*WorkerInfo `json:"worker_info,omitempty"`
DefaultClientPort uint32 `json:"default_client_port,omitempty"`
}