Граф коммитов

19 Коммитов

Автор SHA1 Сообщение Дата
Spencer Judd 4507019a33
Add standard newline/quoting behavior to dotenv store (#622)
Rationale
=========

The dotenv store as it exists right now performs splitting on newlines
to determine where a new key-value pair or comment begins. This works
remarkably well, up until you need to handle values that contain
newlines.

While I couldn't find an offical dotenv file format spec, I sampled a
number of open-source dotenv parsers and it seems that they typically
apply the following rules:

Comments:

* Comments may be written by starting a line with the `#` character.

Newline handling:

* If a value is unquoted or single-quoted and contains the character
  sequence `\n` (`0x5c6e`), it IS NOT decoded to a line feed (`0x0a`).

* If a value is double-quoted and contains the character sequence `\n`
  (`0x5c6e`), it IS decoded to a line feed (`0x0a`).

Whitespace trimming:

* For comments, the whitespace immediately after the `#` character and any
  trailing whitespace is trimmed.

* If a value is unquoted and contains any leading or trailing whitespace, it
  is trimmed.

* If a value is either single- or double-quoted and contains any leading or
  trailing whitespace, it is left untrimmed.

Quotation handling:

* If a value is surrounded by single- or double-quotes, the quotation marks
  are interpreted and not included in the value.

* Any number of single-quote characters may appear in a double-quoted
  value, or within a single-quoted value if they are escaped (i.e.,
  `'foo\'bar'`).

* Any number of double-quote characters may appear in a single-quoted
  value, or within a double-quoted value if they are escaped (i.e.,
  `"foo\"bar"`).

Because single- and double-quoted values may contain actual newlines,
we cannot split our input data on newlines as this may be in the middle
of a quoted value. This, along with the other rules around handling
quoted values, prompted me to try and implement a more robust parsing
solution. This commit is my first stab at that.

Special Considerations
======================

This is _not_ a backwards-compatible change:

* The `dotenv` files produced by this version of SOPS _cannot_ be read
  by an earlier version.

* The `dotenv` files produced by an earlier version of SOPS _can_ be
  read by this version, with the understanding that the semantics around
  quotations and newlines have changed.

Examples
========

The below examples show how double-quoted values are passed to the
running environment:

```console
$ echo 'FOO="foo\\nbar\\nbaz"' > plaintext.env
$ sops -e --output ciphertext.env plaintext.env
$ sops exec-env ciphertext.env 'env | grep FOO | xxd'
00000000: 464f 4f3d 666f 6f5c 6e62 6172 5c6e 6261  FOO=foo\nbar\nba
00000010: 7a0a                                     z.
```

```console
$ echo 'FOO="foo\nbar\nbaz"' > plaintext.env
$ sops -e --output ciphertext.env plaintext.env
$ sops exec-env ciphertext.env 'env | grep -A2 FOO | xxd'
00000000: 464f 4f3d 666f 6f0a 6261 720a 6261 7a0a  FOO=foo.bar.baz.
```
2020-03-20 22:47:14 +01:00
Spencer Judd 16343503c2 Fix newline encoding for dotenv store (#612)
When reading and writing dotenv files, we need to make sure to
encode/decode newline characters. SOPS does not currently do this, as
can be seen from the below:

```console
$ echo '{"foo": "foo\nbar\nbaz"}' > plaintext.json
$ sops -e --output ciphertext.json plaintext.json
$ sops -d --output-type dotenv ciphertext.json
foo=foo
bar
baz
```

This output, is invalid and cannot even be fed back into SOPS:

```console
$ sops -d --output-type dotenv --output plaintext.env ciphertext.json
$ sops -e plaintext.env
Error unmarshalling file: invalid dotenv input line: bar
```

This commit fixes the issue, such that the final `sops -d ...` command
above produces the correct output:

```console
$ sops -d --output-type dotenv ciphertext.json
foo=foo\nbar\nbaz
```
2020-01-24 18:03:34 +01:00
AJ Bahnken 8e21de8dbc
Upgrade sops to go 1.13 (#566) 2019-11-18 10:06:58 -08:00
Adrian Utrilla 9998e16c3e
Merge pull request #491 from adrianosela/golint
Address Go Lint messages
2019-07-16 11:36:51 +02:00
Adriano 4b99fa18b3
go lint 2019-07-11 10:30:32 -07:00
Adriano 87adc130eb
code cleanup [1/5] - go fmt 2019-07-08 09:46:36 -07:00
Adrian Utrilla 2712e3770f
Merge branch 'master' into consolidate-example-trees 2019-02-27 22:03:21 +01:00
Adrian Utrilla d77ae5b1be
Add comment support to dotenv store 2019-01-23 11:07:05 +01:00
Adrian Utrilla bbf17b3d84
Consolidate example trees 2019-01-23 10:54:27 +01:00
James Robson dfa150bf75 Add multidoc encrypt/decrypt for YAML sources 2018-11-21 09:00:49 -07:00
Adrian Utrilla 2a99f0411e
Add test for dotenv EmitValue 2018-10-31 18:19:55 -04:00
Adrian Utrilla 9b45e33cbd
Refactor flattening and unflattening code 2018-10-31 16:01:17 -04:00
Joost Cassee a05f8627c7 Flatten sops metadata into variables 2018-10-31 15:08:25 +01:00
Joost Cassee 5fc86c12fa Implement flattened metadata for dotenv files
This commit contains a bug in the metadata handling.
2018-10-30 16:55:00 +01:00
Joost Cassee 39fbb27c2c Implement dotenv store.EmitValue function 2018-10-30 15:01:15 +01:00
Adrian Utrilla f475b5cecd
Cast input bytes to string when creating a TreeItem 2018-10-30 08:12:52 -04:00
Joost Cassee 700455adff Refactor the code base on review comments
- Ran goimports on store.go and store_test.go.
- LoadPlainFile uses bytes.Split.
2018-10-30 09:53:02 +01:00
Joost Cassee 985943c037 Attempt to output metadata items 2018-10-29 23:49:10 +01:00
Joost Cassee eadef71162 Rename "env" store to "dotenv" 2018-10-29 21:50:13 +01:00