@@ -223,8 +223,6 @@ func New(cfg config.Config) (*Tracee, error) {
223223 return nil , errfmt .Errorf ("validation error: %v" , err )
224224 }
225225
226- policyManager := policy .NewPolicyManager ()
227-
228226 // Create Tracee
229227
230228 t := & Tracee {
@@ -236,10 +234,15 @@ func New(cfg config.Config) (*Tracee, error) {
236234 eventsState : make (map [events.ID ]events.EventState ),
237235 eventSignatures : make (map [events.ID ]bool ),
238236 streamsManager : streams .NewStreamsManager (),
239- policyManager : policyManager ,
237+ policyManager : policy . NewPolicyManager ( cfg . Policies ) ,
240238 requiredKsyms : []string {},
241239 }
242240
241+ // In the future Tracee Config will be changed in runtime, and will demand a proper
242+ // object to manage it. config.Config is currently a transient object that should be
243+ // used only to create the Tracee instance.
244+ t .config .Policies = nil // policies must be managed by the policy manager
245+
243246 eventsDependencies := dependencies .NewDependenciesManager (
244247 func (id events.ID ) events.Dependencies {
245248 return events .Core .GetDefinitionByID (id ).GetDependencies ()
@@ -328,7 +331,7 @@ func New(cfg config.Config) (*Tracee, error) {
328331
329332 // TODO: extract this to a function to be called from here and from
330333 // policies changes.
331- for it := t .config . Policies .CreateAllIterator (); it .HasNext (); {
334+ for it := t .policyManager .CreateAllIterator (); it .HasNext (); {
332335 p := it .Next ()
333336 for e := range p .EventsToTrace {
334337 var submit , emit uint64
@@ -340,7 +343,7 @@ func New(cfg config.Config) (*Tracee, error) {
340343 utils .SetBit (& emit , uint (p .ID ))
341344 t .selectEvent (e , events.EventState {Submit : submit , Emit : emit })
342345
343- policyManager .EnableRule (p .ID , e )
346+ t . policyManager .EnableRule (p .ID , e )
344347 }
345348 }
346349
@@ -675,7 +678,7 @@ func (t *Tracee) initDerivationTable() error {
675678 shouldSubmit := func (id events.ID ) func () bool {
676679 return func () bool { return t .eventsState [id ].Submit > 0 }
677680 }
678- symbolsCollisions := derive .SymbolsCollision (t .contSymbolsLoader , t .config . Policies )
681+ symbolsCollisions := derive .SymbolsCollision (t .contSymbolsLoader , t .policyManager )
679682
680683 executeFailedGen , err := derive .InitProcessExecuteFailedGenerator ()
681684 if err != nil {
@@ -719,7 +722,7 @@ func (t *Tracee) initDerivationTable() error {
719722 Enabled : shouldSubmit (events .SymbolsLoaded ),
720723 DeriveFunction : derive .SymbolsLoaded (
721724 t .contSymbolsLoader ,
722- t .config . Policies ,
725+ t .policyManager ,
723726 ),
724727 },
725728 events .SymbolsCollision : {
@@ -895,12 +898,12 @@ func (t *Tracee) getOptionsConfig() uint32 {
895898
896899// newConfig returns a new Config instance based on the current Tracee state and
897900// the given policies config and version.
898- func (t * Tracee ) newConfig (cfg * policy.PoliciesConfig , version uint16 ) * Config {
901+ func (t * Tracee ) newConfig (cfg * policy.PoliciesConfig ) * Config {
899902 return & Config {
900903 TraceePid : uint32 (os .Getpid ()),
901904 Options : t .getOptionsConfig (),
902905 CgroupV1Hid : uint32 (t .cgroups .GetDefaultCgroupHierarchyID ()),
903- PoliciesVersion : version ,
906+ PoliciesVersion : 1 , // version will be removed soon
904907 PoliciesConfig : * cfg ,
905908 }
906909}
@@ -963,7 +966,7 @@ func (t *Tracee) initKsymTableRequiredSyms() error {
963966 }
964967 }
965968 if _ , ok := t .eventsState [events .PrintMemDump ]; ok {
966- for it := t .config . Policies .CreateAllIterator (); it .HasNext (); {
969+ for it := t .policyManager .CreateAllIterator (); it .HasNext (); {
967970 p := it .Next ()
968971 // This might break in the future if PrintMemDump will become a dependency of another event.
969972 _ , isChosen := p .EventsToTrace [events .PrintMemDump ]
@@ -1145,7 +1148,7 @@ func (t *Tracee) populateBPFMaps() error {
11451148 }
11461149
11471150 // Initialize config and filter maps
1148- err = t .populateFilterMaps (t . config . Policies , false )
1151+ err = t .populateFilterMaps (false )
11491152 if err != nil {
11501153 return errfmt .WrapError (err )
11511154 }
@@ -1217,8 +1220,8 @@ func (t *Tracee) populateBPFMaps() error {
12171220}
12181221
12191222// populateFilterMaps populates the eBPF maps with the given policies
1220- func (t * Tracee ) populateFilterMaps (newPolicies * policy. Policies , updateProcTree bool ) error {
1221- polCfg , err := newPolicies .UpdateBPF (
1223+ func (t * Tracee ) populateFilterMaps (updateProcTree bool ) error {
1224+ polCfg , err := t . policyManager .UpdateBPF (
12221225 t .bpfModule ,
12231226 t .containers ,
12241227 t .eventsState ,
@@ -1232,7 +1235,7 @@ func (t *Tracee) populateFilterMaps(newPolicies *policy.Policies, updateProcTree
12321235
12331236 // Create new config with updated policies and update eBPF map
12341237
1235- cfg := t .newConfig (polCfg , newPolicies . Version () )
1238+ cfg := t .newConfig (polCfg )
12361239 if err := cfg .UpdateBPF (t .bpfModule ); err != nil {
12371240 return errfmt .WrapError (err )
12381241 }
@@ -1382,7 +1385,7 @@ func (t *Tracee) initBPF() error {
13821385 }
13831386
13841387 // returned PoliciesConfig is not used here, therefore it's discarded
1385- _ , err = t .config . Policies .UpdateBPF (t .bpfModule , t .containers , t .eventsState , t .eventsParamTypes , false , true )
1388+ _ , err = t .policyManager .UpdateBPF (t .bpfModule , t .containers , t .eventsState , t .eventsParamTypes , false , true )
13861389 if err != nil {
13871390 return errfmt .WrapError (err )
13881391 }
@@ -1715,11 +1718,11 @@ func (t *Tracee) getSelfLoadedPrograms(kprobesOnly bool) map[string]int {
17151718func (t * Tracee ) invokeInitEvents (out chan * trace.Event ) {
17161719 var matchedPolicies uint64
17171720
1718- setMatchedPolicies := func (event * trace.Event , matchedPolicies uint64 , pols * policy.Policies ) {
1719- event .PoliciesVersion = pols . Version ()
1721+ setMatchedPolicies := func (event * trace.Event , matchedPolicies uint64 , pManager * policy.PolicyManager ) {
1722+ event .PoliciesVersion = 1 // version will be removed soon
17201723 event .MatchedPoliciesKernel = matchedPolicies
17211724 event .MatchedPoliciesUser = matchedPolicies
1722- event .MatchedPolicies = pols .MatchedNames (matchedPolicies )
1725+ event .MatchedPolicies = pManager .MatchedNames (matchedPolicies )
17231726 }
17241727
17251728 policiesMatch := func (state events.EventState ) uint64 {
@@ -1731,7 +1734,7 @@ func (t *Tracee) invokeInitEvents(out chan *trace.Event) {
17311734 matchedPolicies = policiesMatch (t .eventsState [events .InitNamespaces ])
17321735 if matchedPolicies > 0 {
17331736 systemInfoEvent := events .InitNamespacesEvent ()
1734- setMatchedPolicies (& systemInfoEvent , matchedPolicies , t .config . Policies )
1737+ setMatchedPolicies (& systemInfoEvent , matchedPolicies , t .policyManager )
17351738 out <- & systemInfoEvent
17361739 _ = t .stats .EventCount .Increment ()
17371740 }
@@ -1743,7 +1746,7 @@ func (t *Tracee) invokeInitEvents(out chan *trace.Event) {
17431746 existingContainerEvents := events .ExistingContainersEvents (t .containers , t .config .NoContainersEnrich )
17441747 for i := range existingContainerEvents {
17451748 event := & (existingContainerEvents [i ])
1746- setMatchedPolicies (event , matchedPolicies , t .config . Policies )
1749+ setMatchedPolicies (event , matchedPolicies , t .policyManager )
17471750 out <- event
17481751 _ = t .stats .EventCount .Increment ()
17491752 }
@@ -1754,7 +1757,7 @@ func (t *Tracee) invokeInitEvents(out chan *trace.Event) {
17541757 matchedPolicies = policiesMatch (t .eventsState [events .FtraceHook ])
17551758 if matchedPolicies > 0 {
17561759 ftraceBaseEvent := events .GetFtraceBaseEvent ()
1757- setMatchedPolicies (ftraceBaseEvent , matchedPolicies , t .config . Policies )
1760+ setMatchedPolicies (ftraceBaseEvent , matchedPolicies , t .policyManager )
17581761 logger .Debugw ("started ftraceHook goroutine" )
17591762
17601763 // TODO: Ideally, this should be inside the goroutine and be computed before each run,
@@ -1823,18 +1826,7 @@ func (t *Tracee) triggerMemDump(event trace.Event) []error {
18231826
18241827 var errs []error
18251828
1826- // We want to use the policies of relevant to the triggering event
1827- policies , err := policy .Snapshots ().Get (event .PoliciesVersion )
1828- if err != nil {
1829- logger .Debugw ("Error getting policies for print_mem_dump event" , "error" , err )
1830- // For fallback, try to use latest policies
1831- policies , err = policy .Snapshots ().GetLast ()
1832- if err != nil {
1833- return []error {err }
1834- }
1835- }
1836-
1837- for it := policies .CreateAllIterator (); it .HasNext (); {
1829+ for it := t .policyManager .CreateAllIterator (); it .HasNext (); {
18381830 p := it .Next ()
18391831 // This might break in the future if PrintMemDump will become a dependency of another event.
18401832 _ , isChosen := p .EventsToTrace [events .PrintMemDump ]
@@ -1972,7 +1964,7 @@ func (t *Tracee) Subscribe(policyNames []string) (*streams.Stream, error) {
19721964 var policyMask uint64
19731965
19741966 for _ , policyName := range policyNames {
1975- p , err := t .config . Policies .LookupByName (policyName )
1967+ p , err := t .policyManager .LookupByName (policyName )
19761968 if err != nil {
19771969 return nil , err
19781970 }
@@ -2023,7 +2015,7 @@ func (t *Tracee) EnableRule(policyNames []string, ruleId string) error {
20232015 }
20242016
20252017 for _ , policyName := range policyNames {
2026- p , err := t .config . Policies .LookupByName (policyName )
2018+ p , err := t .policyManager .LookupByName (policyName )
20272019 if err != nil {
20282020 return err
20292021 }
@@ -2042,7 +2034,7 @@ func (t *Tracee) DisableRule(policyNames []string, ruleId string) error {
20422034 }
20432035
20442036 for _ , policyName := range policyNames {
2045- p , err := t .config . Policies .LookupByName (policyName )
2037+ p , err := t .policyManager .LookupByName (policyName )
20462038 if err != nil {
20472039 return err
20482040 }
0 commit comments