Beispiel #1
0
func (n *ng) openSealedJSONVal(bytes []byte, val interface{}) error {
	if n.options.Box == nil {
		return fmt.Errorf("need secretbox to open sealed data")
	}
	sv, err := secret.SealedValueFromJSON([]byte(bytes))
	if err != nil {
		return err
	}
	unsealed, err := n.options.Box.Open(sv)
	if err != nil {
		return err
	}
	return json.Unmarshal(unsealed, val)
}
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)
}