Пример #1
0
func (cmd *Command) generateKeyAction(c *cli.Context) {
	key, err := secret.NewKeyString()
	if err != nil {
		cmd.printError(fmt.Errorf("unable to generate key: %v", err))
		return
	}
	stream, closer, err := getStream(c)
	if err != nil {
		cmd.printError(err)
		return
	}
	if closer != nil {
		defer closer.Close()
	}
	_, err = stream.Write([]byte(key))
	if err != nil {
		cmd.printError(fmt.Errorf("failed writing to output stream, error %s", err))
		return
	}
}
Пример #2
0
func (s *CmdSuite) TestReadKeyPair(c *C) {
	keyPair := testutils.NewTestKeyPair()

	key, err := secret.NewKeyString()
	c.Assert(err, IsNil)

	fKey, err := ioutil.TempFile("", "vulcand")
	c.Assert(err, IsNil)
	defer fKey.Close()
	fKey.Write(keyPair.Key)

	fCert, err := ioutil.TempFile("", "vulcand")
	c.Assert(err, IsNil)
	defer fCert.Close()
	fCert.Write(keyPair.Cert)

	fSealed, err := ioutil.TempFile("", "vulcand")
	c.Assert(err, IsNil)
	fSealed.Close()

	s.run("secret", "seal_keypair", "-privateKey", fKey.Name(), "-cert", fCert.Name(), "-sealKey", key, "-f", fSealed.Name())

	bytes, err := ioutil.ReadFile(fSealed.Name())
	c.Assert(err, IsNil)

	box, err := secret.NewBoxFromKeyString(key)
	c.Assert(err, IsNil)

	sealed, err := secret.SealedValueFromJSON(bytes)
	data, err := box.Open(sealed)
	c.Assert(err, IsNil)

	outKeyPair, err := engine.KeyPairFromJSON(data)
	c.Assert(err, IsNil)

	c.Assert(outKeyPair, DeepEquals, keyPair)
}