forked from projectcalico/calico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.go
183 lines (164 loc) · 5.55 KB
/
rules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) 2016-2022 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package iptables
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"regexp"
"strings"
log "github.com/sirupsen/logrus"
"github.com/dtest11/calico/felix/environment"
)
const (
// Compromise: shorter is better for table occupancy and readability. Longer is better for
// collision-resistance. 16 chars gives us 96 bits of entropy, which is fairly collision
// resistant.
HashLength = 16
)
type Rule struct {
Match MatchCriteria
Action Action
Comment []string
}
func (r Rule) RenderAppend(chainName, prefixFragment string, features *environment.Features) string {
fragments := make([]string, 0, 6)
fragments = append(fragments, "-A", chainName)
return r.renderInner(fragments, prefixFragment, features)
}
func (r Rule) RenderInsert(chainName, prefixFragment string, features *environment.Features) string {
fragments := make([]string, 0, 6)
fragments = append(fragments, "-I", chainName)
return r.renderInner(fragments, prefixFragment, features)
}
func (r Rule) RenderInsertAtRuleNumber(chainName string, ruleNum int, prefixFragment string, features *environment.Features) string {
fragments := make([]string, 0, 7)
fragments = append(fragments, "-I", chainName, fmt.Sprintf("%d", ruleNum))
return r.renderInner(fragments, prefixFragment, features)
}
func (r Rule) RenderReplace(chainName string, ruleNum int, prefixFragment string, features *environment.Features) string {
fragments := make([]string, 0, 7)
fragments = append(fragments, "-R", chainName, fmt.Sprintf("%d", ruleNum))
return r.renderInner(fragments, prefixFragment, features)
}
func (r Rule) renderInner(fragments []string, prefixFragment string, features *environment.Features) string {
if prefixFragment != "" {
fragments = append(fragments, prefixFragment)
}
for _, c := range r.Comment {
c = escapeComment(c)
c = truncateComment(c)
commentFragment := fmt.Sprintf("-m comment --comment \"%s\"", c)
fragments = append(fragments, commentFragment)
}
matchFragment := r.Match.Render()
if matchFragment != "" {
fragments = append(fragments, matchFragment)
}
if r.Action != nil {
actionFragment := r.Action.ToFragment(features)
if actionFragment != "" {
fragments = append(fragments, actionFragment)
}
}
return strings.Join(fragments, " ")
}
var shellUnsafe = regexp.MustCompile(`[^\w @%+=:,./-]`)
// escapeComment replaces anything other than "safe" shell characters with an
// underscore (_). This is a lossy conversion, but the expected use case
// for this stuff getting all the way to iptables are either
// - hashes/IDs generated by higher layer systems
// - comments on what the rules do
//
// which should be fine with this limitation.
// There just isn't a good way to escape this stuff in a way that iptables-restore
// will respect. strconv.Quote() leaves actual quote characters in the output,
// which break iptables-restore.
func escapeComment(s string) string {
return shellUnsafe.ReplaceAllString(s, "_")
}
const maxCommentLen = 256
func truncateComment(s string) string {
if len(s) > maxCommentLen {
return s[0:maxCommentLen]
}
return s
}
type Chain struct {
Name string
Rules []Rule
}
func (c *Chain) RuleHashes(features *environment.Features) []string {
if c == nil {
return nil
}
hashes := make([]string, len(c.Rules))
// First hash the chain name so that identical rules in different chains will get different
// hashes.
s := sha256.New224()
_, err := s.Write([]byte(c.Name))
if err != nil {
log.WithFields(log.Fields{
"chain": c.Name,
}).WithError(err).Panic("Failed to write suffix to hash.")
return nil
}
hash := s.Sum(nil)
for ii, rule := range c.Rules {
// Each hash chains in the previous hash, so that its position in the chain and
// the rules before it affect its hash.
s.Reset()
_, err = s.Write(hash)
if err != nil {
log.WithFields(log.Fields{
"action": rule.Action,
"position": ii,
"chain": c.Name,
}).WithError(err).Panic("Failed to write suffix to hash.")
}
ruleForHashing := rule.RenderAppend(c.Name, "HASH", features)
_, err = s.Write([]byte(ruleForHashing))
if err != nil {
log.WithFields(log.Fields{
"ruleFragment": ruleForHashing,
"action": rule.Action,
"position": ii,
"chain": c.Name,
}).WithError(err).Panic("Failed to write rule for hashing.")
}
hash = s.Sum(hash[0:0])
// Encode the hash using a compact character set. We use the URL-safe base64
// variant because it uses '-' and '_', which are more shell-friendly.
hashes[ii] = base64.RawURLEncoding.EncodeToString(hash)[:HashLength]
if log.GetLevel() >= log.DebugLevel {
log.WithFields(log.Fields{
"ruleFragment": ruleForHashing,
"action": rule.Action,
"position": ii,
"chain": c.Name,
"hash": hashes[ii],
}).Debug("Hashed rule")
}
}
return hashes
}
func (c *Chain) IPSetNames() (ipSetNames []string) {
if c == nil {
return nil
}
for _, rule := range c.Rules {
ipSetNames = append(ipSetNames, rule.Match.IPSetNames()...)
}
return
}