Skip to content

Commit e3a8e3c

Browse files
committed
http scheme updates
Signed-off-by: Gunish Matta <33680363+gunishmatta@users.noreply.github.com>
1 parent 7d49160 commit e3a8e3c

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

controllers/event_handling_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net/http"
99
"net/http/httptest"
10+
"net/url"
1011
"testing"
1112
"time"
1213

@@ -52,7 +53,69 @@ func TestEventHandler(t *testing.T) {
5253
t.Fatalf("failed to create memory storage")
5354
}
5455

56+
httpScheme := "http"
57+
58+
eventServerTests := []struct {
59+
name string
60+
isHttpEnabled bool
61+
url string
62+
}{
63+
{
64+
name: "http scheme is enabled",
65+
isHttpEnabled: true,
66+
}, {
67+
name: "http scheme is disabled",
68+
isHttpEnabled: false,
69+
},
70+
}
71+
for _, eventServerTest := range eventServerTests {
72+
t.Run(eventServerTest.name, func(t *testing.T) {
73+
74+
eventServer := server.NewEventServer("127.0.0.1:56789", logf.Log, k8sClient, true, eventServerTest.isHttpEnabled)
75+
76+
stopCh := make(chan struct{})
77+
go eventServer.ListenAndServe(stopCh, eventMdlw, store)
78+
requestsReceived := 0
79+
rcvServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
80+
requestsReceived = requestsReceived + 1
81+
req = r
82+
w.WriteHeader(200)
83+
}))
84+
defer rcvServer.Close()
85+
defer close(stopCh)
86+
87+
providerKey := types.NamespacedName{
88+
Name: fmt.Sprintf("provider-%s", randStringRunes(5)),
89+
Namespace: namespace,
90+
}
91+
provider = &notifyv1.Provider{
92+
ObjectMeta: metav1.ObjectMeta{
93+
Name: providerKey.Name,
94+
Namespace: providerKey.Namespace,
95+
},
96+
Spec: notifyv1.ProviderSpec{
97+
Type: "generic",
98+
Address: rcvServer.URL,
99+
},
100+
}
101+
102+
webhook_url, err := url.Parse(provider.Spec.Address)
103+
if err != nil {
104+
105+
if eventServerTest.isHttpEnabled {
106+
g.Expect(webhook_url.Scheme).To(Equal(httpScheme))
107+
g.Expect(requestsReceived).To(Equal(1))
108+
} else {
109+
g.Expect(webhook_url.Scheme).ToNot(Equal(httpScheme))
110+
g.Expect(requestsReceived).To(Equal(0))
111+
}
112+
}
113+
114+
})
115+
}
116+
55117
eventServer := server.NewEventServer("127.0.0.1:56789", logf.Log, k8sClient, true, true)
118+
56119
stopCh := make(chan struct{})
57120
go eventServer.ListenAndServe(stopCh, eventMdlw, store)
58121

@@ -78,6 +141,8 @@ func TestEventHandler(t *testing.T) {
78141
},
79142
}
80143

144+
g.Expect(err).ToNot(HaveOccurred())
145+
81146
g.Expect(k8sClient.Create(context.Background(), provider)).To(Succeed())
82147
g.Eventually(func() bool {
83148
var obj notifyv1.Provider
@@ -174,6 +239,7 @@ func TestEventHandler(t *testing.T) {
174239
res, err := http.Post("http://localhost:56789/", "application/json", buf)
175240
g.Expect(err).ToNot(HaveOccurred())
176241
g.Expect(res.StatusCode).To(Equal(202)) // event_server responds with 202 Accepted
242+
177243
}
178244

179245
testForwarded := func() {
@@ -295,4 +361,5 @@ func TestEventHandler(t *testing.T) {
295361
req = nil
296362
})
297363
}
364+
298365
}

main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func main() {
7272
leaderElectionOptions leaderelection.Options
7373
aclOptions acl.Options
7474
rateLimiterOptions helper.RateLimiterOptions
75-
insecureNoTLS bool
75+
insecureAllowHTTP bool
7676
)
7777

7878
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
@@ -83,7 +83,7 @@ func main() {
8383
flag.BoolVar(&watchAllNamespaces, "watch-all-namespaces", true,
8484
"Watch for custom resources in all namespaces, if set to false it will only watch the runtime namespace.")
8585
flag.DurationVar(&rateLimitInterval, "rate-limit-interval", 5*time.Minute, "Interval in which rate limit has effect.")
86-
flag.BoolVar(&insecureNoTLS, "insecure-no-tls", false, "Enable the use of HTTP Scheme (no TLS) across all controller level objects. This is not recommended for production environments")
86+
flag.BoolVar(&insecureAllowHTTP, "insecure-allow-http", true, "Enable the use of HTTP Scheme (no HTTPS) across all controller level objects. This is not recommended for production environments")
8787
clientOptions.BindFlags(flag.CommandLine)
8888
logOptions.BindFlags(flag.CommandLine)
8989
leaderElectionOptions.BindFlags(flag.CommandLine)
@@ -171,7 +171,7 @@ func main() {
171171
Registry: crtlmetrics.Registry,
172172
}),
173173
})
174-
eventServer := server.NewEventServer(eventsAddr, log, mgr.GetClient(), aclOptions.NoCrossNamespaceRefs, insecureNoTLS)
174+
eventServer := server.NewEventServer(eventsAddr, log, mgr.GetClient(), aclOptions.NoCrossNamespaceRefs, insecureAllowHTTP)
175175
go eventServer.ListenAndServe(ctx.Done(), eventMdlw, store)
176176

177177
setupLog.Info("starting webhook receiver server", "addr", receiverAddr)

0 commit comments

Comments
 (0)