Skip to content

Commit

Permalink
Fix undesirable regex pattern compliation approach. (#5537)
Browse files Browse the repository at this point in the history
closes #5515

Signed-off-by: Edwin Buck <[email protected]>
  • Loading branch information
edwbuck authored Oct 5, 2024
1 parent 3d48ac3 commit 500e291
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ func buildConfig(coreConfig catalog.CoreConfig, hclText string, status *pluginco

var dnsPatterns []*regexp.Regexp
for _, r := range hclConfig.AllowedDNSPatterns {
re := regexp.MustCompile(r)
re, err := regexp.Compile(r)
if err != nil {
status.ReportErrorf("cannot compile allowed_dns_pattern: %q, %s", r, err)
continue
}
dnsPatterns = append(dnsPatterns, re)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,40 @@ func TestConfigure(t *testing.T) {
coreConf: &configv1.CoreConfiguration{TrustDomain: "example.org"},
hclConf: "tofu = false\nallow_non_root_ports = true",
},
{
name: "allowed_dns_patterns cannot compile, report an error",
expErr: "rpc error: code = InvalidArgument desc = cannot compile allowed_dns_pattern: ",
coreConf: &configv1.CoreConfiguration{TrustDomain: "example.org"},
hclConf: `allowed_dns_patterns = ["*"]`,
},
{
name: "first allowed_dns_patterns cannot compile, report an error",
expErr: "rpc error: code = InvalidArgument desc = cannot compile allowed_dns_pattern: ",
coreConf: &configv1.CoreConfiguration{TrustDomain: "example.org"},
hclConf: `allowed_dns_patterns = [
"*",
"gateway[.]example[.]com"
]`,
},
{
name: "middle allowed_dns_patterns cannot compile, report an error",
expErr: "rpc error: code = InvalidArgument desc = cannot compile allowed_dns_pattern: ",
coreConf: &configv1.CoreConfiguration{TrustDomain: "example.org"},
hclConf: `allowed_dns_patterns = [
"ps1[.]example[.]org",
"*",
"gateway[.]example[.]com"
]`,
},
{
name: "last allowed_dns_patterns cannot compile, report an error",
expErr: "rpc error: code = InvalidArgument desc = cannot compile allowed_dns_pattern: ",
coreConf: &configv1.CoreConfiguration{TrustDomain: "example.org"},
hclConf: `allowed_dns_patterns = [
"gateway[.]example[.]com",
"*"
]`,
},
}

for _, tt := range tests {
Expand All @@ -80,7 +114,8 @@ func TestConfigure(t *testing.T) {
CoreConfiguration: tt.coreConf,
})
if tt.expErr != "" {
require.Contains(t, err.Error(), tt.expErr)
require.Error(t, err, "no error raised when error is expected")
require.ErrorContains(t, err, tt.expErr)
require.Nil(t, resp)
return
}
Expand Down

0 comments on commit 500e291

Please sign in to comment.