pulumi/pkg/resource/config/value_test.go
Matt Ellis ade366544e Encrypt secrets in Pulumi.yaml
We now encrypt secrets at rest based on a key derived from a user
suplied passphrase.

The system is designed in a way such that we should be able to have a
different decrypter (either using a local key or some remote service
in the Pulumi.com case in the future).

Care is taken to ensure that we do not leak decrypted secrets into the
"info" section of the checkpoint file (since we currently store the
config there).

In addtion, secrets are "pay for play", a passphrase is only needed
when dealing with a value that's encrypted. If secure config values
are not used, `pulumi` will never prompt you for a
passphrase. Otherwise, we only prompt if we know we are going to need
to decrypt the value. For example, `pulumi config <key>` only prompts
if `<key>` is encrypted and `pulumi deploy` and friends only prompt if
you are targeting a stack that has secure configuration assoicated
with it.

Secure values show up as unecrypted config values inside the language
hosts and providers.
2017-10-24 16:48:12 -07:00

78 lines
1.6 KiB
Go

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package config
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
yaml "gopkg.in/yaml.v2"
)
func TestMarshallNormalValueYAML(t *testing.T) {
v := NewValue("value")
b, err := yaml.Marshal(v)
assert.NoError(t, err)
assert.Equal(t, []byte("value\n"), b)
newV, err := roundtripYAML(v)
assert.NoError(t, err)
assert.Equal(t, v, newV)
}
func TestMarshallSecureValueYAML(t *testing.T) {
v := NewSecureValue("value")
b, err := yaml.Marshal(v)
assert.NoError(t, err)
assert.Equal(t, []byte("secure: value\n"), b)
newV, err := roundtripYAML(v)
assert.NoError(t, err)
assert.Equal(t, v, newV)
}
func TestMarshallNormalValueJSON(t *testing.T) {
v := NewValue("value")
b, err := json.Marshal(v)
assert.NoError(t, err)
assert.Equal(t, []byte("\"value\""), b)
newV, err := roundtripJSON(v)
assert.NoError(t, err)
assert.Equal(t, v, newV)
}
func TestMarshallSecureValueJSON(t *testing.T) {
v := NewSecureValue("value")
b, err := json.Marshal(v)
assert.NoError(t, err)
assert.Equal(t, []byte("{\"secure\":\"value\"}"), b)
newV, err := roundtripJSON(v)
assert.NoError(t, err)
assert.Equal(t, v, newV)
}
func roundtripYAML(v Value) (Value, error) {
return roundtrip(v, yaml.Marshal, yaml.Unmarshal)
}
func roundtripJSON(v Value) (Value, error) {
return roundtrip(v, json.Marshal, json.Unmarshal)
}
func roundtrip(v Value, marshal func(v interface{}) ([]byte, error), unmarshal func([]byte, interface{}) error) (Value, error) {
b, err := marshal(v)
if err != nil {
return Value{}, err
}
var newV Value
err = unmarshal(b, &newV)
return newV, err
}