Fix Mizu sometimes resolves without namespace (#96)

* Reordered imports.

* Pass all ConnectionInfo to saveHarToDb.

* Resolve destination by IP:Port instead of host name.
This commit is contained in:
nimrod-up9 2021-07-06 16:08:27 +03:00 committed by GitHub
parent 672accba0c
commit 12d873d344
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,10 +5,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"mizuserver/pkg/database"
"mizuserver/pkg/models"
"mizuserver/pkg/resolver"
"mizuserver/pkg/utils"
"net/url" "net/url"
"os" "os"
"path" "path"
@ -19,6 +15,11 @@ import (
"github.com/google/martian/har" "github.com/google/martian/har"
"github.com/up9inc/mizu/tap" "github.com/up9inc/mizu/tap"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"mizuserver/pkg/database"
"mizuserver/pkg/models"
"mizuserver/pkg/resolver"
"mizuserver/pkg/utils"
) )
var k8sResolver *resolver.Resolver var k8sResolver *resolver.Resolver
@ -84,7 +85,14 @@ func startReadingFiles(workingDir string) {
for _, entry := range inputHar.Log.Entries { for _, entry := range inputHar.Log.Entries {
time.Sleep(time.Millisecond * 250) time.Sleep(time.Millisecond * 250)
saveHarToDb(entry, fileInfo.Name(), false) connectionInfo := &tap.ConnectionInfo{
ClientIP: fileInfo.Name(),
ClientPort: "",
ServerIP: "",
ServerPort: "",
IsOutgoing: false,
}
saveHarToDb(entry, connectionInfo)
} }
rmErr := os.Remove(inputFilePath) rmErr := os.Remove(inputFilePath)
utils.CheckErr(rmErr) utils.CheckErr(rmErr)
@ -97,7 +105,7 @@ func startReadingChannel(outputItems <-chan *tap.OutputChannelItem) {
} }
for item := range outputItems { for item := range outputItems {
saveHarToDb(item.HarEntry, item.ConnectionInfo.ClientIP, item.ConnectionInfo.IsOutgoing) saveHarToDb(item.HarEntry, item.ConnectionInfo)
} }
} }
@ -109,17 +117,17 @@ func StartReadingOutbound(outboundLinkChannel <-chan *tap.OutboundLink) {
} }
func saveHarToDb(entry *har.Entry, sender string, isOutgoing bool) { func saveHarToDb(entry *har.Entry, connectionInfo *tap.ConnectionInfo) {
entryBytes, _ := json.Marshal(entry) entryBytes, _ := json.Marshal(entry)
serviceName, urlPath, serviceHostName := getServiceNameFromUrl(entry.Request.URL) serviceName, urlPath := getServiceNameFromUrl(entry.Request.URL)
entryId := primitive.NewObjectID().Hex() entryId := primitive.NewObjectID().Hex()
var ( var (
resolvedSource string resolvedSource string
resolvedDestination string resolvedDestination string
) )
if k8sResolver != nil { if k8sResolver != nil {
resolvedSource = k8sResolver.Resolve(sender) resolvedSource = k8sResolver.Resolve(connectionInfo.ClientIP)
resolvedDestination = k8sResolver.Resolve(serviceHostName) resolvedDestination = k8sResolver.Resolve(fmt.Sprintf("%s:%s", connectionInfo.ServerIP, connectionInfo.ServerPort))
} }
mizuEntry := models.MizuEntry{ mizuEntry := models.MizuEntry{
EntryId: entryId, EntryId: entryId,
@ -129,11 +137,11 @@ func saveHarToDb(entry *har.Entry, sender string, isOutgoing bool) {
Path: urlPath, Path: urlPath,
Method: entry.Request.Method, Method: entry.Request.Method,
Status: entry.Response.Status, Status: entry.Response.Status,
RequestSenderIp: sender, RequestSenderIp: connectionInfo.ClientIP,
Timestamp: entry.StartedDateTime.UnixNano() / int64(time.Millisecond), Timestamp: entry.StartedDateTime.UnixNano() / int64(time.Millisecond),
ResolvedSource: resolvedSource, ResolvedSource: resolvedSource,
ResolvedDestination: resolvedDestination, ResolvedDestination: resolvedDestination,
IsOutgoing: isOutgoing, IsOutgoing: connectionInfo.IsOutgoing,
} }
database.GetEntriesTable().Create(&mizuEntry) database.GetEntriesTable().Create(&mizuEntry)
@ -142,10 +150,10 @@ func saveHarToDb(entry *har.Entry, sender string, isOutgoing bool) {
broadcastToBrowserClients(baseEntryBytes) broadcastToBrowserClients(baseEntryBytes)
} }
func getServiceNameFromUrl(inputUrl string) (string, string, string) { func getServiceNameFromUrl(inputUrl string) (string, string) {
parsed, err := url.Parse(inputUrl) parsed, err := url.Parse(inputUrl)
utils.CheckErr(err) utils.CheckErr(err)
return fmt.Sprintf("%s://%s", parsed.Scheme, parsed.Host), parsed.Path, parsed.Host return fmt.Sprintf("%s://%s", parsed.Scheme, parsed.Host), parsed.Path
} }
func CheckIsServiceIP(address string) bool { func CheckIsServiceIP(address string) bool {