2018-12-19 08:22:49 +00:00
|
|
|
package create
|
|
|
|
|
|
|
|
import (
|
2019-01-23 20:48:57 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2018-12-19 08:22:49 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2019-01-23 20:48:57 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2019-01-23 20:48:57 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/command/tls/ca/create"
|
2018-12-19 08:22:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidateCommand_noTabs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
if strings.ContainsRune(New(nil).Help(), '\t') {
|
|
|
|
t.Fatal("help has tabs")
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 20:48:57 +00:00
|
|
|
|
|
|
|
func TestTlsCertCreateCommand_fileCreate(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
previousDirectory, err := os.Getwd()
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
testDir := testutil.TempDir(t, "tls")
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
defer os.Chdir(previousDirectory)
|
|
|
|
|
|
|
|
os.Chdir(testDir)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := New(ui)
|
|
|
|
|
|
|
|
// Setup CA keys
|
|
|
|
createCA(t, "consul")
|
|
|
|
|
|
|
|
caPath := path.Join(testDir, "consul-agent-ca.pem")
|
|
|
|
require.FileExists(caPath)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-server",
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(0, cmd.Run(args))
|
|
|
|
require.Equal("", ui.ErrorWriter.String())
|
|
|
|
|
|
|
|
certPath := path.Join(testDir, "dc1-server-consul-0.pem")
|
|
|
|
keyPath := path.Join(testDir, "dc1-server-consul-0-key.pem")
|
|
|
|
|
|
|
|
require.FileExists(certPath)
|
|
|
|
require.FileExists(keyPath)
|
|
|
|
|
|
|
|
certData, err := ioutil.ReadFile(certPath)
|
|
|
|
require.NoError(err)
|
|
|
|
keyData, err := ioutil.ReadFile(keyPath)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
cert, err := connect.ParseCert(string(certData))
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(cert)
|
|
|
|
|
|
|
|
signer, err := connect.ParseSigner(string(keyData))
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(signer)
|
|
|
|
|
|
|
|
// TODO - maybe we should validate some certs here.
|
|
|
|
}
|
|
|
|
|
|
|
|
func createCA(t *testing.T, domain string) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
caCmd := create.New(ui)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-domain=" + domain,
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, 0, caCmd.Run(args))
|
|
|
|
require.Equal(t, "", ui.ErrorWriter.String())
|
|
|
|
}
|