2018-05-16 05:10:43 +00:00
|
|
|
package check
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent"
|
|
|
|
"github.com/hashicorp/consul/api"
|
2020-10-06 18:24:05 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2018-05-16 05:10:43 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestIntentionCheck_noTabs(t *testing.T) {
|
2018-05-16 05:10:43 +00:00
|
|
|
t.Parallel()
|
|
|
|
if strings.ContainsRune(New(nil).Help(), '\t') {
|
|
|
|
t.Fatal("help has tabs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestIntentionCheck_Validation(t *testing.T) {
|
2018-05-16 05:10:43 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
args []string
|
|
|
|
output string
|
|
|
|
}{
|
|
|
|
"0 args": {
|
|
|
|
[]string{},
|
|
|
|
"requires exactly two",
|
|
|
|
},
|
|
|
|
|
|
|
|
"1 args": {
|
|
|
|
[]string{"a"},
|
|
|
|
"requires exactly two",
|
|
|
|
},
|
|
|
|
|
|
|
|
"3 args": {
|
|
|
|
[]string{"a", "b", "c"},
|
|
|
|
"requires exactly two",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
c.init()
|
|
|
|
|
|
|
|
// Ensure our buffer is always clear
|
|
|
|
if ui.ErrorWriter != nil {
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
}
|
|
|
|
if ui.OutputWriter != nil {
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(2, c.Run(tc.args))
|
|
|
|
output := ui.ErrorWriter.String()
|
|
|
|
require.Contains(output, tc.output)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestIntentionCheck(t *testing.T) {
|
2018-05-16 05:10:43 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, ``)
|
2018-05-16 05:10:43 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
client := a.Client()
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
2018-05-16 05:10:43 +00:00
|
|
|
// Create the intention
|
|
|
|
{
|
2020-10-06 18:24:05 +00:00
|
|
|
//nolint:staticcheck
|
2018-05-16 05:10:43 +00:00
|
|
|
_, _, err := client.Connect().IntentionCreate(&api.Intention{
|
|
|
|
SourceName: "web",
|
|
|
|
DestinationName: "db",
|
|
|
|
Action: api.IntentionActionDeny,
|
|
|
|
}, nil)
|
|
|
|
require.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get it
|
|
|
|
{
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"foo", "db",
|
|
|
|
}
|
|
|
|
require.Equal(0, c.Run(args), ui.ErrorWriter.String())
|
|
|
|
require.Contains(ui.OutputWriter.String(), "Allow")
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"web", "db",
|
|
|
|
}
|
|
|
|
require.Equal(1, c.Run(args), ui.ErrorWriter.String())
|
|
|
|
require.Contains(ui.OutputWriter.String(), "Denied")
|
|
|
|
}
|
|
|
|
}
|