Skip to content

Commit 572f91f

Browse files
authored
CHAOS-787: Validate dns disruption hostname regex in webhook (#784)
* chaos-787: validate dns disruption hostname regexs * Add a test * fix test panic * make tested spec valid
1 parent 305357c commit 572f91f

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

api/v1beta1/dns_disruption.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package v1beta1
88
import (
99
"errors"
1010
"fmt"
11+
"regexp"
1112
"strings"
1213

1314
"github.com/hashicorp/go-multierror"
@@ -35,6 +36,10 @@ func (s DNSDisruptionSpec) Validate() (retErr error) {
3536
retErr = multierror.Append(retErr, errors.New("no hostname specified in dns disruption"))
3637
}
3738

39+
if _, err := regexp.Compile(pair.Hostname); err != nil {
40+
retErr = multierror.Append(retErr, fmt.Errorf("hostname \"%s\" not a valid regular expression: %w", pair.Hostname, err))
41+
}
42+
3843
if pair.Record.Type != "A" && pair.Record.Type != "CNAME" {
3944
retErr = multierror.Append(retErr, fmt.Errorf("invalid record type specified in dns disruption, must be A or CNAME but found: %s", pair.Record.Type))
4045
}

api/v1beta1/dns_disruption_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed
2+
// under the Apache License Version 2.0.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
// Copyright 2023 Datadog, Inc.
5+
6+
package v1beta1
7+
8+
import (
9+
. "github.com/onsi/ginkgo/v2"
10+
. "github.com/onsi/gomega"
11+
)
12+
13+
var _ = Describe("DNSDisruptionSpec", func() {
14+
When("'Validate' method is called", func() {
15+
Describe("test regex validation cases", func() {
16+
It("errors only on invalid regex", func() {
17+
disruptionSpec := DNSDisruptionSpec{
18+
{
19+
Hostname: "hostname.tld",
20+
Record: DNSRecord{
21+
Type: "A",
22+
Value: "1.2.3.4",
23+
},
24+
},
25+
}
26+
27+
err := disruptionSpec.Validate()
28+
Expect(err).ToNot(HaveOccurred())
29+
30+
disruptionSpec[0].Hostname = "*.hostname.tld"
31+
32+
err = disruptionSpec.Validate()
33+
Expect(err).To(HaveOccurred())
34+
Expect(err.Error()).Should(ContainSubstring("not a valid regular expression"))
35+
36+
disruptionSpec[0].Hostname = ".*.hostname.tld"
37+
err = disruptionSpec.Validate()
38+
Expect(err).ToNot(HaveOccurred())
39+
})
40+
})
41+
})
42+
})

0 commit comments

Comments
 (0)