Add in most of the proxy flow (#326)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-09-03 13:24:34 -04:00
1 parent 1822c47ef5
commit 23156afa11
31 files changed
+1372 -184

No files matched your search

+116 -1
View File
@@ -2,8 +2,14 @@ package controller
import (
"context"
"crypto/rand"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"math/big"
mathrand "math/rand"
"net/http"
"os"
"strings"
@@ -14,11 +20,16 @@ import (
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/gen/controller/api/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/sessions"
"github.com/hashicorp/boundary/internal/types/scope"
"github.com/hashicorp/shared-secure-libs/configutil"
"github.com/hashicorp/vault/sdk/helper/base62"
"github.com/hashicorp/vault/sdk/helper/strutil"
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
@@ -31,6 +42,8 @@ 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 {
@@ -49,7 +62,7 @@ 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)
@@ -311,6 +324,108 @@ 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 []*services.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, &services.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
}
jobId = "s_" + jobId
pubKey, privKey, err := sessions.DeriveED25519Key(wrapper, "u_1234567890", jobId)
template := &x509.Certificate{
ExtKeyUsage: []x509.ExtKeyUsage{
x509.ExtKeyUsageServerAuth,
x509.ExtKeyUsageClientAuth,
},
DNSNames: []string{jobId},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageKeyAgreement | x509.KeyUsageCertSign,
SerialNumber: big.NewInt(mathrand.Int63()),
NotBefore: time.Now().Add(-1 * time.Minute),
NotAfter: time.Now().Add(5 * time.Minute),
BasicConstraintsValid: true,
IsCA: true,
}
certBytes, err := x509.CreateCertificate(rand.Reader, template, template, pubKey, privKey)
if err != nil {
errorResp(err)
return
}
ret := &services.ValidateSessionResponse{
Id: jobId,
ScopeId: scope.Global.String(),
UserId: "u_1234567890",
Type: "tcp",
Endpoint: endpoint,
Certificate: certBytes,
PrivateKey: privKey,
WorkerInfo: workers,
ExpirationTime: &timestamppb.Timestamp{Seconds: time.Now().Add(timeout).Unix()},
}
marshaled, err := proto.Marshal(ret)
if err != nil {
errorResp(err)
return
}
if _, err := w.Write([]byte(base64.RawStdEncoding.EncodeToString(marshaled))); err != nil {
errorResp(err)
return
}
ret.PrivateKey = nil
c.jobMap.Store(jobId, ret)
})
}
/*
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) {