Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to override fileName with different value #1332

Merged
merged 3 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
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
46 changes: 45 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,51 @@ Now you can encrypt a file using::

And decrypt it using::

$ sops --decrypt test.enc.yaml
$ sops --decrypt test.enc.yaml


Encrypting and decrypting from other programs
Copy link
Member

Choose a reason for hiding this comment

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

In think we need to line wrap the documentation, as that seems to be the case for the majority of the sections in the README and eases reading in e.g. a terminal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd actually argue that explicitly wrapping lines is bad, since terminals by default wrap lines and thus you use the full terminal width, as opposed to explicit line wrapping, which looks mostly bad except if it is applied consistently and your terminal is wider than the limit used in the file. But that's something we should look at at another point in time :)

I've wrapped lines to a similar length as other parts of this file for now.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When using ``sops`` in scripts or from other programs, there are often situations where you do not want to write
encrypted or decrypted data to disk. The best way to avoid this is to pass data to SOPS via stdin, and to let
SOPS write data to stdout. By default, the encrypt and decrypt operations write data to stdout already. To pass
data via stdin, you need to pass ``/dev/stdin`` as the input filename. Please note that this only works on
Unix-like operating systems such as macOS and Linux. On Windows, you have to use named pipes.

To decrypt data, you can simply do:

.. code:: sh

$ cat encrypted-data | sops --decrypt /dev/stdin > decrypted-data

To control the input and output format, pass ``--input-type`` and ``--output-type`` as appropriate. By default,
``sops`` determines the input and output format from the provided filename, which is ``/dev/stdin`` here, and
thus will use the binary store which expects JSON input and outputs binary data on decryption.

For example, to decrypt YAML data and obtain the decrypted result as YAML, use:

.. code:: sh

$ cat encrypted-data | sops --input-type yaml --output-type yaml --decrypt /dev/stdin > decrypted-data

To encrypt, it is important to note that SOPS also uses the filename to look up the correct creation rule from
``.sops.yaml``. Likely ``/dev/stdin`` will not match a creation rule, or only match the fallback rule without
``path_regex``, which is usually not what you want. For that, ``sops`` provides the ``--filename-override``
parameter which allows you to tell SOPS which filename to use to match creation rules:

.. code:: sh

$ echo 'foo: bar' | sops --filename-override path/filename.sops.yaml --encrypt /dev/stdin > encrypted-data

SOPS will find a matching creation rule for ``path/filename.sops.yaml`` in ``.sops.yaml`` and use that one to
encrypt the data from stdin. This filename will also be used to determine the input and output store. As always,
the input store type can be adjusted by passing ``--input-type``, and the output store type by passing
``--output-type``:

.. code:: sh

$ echo foo=bar | sops --filename-override path/filename.sops.yaml --input-type dotenv --encrypt /dev/stdin > encrypted-data


Encrypting using Hashicorp Vault
Expand Down
22 changes: 15 additions & 7 deletions cmd/sops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,10 @@ func main() {
Name: "output",
Usage: "Save the output after encryption or decryption to the file specified",
},
cli.StringFlag{
Name: "filename-override",
Usage: "Use this filename instead of the provided argument for loading configuration, and for determining input type and output type",
},
}, keyserviceFlags...)

app.Action = func(c *cli.Context) error {
Expand All @@ -795,13 +799,17 @@ func main() {
return common.NewExitError("Error: cannot operate on non-existent file", codes.NoFileSpecified)
}
}
fileNameOverride := c.String("filename-override")
if fileNameOverride == "" {
fileNameOverride = fileName
}
devstein marked this conversation as resolved.
Show resolved Hide resolved

unencryptedSuffix := c.String("unencrypted-suffix")
encryptedSuffix := c.String("encrypted-suffix")
encryptedRegex := c.String("encrypted-regex")
unencryptedRegex := c.String("unencrypted-regex")
macOnlyEncrypted := c.Bool("mac-only-encrypted")
conf, err := loadConfig(c, fileName, nil)
conf, err := loadConfig(c, fileNameOverride, nil)
if err != nil {
return toExitError(err)
}
Expand Down Expand Up @@ -847,19 +855,19 @@ func main() {
unencryptedSuffix = sops.DefaultUnencryptedSuffix
}

inputStore := inputStore(c, fileName)
outputStore := outputStore(c, fileName)
inputStore := inputStore(c, fileNameOverride)
outputStore := outputStore(c, fileNameOverride)
svcs := keyservices(c)

var output []byte
if c.Bool("encrypt") {
var groups []sops.KeyGroup
groups, err = keyGroups(c, fileName)
groups, err = keyGroups(c, fileNameOverride)
if err != nil {
return toExitError(err)
}
var threshold int
threshold, err = shamirThreshold(c, fileName)
threshold, err = shamirThreshold(c, fileNameOverride)
if err != nil {
return toExitError(err)
}
Expand Down Expand Up @@ -1015,12 +1023,12 @@ func main() {
} else {
// File doesn't exist, edit the example file instead
var groups []sops.KeyGroup
groups, err = keyGroups(c, fileName)
groups, err = keyGroups(c, fileNameOverride)
if err != nil {
return toExitError(err)
}
var threshold int
threshold, err = shamirThreshold(c, fileName)
threshold, err = shamirThreshold(c, fileNameOverride)
if err != nil {
return toExitError(err)
}
Expand Down
Loading