From f1231be4580de72abf4470f979b9075989269912 Mon Sep 17 00:00:00 2001 From: cornfeedhobo Date: Mon, 4 Jun 2018 09:46:09 -0500 Subject: [PATCH] travisci fixes. simplify test and remove suppport for go1.6. --- .travis.yml | 5 +- README.md | 77 ++++++++++---------- keydgen_test.go | 187 ++++++++++++++++++------------------------------ 3 files changed, 111 insertions(+), 158 deletions(-) diff --git a/.travis.yml b/.travis.yml index 852ce81..ad71721 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,14 @@ language: go go: - - 1.6 - 1.7 - 1.8 - 1.9 +install: false + script: - - go test -v ./... + - go test -v || travis_terminate 1 - go get github.com/mitchellh/gox - CGO_ENABLED=0 gox -ldflags "-s" -rebuild diff --git a/README.md b/README.md index 2290073..4aebfe5 100644 --- a/README.md +++ b/README.md @@ -1,49 +1,50 @@ ssh-keydgen [![Travis](https://img.shields.io/travis/cornfeedhobo/ssh-keydgen.svg)]() [![Github All Releases](https://img.shields.io/github/downloads/cornfeedhobo/ssh-keydgen/total.svg)]() =========== - Generate _Deterministic_ SSH keys - - ```text - NAME: - ssh-keydgen - Deterministic authentication key generation - - USAGE: - ssh-keydgen [-t] [-b] [-c] [-n] [-f] [-a] - - AUTHOR: - cornfeedhobo - - OPTIONS: - -t type Specifies the type of key to create. The possible values are "dsa", "ecdsa", "rsa", or "ed25519". (default: "ed25519") - -b bits Specifies the number of bits in the key to create. Possible values are restricted by key type. (default: 2048) - -c curve Specifies the elliptic curve to use. The possible values are 256, 384, or 521. (default: 256) - -n factor Specifies the work factor, or "difficulty", applied to the key generation function. (default: 16384) - -f filename Specifies the filename of the key file. - -a Add the generated key to the running ssh-agent. - - COPYRIGHT: - (c) 2017 cornfeedhobo - ``` +Generate _Deterministic_ SSH keys + +```text +NAME: + ssh-keydgen - deterministic authentication key generation + +USAGE: + ssh-keydgen [-t] [-b] [-c] [-n] [-f] [-a] [-w] + +AUTHOR: + cornfeedhobo + +OPTIONS: + -t type Specifies the type of key to create. The possible values are "dsa", "ecdsa", "rsa", or "ed25519". (default: "ed25519") + -b bits Specifies the number of bits in the key to create. Possible values are restricted by key type. (default: 2048) + -c curve Specifies the elliptic curve to use. The possible values are 256, 384, or 521. (default: 256) + -n factor Specifies the work factor, or "difficulty", applied to the key generation function. (default: 16384) + -f filename Specifies the filename of the key file. + -a Add the generated key to the running ssh-agent. + -w seed Provides the deterministic seed + +COPYRIGHT: + (c) 2018 cornfeedhobo +``` ## Usage - 1) Generate your keys - ```bash - keydgen -f deterministic_key - ls -lh deterministic_key* - ``` - - 2) Allow time to pass, hoping an emergency does not arise when you have no access to your keys ... - - _If_ the time comes where you need access but can't get to your keys, you can then obtain this - utility and re-generate, or even directly add your key to a running `ssh-agent`. - ```bash - keydgen -a - ``` - - 3) Profit! +1) Generate your keys + ```bash + keydgen -f deterministic_key + ls -lh deterministic_key* + ``` + +2) Allow time to pass, hoping an emergency does not arise when you have no access to your keys ... + + _If_ the time comes where you need access but can't get to your keys, you can then obtain this + utility and re-generate, or even directly add your key to a running `ssh-agent`. + ```bash + keydgen -a + ``` + +3) Profit! diff --git a/keydgen_test.go b/keydgen_test.go index 81cbf92..4a8ea42 100644 --- a/keydgen_test.go +++ b/keydgen_test.go @@ -1,7 +1,6 @@ package main import ( - "errors" "fmt" "io/ioutil" "os" @@ -12,69 +11,27 @@ import ( "github.com/google/goexpect" ) -type keygenCase struct { - name string - expectFailure bool - keydgen *Keydgen -} - -func init() { - WorkFactor = 4096 -} - -func testKeygenImport(k *Keydgen) error { - - if _, err := k.GenerateKey(); err != nil { - return err - } - - pub, err := k.MarshalPublicKey() - if err != nil { - return err - } - - tmpFile, err := ioutil.TempFile("", "keydgen_test") - if err != nil { - return err - } - - if err := writeKeyToFile(k, tmpFile.Name()); err != nil { - return err - } - defer func() { - os.Remove(tmpFile.Name()) - os.Remove(tmpFile.Name() + ".pub") - }() - - e, _, err := expect.Spawn("ssh-keygen -y -f "+tmpFile.Name(), -1) - if err != nil { - return err - } - defer e.Close() - - expr := regexp.MustCompilePOSIX(regexp.QuoteMeta(string(pub))) - if _, _, eErr := e.Expect(expr, -1); eErr != nil { - err = errors.New("unable to verify generated public key with ssh-keygen") - } - - return err - -} +func TestKeydgen_GenerateKey(t *testing.T) { -func TestKeydgen_GenerateKey_DSA(t *testing.T) { + WorkFactor = 1024 * 1 - cases := []keygenCase{ + cases := []struct { + name string + expectFailure bool + keydgen *Keydgen + }{ + // DSA { - name: "InvalidKeyLength", - expectFailure: true, + name: "DSA_InvalidKeyLength", keydgen: &Keydgen{ Type: DSA, Bits: 100, Seed: []byte("keydgen"), }, + expectFailure: true, }, { - name: "1024", + name: "DSA_1024", keydgen: &Keydgen{ Type: DSA, Bits: 1024, @@ -82,7 +39,7 @@ func TestKeydgen_GenerateKey_DSA(t *testing.T) { }, }, { - name: "2048", + name: "DSA_2048", keydgen: &Keydgen{ Type: DSA, Bits: 2048, @@ -90,42 +47,25 @@ func TestKeydgen_GenerateKey_DSA(t *testing.T) { }, }, { - name: "3072", + name: "DSA_3072", keydgen: &Keydgen{ Type: DSA, Bits: 3072, Seed: []byte("keydgen"), }, }, - } - - for _, c := range cases { - t.Run(c.name, func(t *testing.T) { - start := time.Now() - fmt.Print(t.Name()) - if err := testKeygenImport(c.keydgen); err != nil && !c.expectFailure { - t.Fatal(err) - } - fmt.Printf(" PASS %s\n", time.Since(start)) - }) - } - -} - -func TestKeydgen_GenerateKey_ECDSA(t *testing.T) { - - cases := []keygenCase{ + // ECDSA { - name: "InvalidCurve", - expectFailure: true, + name: "ECDSA_InvalidCurve", keydgen: &Keydgen{ Type: ECDSA, Curve: 128, Seed: []byte("keydgen"), }, + expectFailure: true, }, { - name: "256", + name: "ECDSA_256", keydgen: &Keydgen{ Type: ECDSA, Curve: 256, @@ -133,7 +73,7 @@ func TestKeydgen_GenerateKey_ECDSA(t *testing.T) { }, }, { - name: "384", + name: "ECDSA_384", keydgen: &Keydgen{ Type: ECDSA, Curve: 384, @@ -141,58 +81,25 @@ func TestKeydgen_GenerateKey_ECDSA(t *testing.T) { }, }, { - name: "521", + name: "ECDSA_521", keydgen: &Keydgen{ Type: ECDSA, Curve: 521, Seed: []byte("keydgen"), }, }, - } - - for _, c := range cases { - t.Run(c.name, func(t *testing.T) { - start := time.Now() - fmt.Print(t.Name()) - if err := testKeygenImport(c.keydgen); err != nil && !c.expectFailure { - t.Fatal(err) - } - fmt.Printf(" PASS %s\n", time.Since(start)) - }) - } - -} - -func TestKeydgen_GenerateKey_RSA(t *testing.T) { - - cases := []keygenCase{ + // RSA { - name: "2048", + name: "RSA_2048", keydgen: &Keydgen{ Type: RSA, Bits: 2048, Seed: []byte("keydgen"), }, }, - } - - for _, c := range cases { - t.Run(c.name, func(t *testing.T) { - start := time.Now() - fmt.Print(t.Name()) - if err := testKeygenImport(c.keydgen); err != nil && !c.expectFailure { - t.Fatal(err) - } - fmt.Printf(" PASS %s\n", time.Since(start)) - }) - } - -} - -func TestKeydgen_GenerateKey_ED25519(t *testing.T) { - - cases := []keygenCase{ + // ED25519 { + name: "ED25519", keydgen: &Keydgen{ Type: ED25519, Seed: []byte("keydgen"), @@ -202,12 +109,56 @@ func TestKeydgen_GenerateKey_ED25519(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { + start := time.Now() - fmt.Print(t.Name()) - if err := testKeygenImport(c.keydgen); err != nil && !c.expectFailure { + fmt.Print(c.name) + + if _, err := c.keydgen.GenerateKey(); err != nil && !c.expectFailure { t.Fatal(err) } + + if !c.expectFailure { + + pub, err := c.keydgen.MarshalPublicKey() + if err != nil { + t.Fatal(err) + } + + filename := fmt.Sprintf("keydgen_test_%s_", c.keydgen.Type) + if c.keydgen.Type == RSA || c.keydgen.Type == DSA { + filename += fmt.Sprintf("%d_", c.keydgen.Bits) + } else if c.keydgen.Type == ECDSA { + filename += fmt.Sprintf("%d_", c.keydgen.Curve) + } + + tmpFile, err := ioutil.TempFile("", filename) + if err != nil { + t.Fatal(err) + } + + if err := writeKeyToFile(c.keydgen, tmpFile.Name()); err != nil { + t.Fatal(err) + } + defer func() { + os.Remove(tmpFile.Name()) + os.Remove(tmpFile.Name() + ".pub") + }() + + e, _, err := expect.Spawn("ssh-keygen -y -f "+tmpFile.Name(), -1) + if err != nil { + t.Fatal(err) + } + defer e.Close() + + expr := regexp.MustCompilePOSIX(regexp.QuoteMeta(string(pub))) + if out, _, err := e.Expect(expr, -1); err != nil { + t.Fatal("unable to verify generated public key with ssh-keygen: " + out) + } + + } + fmt.Printf(" PASS %s\n", time.Since(start)) + }) }