-
Notifications
You must be signed in to change notification settings - Fork 148
[RFC-0004] Allow disabling of insecure HTTP connections for alert providers #404
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import ( | |
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
"time" | ||
|
@@ -44,7 +45,6 @@ import ( | |
|
||
func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
r.Context() | ||
body, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
s.logger.Error(err, "reading the request body failed") | ||
|
@@ -243,6 +243,22 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request) | |
continue | ||
} | ||
|
||
webhookUrl, err := url.Parse(webhook) | ||
if err != nil { | ||
s.logger.Error(nil, "Error parsing webhook url", | ||
"reconciler kind", v1beta1.ProviderKind, | ||
"name", providerName.Name, | ||
"namespace", providerName.Namespace) | ||
continue | ||
} | ||
|
||
if !s.supportHttpScheme && webhookUrl.Scheme == "http" { | ||
s.logger.Error(nil, "http scheme is blocked", | ||
"reconciler kind", v1beta1.ProviderKind, | ||
"name", providerName.Name, | ||
"namespace", providerName.Namespace) | ||
continue | ||
} | ||
gunishmatta marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having reviewed #435, I think we can implement this block in a different way such that it becomes more apparent to the user that their Provider and Alert won't work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I propose we delay this PR until v1beta2 is released. If RFC-0004 instructs the objects to marked as stalled, then we'll probably need to add secret watches to NC and cascade stalling from Provider to all dependant Alerts. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gunishmatta unfortunately, we will have to label these changes on hold until the #435 is merged, by which point we will need to review the implementation based on @darkowlzz comments above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No Worries, Will keep following it and would focus on contributing to other issues at Flux and understanding the code in depth. |
||
factory := notifier.NewFactory(webhook, proxy, username, provider.Spec.Channel, token, headers, certPool, password) | ||
sender, err := factory.Notifier(provider.Spec.Type) | ||
if err != nil { | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -20,29 +20,39 @@ import ( | |||
"bytes" | ||||
"encoding/json" | ||||
"fmt" | ||||
"io" | ||||
"net/http" | ||||
"net/http/httptest" | ||||
"testing" | ||||
"time" | ||||
|
||||
"github.com/onsi/gomega" | ||||
. "github.com/onsi/gomega" | ||||
"github.com/sethvargo/go-limiter/httplimit" | ||||
"github.com/sethvargo/go-limiter/memorystore" | ||||
"github.com/sethvargo/go-limiter/noopstore" | ||||
"github.com/slok/go-http-metrics/middleware" | ||||
corev1 "k8s.io/api/core/v1" | ||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||||
"k8s.io/kubectl/pkg/scheme" | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be able to get rid of this new dependency by using
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||||
|
||||
notifyv1 "github.com/fluxcd/notification-controller/api/v1beta1" | ||||
"github.com/fluxcd/pkg/apis/meta" | ||||
"github.com/fluxcd/pkg/runtime/events" | ||||
) | ||||
|
||||
func TestEventKeyFunc(t *testing.T) { | ||||
g := gomega.NewGomegaWithT(t) | ||||
g := NewWithT(t) | ||||
|
||||
// Setup middleware | ||||
store, err := memorystore.New(&memorystore.Config{ | ||||
Interval: 10 * time.Minute, | ||||
}) | ||||
g.Expect(err).ShouldNot(gomega.HaveOccurred()) | ||||
g.Expect(err).ShouldNot(HaveOccurred()) | ||||
middleware, err := httplimit.NewMiddleware(store, eventKeyFunc) | ||||
g.Expect(err).ShouldNot(gomega.HaveOccurred()) | ||||
g.Expect(err).ShouldNot(HaveOccurred()) | ||||
handler := middleware.Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
w.WriteHeader(http.StatusOK) | ||||
})) | ||||
|
@@ -129,19 +139,135 @@ func TestEventKeyFunc(t *testing.T) { | |||
Message: tt.message, | ||||
} | ||||
eventData, err := json.Marshal(event) | ||||
g.Expect(err).ShouldNot(gomega.HaveOccurred()) | ||||
g.Expect(err).ShouldNot(HaveOccurred()) | ||||
|
||||
req := httptest.NewRequest("POST", "/", bytes.NewBuffer(eventData)) | ||||
g.Expect(err).ShouldNot(gomega.HaveOccurred()) | ||||
g.Expect(err).ShouldNot(HaveOccurred()) | ||||
res := httptest.NewRecorder() | ||||
handler.ServeHTTP(res, req) | ||||
|
||||
if tt.rateLimit { | ||||
g.Expect(res.Code).Should(gomega.Equal(429)) | ||||
g.Expect(res.Header().Get("X-Ratelimit-Remaining")).Should(gomega.Equal("0")) | ||||
g.Expect(res.Code).Should(Equal(http.StatusTooManyRequests)) | ||||
g.Expect(res.Header().Get("X-Ratelimit-Remaining")).Should(Equal("0")) | ||||
} else { | ||||
g.Expect(res.Code).Should(gomega.Equal(200)) | ||||
g.Expect(res.Code).Should(Equal(http.StatusOK)) | ||||
} | ||||
}) | ||||
} | ||||
} | ||||
|
||||
func TestBlockInsecureHTTP(t *testing.T) { | ||||
g := NewWithT(t) | ||||
|
||||
var requestsReceived int | ||||
rcvServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
requestsReceived++ | ||||
io.Copy(io.Discard, r.Body) | ||||
w.WriteHeader(http.StatusOK) | ||||
})) | ||||
defer rcvServer.Close() | ||||
|
||||
utilruntime.Must(notifyv1.AddToScheme(scheme.Scheme)) | ||||
|
||||
testNamespace := "test-ns" | ||||
providerKey := "provider" | ||||
client := fake.NewFakeClientWithScheme(scheme.Scheme, | ||||
¬ifyv1.Provider{ | ||||
ObjectMeta: metav1.ObjectMeta{ | ||||
Name: providerKey, | ||||
Namespace: testNamespace, | ||||
}, | ||||
Spec: notifyv1.ProviderSpec{ | ||||
Type: "generic", | ||||
Address: rcvServer.URL, | ||||
}, | ||||
}, | ||||
¬ifyv1.Alert{ | ||||
ObjectMeta: metav1.ObjectMeta{ | ||||
Name: "some-alert-name", | ||||
Namespace: testNamespace, | ||||
}, | ||||
Spec: notifyv1.AlertSpec{ | ||||
ProviderRef: meta.LocalObjectReference{ | ||||
Name: providerKey, | ||||
}, | ||||
EventSeverity: "info", | ||||
EventSources: []notifyv1.CrossNamespaceObjectReference{ | ||||
{ | ||||
Kind: "Bucket", | ||||
Name: "hyacinth", | ||||
Namespace: testNamespace, | ||||
}, | ||||
}, | ||||
}, | ||||
Status: notifyv1.AlertStatus{ | ||||
Conditions: []metav1.Condition{ | ||||
{Type: meta.ReadyCondition, Status: metav1.ConditionTrue}, | ||||
}, | ||||
}, | ||||
}, | ||||
) | ||||
|
||||
eventMdlw := middleware.New(middleware.Config{}) | ||||
|
||||
store, err := noopstore.New() | ||||
g.Expect(err).ToNot(HaveOccurred()) | ||||
|
||||
serverEndpoint := "127.0.0.1:56789" | ||||
eventServer := NewEventServer(serverEndpoint, logf.Log, client, true, true) | ||||
stopCh := make(chan struct{}) | ||||
go eventServer.ListenAndServe(stopCh, eventMdlw, store) | ||||
defer close(stopCh) | ||||
|
||||
event := events.Event{ | ||||
InvolvedObject: corev1.ObjectReference{ | ||||
Kind: "Bucket", | ||||
Name: "hyacinth", | ||||
Namespace: testNamespace, | ||||
}, | ||||
Severity: "info", | ||||
Timestamp: metav1.Now(), | ||||
Message: "well that happened", | ||||
Reason: "event-happened", | ||||
ReportingController: "source-controller", | ||||
} | ||||
|
||||
eventServerTests := []struct { | ||||
name string | ||||
isHttpEnabled bool | ||||
url string | ||||
wantRequest int | ||||
}{ | ||||
{ | ||||
name: "http scheme is disabled", | ||||
isHttpEnabled: false, | ||||
wantRequest: 0, | ||||
}, | ||||
{ | ||||
name: "http scheme is enabled", | ||||
isHttpEnabled: true, | ||||
wantRequest: 1, | ||||
}, | ||||
} | ||||
for _, tt := range eventServerTests { | ||||
t.Run(tt.name, func(t *testing.T) { | ||||
g := NewWithT(t) | ||||
requestsReceived = 0 // reset counter | ||||
|
||||
// Change the internal state instead of creating a new server. | ||||
eventServer.supportHttpScheme = tt.isHttpEnabled | ||||
|
||||
buf := &bytes.Buffer{} | ||||
g.Expect(json.NewEncoder(buf).Encode(&event)).To(Succeed()) | ||||
res, err := http.Post("http://"+serverEndpoint, "application/json", buf) | ||||
|
||||
g.Expect(err).ToNot(HaveOccurred()) | ||||
g.Expect(res.StatusCode).To(Equal(http.StatusAccepted)) | ||||
|
||||
// Requests happens async, so should the assertion. | ||||
g.Eventually(func() bool { | ||||
return requestsReceived == tt.wantRequest | ||||
}, 5*time.Second).Should(BeTrue()) | ||||
}) | ||||
} | ||||
} |
Uh oh!
There was an error while loading. Please reload this page.