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

Add log prefix annotation to rules #9519

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 9 additions & 1 deletion felix/iptables/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/projectcalico/calico/felix/generictables"
)

var shellUnsafe = regexp.MustCompile(`[^\w @%+=:,./-]`)

func Actions() generictables.ActionFactory {
return &actionFactory{}
}
Expand Down Expand Up @@ -189,7 +191,7 @@ type LogAction struct {
}

func (g LogAction) ToFragment(features *environment.Features) string {
return fmt.Sprintf(`--jump LOG --log-prefix "%s: " --log-level 5`, g.Prefix)
return fmt.Sprintf(`--jump LOG --log-prefix "%s: " --log-level 5`, escapeLogPrefix(g.Prefix))
}

func (g LogAction) String() string {
Expand Down Expand Up @@ -375,3 +377,9 @@ func (c SetConnMarkAction) ToFragment(features *environment.Features) string {
func (c SetConnMarkAction) String() string {
return fmt.Sprintf("SetConnMarkWithMask:%#x/%#x", c.Mark, c.Mask)
}

// escapeLogPrefix replaces anything other than "safe" shell characters with an
// underscore (_) and truncates to 27 characters.
func escapeLogPrefix(s string) string {
return shellUnsafe.ReplaceAllString(s, "_")[:27]
}
11 changes: 10 additions & 1 deletion felix/nftables/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/projectcalico/calico/felix/generictables"
)

var shellUnsafe = regexp.MustCompile(`[^\w @%+=:,./-]`)

type namespaceable interface {
Namespace(string) generictables.Action
}
Expand Down Expand Up @@ -229,7 +231,7 @@ type LogAction struct {
}

func (g LogAction) ToFragment(features *environment.Features) string {
return fmt.Sprintf(`log prefix %s level info`, g.Prefix)
return fmt.Sprintf(`log prefix "%s" level info`, escapeLogPrefix(g.Prefix))
}

func (g LogAction) String() string {
Expand Down Expand Up @@ -406,3 +408,10 @@ func (c SetConnMarkAction) ToFragment(features *environment.Features) string {
func (c SetConnMarkAction) String() string {
return fmt.Sprintf("SetConnMarkWithMask:%#x/%#x", c.Mark, c.Mask)
}


// escapeLogPrefix replaces anything other than "safe" shell characters with an
// underscore (_).
func escapeLogPrefix(s string) string {
return shellUnsafe.ReplaceAllString(s, "_")
}
6 changes: 5 additions & 1 deletion felix/rules/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,11 @@ func (r *DefaultRuleRenderer) CalculateActions(pRule *proto.Rule, ipVersion uint
actions = append(actions, r.IptablesFilterDenyAction())
case "log":
// This rule should log.
actions = append(actions, r.Log(r.LogPrefix))
prefix, ok := pRule.Metadata.Annotations["projectcalico.org/LogPrefix"]
if ! ok {
prefix = r.LogPrefix
}
actions = append(actions, r.Log(prefix))
default:
log.WithField("action", pRule.Action).Panic("Unknown rule action")
}
Expand Down