-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new policyfilter tetra command. We add two sub-commands: dump (which is the same as tetra dump policyfilter) and add to add entries to the map. This is strictly for development/debugging, so we mark the command hidden. Signed-off-by: Kornilios Kourtis <kornilios@isovalent.com>
- Loading branch information
Showing
4 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Tetragon | ||
|
||
package policyfilter | ||
|
||
import ( | ||
"path/filepath" | ||
"strconv" | ||
|
||
"github.com/cilium/tetragon/cmd/tetra/dump" | ||
"github.com/cilium/tetragon/pkg/cgroups" | ||
"github.com/cilium/tetragon/pkg/defaults" | ||
"github.com/cilium/tetragon/pkg/logger" | ||
"github.com/cilium/tetragon/pkg/policyfilter" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func New() *cobra.Command { | ||
ret := &cobra.Command{ | ||
Use: "policyfilter", | ||
Short: "manage policyfilter map (only for debugging)", | ||
Hidden: true, | ||
SilenceUsage: true, | ||
} | ||
|
||
ret.AddCommand( | ||
dumpCmd(), | ||
addCommand(), | ||
) | ||
|
||
return ret | ||
} | ||
|
||
func dumpCmd() *cobra.Command { | ||
mapFname := filepath.Join(defaults.DefaultMapRoot, defaults.DefaultMapPrefix, policyfilter.MapName) | ||
ret := &cobra.Command{ | ||
Use: "dump", | ||
Short: "dump policyfilter state", | ||
Args: cobra.ExactArgs(0), | ||
Run: func(cmd *cobra.Command, _ []string) { | ||
dump.PolicyfilterState(mapFname) | ||
}, | ||
} | ||
|
||
flags := ret.Flags() | ||
flags.StringVar(&mapFname, "map-fname", mapFname, "policyfilter map filename") | ||
return ret | ||
} | ||
|
||
func addCommand() *cobra.Command { | ||
var argType string | ||
mapFname := filepath.Join(defaults.DefaultMapRoot, defaults.DefaultMapPrefix, policyfilter.MapName) | ||
ret := &cobra.Command{ | ||
Use: "add [policy id] [cgroup]", | ||
Short: "add policyfilter entry", | ||
Args: cobra.ExactArgs(2), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
x, err := strconv.ParseUint(args[0], 10, 32) | ||
if err != nil { | ||
logger.GetLogger().WithError(err).Fatal("Failed to parse policy id") | ||
} | ||
polID := policyfilter.PolicyID(x) | ||
|
||
var cgID uint64 | ||
switch argType { | ||
case "file": | ||
cgID, err = cgroups.GetCgroupIdFromPath(args[1]) | ||
case "id": | ||
cgID, err = strconv.ParseUint(args[1], 10, 32) | ||
default: | ||
logger.GetLogger().WithField("type", argType).WithError(err).Fatal("Unknown type") | ||
} | ||
|
||
if err != nil { | ||
logger.GetLogger().WithError(err).Fatal("Failed to parse cgroup") | ||
} | ||
|
||
addCgroup(mapFname, polID, policyfilter.CgroupID(cgID)) | ||
}, | ||
} | ||
|
||
flags := ret.Flags() | ||
flags.StringVar(&argType, "arg-type", "file", "cgroup type (id,file)") | ||
flags.StringVar(&mapFname, "map-fname", mapFname, "policyfilter map filename") | ||
return ret | ||
} | ||
|
||
func addCgroup(fname string, polID policyfilter.PolicyID, cgID policyfilter.CgroupID) { | ||
m, err := policyfilter.OpenMap(fname) | ||
if err != nil { | ||
logger.GetLogger().WithError(err).Fatal("Failed to open policyfilter map") | ||
return | ||
} | ||
defer m.Close() | ||
|
||
err = m.AddCgroup(polID, cgID) | ||
if err != nil { | ||
logger.GetLogger().WithError(err).Fatal("Failed to add cgroup id") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters