Skip to content

Commit

Permalink
More upgrades across the application!
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Shanley <dave@quobix.com>
  • Loading branch information
daveshanley committed Jul 20, 2023
1 parent bd89be2 commit 4350882
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 88 deletions.
3 changes: 3 additions & 0 deletions cmd/root_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ var (
if config.StaticIndex == "" {
config.StaticIndex = staticIndex
}
if config.Spec != "" {
spec = config.Spec
}
} else {

pterm.Info.Println("No wiretap configuration located. Using defaults")
Expand Down
96 changes: 72 additions & 24 deletions daemon/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/ranch/bus"
"github.com/pb33f/ranch/model"
"github.com/pb33f/wiretap/config"
"github.com/pb33f/wiretap/controls"
"github.com/pb33f/wiretap/shared"
"github.com/pterm/pterm"
"io"
"net/http"
"net/textproto"
"net/url"
"strings"
"time"
)
Expand All @@ -33,14 +35,18 @@ type HttpCookie struct {
}

type HttpRequest struct {
Timestamp int64 `json:"timestamp,omitempty"`
URL string `json:"url,omitempty"`
Method string `json:"method,omitempty"`
Path string `json:"path,omitempty"`
Query string `json:"query,omitempty"`
Headers map[string]any `json:"headers,omitempty"`
Body string `json:"requestBody,omitempty"`
Cookies map[string]*HttpCookie `json:"cookies,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
URL string `json:"url,omitempty"`
Method string `json:"method,omitempty"`
Host string `json:"host,omitempty"`
Path string `json:"path,omitempty"`
OriginalPath string `json:"originalPath,omitempty"`
DroppedHeaders []string `json:"droppedHeaders,omitempty"`
InjectedHeaders map[string]string `json:"injectedHeaders,omitempty"`
Query string `json:"query,omitempty"`
Headers map[string]any `json:"headers,omitempty"`
Body string `json:"requestBody,omitempty"`
Cookies map[string]*HttpCookie `json:"cookies,omitempty"`
}

type HttpResponse struct {
Expand Down Expand Up @@ -101,20 +107,51 @@ func buildResponse(r *model.Request, response *http.Response) *HttpTransaction {
}
}

func buildRequest(r *model.Request) *HttpTransaction {
func buildRequest(r *model.Request, newRequest *http.Request) *HttpTransaction {

config, _ := bus.
configuration, _ := bus.
GetBus().
GetStoreManager().
GetStore(controls.ControlServiceChan).
Get(shared.ConfigKey)

var dropHeaders []string
var injectHeaders map[string]string

cf := configuration.(*shared.WiretapConfiguration)

// add global headers with injection.
if cf.Headers != nil {
dropHeaders = cf.Headers.DropHeaders
injectHeaders = cf.Headers.InjectHeaders
}

// now add path specific headers.
matchedPaths := config.FindPaths(r.HttpRequest.URL.Path, cf)
auth := ""
if len(matchedPaths) > 0 {
for _, path := range matchedPaths {
auth = path.Auth
if path.Headers != nil {
dropHeaders = append(dropHeaders, path.Headers.DropHeaders...)
newInjectHeaders := path.Headers.InjectHeaders
for key := range injectHeaders {
newInjectHeaders[key] = injectHeaders[key]
}
injectHeaders = newInjectHeaders
}
break
}
}

newReq := cloneRequest(CloneRequest{
Request: r.HttpRequest,
Protocol: config.(*shared.WiretapConfiguration).RedirectProtocol,
Host: config.(*shared.WiretapConfiguration).RedirectHost,
Port: config.(*shared.WiretapConfiguration).RedirectPort,
DropHeaders: config.(*shared.WiretapConfiguration).Headers.DropHeaders,
Request: newRequest,
Protocol: configuration.(*shared.WiretapConfiguration).RedirectProtocol,
Host: configuration.(*shared.WiretapConfiguration).RedirectHost,
Port: configuration.(*shared.WiretapConfiguration).RedirectPort,
DropHeaders: dropHeaders,
Auth: auth,
InjectHeaders: injectHeaders,
})

var requestBody []byte
Expand Down Expand Up @@ -174,22 +211,33 @@ func buildRequest(r *model.Request) *HttpTransaction {
}
requestBody, _ = json.Marshal(parts)
} else {

requestBody, _ = io.ReadAll(newReq.Body)
}

replaced := config.RewritePath(newRequest.URL.Path, cf)
var newUrl = newRequest.URL
if replaced != "" {
newUrl, _ = url.Parse(replaced)
if newRequest.URL.RawQuery != "" {
newUrl.RawQuery = newRequest.URL.RawQuery
}
}

return &HttpTransaction{
Id: r.Id.String(),
Request: &HttpRequest{
URL: r.HttpRequest.URL.String(),
Method: r.HttpRequest.Method,
Path: r.HttpRequest.URL.Path,
Query: r.HttpRequest.URL.RawQuery,
Cookies: cookies,
Headers: headers,
Body: string(requestBody),
Timestamp: time.Now().UnixMilli(),
URL: newUrl.String(),
Method: newRequest.Method,
Path: newUrl.Path,
Host: newUrl.Host,
Query: newUrl.RawQuery,
DroppedHeaders: dropHeaders,
InjectedHeaders: injectHeaders,
OriginalPath: newRequest.URL.Path,
Cookies: cookies,
Headers: headers,
Body: string(requestBody),
Timestamp: time.Now().UnixMilli(),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion daemon/handle_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (ws *WiretapService) handleHttpRequest(request *model.Request) {
returnedResponse, returnedError = ws.callAPI(apiRequest)

if returnedResponse == nil && returnedError != nil {
utils.Log.Infof("[wiretap] request %s: Failed (%d)", request.HttpRequest.URL.String(), 500)
utils.Log.Infof("[wiretap] request %s: Failed (%d)", apiRequest.URL.String(), 500)
go ws.broadcastResponseError(request, cloneResponse(returnedResponse), returnedError)
request.HttpResponseWriter.WriteHeader(500)
wtError := shared.GenerateError("Unable to call API", 500, returnedError.Error(), "")
Expand Down
6 changes: 3 additions & 3 deletions daemon/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,16 @@ func (ws *WiretapService) validateRequest(
}
}
// record results
transaction := buildRequest(modelRequest)
transaction := buildRequest(modelRequest, httpRequest)
if len(cleanedErrors) > 0 {
transaction.RequestValidation = cleanedErrors
}
ws.transactionStore.Put(modelRequest.Id.String(), modelRequest, nil)

// broadcast what we found.
if len(cleanedErrors) > 0 {
ws.broadcastRequestValidationErrors(modelRequest, cleanedErrors)
ws.broadcastRequestValidationErrors(modelRequest, cleanedErrors, transaction)
} else {
ws.broadcastRequest(modelRequest)
ws.broadcastRequest(modelRequest, transaction)
}
}
10 changes: 5 additions & 5 deletions daemon/wiretap_broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"net/http"
)

func (ws *WiretapService) broadcastRequestValidationErrors(request *model.Request, errors []*errors.ValidationError) {
func (ws *WiretapService) broadcastRequestValidationErrors(request *model.Request,
errors []*errors.ValidationError, transaction *HttpTransaction) {
id, _ := uuid.NewUUID()

ht := buildRequest(request)
ht := transaction
ht.RequestValidation = errors

ws.broadcastChan.Send(&model.Message{
Expand All @@ -26,14 +26,14 @@ func (ws *WiretapService) broadcastRequestValidationErrors(request *model.Reques
})
}

func (ws *WiretapService) broadcastRequest(request *model.Request) {
func (ws *WiretapService) broadcastRequest(request *model.Request, transaction *HttpTransaction) {
id, _ := uuid.NewUUID()
ws.broadcastChan.Send(&model.Message{
Id: &id,
DestinationId: request.Id,
Channel: WiretapBroadcastChan,
Destination: WiretapBroadcastChan,
Payload: buildRequest(request),
Payload: transaction,
Direction: model.ResponseDir,
})
}
Expand Down
1 change: 1 addition & 0 deletions shared/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type WiretapConfiguration struct {
Headers *WiretapHeaderConfig `json:"headers,omitempty" yaml:"headers,omitempty"`
StaticPaths []string `json:"staticPaths,omitempty" yaml:"staticPaths,omitempty"`
Variables map[string]string `json:"variables,omitempty" yaml:"variables,omitempty"`
Spec string `json:"contract,omitempty" yaml:"contract,omitempty"`
CompiledVariables map[string]*CompiledVariable `json:"-" yaml:"-"`
StaticPathsCompiled []glob.Glob `json:"-" yaml:"-"`
CompiledPaths map[string]*CompiledPath `json:"-"`
Expand Down
7 changes: 4 additions & 3 deletions ui/src/components/kv-view/kv-view.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ export default css`
.kv-table > table > thead > tr > th:first-child {
font-weight: bold;
font-family: var(--font-stack-paragraph);
font-size: 0.9em;
font-family: var(--mono-font-stack);
background: var(--kv-table-header-background-reversed);
text-align: right;
color: var(--secondary-color);
padding-right: 10px;
}
.kv-table > table > tbody > tr > td {
font-family: var(--font-stack-paragraph);
color: var(--darker-font-color);
font-family: var(--mono-font-stack);
padding: 10px 0 10px 10px;
font-size: 1em;
border-bottom: 1px dashed var(--secondary-color-dimmer);
}
Expand Down
5 changes: 3 additions & 2 deletions ui/src/components/kv-view/kv-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export class KVViewComponent extends LitElement {
}

set data(value: Map<string, any>) {
this._data = value;
if (value.size > 0) {
this._data = value;
}
}

render() {
Expand Down Expand Up @@ -83,7 +85,6 @@ export class KVViewComponent extends LitElement {
`;

const output = this._data?.size > 0 ? table : noData;

return html`${output}`;
}
}
2 changes: 1 addition & 1 deletion ui/src/components/transaction/transaction-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class HttpTransactionItemComponent extends LitElement {
<div class="${tClass}" @click="${this.setActive}">
<header>
<sl-tag variant="${ExchangeMethod(req.method)}" class="method">${req.method}</sl-tag>
${decodeURI(req.url)}
${decodeURI(req.path)}
</header>
${delay}
Expand Down
4 changes: 4 additions & 0 deletions ui/src/components/transaction/transaction-view.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,8 @@ export default css`
color: var(--primary-color);
}
.dropped-header {
color: var(--error-color);
}
`
Loading

0 comments on commit 4350882

Please sign in to comment.