2018-10-19 16:04:07 +00:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent"
|
|
|
|
"github.com/hashicorp/consul/logger"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2019-04-15 18:34:36 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2018-10-19 16:04:07 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2019-04-15 18:34:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-19 16:04:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRulesTranslateCommand_noTabs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
|
|
|
|
t.Fatal("help has tabs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRulesTranslateCommand(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testDir := testutil.TempDir(t, "acl")
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
|
2019-02-14 15:59:14 +00:00
|
|
|
a := agent.NewTestAgent(t, t.Name(), `
|
2018-10-19 16:04:07 +00:00
|
|
|
primary_datacenter = "dc1"
|
|
|
|
acl {
|
|
|
|
enabled = true
|
|
|
|
tokens {
|
|
|
|
master = "root"
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
a.Agent.LogWriter = logger.NewLogWriter(512)
|
|
|
|
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
stdinR, stdinW := io.Pipe()
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := New(ui)
|
|
|
|
cmd.testStdin = stdinR
|
|
|
|
|
|
|
|
rules := "service \"\" { policy = \"write\" }"
|
|
|
|
expected := "service_prefix \"\" {\n policy = \"write\"\n}"
|
|
|
|
|
|
|
|
// From a file
|
2019-04-15 18:34:36 +00:00
|
|
|
t.Run("file", func(t *testing.T) {
|
2018-10-19 16:04:07 +00:00
|
|
|
err := ioutil.WriteFile(testDir+"/rules.hcl", []byte(rules), 0644)
|
2019-04-15 18:34:36 +00:00
|
|
|
require.NoError(t, err)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-token=root",
|
|
|
|
"@" + testDir + "/rules.hcl",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
2019-04-15 18:34:36 +00:00
|
|
|
require.Equal(t, code, 0)
|
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
|
|
|
require.Contains(t, ui.OutputWriter.String(), expected)
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
// From stdin
|
2019-04-15 18:34:36 +00:00
|
|
|
t.Run("stdin", func(t *testing.T) {
|
2018-10-19 16:04:07 +00:00
|
|
|
go func() {
|
|
|
|
stdinW.Write([]byte(rules))
|
|
|
|
stdinW.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-token=root",
|
|
|
|
"-",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
2019-04-15 18:34:36 +00:00
|
|
|
require.Equal(t, code, 0)
|
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
|
|
|
require.Contains(t, ui.OutputWriter.String(), expected)
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
// From arg
|
2019-04-15 18:34:36 +00:00
|
|
|
t.Run("arg", func(t *testing.T) {
|
2018-10-19 16:04:07 +00:00
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-token=root",
|
|
|
|
rules,
|
|
|
|
}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
2019-04-15 18:34:36 +00:00
|
|
|
require.Equal(t, code, 0)
|
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
|
|
|
require.Contains(t, ui.OutputWriter.String(), expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
// cannot specify both secret and accessor
|
|
|
|
t.Run("exclusive-options", func(t *testing.T) {
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-token=root",
|
|
|
|
"-token-secret",
|
|
|
|
"-token-accessor",
|
|
|
|
`token "" { policy = "write" }`,
|
|
|
|
}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
|
|
|
require.Equal(t, 1, code, 0)
|
|
|
|
require.Equal(t, "Error - cannot specify both -token-secret and -token-accessor\n", ui.ErrorWriter.String())
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|