Skip to content

Commit f209766

Browse files
committed
Merge branch 'master' into releases/1.6.x
2 parents d1be57b + d64f196 commit f209766

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

pkg/apiclient/allowlists_service.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"net/url"
78

89
qs "github.com/google/go-querystring/query"
910
log "github.com/sirupsen/logrus"
@@ -74,7 +75,8 @@ func (s *AllowlistsService) Get(ctx context.Context, name string, opts Allowlist
7475
}
7576

7677
func (s *AllowlistsService) CheckIfAllowlisted(ctx context.Context, value string) (bool, *Response, error) {
77-
u := s.client.URLPrefix + "/allowlists/check/" + value
78+
escapedValue := url.PathEscape(value)
79+
u := s.client.URLPrefix + "/allowlists/check/" + escapedValue
7880

7981
req, err := s.client.PrepareRequest(ctx, http.MethodHead, u, nil)
8082
if err != nil {
@@ -92,7 +94,8 @@ func (s *AllowlistsService) CheckIfAllowlisted(ctx context.Context, value string
9294
}
9395

9496
func (s *AllowlistsService) CheckIfAllowlistedWithReason(ctx context.Context, value string) (*models.CheckAllowlistResponse, *Response, error) {
95-
u := s.client.URLPrefix + "/allowlists/check/" + value
97+
escapedValue := url.PathEscape(value)
98+
u := s.client.URLPrefix + "/allowlists/check/" + escapedValue
9699

97100
req, err := s.client.PrepareRequest(ctx, http.MethodGet, u, nil)
98101
if err != nil {

pkg/apiserver/allowlists_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,35 @@ func TestCheckInAllowlist(t *testing.T) {
115115
require.NoError(t, err)
116116
require.False(t, resp.Allowlisted)
117117

118+
// GET request, should return 200 and status in body
119+
w = lapi.RecordResponse(t, ctx, http.MethodGet, "/v1/allowlists/check/2.3.4.0%2F24", emptyBody, passwordAuthType)
120+
121+
require.Equal(t, http.StatusOK, w.Code)
122+
123+
resp = models.CheckAllowlistResponse{}
124+
125+
err = json.Unmarshal(w.Body.Bytes(), &resp)
126+
127+
require.NoError(t, err)
128+
require.False(t, resp.Allowlisted)
129+
118130
// HEAD request, should return 200
119131
w = lapi.RecordResponse(t, ctx, http.MethodHead, "/v1/allowlists/check/1.2.3.4", emptyBody, passwordAuthType)
120132

121133
require.Equal(t, http.StatusOK, w.Code)
122134

135+
// HEAD request, should return 200
136+
w = lapi.RecordResponse(t, ctx, http.MethodHead, "/v1/allowlists/check/1.2.3.0%2F24", emptyBody, passwordAuthType)
137+
138+
require.Equal(t, http.StatusOK, w.Code)
139+
123140
// HEAD request, should return 204
124141
w = lapi.RecordResponse(t, ctx, http.MethodHead, "/v1/allowlists/check/2.3.4.5", emptyBody, passwordAuthType)
125142

126143
require.Equal(t, http.StatusNoContent, w.Code)
144+
145+
// HEAD request, should return 204
146+
w = lapi.RecordResponse(t, ctx, http.MethodHead, "/v1/allowlists/check/2.3.4.5%2F24", emptyBody, passwordAuthType)
147+
148+
require.Equal(t, http.StatusNoContent, w.Code)
127149
}

pkg/apiserver/controllers/controller.go

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ func (c *Controller) NewV1() error {
9898
c.Router.GET("/health", gin.WrapF(serveHealth()))
9999
c.Router.Use(v1.PrometheusMiddleware())
100100
c.Router.HandleMethodNotAllowed = true
101+
c.Router.UnescapePathValues = true
102+
c.Router.UseRawPath = true
101103
c.Router.NoRoute(func(ctx *gin.Context) {
102104
ctx.AbortWithStatus(http.StatusNotFound)
103105
})

test/bats/cscli-allowlists.bats

+11
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ teardown() {
135135
refute_stderr
136136
}
137137

138+
@test "cscli allolists: range check" {
139+
rune -0 cscli allowlist create foo -d 'a foo'
140+
rune -0 cscli allowlist add foo 192.168.0.0/16
141+
rune -1 cscli decisions add -r 192.168.10.20/24
142+
assert_stderr 'Error: 192.168.10.20/24 is allowlisted by item 192.168.0.0/16 from foo, use --bypass-allowlist to add the decision anyway'
143+
refute_output
144+
rune -0 cscli decisions add -r 192.168.10.20/24 --bypass-allowlist
145+
assert_stderr --partial 'Decision successfully added'
146+
refute_output
147+
}
148+
138149
@test "cscli allowlists delete" {
139150
rune -1 cscli allowlist delete
140151
assert_stderr 'Error: accepts 1 arg(s), received 0'

0 commit comments

Comments
 (0)