2018-05-16 15:41:12 +00:00
|
|
|
package delete
|
|
|
|
|
|
|
|
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 15:41:12 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestIntentionDelete_noTabs(t *testing.T) {
|
2018-05-16 15:41:12 +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 TestIntentionDelete_Validation(t *testing.T) {
|
2018-05-16 15:41:12 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
args []string
|
|
|
|
output string
|
|
|
|
}{
|
|
|
|
"0 args": {
|
|
|
|
[]string{},
|
|
|
|
"requires exactly 1 or 2",
|
|
|
|
},
|
|
|
|
|
|
|
|
"3 args": {
|
|
|
|
[]string{"a", "b", "c"},
|
|
|
|
"requires exactly 1 or 2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
c.init()
|
|
|
|
|
|
|
|
// Ensure our buffer is always clear
|
|
|
|
if ui.ErrorWriter != nil {
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
}
|
|
|
|
if ui.OutputWriter != nil {
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Equal(t, 1, c.Run(tc.args))
|
2018-05-16 15:41:12 +00:00
|
|
|
output := ui.ErrorWriter.String()
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Contains(t, output, tc.output)
|
2018-05-16 15:41:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestIntentionDelete(t *testing.T) {
|
2018-05-16 15:41:12 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, ``)
|
2018-05-16 15:41:12 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
client := a.Client()
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create some intentions.
|
|
|
|
|
|
|
|
//nolint:staticcheck
|
|
|
|
id0, _, err := client.Connect().IntentionCreate(&api.Intention{
|
|
|
|
SourceName: "web",
|
|
|
|
DestinationName: "db",
|
|
|
|
Action: api.IntentionActionDeny,
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
//nolint:staticcheck
|
|
|
|
_, _, err = client.Connect().IntentionCreate(&api.Intention{
|
|
|
|
SourceName: "web",
|
|
|
|
DestinationName: "queue",
|
|
|
|
Action: api.IntentionActionDeny,
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-06 22:09:13 +00:00
|
|
|
// Ensure "api" is L7
|
|
|
|
_, _, err = client.ConfigEntries().Set(&api.ServiceConfigEntry{
|
|
|
|
Kind: api.ServiceDefaults,
|
|
|
|
Name: "api",
|
|
|
|
Protocol: "http",
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = client.Connect().IntentionUpsert(&api.Intention{
|
|
|
|
SourceName: "web",
|
|
|
|
DestinationName: "api",
|
|
|
|
Permissions: []*api.IntentionPermission{
|
|
|
|
{
|
|
|
|
Action: api.IntentionActionAllow,
|
|
|
|
HTTP: &api.IntentionHTTPPermission{
|
|
|
|
PathExact: "/foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
t.Run("l4 intention", func(t *testing.T) {
|
|
|
|
t.Run("one arg", func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
id0,
|
|
|
|
}
|
|
|
|
require.Equal(t, 0, c.Run(args), ui.ErrorWriter.String())
|
|
|
|
require.Contains(t, ui.OutputWriter.String(), "deleted")
|
|
|
|
})
|
|
|
|
t.Run("two args", func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
2018-05-16 15:41:12 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"web", "queue",
|
|
|
|
}
|
|
|
|
require.Equal(t, 0, c.Run(args), ui.ErrorWriter.String())
|
|
|
|
require.Contains(t, ui.OutputWriter.String(), "deleted")
|
|
|
|
})
|
|
|
|
})
|
2018-05-16 15:41:12 +00:00
|
|
|
|
2020-10-06 22:09:13 +00:00
|
|
|
t.Run("l7 intention", func(t *testing.T) {
|
|
|
|
t.Run("two args", func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"web", "api",
|
|
|
|
}
|
|
|
|
require.Equal(t, 0, c.Run(args), ui.ErrorWriter.String())
|
|
|
|
require.Contains(t, ui.OutputWriter.String(), "deleted")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// They should all be gone.
|
2018-05-16 15:41:12 +00:00
|
|
|
{
|
|
|
|
ixns, _, err := client.Connect().Intentions(nil)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, ixns, 0)
|
2018-05-16 15:41:12 +00:00
|
|
|
}
|
|
|
|
}
|