Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions eval/crypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,11 @@ func EncryptSecrets(ctx context.Context, filename string, source []byte, encrypt
}

// Encrypt the plaintext.
ciphertext, err := encrypter.Encrypt(ctx, []byte(plaintext.Value()))
ciphertext, err := EncryptPlaintext(ctx, encrypter, []byte(plaintext.Value()))
if err != nil {
return nil, nil, err
}
encodedCiphertext := base64.StdEncoding.EncodeToString(ciphertext)

// Replace the original call to `fn::secret` with a new call whose argument is the encrypted ciphertext.
//
Expand All @@ -134,7 +135,7 @@ func EncryptSecrets(ctx context.Context, filename string, source []byte, encrypt
syntax.Object(
syntax.ObjectProperty(
syntax.String("ciphertext"),
syntax.StringSyntax(syntax.CopyTrivia(plaintext.Syntax()), encodeCiphertext(ciphertext)),
syntax.StringSyntax(syntax.CopyTrivia(plaintext.Syntax()), encodedCiphertext),
),
),
),
Expand Down Expand Up @@ -178,6 +179,15 @@ func DecryptSecrets(ctx context.Context, filename string, source []byte, decrypt
})
}

func EncryptPlaintext(ctx context.Context, encrypter Encrypter, plaintext []byte) ([]byte, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's call this EncryptSecret for congruence with DecryptSecrets.

Suggested change
func EncryptPlaintext(ctx context.Context, encrypter Encrypter, plaintext []byte) ([]byte, error) {
func EncryptSecret(ctx context.Context, encrypter Encrypter, plaintext []byte) ([]byte, error) {

This also needs a doc comment. A simple test would be great as well--making sure it round-trips should be sufficient.

ciphertext, err := encrypter.Encrypt(ctx, plaintext)
if err != nil {
return nil, err
}

return encodeCiphertext(ciphertext), nil
}

const envelopeMagic = "escx"
const envelopeVersion = uint32(1)

Expand Down Expand Up @@ -223,11 +233,11 @@ func decodeCiphertext(repr string) ([]byte, error) {
return bin[8 : len(bin)-4], nil
}

func encodeCiphertext(ciphertext []byte) string {
func encodeCiphertext(ciphertext []byte) []byte {
var b bytes.Buffer
b.WriteString(envelopeMagic) // "escx"
b.Write(binary.BigEndian.AppendUint32(nil, envelopeVersion)) // version
b.Write(ciphertext) // ciphertext
b.Write(binary.BigEndian.AppendUint32(nil, crc32.Checksum(b.Bytes(), crc32.IEEETable))) // crc32
return base64.StdEncoding.EncodeToString(b.Bytes())
return b.Bytes()
}
Loading