File tree 3 files changed +41
-1
lines changed
3 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import (
12
12
"fmt"
13
13
"io"
14
14
"net"
15
+ "net/url"
15
16
"os"
16
17
"path/filepath"
17
18
"reflect"
@@ -2937,6 +2938,13 @@ func (sc *ServiceCheck) validate() error {
2937
2938
if sc .Path == "" {
2938
2939
return fmt .Errorf ("http type must have a valid http path" )
2939
2940
}
2941
+ url , err := url .Parse (sc .Path )
2942
+ if err != nil {
2943
+ return fmt .Errorf ("http type must have a valid http path" )
2944
+ }
2945
+ if url .IsAbs () {
2946
+ return fmt .Errorf ("http type must have a relative http path" )
2947
+ }
2940
2948
2941
2949
case ServiceCheckScript :
2942
2950
if sc .Command == "" {
Original file line number Diff line number Diff line change @@ -1230,6 +1230,37 @@ func TestTask_Validate_Service_Check(t *testing.T) {
1230
1230
if err != nil {
1231
1231
t .Fatalf ("err: %v" , err )
1232
1232
}
1233
+
1234
+ check2 := ServiceCheck {
1235
+ Name : "check-name-2" ,
1236
+ Type : ServiceCheckHTTP ,
1237
+ Interval : 10 * time .Second ,
1238
+ Timeout : 2 * time .Second ,
1239
+ Path : "/foo/bar" ,
1240
+ }
1241
+
1242
+ err = check2 .validate ()
1243
+ if err != nil {
1244
+ t .Fatalf ("err: %v" , err )
1245
+ }
1246
+
1247
+ check2 .Path = ""
1248
+ err = check2 .validate ()
1249
+ if err == nil {
1250
+ t .Fatal ("Expected an error" )
1251
+ }
1252
+ if ! strings .Contains (err .Error (), "valid http path" ) {
1253
+ t .Fatalf ("err: %v" , err )
1254
+ }
1255
+
1256
+ check2 .Path = "http://www.example.com"
1257
+ err = check2 .validate ()
1258
+ if err == nil {
1259
+ t .Fatal ("Expected an error" )
1260
+ }
1261
+ if ! strings .Contains (err .Error (), "relative http path" ) {
1262
+ t .Fatalf ("err: %v" , err )
1263
+ }
1233
1264
}
1234
1265
1235
1266
// TestTask_Validate_Service_Check_AddressMode asserts that checks do not
Original file line number Diff line number Diff line change @@ -412,7 +412,8 @@ The `Task` object supports the following keys:
412
412
- `Path`: The path of the HTTP endpoint which Consul will query to query
413
413
the health of a service if the type of the check is `http`. Nomad
414
414
will add the IP of the service and the port, users are only required
415
- to add the relative URL of the health check endpoint.
415
+ to add the relative URL of the health check endpoint. Absolute paths
416
+ are not allowed.
416
417
417
418
- `Protocol`: This indicates the protocol for the HTTP checks. Valid
418
419
options are `http` and `https`. We default it to `http`.
You can’t perform that action at this time.
0 commit comments