From cdf1c39a52679fedc839251af2ebefdf2b505e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Mert=20Y=C4=B1ld=C4=B1ran?= Date: Sat, 25 Sep 2021 18:15:54 +0300 Subject: [PATCH] Omit the `RULES` tab if the policy rules feature is inactive (#303) * Omit the `RULES` tab if the policy rules feature is inactive (WIP) * Propagate the boolean value `isRulesEnabled` from file read error to UI * Remove the debug log --- agent/pkg/api/main.go | 2 +- agent/pkg/controllers/entries_controller.go | 5 ++++- agent/pkg/models/models.go | 6 +++--- agent/pkg/rules/rulesHTTP.go | 13 ++++++++----- cli/kubernetes/provider.go | 2 +- tap/api/api.go | 1 + ui/src/App.tsx | 2 +- ui/src/components/EntryDetailed.tsx | 2 +- .../components/EntryDetailed/EntrySections.tsx | 4 +--- ui/src/components/EntryDetailed/EntryViewer.tsx | 17 +++++++++++------ 10 files changed, 32 insertions(+), 22 deletions(-) diff --git a/agent/pkg/api/main.go b/agent/pkg/api/main.go index 02525fabd..7163877fb 100644 --- a/agent/pkg/api/main.go +++ b/agent/pkg/api/main.go @@ -113,7 +113,7 @@ func startReadingChannel(outputItems <-chan *tapApi.OutputChannelItem, extension json.Unmarshal([]byte(mizuEntry.Entry), &pair) harEntry, err := utils.NewEntry(&pair) if err == nil { - rules, _ := models.RunValidationRulesState(*harEntry, mizuEntry.Service) + rules, _, _ := models.RunValidationRulesState(*harEntry, mizuEntry.Service) baseEntry.Rules = rules } } diff --git a/agent/pkg/controllers/entries_controller.go b/agent/pkg/controllers/entries_controller.go index 035d99fb6..4737884ff 100644 --- a/agent/pkg/controllers/entries_controller.go +++ b/agent/pkg/controllers/entries_controller.go @@ -143,11 +143,13 @@ func GetEntry(c *gin.Context) { protocol, representation, bodySize, _ := extension.Dissector.Represent(&entryData) var rules []map[string]interface{} + var isRulesEnabled bool if entryData.ProtocolName == "http" { var pair tapApi.RequestResponsePair json.Unmarshal([]byte(entryData.Entry), &pair) harEntry, _ := utils.NewEntry(&pair) - _, rulesMatched := models.RunValidationRulesState(*harEntry, entryData.Service) + _, rulesMatched, _isRulesEnabled := models.RunValidationRulesState(*harEntry, entryData.Service) + isRulesEnabled = _isRulesEnabled inrec, _ := json.Marshal(rulesMatched) json.Unmarshal(inrec, &rules) } @@ -158,6 +160,7 @@ func GetEntry(c *gin.Context) { BodySize: bodySize, Data: entryData, Rules: rules, + IsRulesEnabled: isRulesEnabled, }) } diff --git a/agent/pkg/models/models.go b/agent/pkg/models/models.go index fa733b6cb..3356dc75a 100644 --- a/agent/pkg/models/models.go +++ b/agent/pkg/models/models.go @@ -97,8 +97,8 @@ type ExtendedCreator struct { Source *string `json:"_source"` } -func RunValidationRulesState(harEntry har.Entry, service string) (tapApi.ApplicableRules, []rules.RulesMatched) { - resultPolicyToSend := rules.MatchRequestPolicy(harEntry, service) +func RunValidationRulesState(harEntry har.Entry, service string) (tapApi.ApplicableRules, []rules.RulesMatched, bool) { + resultPolicyToSend, isEnabled := rules.MatchRequestPolicy(harEntry, service) statusPolicyToSend, latency, numberOfRules := rules.PassedValidationRules(resultPolicyToSend) - return tapApi.ApplicableRules{Status: statusPolicyToSend, Latency: latency, NumberOfRules: numberOfRules}, resultPolicyToSend + return tapApi.ApplicableRules{Status: statusPolicyToSend, Latency: latency, NumberOfRules: numberOfRules}, resultPolicyToSend, isEnabled } diff --git a/agent/pkg/rules/rulesHTTP.go b/agent/pkg/rules/rulesHTTP.go index e4dc927b4..855768344 100644 --- a/agent/pkg/rules/rulesHTTP.go +++ b/agent/pkg/rules/rulesHTTP.go @@ -4,11 +4,12 @@ import ( "encoding/base64" "encoding/json" "fmt" - "github.com/romana/rlog" "reflect" "regexp" "strings" + "github.com/romana/rlog" + "github.com/google/martian/har" "github.com/up9inc/mizu/shared" jsonpath "github.com/yalp/jsonpath" @@ -43,9 +44,11 @@ func ValidateService(serviceFromRule string, service string) bool { return true } -func MatchRequestPolicy(harEntry har.Entry, service string) []RulesMatched { - enforcePolicy, _ := shared.DecodeEnforcePolicy(fmt.Sprintf("%s/%s", shared.RulePolicyPath, shared.RulePolicyFileName)) - var resultPolicyToSend []RulesMatched +func MatchRequestPolicy(harEntry har.Entry, service string) (resultPolicyToSend []RulesMatched, isEnabled bool) { + enforcePolicy, err := shared.DecodeEnforcePolicy(fmt.Sprintf("%s/%s", shared.RulePolicyPath, shared.RulePolicyFileName)) + if err == nil { + isEnabled = true + } for _, rule := range enforcePolicy.Rules { if !ValidatePath(rule.Path, harEntry.Request.URL) || !ValidateService(rule.Service, service) { continue @@ -93,7 +96,7 @@ func MatchRequestPolicy(harEntry har.Entry, service string) []RulesMatched { resultPolicyToSend = appendRulesMatched(resultPolicyToSend, true, rule) } } - return resultPolicyToSend + return } func PassedValidationRules(rulesMatched []RulesMatched) (bool, int64, int) { diff --git a/cli/kubernetes/provider.go b/cli/kubernetes/provider.go index bc0463791..5f361fe73 100644 --- a/cli/kubernetes/provider.go +++ b/cli/kubernetes/provider.go @@ -447,7 +447,7 @@ func (provider *Provider) RemoveDaemonSet(ctx context.Context, namespace string, func (provider *Provider) handleRemovalError(err error) error { // Ignore NotFound - There is nothing to delete. // Ignore Forbidden - Assume that a user could not have created the resource in the first place. - if k8serrors.IsNotFound(err) || k8serrors.IsForbidden(err){ + if k8serrors.IsNotFound(err) || k8serrors.IsForbidden(err) { return nil } diff --git a/tap/api/api.go b/tap/api/api.go index d65511012..da629ad07 100644 --- a/tap/api/api.go +++ b/tap/api/api.go @@ -138,6 +138,7 @@ type MizuEntryWrapper struct { BodySize int64 `json:"bodySize"` Data MizuEntry `json:"data"` Rules []map[string]interface{} `json:"rulesMatched,omitempty"` + IsRulesEnabled bool `json:"isRulesEnabled"` } type BaseEntryDetails struct { diff --git a/ui/src/App.tsx b/ui/src/App.tsx index 003fbc8fe..3783e061f 100644 --- a/ui/src/App.tsx +++ b/ui/src/App.tsx @@ -91,7 +91,7 @@ const App = () => { - + return (
diff --git a/ui/src/components/EntryDetailed.tsx b/ui/src/components/EntryDetailed.tsx index 538550c7b..65e6ce8b3 100644 --- a/ui/src/components/EntryDetailed.tsx +++ b/ui/src/components/EntryDetailed.tsx @@ -71,7 +71,7 @@ export const EntryDetailed: React.FC = ({entryData}) => { /> {entryData.data && } <> - {entryData.data && } + {entryData.data && } }; diff --git a/ui/src/components/EntryDetailed/EntrySections.tsx b/ui/src/components/EntryDetailed/EntrySections.tsx index 9b9d9d661..586d40d79 100644 --- a/ui/src/components/EntryDetailed/EntrySections.tsx +++ b/ui/src/components/EntryDetailed/EntrySections.tsx @@ -153,10 +153,8 @@ export const EntryTableSection: React.FC = ({title, color, ar interface EntryPolicySectionProps { - service: string, title: string, color: string, - response: any, latency?: number, arrayToIterate: any[], } @@ -200,7 +198,7 @@ export const EntryPolicySectionContainer: React.FC } -export const EntryTablePolicySection: React.FC = ({service, title, color, response, latency, arrayToIterate}) => { +export const EntryTablePolicySection: React.FC = ({title, color, latency, arrayToIterate}) => { return { arrayToIterate && arrayToIterate.length > 0 ? diff --git a/ui/src/components/EntryDetailed/EntryViewer.tsx b/ui/src/components/EntryDetailed/EntryViewer.tsx index becae4d56..04ef31074 100644 --- a/ui/src/components/EntryDetailed/EntryViewer.tsx +++ b/ui/src/components/EntryDetailed/EntryViewer.tsx @@ -33,8 +33,8 @@ const SectionsRepresentation: React.FC = ({data, color}) => { return <>{sections}; } -const AutoRepresentation: React.FC = ({representation, rulesMatched, elapsedTime, color}) => { - const TABS = [ +const AutoRepresentation: React.FC = ({representation, isRulesEnabled, rulesMatched, elapsedTime, color}) => { + var TABS = [ { tab: 'request' }, @@ -58,6 +58,10 @@ const AutoRepresentation: React.FC = ({representation, rulesMatched, elapse TABS[1]['hidden'] = true; } + if (!isRulesEnabled) { + TABS.pop() + } + return
{
@@ -70,8 +74,8 @@ const AutoRepresentation: React.FC = ({representation, rulesMatched, elapse {response && currentTab === TABS[1].tab && } - {currentTab === TABS[2].tab && - + {TABS.length > 2 && currentTab === TABS[2].tab && + }
}
; @@ -79,13 +83,14 @@ const AutoRepresentation: React.FC = ({representation, rulesMatched, elapse interface Props { representation: any; + isRulesEnabled: boolean; rulesMatched: any; color: string; elapsedTime: number; } -const EntryViewer: React.FC = ({representation, rulesMatched, elapsedTime, color}) => { - return +const EntryViewer: React.FC = ({representation, isRulesEnabled, rulesMatched, elapsedTime, color}) => { + return }; export default EntryViewer;