Skip to content

Commit 3028179

Browse files
authored
Merge pull request #1332 from felixfontein/filename-override
Allow to override fileName with different value
2 parents 7655a68 + 2678f2d commit 3028179

2 files changed

Lines changed: 60 additions & 8 deletions

File tree

README.rst

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,51 @@ Now you can encrypt a file using::
323323

324324
And decrypt it using::
325325

326-
$ sops --decrypt test.enc.yaml
326+
$ sops --decrypt test.enc.yaml
327+
328+
329+
Encrypting and decrypting from other programs
330+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
331+
332+
When using ``sops`` in scripts or from other programs, there are often situations where you do not want to write
333+
encrypted or decrypted data to disk. The best way to avoid this is to pass data to SOPS via stdin, and to let
334+
SOPS write data to stdout. By default, the encrypt and decrypt operations write data to stdout already. To pass
335+
data via stdin, you need to pass ``/dev/stdin`` as the input filename. Please note that this only works on
336+
Unix-like operating systems such as macOS and Linux. On Windows, you have to use named pipes.
337+
338+
To decrypt data, you can simply do:
339+
340+
.. code:: sh
341+
342+
$ cat encrypted-data | sops --decrypt /dev/stdin > decrypted-data
343+
344+
To control the input and output format, pass ``--input-type`` and ``--output-type`` as appropriate. By default,
345+
``sops`` determines the input and output format from the provided filename, which is ``/dev/stdin`` here, and
346+
thus will use the binary store which expects JSON input and outputs binary data on decryption.
347+
348+
For example, to decrypt YAML data and obtain the decrypted result as YAML, use:
349+
350+
.. code:: sh
351+
352+
$ cat encrypted-data | sops --input-type yaml --output-type yaml --decrypt /dev/stdin > decrypted-data
353+
354+
To encrypt, it is important to note that SOPS also uses the filename to look up the correct creation rule from
355+
``.sops.yaml``. Likely ``/dev/stdin`` will not match a creation rule, or only match the fallback rule without
356+
``path_regex``, which is usually not what you want. For that, ``sops`` provides the ``--filename-override``
357+
parameter which allows you to tell SOPS which filename to use to match creation rules:
358+
359+
.. code:: sh
360+
361+
$ echo 'foo: bar' | sops --filename-override path/filename.sops.yaml --encrypt /dev/stdin > encrypted-data
362+
363+
SOPS will find a matching creation rule for ``path/filename.sops.yaml`` in ``.sops.yaml`` and use that one to
364+
encrypt the data from stdin. This filename will also be used to determine the input and output store. As always,
365+
the input store type can be adjusted by passing ``--input-type``, and the output store type by passing
366+
``--output-type``:
367+
368+
.. code:: sh
369+
370+
$ echo foo=bar | sops --filename-override path/filename.sops.yaml --input-type dotenv --encrypt /dev/stdin > encrypted-data
327371
328372
329373
Encrypting using Hashicorp Vault

cmd/sops/main.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,10 @@ func main() {
769769
Name: "output",
770770
Usage: "Save the output after encryption or decryption to the file specified",
771771
},
772+
cli.StringFlag{
773+
Name: "filename-override",
774+
Usage: "Use this filename instead of the provided argument for loading configuration, and for determining input type and output type",
775+
},
772776
}, keyserviceFlags...)
773777

774778
app.Action = func(c *cli.Context) error {
@@ -795,13 +799,17 @@ func main() {
795799
return common.NewExitError("Error: cannot operate on non-existent file", codes.NoFileSpecified)
796800
}
797801
}
802+
fileNameOverride := c.String("filename-override")
803+
if fileNameOverride == "" {
804+
fileNameOverride = fileName
805+
}
798806

799807
unencryptedSuffix := c.String("unencrypted-suffix")
800808
encryptedSuffix := c.String("encrypted-suffix")
801809
encryptedRegex := c.String("encrypted-regex")
802810
unencryptedRegex := c.String("unencrypted-regex")
803811
macOnlyEncrypted := c.Bool("mac-only-encrypted")
804-
conf, err := loadConfig(c, fileName, nil)
812+
conf, err := loadConfig(c, fileNameOverride, nil)
805813
if err != nil {
806814
return toExitError(err)
807815
}
@@ -847,19 +855,19 @@ func main() {
847855
unencryptedSuffix = sops.DefaultUnencryptedSuffix
848856
}
849857

850-
inputStore := inputStore(c, fileName)
851-
outputStore := outputStore(c, fileName)
858+
inputStore := inputStore(c, fileNameOverride)
859+
outputStore := outputStore(c, fileNameOverride)
852860
svcs := keyservices(c)
853861

854862
var output []byte
855863
if c.Bool("encrypt") {
856864
var groups []sops.KeyGroup
857-
groups, err = keyGroups(c, fileName)
865+
groups, err = keyGroups(c, fileNameOverride)
858866
if err != nil {
859867
return toExitError(err)
860868
}
861869
var threshold int
862-
threshold, err = shamirThreshold(c, fileName)
870+
threshold, err = shamirThreshold(c, fileNameOverride)
863871
if err != nil {
864872
return toExitError(err)
865873
}
@@ -1015,12 +1023,12 @@ func main() {
10151023
} else {
10161024
// File doesn't exist, edit the example file instead
10171025
var groups []sops.KeyGroup
1018-
groups, err = keyGroups(c, fileName)
1026+
groups, err = keyGroups(c, fileNameOverride)
10191027
if err != nil {
10201028
return toExitError(err)
10211029
}
10221030
var threshold int
1023-
threshold, err = shamirThreshold(c, fileName)
1031+
threshold, err = shamirThreshold(c, fileNameOverride)
10241032
if err != nil {
10251033
return toExitError(err)
10261034
}

0 commit comments

Comments
 (0)