Skip to content

Commit 71f2414

Browse files
authored
Fix: only one of trust_bundle_path, trust_bundle_url, or insecure_bootstrap can be set (spiffe#4532)
@mnp reported in issue 4530 that it was possible to set trust_bundle_url and insecure_bootstrap in the Agent configuration. There was a test for this case. However, the test was just checking if there was an error. There was an error but not the expected one. This commit also adds expectErrorContains to the test case struct so tests can check the expected error message. Also, more tests added. Signed-off-by: Matteus Silva <[email protected]>
1 parent 8d6036a commit 71f2414

File tree

2 files changed

+84
-10
lines changed

2 files changed

+84
-10
lines changed

Diff for: cmd/spire-agent/cli/run/run.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,21 @@ func (c *agentConfig) validate() error {
233233
return errors.New("trust_domain must be configured")
234234
}
235235

236+
// If insecure_bootstrap is set, trust_bundle_path or trust_bundle_url cannot be set
236237
// If trust_bundle_url is set, download the trust bundle using HTTP and parse it from memory
237238
// If trust_bundle_path is set, parse the trust bundle file on disk
238239
// Both cannot be set
239240
// The trust bundle URL must start with HTTPS
240-
if c.TrustBundlePath == "" && c.TrustBundleURL == "" && !c.InsecureBootstrap {
241+
if c.InsecureBootstrap {
242+
switch {
243+
case c.TrustBundleURL != "" && c.TrustBundlePath != "":
244+
return errors.New("only one of insecure_bootstrap, trust_bundle_url, or trust_bundle_path can be specified, not the three options")
245+
case c.TrustBundleURL != "":
246+
return errors.New("only one of insecure_bootstrap or trust_bundle_url can be specified, not both")
247+
case c.TrustBundlePath != "":
248+
return errors.New("only one of insecure_bootstrap or trust_bundle_path can be specified, not both")
249+
}
250+
} else if c.TrustBundlePath == "" && c.TrustBundleURL == "" {
241251
return errors.New("trust_bundle_path or trust_bundle_url must be configured unless insecure_bootstrap is set")
242252
}
243253

Diff for: cmd/spire-agent/cli/run/run_test.go

+73-9
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ type mergeInputCase struct {
3030
}
3131

3232
type newAgentConfigCase struct {
33-
msg string
34-
expectError bool
35-
input func(*Config)
36-
logOptions func(t *testing.T) []log.Option
37-
test func(*testing.T, *agent.Config)
33+
msg string
34+
expectError bool
35+
requireErrorPrefix string
36+
input func(*Config)
37+
logOptions func(t *testing.T) []log.Option
38+
test func(*testing.T, *agent.Config)
3839
}
3940

4041
func TestDownloadTrustBundle(t *testing.T) {
@@ -677,6 +678,9 @@ func TestNewAgentConfig(t *testing.T) {
677678
{
678679
msg: "insecure_bootstrap should be correctly set to true",
679680
input: func(c *Config) {
681+
// in this case, remove trust_bundle_path provided by defaultValidConfig()
682+
// because trust_bundle_path and insecure_bootstrap cannot be set at the same time
683+
c.Agent.TrustBundlePath = ""
680684
c.Agent.InsecureBootstrap = true
681685
},
682686
test: func(t *testing.T, c *agent.Config) {
@@ -730,8 +734,9 @@ func TestNewAgentConfig(t *testing.T) {
730734
},
731735
},
732736
{
733-
msg: "trust_bundle_path and trust_bundle_url cannot both be set",
734-
expectError: true,
737+
msg: "trust_bundle_path and trust_bundle_url cannot both be set",
738+
expectError: true,
739+
requireErrorPrefix: "only one of trust_bundle_url or trust_bundle_path can be specified, not both",
735740
input: func(c *Config) {
736741
c.Agent.TrustBundlePath = "foo"
737742
c.Agent.TrustBundleURL = "foo2"
@@ -741,10 +746,66 @@ func TestNewAgentConfig(t *testing.T) {
741746
},
742747
},
743748
{
744-
msg: "insecure_bootstrap and trust_bundle_url cannot both be set",
745-
expectError: true,
749+
msg: "insecure_bootstrap and trust_bundle_path cannot both be set",
750+
expectError: true,
751+
requireErrorPrefix: "only one of insecure_bootstrap or trust_bundle_path can be specified, not both",
746752
input: func(c *Config) {
753+
c.Agent.TrustBundlePath = "foo"
754+
c.Agent.InsecureBootstrap = true
755+
},
756+
test: func(t *testing.T, c *agent.Config) {
757+
require.Nil(t, c)
758+
},
759+
},
760+
{
761+
msg: "insecure_bootstrap and trust_bundle_url cannot both be set",
762+
expectError: true,
763+
requireErrorPrefix: "only one of insecure_bootstrap or trust_bundle_url can be specified, not both",
764+
input: func(c *Config) {
765+
// in this case, remove trust_bundle_path provided by defaultValidConfig()
766+
c.Agent.TrustBundlePath = ""
747767
c.Agent.TrustBundleURL = "foo"
768+
c.Agent.InsecureBootstrap = true
769+
},
770+
test: func(t *testing.T, c *agent.Config) {
771+
require.Nil(t, c)
772+
},
773+
},
774+
{
775+
msg: "insecure_bootstrap, trust_bundle_url, trust_bundle_path cannot be set at the same time",
776+
expectError: true,
777+
requireErrorPrefix: "only one of insecure_bootstrap, trust_bundle_url, or trust_bundle_path can be specified, not the three options",
778+
input: func(c *Config) {
779+
c.Agent.TrustBundlePath = "bar"
780+
c.Agent.TrustBundleURL = "foo"
781+
c.Agent.InsecureBootstrap = true
782+
},
783+
test: func(t *testing.T, c *agent.Config) {
784+
require.Nil(t, c)
785+
},
786+
},
787+
{
788+
msg: "trust_bundle_path or trust_bundle_url must be configured unless insecure_bootstrap is set",
789+
expectError: true,
790+
requireErrorPrefix: "trust_bundle_path or trust_bundle_url must be configured unless insecure_bootstrap is set",
791+
input: func(c *Config) {
792+
// in this case, remove trust_bundle_path provided by defaultValidConfig()
793+
c.Agent.TrustBundlePath = ""
794+
c.Agent.TrustBundleURL = ""
795+
c.Agent.InsecureBootstrap = false
796+
},
797+
test: func(t *testing.T, c *agent.Config) {
798+
require.Nil(t, c)
799+
},
800+
},
801+
{
802+
msg: "trust_bundle_url must start with https://",
803+
expectError: true,
804+
requireErrorPrefix: "trust bundle URL must start with https://",
805+
input: func(c *Config) {
806+
// remove trust_bundle_path provided by defaultValidConfig()
807+
c.Agent.TrustBundlePath = ""
808+
c.Agent.TrustBundleURL = "foo.bar"
748809
c.Agent.InsecureBootstrap = false
749810
},
750811
test: func(t *testing.T, c *agent.Config) {
@@ -931,6 +992,9 @@ func TestNewAgentConfig(t *testing.T) {
931992
ac, err := NewAgentConfig(input, logOpts, false)
932993
if testCase.expectError {
933994
require.Error(t, err)
995+
if testCase.requireErrorPrefix != "" {
996+
spiretest.RequireErrorPrefix(t, err, testCase.requireErrorPrefix)
997+
}
934998
} else {
935999
require.NoError(t, err)
9361000
}

0 commit comments

Comments
 (0)