Skip to content

Commit cc34c52

Browse files
[release-0.4] fix pod alerts (take 2) (#138)
* fix pod alerts * fix review wording comments * fix e2e - wait for guard-service as well * fix e2e - wait also after setting TLS * fix e2e - typo nit in comment * fix e2e ready Co-authored-by: David Hadas <david.hadas@gmail.com>
1 parent fc019d0 commit cc34c52

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

.github/workflows/e2e.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ jobs:
7171
run: |
7272
echo Waiting for Pods to become ready.
7373
kubectl wait pod --timeout 300s --for=condition=Ready -n knative-serving -l "app.kubernetes.io/name=knative-serving"
74+
kubectl wait pod --timeout 300s --for=condition=Ready -n knative-serving -l "app=guard-service"
7475
# For debugging.
7576
kubectl get pods --all-namespaces
7677
@@ -113,6 +114,13 @@ jobs:
113114
URL=`kn service list|head -2|tail -1|awk '{print $2}'`
114115
echo "SERVICE_URL=$URL" >> $GITHUB_ENV
115116
117+
- name: Wait for Ready2
118+
run: |
119+
echo Waiting for Pods to become ready.
120+
kubectl wait pod --timeout 300s --for=condition=Ready -n knative-serving -l "app=guard-service"
121+
# For debugging.
122+
kubectl get pods --all-namespaces
123+
116124
- name: Run e2e Tests With TLS
117125
run: |
118126
./test/e2e/e2e-tests.sh $SERVICE_URL "httptest2"

pkg/apis/guard/v1alpha1/pod.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,17 @@ func IpNetFromProc(protocol string) (ips []net.IP) {
115115
return
116116
}
117117

118+
ipMap := make(map[string]bool)
118119
ips = make([]net.IP, 0)
120+
119121
ip, data := nextRemoteIp(data)
120122
for data != nil {
121-
ips = append(ips, ip)
123+
ipStr := ip.String()
124+
if _, ok := ipMap[ipStr]; !ok {
125+
// New IP address
126+
ipMap[ipStr] = true
127+
ips = append(ips, ip)
128+
}
122129
ip, data = nextRemoteIp(data)
123130
}
124131
return ips

pkg/guard-gate/gate.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func (p *plug) ApproveRequest(req *http.Request) (*http.Request, error) {
7878

7979
if p.gateState.shouldBlock() && (s.hasAlert() || p.gateState.hasAlert()) {
8080
p.gateState.addStat("BlockOnRequest")
81+
pi.Log.Debugf("Request blocked")
8182
cancelFunction()
8283
return nil, errSecurity
8384
}
@@ -107,8 +108,9 @@ func (p *plug) ApproveResponse(req *http.Request, resp *http.Response) (*http.Re
107108
s.screenResponseBody(resp)
108109
s.screenEnvelop()
109110
if p.gateState.shouldBlock() && (s.hasAlert() || p.gateState.hasAlert()) {
110-
s.cancel()
111111
p.gateState.addStat("BlockOnResponse")
112+
pi.Log.Debugf("Response blocked")
113+
s.cancel()
112114
return nil, errSecurity
113115
}
114116

pkg/guard-gate/session.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (s *session) sessionEventLoop(ctx context.Context) {
9696

9797
// Should we alert?
9898
if s.gateState.hasAlert() {
99-
logAlert("Pod has an alert")
99+
s.gateState.logAlert()
100100
s.gateState.addStat("BlockOnPod")
101101
return
102102
}
@@ -133,8 +133,8 @@ func (s *session) sessionEventLoop(ctx context.Context) {
133133
s.screenEnvelop()
134134
s.screenPod()
135135
if s.gateState.shouldBlock() && (s.hasAlert() || s.gateState.hasAlert()) {
136+
pi.Log.Debugf("Request processing canceled during sessionTicker")
136137
s.cancel()
137-
pi.Log.Debugf("Session Canceled")
138138
return
139139
}
140140
pi.Log.Debugf("Session Tick")

pkg/guard-gate/state.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type gateState struct {
4040
pod spec.PodProfile // pod profile
4141
srv *gateClient // maintainer of the pile, include client to the guard-service & kubeApi
4242
certPool *x509.CertPool // rootCAs
43+
prevAlert string // previous gate alert
4344
}
4445

4546
func (gs *gateState) init(cancelFunc context.CancelFunc, monitorPod bool, guardServiceUrl string, sid string, ns string, useCm bool) {
@@ -96,7 +97,7 @@ func (gs *gateState) loadConfig() {
9697
}
9798
criteria.Prepare()
9899
gs.criteria = criteria
99-
pi.Log.Infof("Loading Guardian - Active %t Auto %t", gs.criteria.Active, gs.ctrl.Auto)
100+
pi.Log.Infof("Loading Guardian - Active %t Auto %t Block %t", gs.criteria.Active, gs.ctrl.Auto, gs.ctrl.Block)
100101
}
101102

102103
// flushPile is called periodically to send the pile to the guard-service
@@ -131,14 +132,24 @@ func (gs *gateState) profileAndDecidePod() {
131132
if decision != nil {
132133
gs.addStat("PodAlert")
133134
gs.alert = decision.String("Pod -> ")
134-
135-
logAlert(gs.alert)
136-
// terminate the reverse proxy
137-
gs.cancelFunc()
135+
gs.logAlert()
136+
if gs.shouldBlock() {
137+
// Terminate the reverse proxy since all requests will block from now on
138+
pi.Log.Infof("Terminating")
139+
gs.cancelFunc()
140+
}
138141
}
139142
}
140143
}
141144

145+
func (gs *gateState) logAlert() {
146+
if gs.prevAlert == gs.alert {
147+
return
148+
}
149+
gs.prevAlert = gs.alert
150+
logAlert(gs.alert)
151+
}
152+
142153
// if pod is monitored, copy its profile to the session profile
143154
func (gs *gateState) copyPodProfile(pp *spec.PodProfile) {
144155
if !gs.monitorPod {

0 commit comments

Comments
 (0)