| 1 |
|
| 2 |
|
| 3 |
package integration |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/ThomasRooney/gexpect" |
| 11 |
"github.com/smallstep/assert" |
| 12 |
) |
| 13 |
|
| 14 |
type KeypairCmd struct { |
| 15 |
name string |
| 16 |
command CLICommand |
| 17 |
pubfile string |
| 18 |
prvfile string |
| 19 |
password string |
| 20 |
} |
| 21 |
|
| 22 |
func (k KeypairCmd) setFlag(key, value string) KeypairCmd { |
| 23 |
return KeypairCmd{k.name, k.command.setFlag(key, value), k.pubfile, k.prvfile, k.password} |
| 24 |
} |
| 25 |
|
| 26 |
func (k KeypairCmd) setPassword(password string) KeypairCmd { |
| 27 |
return KeypairCmd{k.name, k.command, k.pubfile, k.prvfile, password} |
| 28 |
} |
| 29 |
|
| 30 |
func (k KeypairCmd) testJwtSignVerify(t *testing.T) { |
| 31 |
aud := FakePrincipal() |
| 32 |
iss := FakePrincipal() |
| 33 |
sub := FakePrincipal() |
| 34 |
jwk := JWK{k.pubfile, k.prvfile, k.password, true, false} |
| 35 |
test := NewJWTTest(jwk).setFlag("aud", aud).setFlag("iss", iss).setSFlag("sub", sub).exp(1 * time.Minute) |
| 36 |
if k.command.flags["kty"] == "RSA" { |
| 37 |
test = test.setFlag("alg", "RS256") |
| 38 |
} |
| 39 |
test.test(t, fmt.Sprintf("%s-jwt-sign-verify", k.name)) |
| 40 |
} |
| 41 |
|
| 42 |
func (k KeypairCmd) test(t *testing.T) { |
| 43 |
t.Run(k.name, func(t *testing.T) { |
| 44 |
cmd, err := gexpect.Spawn(k.command.cmd()) |
| 45 |
assert.FatalError(t, err) |
| 46 |
prompt := fmt.Sprintf("Please enter the password to encrypt the private key: ") |
| 47 |
assert.FatalError(t, cmd.ExpectTimeout(prompt, 15*time.Second)) |
| 48 |
assert.FatalError(t, cmd.SendLine(k.password)) |
| 49 |
k.testJwtSignVerify(t) |
| 50 |
}) |
| 51 |
} |
| 52 |
|
| 53 |
func (k KeypairCmd) testNoPass(t *testing.T) { |
| 54 |
k.command.test(t, k.name, "Your public key has been saved in testdata-tmp/no-pass.pub.\nYour private key has been saved in testdata-tmp/no-pass.pem.\n") |
| 55 |
k.testJwtSignVerify(t) |
| 56 |
} |
| 57 |
|
| 58 |
func (k KeypairCmd) fail(t *testing.T, expected string) { |
| 59 |
k.command.fail(t, k.name, expected) |
| 60 |
} |
| 61 |
|
| 62 |
func (k KeypairCmd) failNoPass(t *testing.T, expected string) { |
| 63 |
k.command.fail(t, k.name, expected) |
| 64 |
} |
| 65 |
|
| 66 |
func NewKeypairCmd(name string) KeypairCmd { |
| 67 |
pubfile := fmt.Sprintf("%s/%s.pub", TempDirectory, name) |
| 68 |
prvfile := fmt.Sprintf("%s/%s.pem", TempDirectory, name) |
| 69 |
command := NewCLICommand().setCommand(fmt.Sprintf("step crypto keypair %s %s", pubfile, prvfile)) |
| 70 |
return KeypairCmd{name, command, pubfile, prvfile, "password"} |
| 71 |
} |
| 72 |
|
| 73 |
func TestCryptoKeypair(t *testing.T) { |
| 74 |
NewCLICommand().setCommand("step crypto keypair").fail(t, "no-args", "not enough positional arguments were provided in 'step crypto keypair <pub_file> <priv_file>'\n", "") |
| 75 |
NewCLICommand().setCommand("step crypto keypair foo").fail(t, "no-args", "not enough positional arguments were provided in 'step crypto keypair <pub_file> <priv_file>'\n", "") |
| 76 |
NewKeypairCmd("default").test(t) |
| 77 |
t.Run("RSA", func(t *testing.T) { |
| 78 |
NewKeypairCmd("RSA-default").setFlag("kty", "RSA").test(t) |
| 79 |
NewKeypairCmd("RSA-size-0-fail").setFlag("kty", "RSA").setFlag("size", "0").fail(t, "flag '--size' requires at least 2048 unless '--insecure' flag is provided\n") |
| 80 |
NewKeypairCmd("RSA-size-16-fail").setFlag("kty", "RSA").setFlag("size", "16").fail(t, "flag '--size' requires at least 2048 unless '--insecure' flag is provided\n") |
| 81 |
NewKeypairCmd("RSA-size-neg1-fail").setFlag("kty", "RSA").setFlag("size", "-1").setFlag("insecure", "").fail(t, "flag '--size' must be greater or equal than 0\n") |
| 82 |
|
| 83 |
|
| 84 |
NewKeypairCmd("RSA-size-1024-fail").setFlag("kty", "RSA").setFlag("size", "1024").fail(t, "flag '--size' requires at least 2048 unless '--insecure' flag is provided\n") |
| 85 |
NewKeypairCmd("RSA-size-1024").setFlag("kty", "RSA").setFlag("size", "1024").setFlag("insecure", "").test(t) |
| 86 |
NewKeypairCmd("RSA-size-3072").setFlag("kty", "RSA").setFlag("size", "3072").test(t) |
| 87 |
NewKeypairCmd("RSA-size-4096").setFlag("kty", "RSA").setFlag("size", "4096").test(t) |
| 88 |
NewKeypairCmd("RSA-curve").setFlag("kty", "RSA").setFlag("size", "2048").setFlag("crv", "P-256").fail(t, "flag '--curve' is incompatible with flag '--kty RSA'\n") |
| 89 |
}) |
| 90 |
t.Run("EC", func(t *testing.T) { |
| 91 |
NewKeypairCmd("EC-default").setFlag("kty", "EC").test(t) |
| 92 |
NewKeypairCmd("P-256").setFlag("kty", "EC").setFlag("crv", "P-256").test(t) |
| 93 |
NewKeypairCmd("P-384").setFlag("kty", "EC").setFlag("curve", "P-384").test(t) |
| 94 |
NewKeypairCmd("P-521").setFlag("kty", "EC").setFlag("crv", "P-521").test(t) |
| 95 |
NewKeypairCmd("bad-crv").setFlag("kty", "EC").setFlag("curve", "P-512").fail(t, "flag '--kty EC' is incompatible with flag '--curve P-512'\n\n Option(s): --curve P-256, P-384, P-521\n") |
| 96 |
NewKeypairCmd("EC-size").setFlag("kty", "EC").setFlag("size", "2048").fail(t, "flag '--size' is incompatible with flag '--kty EC'\n") |
| 97 |
}) |
| 98 |
NewKeypairCmd("bad-type").setFlag("kty", "foo").fail(t, "invalid value 'foo' for flag '--kty'; options are RSA, EC, OKP\n") |
| 99 |
NewKeypairCmd("no-pass-fail").setFlag("no-password", "").failNoPass(t, "flag '--insecure' requires the '--no-password' flag\n") |
| 100 |
NewKeypairCmd("no-pass").setPassword("").setFlag("no-password", "").setFlag("insecure", "").testNoPass(t) |
| 101 |
} |