Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synthesis pre processing #94

Merged
merged 39 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c519f8e
added source and destination Groups to Rule
ShiriMoran Dec 16, 2024
89ffc7d
added source and destination Groups to FwRule
ShiriMoran Dec 17, 2024
6f528f6
label can be group
ShiriMoran Dec 16, 2024
167bfe9
synthesis skeleton
ShiriMoran Dec 17, 2024
24440c2
typo fix
ShiriMoran Dec 17, 2024
33039e8
synthesis test
ShiriMoran Dec 17, 2024
3214816
NewAtomicTerm
ShiriMoran Dec 17, 2024
439cb1e
renaming
ShiriMoran Dec 17, 2024
f79b9ec
typo fix
ShiriMoran Dec 17, 2024
44ef571
typo fix
ShiriMoran Dec 18, 2024
ac06b52
1. Gather the relevant group info into parsedRule
ShiriMoran Dec 18, 2024
5f06a8c
added relevant group info to FWRule
ShiriMoran Dec 18, 2024
ab29c8e
Scope added to FWRule
ShiriMoran Dec 18, 2024
57080c7
Added test printing and exported fields to be used by synthesis
ShiriMoran Dec 18, 2024
e17dfd6
update model
ShiriMoran Dec 18, 2024
a583ac2
code for convertFWRuleToSymbolicPaths
ShiriMoran Dec 18, 2024
7903b88
print path conversion result
ShiriMoran Dec 18, 2024
b58a20d
gathered the data into the relevant data structure and moved printing…
ShiriMoran Dec 18, 2024
f44b339
renaming
ShiriMoran Dec 19, 2024
a8380bb
minor reorg
ShiriMoran Dec 19, 2024
33455b8
test preProcessing
ShiriMoran Dec 19, 2024
91286b5
moved redundant code
ShiriMoran Dec 19, 2024
266ebf1
temp WA
ShiriMoran Dec 19, 2024
317c27b
fixed code so that tests will work after the changes
ShiriMoran Dec 19, 2024
509fe93
the issue seems to be in the PR pipe and not in my code
ShiriMoran Dec 19, 2024
9dc3066
Merge branch 'main' into synthesisPreProcessing
ShiriMoran Dec 19, 2024
c48a6fb
update wrt latest changes
ShiriMoran Dec 19, 2024
61543c3
lint
ShiriMoran Dec 19, 2024
51dca54
lint
ShiriMoran Dec 19, 2024
4416ed3
added explaination
ShiriMoran Dec 19, 2024
491b9aa
move getAtomicTermsForGroups to the correct file
ShiriMoran Dec 22, 2024
f0217e2
Synthesis test (#95)
ShiriMoran Dec 22, 2024
12ae417
block comment
ShiriMoran Dec 24, 2024
6e91d6c
fixed flow - category is stll essential after preprocessing
ShiriMoran Dec 24, 2024
607065e
lint
ShiriMoran Dec 24, 2024
a908ad0
removed commented code which is not relevant to this branch
ShiriMoran Dec 24, 2024
b0ad265
Update pkg/collector/data_model.go
ShiriMoran Dec 24, 2024
b578ec8
Update pkg/synthesis/synthesis.go
ShiriMoran Dec 24, 2024
4c8e72f
lint
ShiriMoran Dec 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/collector/data_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,13 @@ func (group *Group) UnmarshalJSON(b []byte) error {
)
}

func (group *Group) Name() string {
if group.Group.DisplayName == nil {
return ""
}
return *group.Group.DisplayName
ShiriMoran marked this conversation as resolved.
Show resolved Hide resolved
}

///////////////////////////////////////////////////////////////////////////////////////

