Skip to content

Commit

Permalink
chores: fix code according to readability review
Browse files Browse the repository at this point in the history
fix LICENSE file
  • Loading branch information
barp committed Jan 19, 2023
1 parent d175570 commit a8d2021
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 42 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2022 Google LLC
Copyright [yyyy] [name of copyright owner]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
23 changes: 11 additions & 12 deletions pkg/analyze/analyze.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package analyze is responsible for failure analysis logic from debugged container
//
// Copyright 2022 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -81,9 +83,9 @@ func (s sources) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

func findAllSources(inputFilename string) ([]*proto.SourceId, [][]string, error) {
sourceMap := map[sourceTuple]*proto.SourceId{}
searchMap := map[sourceTuple][]string{}
func findAllSources(inputFilename string) (result []*proto.SourceId, searchResult [][]string, err error) {
sourceToId := map[sourceTuple]*proto.SourceId{}
searchToQueries := map[sourceTuple][]string{}

f, err := os.Open(inputFilename)
if err != nil {
Expand All @@ -106,33 +108,30 @@ func findAllSources(inputFilename string) ([]*proto.SourceId, [][]string, error)
Id: event.GetSource().GetId(),
}

sourceMap[k] = event.Source
sourceToId[k] = event.Source
if event.GetDnsSearch() != nil {
searchMap[k] = event.GetDnsSearch().GetSearch()
searchToQueries[k] = event.GetDnsSearch().GetSearch()
}
}

if err != nil && !errors.Is(err, io.EOF) {
return nil, nil, err
}

result := sources{}

for _, s := range sourceMap {
for _, s := range sourceToId {
result = append(result, s)
}

sort.Sort(result)
sort.Sort(sources(result))

searchResult := [][]string{}
for _, source := range result {
searchResult = append(searchResult, searchMap[sourceTuple{
searchResult = append(searchResult, searchToQueries[sourceTuple{
Type: source.GetType(),
Id: source.GetId(),
}])
}

return result, searchResult, nil
return
}

func Analyze(inputFilename string, filters *Filters) (*proto.AnalysisSummary, error) {
Expand Down
58 changes: 29 additions & 29 deletions pkg/analyze/analyze_failed_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func newConnectionAnalyzer(searchPaths []string, sourceId *proto.SourceId) *conn

var _ analyzer = &connectionAnalyzer{}

func (analyzer *connectionAnalyzer) handleNetEvent(source *proto.SourceId, net *proto.Event_NetworkEvent) bool {
func (an *connectionAnalyzer) handleNetEvent(source *proto.SourceId, net *proto.Event_NetworkEvent) bool {

connection := &proto.ContainerAnalysisSummary_Connection{
TargetIp: net.GetDstAddr(),
Expand All @@ -66,34 +66,34 @@ func (analyzer *connectionAnalyzer) handleNetEvent(source *proto.SourceId, net *
}

if source.GetType() != "host" {
analyzer.connections[index] = connection
an.connections[index] = connection
}

if net.GetEventType() == proto.Event_NetworkEvent_FAILED_CONNECTION {
if source.GetType() == "host" {
// if we got it on the host we need to check if there was a corresponding connection starting from the container
if _, ok := analyzer.initiatedConnection[index]; ok {
analyzer.failedConnections[index] = connection
if _, ok := an.initiatedConnection[index]; ok {
an.failedConnections[index] = connection
}
} else {
analyzer.failedConnections[index] = connection
an.failedConnections[index] = connection
}
return true
} else if source.GetType() != "host" && net.GetEventType() == proto.Event_NetworkEvent_INITIATE_CONNECTION {
analyzer.initiatedConnection[index] = connection
an.initiatedConnection[index] = connection
return true
}

return false
}

func (analyzer *connectionAnalyzer) cleanDnsRequest(query string) string {
func (an *connectionAnalyzer) cleanDnsRequest(query string) string {
query = strings.TrimSuffix(query, ".")
if analyzer.searchPaths == nil {
if an.searchPaths == nil {
return query
}
result := query
for _, p := range analyzer.searchPaths {
for _, p := range an.searchPaths {
trimmed := strings.TrimSuffix(query, p)
if trimmed != query {
if len(trimmed) < len(result) {
Expand All @@ -107,31 +107,31 @@ func (analyzer *connectionAnalyzer) cleanDnsRequest(query string) string {
return result
}

func (analyzer *connectionAnalyzer) handleDnsEvent(dns *proto.Event_DnsQueryEvent) bool {
cleanReq := analyzer.cleanDnsRequest(dns.GetQuery())
func (an *connectionAnalyzer) handleDnsEvent(dns *proto.Event_DnsQueryEvent) bool {
cleanReq := an.cleanDnsRequest(dns.GetQuery())
if dns.GetError() != nil {
if _, ok := analyzer.successfulDns[cleanReq]; !ok {
analyzer.failedDns[cleanReq] = dns.GetError()
if _, ok := an.successfulDns[cleanReq]; !ok {
an.failedDns[cleanReq] = dns.GetError()
return true
}
}
analyzer.ipToDns[dns.GetIp()] = cleanReq
analyzer.successfulDns[cleanReq] = dns.GetIp()
delete(analyzer.failedDns, cleanReq)
an.ipToDns[dns.GetIp()] = cleanReq
an.successfulDns[cleanReq] = dns.GetIp()
delete(an.failedDns, cleanReq)
return true
}

func (analyzer *connectionAnalyzer) isEventRelevant(event *proto.Event) bool {
func (an *connectionAnalyzer) isEventRelevant(event *proto.Event) bool {
// TODO: compare the host id
if event.GetSource().GetType() == "host" {
return true
}
if event.GetSource().GetType() == "container" {
if event.GetSource().GetId() == analyzer.sourceId.GetId() {
if event.GetSource().GetId() == an.sourceId.GetId() {
return true
}
} else if event.GetSource().GetType() == "pod" {
if event.GetSource().GetId() == analyzer.sourceId.GetParent() {
if event.GetSource().GetId() == an.sourceId.GetParent() {
return true
}
}
Expand All @@ -140,43 +140,43 @@ func (analyzer *connectionAnalyzer) isEventRelevant(event *proto.Event) bool {

}

func (analyzer *connectionAnalyzer) handleEvent(event *proto.Event) bool {
if !analyzer.isEventRelevant(event) {
func (an *connectionAnalyzer) handleEvent(event *proto.Event) bool {
if !an.isEventRelevant(event) {
return true
}
net := event.GetNetwork()
if net != nil {
return analyzer.handleNetEvent(event.GetSource(), net)
return an.handleNetEvent(event.GetSource(), net)
}
dns := event.GetDnsQuery()
if dns != nil {
return analyzer.handleDnsEvent(dns)
return an.handleDnsEvent(dns)
}

return false
}

func (analyzer *connectionAnalyzer) updateSummary(summary *proto.ContainerAnalysisSummary) {
func (an *connectionAnalyzer) updateSummary(summary *proto.ContainerAnalysisSummary) {
failedConn := []*proto.ContainerAnalysisSummary_Connection{}
for _, conn := range analyzer.failedConnections {
if dns, ok := analyzer.ipToDns[conn.GetTargetIp()]; ok {
for _, conn := range an.failedConnections {
if dns, ok := an.ipToDns[conn.GetTargetIp()]; ok {
conn.TargetFqdn = dns
}
failedConn = append(failedConn, conn)
}

staticIPs := []*proto.ContainerAnalysisSummary_Connection{}
for _, conn := range analyzer.connections {
for _, conn := range an.connections {
if conn.GetTargetIp() == "0.0.0.0" {
continue
}
if _, ok := analyzer.ipToDns[conn.GetTargetIp()]; !ok {
if _, ok := an.ipToDns[conn.GetTargetIp()]; !ok {
staticIPs = append(staticIPs, conn)
}
}

failedDns := []*proto.ContainerAnalysisSummary_DnsFailure{}
for query, dnsError := range analyzer.failedDns {
for query, dnsError := range an.failedDns {
failedDns = append(failedDns, &proto.ContainerAnalysisSummary_DnsFailure{
Query: query,
Error: dnsError,
Expand Down

0 comments on commit a8d2021

Please sign in to comment.