type Domain struct {
Expand Down
12 changes: 6 additions & 6 deletions pkg/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type config struct {
vms []*endpoints.VM // list of all vms
vmsMap map[string]*endpoints.VM // map from uid to vm objects
fw *dfw.DFW // currently assuming one DFW only (todo: rename pkg dfw)
Fw *dfw.DFW // currently assuming one DFW only (todo: rename pkg dfw)
analyzedConnectivity connMap // the resulting connectivity map from analyzing this configuration
analysisDone bool
}
Expand All @@ -29,14 +29,14 @@ func (c *config) ComputeConnectivity(vmsFilter []string) {
logging.Debugf("compute connectivity on parsed config")
res := connMap{}
// make sure all vm pairs are in the result, by init with global default
res.initPairs(c.fw.GlobalDefaultAllow(), c.vms, vmsFilter)
res.initPairs(c.Fw.GlobalDefaultAllow(), c.vms, vmsFilter)
// iterate over all vm pairs in the initialized map at res, get the analysis result per pair
for src, srcMap := range res {
for dst := range srcMap {
if src == dst {
continue
}
conn := c.fw.AllowedConnections(src, dst)
conn := c.Fw.AllowedConnections(src, dst)
res.add(src, dst, conn)
}
}
Expand All @@ -55,11 +55,11 @@ func (c *config) getConfigInfoStr() string {
sb.WriteString(common.OutputSectionSep)

sb.WriteString("DFW:\n")
sb.WriteString(c.fw.OriginalRulesStrFormatted())
sb.WriteString(c.Fw.OriginalRulesStrFormatted())
sb.WriteString(common.ShortSep)
sb.WriteString(c.fw.String())
sb.WriteString(c.Fw.String())
sb.WriteString(common.ShortSep)
sb.WriteString(c.fw.AllEffectiveRules())
sb.WriteString(c.Fw.AllEffectiveRules())
sb.WriteString(common.OutputSectionSep)

return sb.String()
Expand Down
4 changes: 2 additions & 2 deletions pkg/model/connectivity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ var dfwAllowAllByDefault = dfw.NewEmptyDFW(true) // no rules and global def
// basic test
var config1 = &config{
vms: allVms,
fw: dfwAllowNothingByDefault,
Fw: dfwAllowNothingByDefault,
}

var config2 = &config{
vms: allVms,
fw: dfwAllowAllByDefault,
Fw: dfwAllowAllByDefault,
}

func sumPairs(c connMap) int {
Expand Down
90 changes: 46 additions & 44 deletions pkg/model/dfw/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/np-guard/vmware-analyzer/pkg/collector"
"github.com/np-guard/vmware-analyzer/pkg/logging"
"github.com/np-guard/vmware-analyzer/pkg/model/endpoints"
"github.com/np-guard/vmware-analyzer/pkg/symbolicexpr"
)

// https://dp-downloads.broadcom.com/api-content/apis/API_NTDCRA_001/4.2/html/api_includes/types_SecurityPolicy.html
Expand Down Expand Up @@ -52,7 +51,7 @@ const (
}
}*/

func (d DfwCategory) string() string {
func (d DfwCategory) String() string {
switch d {
case ethernetCategory:
return EthernetStr
Expand All @@ -75,30 +74,30 @@ var categoriesList = []DfwCategory{
ethernetCategory, emergencyCategory, infrastructureCategory, envCategory, appCategoty, emptyCategory,
}

// effectiveRules are built from original rules, split to separate inbound & outbound rules
// EffectiveRules are built from original rules, split to separate Inbound & Outbound rules
// consider already the scope from the original rules
type effectiveRules struct {
inbound []*FwRule
outbound []*FwRule
type EffectiveRules struct {
Inbound []*FwRule
Outbound []*FwRule
}

func (e *effectiveRules) addInboundRule(r *FwRule) {
func (e *EffectiveRules) addInboundRule(r *FwRule) {
if r != nil {
e.inbound = append(e.inbound, r)
e.Inbound = append(e.Inbound, r)
}
}

func (e *effectiveRules) addOutboundRule(r *FwRule) {
func (e *EffectiveRules) addOutboundRule(r *FwRule) {
if r != nil {
e.outbound = append(e.outbound, r)
e.Outbound = append(e.Outbound, r)
}
}

type categorySpec struct {
category DfwCategory
type CategorySpec struct {
Category DfwCategory
rules []*FwRule // ordered list of rules
defaultAction ruleAction
processedRules *effectiveRules // ordered list of effective rules
defaultAction RuleAction
ProcessedRules *EffectiveRules // ordered list of effective rules
dfwRef *DFW
}

Expand All @@ -110,16 +109,16 @@ type categorySpec struct {
// todo: may possibly eliminate jumpToAppConns and unify them with notDeterminedConns
//
//nolint:gocritic // for now keep commentedOutCode
func (c *categorySpec) analyzeCategory(src, dst *endpoints.VM, isIngress bool,
func (c *CategorySpec) analyzeCategory(src, dst *endpoints.VM, isIngress bool,
) (allowedConns, jumpToAppConns, deniedConns, nonDet *netset.TransportSet) {
allowedConns, jumpToAppConns, deniedConns = netset.NoTransports(), netset.NoTransports(), netset.NoTransports()
rules := c.processedRules.inbound // inbound effective rules
rules := c.ProcessedRules.Inbound // inbound effective rules
if !isIngress {
rules = c.processedRules.outbound // outbound effective rules
rules = c.ProcessedRules.Outbound // outbound effective rules
}
for _, rule := range rules /*c.rules*/ {
if rule.processedRuleCapturesPair(src, dst) /*rule.capturesPair(src, dst, isIngress)*/ {
switch rule.action {
switch rule.Action {
case actionAllow:
addedAllowedConns := rule.conn.Subtract(deniedConns).Subtract(jumpToAppConns)
allowedConns = allowedConns.Union(addedAllowedConns)
Expand Down Expand Up @@ -147,76 +146,79 @@ func (c *categorySpec) analyzeCategory(src, dst *endpoints.VM, isIngress bool,
return allowedConns, jumpToAppConns, deniedConns, nonDet
}

func (c *categorySpec) originalRulesStr() []string {
func (c *CategorySpec) originalRulesStr() []string {
rulesStr := make([]string, len(c.rules))
for i := range c.rules {
rulesStr[i] = c.rules[i].originalRuleStr()
}
return rulesStr
}

func (c *categorySpec) string() string {
func (c *CategorySpec) string() string {
rulesStr := make([]string, len(c.rules)+1)
rulesStr[0] = "rules:"
for i := range c.rules {
rulesStr[i+1] = c.rules[i].string()
}
return fmt.Sprintf("category: %s\n%s\ndefault action: %s", c.category.string(),
return fmt.Sprintf("category: %s\n%s\ndefault action: %s", c.Category.String(),
strings.Join(rulesStr, lineSeparatorStr), string(c.defaultAction))
}

func (c *categorySpec) inboundEffectiveRules() string {
rulesStr := make([]string, len(c.processedRules.inbound))
for i := range c.processedRules.inbound {
rulesStr[i] = c.processedRules.inbound[i].effectiveRuleStr()
func (c *CategorySpec) inboundEffectiveRules() string {
rulesStr := make([]string, len(c.ProcessedRules.Inbound))
for i := range c.ProcessedRules.Inbound {
rulesStr[i] = c.ProcessedRules.Inbound[i].effectiveRuleStr()
}
return strings.Join(rulesStr, lineSeparatorStr)
}

func (c *categorySpec) outboundEffectiveRules() string {
rulesStr := make([]string, len(c.processedRules.outbound))
for i := range c.processedRules.outbound {
rulesStr[i] = c.processedRules.outbound[i].effectiveRuleStr()
func (c *CategorySpec) outboundEffectiveRules() string {
rulesStr := make([]string, len(c.ProcessedRules.Outbound))
for i := range c.ProcessedRules.Outbound {
rulesStr[i] = c.ProcessedRules.Outbound[i].effectiveRuleStr()
}
return strings.Join(rulesStr, lineSeparatorStr)
}

func (c *categorySpec) addRule(src, dst []*endpoints.VM, conn *netset.TransportSet,
action, direction string, ruleID int, origRule *collector.Rule, scope []*endpoints.VM,
secPolicyName string, origDefaultRule *collector.FirewallRule) {
func (c *CategorySpec) addRule(src, dst []*endpoints.VM, srcGroups, dstGroups, scopeGroups []*collector.Group,
isAllSrcGroup, isAllDstGroup bool, conn *netset.TransportSet, action, direction string, ruleID int,
origRule *collector.Rule, scope []*endpoints.VM, secPolicyName string, origDefaultRule *collector.FirewallRule) {
newRule := &FwRule{
srcVMs: src,
dstVMs: dst,
SrcGroups: srcGroups,
IsAllSrcGroups: isAllSrcGroup,
DstGroups: dstGroups,
IsAllDstGroups: isAllDstGroup,
conn: conn,
action: actionFromString(action),
Action: actionFromString(action),
direction: direction,
ruleID: ruleID,
origRuleObj: origRule,
origDefaultRuleObj: origDefaultRule,
scope: scope,
ScopeGroups: scopeGroups,
secPolicyName: secPolicyName,
secPolicyCategory: c.category.string(),
secPolicyCategory: c.Category.String(),
categoryRef: c,
dfwRef: c.dfwRef,
symbolicSrc: []*symbolicexpr.SymbolicPath{}, // todo tmp
symbolicDst: []*symbolicexpr.SymbolicPath{}, // todo tmp
}
c.rules = append(c.rules, newRule)

inbound, outbound := newRule.effectiveRules()
if c.category != ethernetCategory {
c.processedRules.addInboundRule(inbound)
c.processedRules.addOutboundRule(outbound)
if c.Category != ethernetCategory {
c.ProcessedRules.addInboundRule(inbound)
c.ProcessedRules.addOutboundRule(outbound)
} else {
logging.Debugf("rule %d in ethernet category is ignored and not added to list of effective rules", ruleID)
logging.Debugf("rule %d in ethernet Category is ignored and not added to list of effective rules", ruleID)
}
}

func newEmptyCategory(c DfwCategory, d *DFW) *categorySpec {
return &categorySpec{
category: c,
func newEmptyCategory(c DfwCategory, d *DFW) *CategorySpec {
return &CategorySpec{
Category: c,
dfwRef: d,
defaultAction: actionNone,
processedRules: &effectiveRules{},
ProcessedRules: &EffectiveRules{},
}
}
54 changes: 28 additions & 26 deletions pkg/model/dfw/dfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
)

type DFW struct {
categoriesSpecs []*categorySpec // ordered list of categories
defaultAction ruleAction // global default (?)
CategoriesSpecs []*CategorySpec // ordered list of categories
defaultAction RuleAction // global default (?)

pathsToDisplayNames map[string]string // map from printing paths references as display names instead
}
Expand All @@ -36,8 +36,8 @@ func (d *DFW) AllowedConnectionsIngressOrEgress(src, dst *endpoints.VM, isIngres
allDeniedConns := netset.NoTransports()
allNotDeterminedConns := netset.NoTransports()

for _, dfwCategory := range d.categoriesSpecs {
if dfwCategory.category == ethernetCategory {
for _, dfwCategory := range d.CategoriesSpecs {
if dfwCategory.Category == ethernetCategory {
continue // cuurently skip L2 rules
}
// get analyzed conns from this category
Expand Down Expand Up @@ -75,7 +75,7 @@ func (d *DFW) OriginalRulesStrFormatted() string {
writer := tabwriter.NewWriter(&builder, 1, 1, 1, ' ', tabwriter.Debug)
fmt.Fprintln(writer, "original rules:")
fmt.Fprintln(writer, getRulesFormattedHeaderLine())
for _, c := range d.categoriesSpecs {
for _, c := range d.CategoriesSpecs {
for _, ruleStr := range c.originalRulesStr() {
if ruleStr == "" {
continue
Expand All @@ -89,22 +89,22 @@ func (d *DFW) OriginalRulesStrFormatted() string {

// return a string rep that shows the fw-rules in all categories
func (d *DFW) String() string {
categoriesStrings := make([]string, len(d.categoriesSpecs))
for i := range d.categoriesSpecs {
categoriesStrings[i] = d.categoriesSpecs[i].string()
categoriesStrings := make([]string, len(d.CategoriesSpecs))
for i := range d.CategoriesSpecs {
categoriesStrings[i] = d.CategoriesSpecs[i].string()
}
return strings.Join(categoriesStrings, lineSeparatorStr)
}

func (d *DFW) AllEffectiveRules() string {
inboundRes := []string{}
outboundRes := []string{}
for i := range d.categoriesSpecs {
if len(d.categoriesSpecs[i].processedRules.inbound) > 0 {
inboundRes = append(inboundRes, d.categoriesSpecs[i].inboundEffectiveRules())
for i := range d.CategoriesSpecs {
if len(d.CategoriesSpecs[i].ProcessedRules.Inbound) > 0 {
inboundRes = append(inboundRes, d.CategoriesSpecs[i].inboundEffectiveRules())
}
if len(d.categoriesSpecs[i].processedRules.outbound) > 0 {
outboundRes = append(outboundRes, d.categoriesSpecs[i].outboundEffectiveRules())
if len(d.CategoriesSpecs[i].ProcessedRules.Outbound) > 0 {
outboundRes = append(outboundRes, d.CategoriesSpecs[i].outboundEffectiveRules())
}
}
inbound := fmt.Sprintf("\nInbound effective rules only:%s%s\n", common.ShortSep, strings.Join(inboundRes, lineSeparatorStr))
Expand All @@ -114,34 +114,36 @@ func (d *DFW) AllEffectiveRules() string {

// AddRule func for testing purposes

func (d *DFW) AddRule(src, dst []*endpoints.VM, conn *netset.TransportSet, categoryStr, actionStr, direction string,
func (d *DFW) AddRule(src, dst []*endpoints.VM, srcGroups, dstGroups, scopeGroups []*collector.Group,
isAllSrcGroups, isAllDstGroups bool, conn *netset.TransportSet, categoryStr, actionStr, direction string,
ruleID int, origRule *collector.Rule, scope []*endpoints.VM, secPolicyName string, origDefaultRule *collector.FirewallRule) {
for _, fwCategory := range d.categoriesSpecs {
if fwCategory.category.string() == categoryStr {
fwCategory.addRule(src, dst, conn, actionStr, direction, ruleID, origRule, scope, secPolicyName, origDefaultRule)
for _, fwCategory := range d.CategoriesSpecs {
if fwCategory.Category.String() == categoryStr {
fwCategory.addRule(src, dst, srcGroups, dstGroups, scopeGroups, isAllSrcGroups, isAllDstGroups, conn,
actionStr, direction, ruleID, origRule, scope, secPolicyName, origDefaultRule)
}
}
}

/*func (d *DFW) AddRule(src, dst []*endpoints.VM, conn *netset.TransportSet, categoryStr string, actionStr string) {
var categoryObj *categorySpec
for _, c := range d.categoriesSpecs {
if c.category.string() == categoryStr {
var categoryObj *CategorySpec
for _, c := range d.CategoriesSpecs {
if c.Category.string() == categoryStr {
categoryObj = c
}
}
if categoryObj == nil { // create new category if missing
categoryObj = &categorySpec{
category: dfwCategoryFromString(categoryStr),
if categoryObj == nil { // create new Category if missing
categoryObj = &CategorySpec{
Category: dfwCategoryFromString(categoryStr),
}
d.categoriesSpecs = append(d.categoriesSpecs, categoryObj)
d.CategoriesSpecs = append(d.CategoriesSpecs, categoryObj)
}

newRule := &FwRule{
srcVMs: src,
dstVMs: dst,
conn: netset.All(), // todo: change
action: actionFromString(actionStr),
Action: actionFromString(actionStr),
}
categoryObj.rules = append(categoryObj.rules, newRule)
}*/
Expand All @@ -155,7 +157,7 @@ func NewEmptyDFW(globalDefaultAllow bool) *DFW {
res.defaultAction = actionAllow
}
for _, c := range categoriesList {
res.categoriesSpecs = append(res.categoriesSpecs, newEmptyCategory(c, res))
res.CategoriesSpecs = append(res.CategoriesSpecs, newEmptyCategory(c, res))
}
return res
}
Expand Down
Loading
Loading