File-SOPS
view release on metacpan or search on metacpan
lib/File/SOPS.pm view on Meta::CPAN
decrypt / extract, encrypted slot unchanged. sops already wrote
type:float there, so this library
has always read a float back
decrypt / extract, unencrypted slot prints the same digits as before,
but 0 + $value is now the double
those digits name where it used to
be the digits themselves
decrypt_file unchanged, byte for byte
C<0 + $value> for C<9223372036854776000> is C<9223372036854775808>, 192 short
of what the same scalar prints. That is not a number this library chose: it is
the C<float64> Go has been reading out of that leaf since sops wrote it, and
digesting for the MAC.
What B<is> lost is a digit the double never held. C<value_to_bytes> re-derives
the text from the number, so a hand-written literal that is not its double's
canonical decimal is written -- and read back -- rounded:
C<12345678901234567890> becomes C<12345678901234567000> and
C<99999999999999999999> becomes C<100000000000000000000>, which is what
C<sops -e> does to the same document.
The same re-derivation settles a B<verification> difference that used to go the
other way. A document whose unencrypted number is spelled differently from its
double's canonical decimal -- C<9223372036854776832>, whose double is the same
C<9223372036854775808> -- is one C<sops -d> accepts, because Go digests the
number and not the text. Until 0.003 this library digested the integer's own
digits and reported C<MAC verification failed> on such a file; it now digests
what Go digests, reads it, and L</decrypt_file> writes it out with the same
normalisation C<sops -d> writes. Measured against sops 3.13.3 on both sides.
Three things around these windows are still B<refused> rather than written --
the magnitude above them where a real non-finite double reaches an unencrypted
slot or states its own text, one whole format, and one kind of caller -- and a
caller meeting a wide number should know where all three are.
A literal that overflows a double -- C<1> followed by 400 zeros -- B<dies> in
L<File::SOPS::Encrypted/assert_representable>'s non-finite guard wherever it
reaches the tree as a real non-finite double, on every path that B<writes> the
value: L</encrypt>, L</encrypt_file>, L</encrypt_in_place>, L</rotate> and
L</edit>. That is any B<JSON> document, where L<Cpanel::JSON::XS> returns
C<+Inf> carrying the literal's own digits as its string half -- and it is that
B<stated text> the guard refuses, in either slot, because an encrypted slot is
derived from the number and would drop it without a trace. In JSON it is not an
extra restriction of ours -- sops refuses the same document itself and never
reaches a type: C<sops -e> stops at
C<Error unmarshalling file: [...] strconv.ParseFloat: value out of range>, exit
2, in an encrypted and an unencrypted slot alike, and C<sops -d> stops with the
same message, exit 1, on a document hand-edited to carry one. Measured against
sops 3.13.3.
A caller's own bare C<9**9**9> states nothing, and it is refused in an
B<unencrypted> slot only. In an B<encrypted> one it is now written, as
C<type:float> and the plaintext C<+Inf>, which is what C<sops -e> stores for
the same value in both formats. See
L<File::SOPS::Encrypted/assert_representable>, k122 and
L<docs/adr/0040|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0040-an-encrypted-slot-carries-a-non-finite-float-because-sops-writes-one.md>.
B<In YAML that literal is not refused any more, because it is not a number
there.> Since 0.003 L<File::SOPS::Format::YAML/parse> hands back the string
go-yaml reads -- C<1e400>, a 401-digit integer and the bare spellings C<Inf>,
C<NaN> and their relatives -- so the guard never sees a float, and the leaf is
written as C<type:str>, which is what C<sops -e> writes for the identical
plaintext (exit 0, measured against sops 3.13.3, encrypted and unencrypted slot
alike). Until 0.003 the same document could be neither written here -- all 20
measured hit the guard -- nor read, 17 of those 20 failing with C<MAC
verification failed>.
The B<read> paths follow from that and differ by format. In YAML L</decrypt>
and L</extract> hand the leaf back as a plain string carrying the literal's own
text, and the document verifies; L</A YAML literal that overflows a double
comes back as a string> under L</decrypt> has what that changed for a caller.
In JSON no such document verifies in either implementation: L</decrypt> stops
at C<MAC verification failed>, and with C<ignore_mac> the value is C<Inf> with
its digits gone.
The lower window in a B<YAML> document is refused, and sops cannot write one
either. L<YAML::XS> hands those digits back as a Perl integer, so the C<int64>
refusal above applies and L</encrypt> dies with that message; C<sops -e> on the
same plaintext stops at C<Error walking tree: Cannot walk value, unknown type:
uint64>, exit 23, in both slots, measured against sops 3.13.3. The upper window
in YAML has always been a float and is unaffected. Only JSON has a window Go
reads as a number and Perl reads as an integer.
A lower-window value that did B<not> come from a JSON parse -- a Perl integer
literal, a computed one, or one that came out of a YAML document -- is still
refused by L<File::SOPS::Encrypted/assert_representable>. The scalar the caller
handed over is an exact integer and no reader has spoken for it, so turning it
into a lossy double would be this library guessing. Say which one you mean:
C<unpack('d', pack('d', $v))> makes it the float sops writes, and C<"$v"> makes
it a C<type:str> that stores the digits exactly. That refusal is wider than it
strictly has to be -- of 35 literals measured across the window, 14 would have
produced a document C<sops -d> accepts in an unencrypted JSON slot -- and
narrowing it is open as k104.
=head3 Saying what a value is
There is no per-leaf type argument to L</encrypt>, and none is needed: the
scalar is the type, so you say what a value is by handing over the scalar that
says it.
use JSON::MaybeXS qw(JSON); # JSON->true is not exported by File::SOPS
$data->{port} = "$data->{port}"; # type:str
$data->{port} = 0 + $data->{port}; # type:int
$data->{ratio} = unpack('d', pack('d', $data->{ratio})); # type:float
$data->{flag} = JSON->true; # type:bool
$data->{admin} = ($level > 3); # type:bool too, on perl 5.36+
The last line works because Perl marks its own booleans on the scalar since
5.36 -- C<!!1>, C<!!0> and every comparison's result -- and both emitters write
such an SV as a bare C<true>/C<false>. Until 0.003 it was C<type:int>, and the
resulting file failed its own MAC. See
L<File::SOPS::Encrypted/detect_type> and k90.
The C<ratio> line goes through C<pack>/C<unpack> rather than the more obvious
C<+ 0.0> because addition sets Perl's public C<SVf_IOK> on an B<integral>
result, and that flag is what decides the type: measured, C<0.0 + 2> is
C<type:int>, and so is C<0.0 + '2'>. C<pack('d', ...)> lays the value out as a
native double and C<unpack> builds a fresh scalar from those bytes, which is a
float and nothing else -- the same conversion
L<File::SOPS::Encrypted/decrypt_value> makes for the same reason. A literal
C<2.0> written in your own source is already a float and needs none of this.
lib/File/SOPS.pm view on Meta::CPAN
The threshold is B<not> this library's. It is go-yaml's, reproduced on
go-yaml's own counters, because an independently chosen one would refuse
documents sops accepts. What it budgets is a B<ratio> -- roughly how far the
expanded document exceeds the document as written, up to about 100 times,
tightening to about 1.1 times as the expansion approaches four million values
-- and B<not> a size. sops accepts a 206,104-value expansion and refuses an
8,146-value one, and so does this. Bisected against sops 3.13.3 in four
differently shaped families, the accept/refuse boundary is the same document
on both sides.
There is no size at which a document is refused for being large: one that
shares nothing amplifies nothing and is accepted however big it gets.
A caller's own structure reaches the same guard by the other road. One hash
reference held in many places is the same blowup with no YAML involved, and
C<format =E<gt> 'json'> is exposed to it exactly as YAML is.
Refusing is what the reference implementation does, in both directions --
C<Error unmarshalling file: yaml: document contains excessive aliasing>
(exit 2) from C<sops -e>, and C<yaml: document contains excessive aliasing>
(exit 1) from C<sops -d>.
See k112 and
L<docs/adr/0027|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0027-the-alias-budget-is-a-ratio-and-it-is-go-yamls-ratio.md>.
=head3 A document nested deeper than sops can carry is refused
B<New in 0.003.> A document is walked at most C<$File::SOPS::MAX_DEPTH>
containers deep, 10000 by default, and one nested deeper is refused rather than
walked:
# dies: this document nests containers more than 10000 deep, which is as
# deep as this library walks. sops stops as well: ...
The number is sops's, not this library's. Measured against sops 3.13.3,
counting containers from the document's own root mapping: go-yaml accepts
10001 and refuses 10002 with C<yaml: exceeded max depth of 10000>, in both
directions and B<ahead> of the data key; Go's JSON encoder accepts 10000 and
refuses 10001 with C<exceeded max depth>. 10000 is therefore the deepest a
document can be and still be readable in both formats. It is one level tighter
than go-yaml alone would be, deliberately: the alternative is writing a JSON
file sops cannot read back.
Nothing that came from a sops-written document can reach this. It exists for
what a caller can build in Perl and for what a hand-written file can carry, and
because a walk that cannot say no has only a hang to offer -- before this,
4000 levels took 101s and 410 MB in the MAC's walk alone.
C<$File::SOPS::MAX_DEPTH> is writable and is what a caller processing
untrusted documents can lower to refuse deep documents earlier. Raising it
above 10000 produces documents sops will refuse to read.
See k117.
=head3 A plain YAML infinity is the float go-yaml reads
B<New in 0.003, and a change for existing callers of every method that reads a
YAML file.> A leaf a YAML document wrote as a B<plain> scalar whose token
go-yaml resolves to a non-finite float -- C<.inf>, C<.Inf>, C<.INF>, the same
three with a leading C<+> or C<->, C<.nan>, C<.NaN>, C<.NAN> -- is that float
here too, carrying the document's own token as its text. A B<quoted> C<".inf">
is the string it has always been; the difference is decided by asking
L<YAML::PP> how the document wrote the scalar, never by matching the leaf's
text. See L<File::SOPS::Format::YAML/A plain infinity comes back as the float
go-yaml reads>.
B<This applies to a plaintext file as well as to an encrypted one>, which is
what changed last: until then the repair ran only for a document that carried a
C<sops:> section, so this library's own L</decrypt_file> wrote
C<v_unencrypted: .inf> and its own L</encrypt_file> refused to read that file
back, and L</edit> could not save a document it had just opened. sops makes one
parse and this now makes one too.
What it means per slot, measured against sops 3.13.3 for all twelve spellings:
=over 4
=item * In an B<unencrypted> slot the leaf is written as the token the document
had, and the MAC digest covers C<+Inf> / C<-Inf> / C<NaN>. C<sops -d> reads it,
and for the three spellings C<sops -e> itself writes the wire bytes are
identical to sops's.
=item * In an B<encrypted> slot the leaf is B<refused>, naming the key path:
the wire form there is C<type:float> with the plaintext C<+Inf>, and
L<File::SOPS::Encrypted/encrypt_value> cannot see which format it is writing
for -- YAML would carry it, JSON cannot (C<sops -e> exit 4,
C<Error marshaling to json>). Before this such a leaf was written as a
C<type:str> holding C<.inf>, where C<sops -e> on the same plaintext writes a
C<type:float>: a working file that had silently stopped being a number. This is
a refusal where sops succeeds, and it is deliberate -- see k122, which is
where the encrypted slot gets the format it needs.
=item * In B<JSON> nothing changes at either end. C<.inf> is not JSON, so the
token cannot reach a JSON document to begin with.
=back
See k123 and
L<docs/adr/0034|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0034-a-plain-scalar-is-resolved-the-same-way-on-every-parse.md>.
=head2 decrypt
my $data = File::SOPS->decrypt(
encrypted => $encrypted_content,
identities => \@age_secret_keys,
format => 'yaml', # optional, auto-detected
ignore_mac => 0, # optional, see below
max_stanzas => 64, # optional, see below
);
Decrypts SOPS-encrypted content.
Takes encrypted content as a string, decrypts the data key using provided age
identities, verifies the MAC, and returns the decrypted data structure -- a
HashRef for a single-document file, or an B<ArrayRef of HashRefs> for a
multi-document YAML stream (see L</A multi-document YAML stream is an ArrayRef>).
The returned structure holds B<character strings>, so it compares equal to the
structure L</encrypt> was given. Do not decode it again. See
L</Character encoding>.
=head3 A multi-document YAML stream is an ArrayRef
B<New in 0.003.> A YAML file holding more than one document -- documents joined
by C<--->, as sops writes them -- is one tree with N branches carrying B<one>
C<sops> metadata section and B<one> MAC spanning every document in order. This
method reads such a stream and returns an ArrayRef whose elements are the
decrypted documents in document order; a file holding a single document still
returns a bare HashRef. Which shape you get mirrors the B<document>, not the
call, so code that wants to be shape-agnostic writes
my @docs = ref $result eq 'ARRAY' ? @$result : ($result);
The metadata is taken from the B<first> document, exactly as sops does. A stream
carrying a C<sops> section only in a I<later> document surfaces no metadata and
is refused with C<No SOPS metadata found> -- the same C<sops metadata not found>
sops reports for it.
B<The MAC authenticates the concatenated leaf sequence across all documents, not
lib/File/SOPS.pm view on Meta::CPAN
B<write> under such a rule. What stays refused on this path is a pattern the two
dialects compile and read B<differently> (C<\v>, C<\Q>, C<\E>) or one Perl
cannot compile at all (C<(?U)>): there is no sops answer to reproduce for those,
so guessing at one would classify leaves wrongly and silently. See
F<docs/adr/0051>.
=head3 A comment in a list comes back as a C<File::SOPS::Comment>
B<New in 0.003.> sops attaches a comment to the node that B<follows> it, and
where that node is a sequence entry it writes the comment as a sequence entry of
its own (C<- ENC[...,type:comment]>) -- in YAML and, measured, in JSON written
with C<--output-type json> as well. Such an entry comes back as a
C<File::SOPS::Comment> object holding the comment's text, sitting at the index
the file puts it at. Hand the same tree back to L</encrypt> or use L</rotate>
and it is written out again as a C<type:comment> entry; C<sops -d> then restores
it as a real comment, on the node it was attached to.
It is deliberately an B<object> and not the text: a comment is not a value, and
a string here would be an extra element the file does not contain. That is what
this used to return, and a C<decrypt> plus C<encrypt> cycle made it a permanent
value with C<sops -d> reporting success at every step (k108). A comment
leaf is B<not covered by the MAC>, which is what sops does with one.
Two shapes are still refused, both naming the path: a comment in a B<mapping
value> slot, which no SOPS store writes and which sops reads back as a dump of
Go's comment struct; and writing a comment into B<plaintext>, so
L</decrypt_file> and L</edit> refuse a document that has one --
L<YAML::XS> cannot emit a comment, and a comment line handed to an editor would
be dropped on the way back in. A comment above a B<mapping key> never reaches
this library at all: it stays a comment line, which L<YAML::XS> discards on the
way in and cannot write on the way out. See
L<docs/adr/0041|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0041-a-sops-comment-is-a-leaf-of-its-own-not-a-value-and-not-a-refusal.md>.
B<New in 0.003: a I<plaintext> comment in an encrypted slot is warned about.>
Distinct from the encrypted C<type:comment> entry above: where a
C<File::SOPS::Comment> stands B<bare> in a slot the document's encryption rule
B<selects>, this method C<carp>s. That is the third of the four bare shapes
L</The rule decides what a value is, in both directions> tolerates in a selected
slot -- the document is still read, the comment comes back unchanged and stays
out of the MAC, exactly as C<sops -d> reads the same document at exit 0 and warns
the same way (its message quotes sops's own C<Found possibly unencrypted comment
in file>). The warning is advisory -- such a comment is neither encrypted nor
authenticated and may hold a secret in the clear -- and B<its text is not put in
the warning>, for that same reason: a value that lands in a log was not
encrypted for any practical purpose. Any re-encryption of the document
(L</rotate>, L</edit>, L</encrypt_in_place>, or L</decrypt_file> then
L</encrypt_file>) turns the comment into an encrypted C<type:comment> leaf and
silences it.
This reaches B<dotenv and INI only>. A plaintext comment has to survive parsing
into a C<File::SOPS::Comment> before there is anything to warn about, and
L<YAML::XS> discards one before this library sees the tree -- so a YAML document
never gets here, though C<sops> itself warns for YAML as well. See
L<docs/adr/0067|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0067-a-plaintext-comment-in-an-encrypted-slot-is-warned-about-not-silenced.md>.
=head3 A YAML literal that overflows a double comes back as a string
B<New in 0.003, and a change for existing callers.> From an B<unencrypted>
YAML slot, a literal libyaml resolves to a non-finite double -- C<1e400>, a
401-digit integer, and the bare spellings C<Inf>, C<inf>, C<INF>, C<Infinity>,
C<NaN>, C<nan>, C<NAN>, C<-Inf> and C<+Inf> -- comes back as a plain B<string>
holding the literal's own text. It used to carry C<Inf> or C<NaN> as its
numeric half as well, because L<YAML::XS> returns those literals with both
halves set.
B<This is a correction, and it does not extend to the encrypted slot.>
go-yaml -- the parser sops reads a document with -- resolves none of those
spellings to a number either: C<strconv.ParseFloat> answers C<ErrRange>, so
sops keeps a string, writes C<type:str> and digests the literal's own text. The
old return value also belonged to a document this library mostly could not read
at all: of 20 such documents C<sops -e> wrote and C<sops -d> read, 17 failed
here with C<MAC verification failed> and 3 verified by coincidence. An
B<encrypted> C<type:float> whose plaintext is C<+Inf>, C<-Inf> or C<NaN> is
B<unchanged> and still decrypts to a real Perl non-finite float, bit for bit --
measured on a document sops wrote, C<000000000000f07f>, C<000000000000f0ff> and
C<000000000000f8ff> -- because such a leaf is still an C<ENC[...]> string when
the document is parsed and cannot reach the retyping walk at all.
Arithmetic is not what changes. Perl numifies every one of those spellings to
the same double and does not warn, so C<0 + $value> answers exactly as before.
What moves is the scalar's B<type>, and with it what re-encrypting the returned
tree writes: C<type:str>, which is what sops writes for the same leaf, where
the numeric half used to hit the non-finite refusal in
L<File::SOPS::Encrypted/assert_representable>. Untouched: the twelve spellings
go-yaml really does resolve to a non-finite float (C<.inf>, C<.nan> and their
case variants -- an B<encrypted> one of those decrypts to a real C<Inf> here as
it always did, and an B<unencrypted> one is the separate repair described under
L</A plain YAML infinity is the float go-yaml reads>), and JSON, where sops
refuses the document itself. See
L<File::SOPS::Format::YAML/parse>, L</A number past Go's int64 is a float>,
k102 and
L<docs/adr/0023|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0023-a-yaml-literal-that-overflows-a-double-is-a-string-not-a-float.md>.
=head3 A document that contains itself is refused
B<New in 0.003.> A document carrying a B<recursive YAML anchor> -- one whose
value is nested inside itself -- is refused here, before the data key is
unwrapped, rather than hanging the process as it did until now. The check and
the reasoning are the same in both directions and are described under
L</A structure that contains itself is refused>.
The refusal deliberately comes B<ahead> of the key: a cyclic document reports
the cycle even when none of the C<identities> could have opened it. That is the
order sops answers in, measured -- C<sops -d> on such a file with no age
identity available reports C<yaml: anchor 'a' value contains itself>, not a
failure to get the data key.
C<ignore_mac> does not get past this. It suppresses verification, not the
document's shape.
The same holds, at the same place and in the same order, for a document whose
aliases are acyclic but exponentially shared -- see
L</A document that expands far beyond what it contains is refused>. Measured,
C<sops -d> on such a file with no age identity available reports
C<yaml: document contains excessive aliasing> rather than a failure to get the
data key, so this reports it there too. Before the guard, C<decrypt> at 15
levels walked 65,535 leaves before it could report anything at all, and with
C<ignore_mac =E<gt> 1> it walked the same tree with nothing to report.
And for a document nested deeper than this library walks -- see
L</A document nested deeper than sops can carry is refused>. Measured the same
way: C<sops -d> on an over-deep encrypted file reports
C<yaml: exceeded max depth of 10000> whether or not an age identity is
available, so the depth is reported here ahead of the key as well.
=head2 encrypt_file
File::SOPS->encrypt_file(
input => 'secrets.yaml',
output => 'secrets.enc.yaml', # optional, defaults to input (in-place)
recipients => \@age_public_keys,
format => 'yaml', # optional, auto-detected from filename
);
lib/File/SOPS.pm view on Meta::CPAN
A B<float> leaf comes back as a L<Scalar::Util/dualvar>: numerically the value
itself, as a string the canonical decimal the document holds. A decrypted float
is a bare NV with no string form of its own, so printing one went through
Perl's 15 significant digits -- an encrypted C<0.30000000000000004> arrived as
C<0.3>, where C<sops -d --extract> prints all 17. Arithmetic, C<==>, C<sprintf
'%f'> and C<is_deeply> are unchanged; what changes is C<"$value">, and that
change is the point. New in 0.003; see k61 and
L<docs/adr/0010|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0010-extract-returns-a-float-that-prints-all-its-digits.md>.
Two things that dualvar does not do. The spelling is the B<wire's> -- the
canonical decimal L<File::SOPS::Encrypted/value_to_bytes> derives, which is what
the ciphertext holds and what the MAC covers, and which is B<positional at every
magnitude>. C<sops -d --extract> prints the value's YAML or JSON serialization
instead, and that switches to an exponent at the ends of the range. Measured,
sops 3.13.3, one document per row:
format sops prints an exponent when example
yaml decimal exponent >= 6 or < -4 1000000 -> 1e+06, 1e-5 -> 1e-05
json decimal exponent >= 21 or < -6 1e21 -> 1e+21, 1e-7 -> 1e-7
So C<1e20> stringifies as C<100000000000000000000> here and prints as C<1e+20>
from a YAML document -- but as C<100000000000000000000> from a JSON one, where
the two agree up to 1e21. The exponent's own spelling differs between the
formats as well (C<1e-05> in YAML, C<1e-7> in JSON). B<No digits are lost in
any of it>: measured from the smallest subnormal to C<DBL_MAX>, in both
formats, every spelling either side prints parses back to the identical double.
What the positional form costs is length -- C<DBL_MAX> stringifies as 309
digits and C<5e-324> as C<0.> followed by 323 zeros and a C<5>. Matching sops
would mean a second float formatter, Go's C<%g> rules beside the C<%f> ones
this distribution has, and then a third for JSON's; the decision to keep the
wire's spelling is recorded in ADR 0010 (k79).
The wire's spelling is not the B<document's> either, where the two differ: an
unencrypted C<1.50> or C<42.0> comes back as C<1.5> and C<42>, because the
string half is the canonical decimal and not the source text.
Since 0.003 that paragraph covers a JSON number past Go's C<int64> as well:
such a leaf is a float now, where past C<2**64-1> it used to come back as a
plain string and between C<2**63> and C<2**64-1> as an exact Perl integer, so
C<extract> wraps it and prints all its digits in an encrypted slot as well as
an unencrypted one. A hand-written C<99999999999999999999> comes back as
C<100000000000000000000>, which is also what the document itself now holds.
For the lower window the digits printed are the ones the document already
carried, and it is C<0 + $value> that moved instead.
See L</A number past Go's int64 is a float>.
The second thing the dualvar does not do is travel: it applies to the B<leaf>
this method returns and to nothing else. Floats inside a returned branch are
the plain scalars they have always been, because a dualvar in a structure
changes the bytes the emitters write. Not the value: putting one into an
B<unencrypted> slot stores the canonical decimal as a number, in JSON as in
YAML (k78,
L<docs/adr/0011|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0011-a-float-leaf-that-carries-its-own-string-form-is-repaired.md>).
What changes is the spelling at the extremes -- C<1e300> written as 301
positional digits rather than C<1e+300> -- and an encrypted slot is unaffected
either way.
B<Since 0.003 a non-finite YAML literal comes back from an unencrypted slot as
a string, and is not wrapped either.> C<1e400>, a 401-digit integer and the
bare spellings C<Inf>, C<inf>, C<INF>, C<Infinity>, C<NaN>, C<nan>, C<NAN>,
C<-Inf> and C<+Inf> are the string go-yaml reads there, and a string is not a
float. This is a B<correction> -- such a leaf used to carry C<Inf> or C<NaN> as
its numeric half, on the few of those documents that could be read here at all
-- and it stops at the unencrypted slot: an B<encrypted> C<type:float> whose
plaintext is C<+Inf>, C<-Inf> or C<NaN> still comes back as the real Perl
non-finite float it always did. That one is the single float this method does
B<not> wrap, because C<+Inf> and C<NaN> are wire spellings rather than a
number's decimal, so C<"$value"> is Perl's own C<Inf> / C<NaN> and not a
canonical form. See L</A YAML literal that overflows a double comes back as a
string> under L</decrypt> and
L<File::SOPS::Encrypted/canonical_float_dualvar>.
B<Dies if the path does not exist>, at any depth, naming the component that was
not found. Before 0.003 a missing B<top-level> key returned C<undef> while a
missing nested one died, so the same mistake was silent or loud depending on
where it was made -- and C<undef> was indistinguishable from a key whose value
really is null. sops reports C<component ['nope'] not found> at every level.
=head2 rotate
File::SOPS->rotate(
file => 'secrets.enc.yaml',
identities => \@age_secret_keys,
recipients => \@new_recipients, # optional, keeps current recipients
format => 'yaml', # optional, auto-detected from filename
);
Rotates the data key (re-encrypts all values with a new key).
This operation:
=over 4
=item 1. Decrypts the file using C<identities>
=item 2. Generates a new random data key
=item 3. Re-encrypts all values with the new data key
=item 4. Encrypts the new data key for C<recipients> (or existing recipients if not specified)
=item 5. Writes back to the same file
=back
Step 5 is atomic: the rotated document is written next to the file and renamed
over it, so an interrupted rotation leaves the file readable under its old data
key rather than truncated. Until 0.003 the file was opened with C<< '>' >>
first, which is the worst moment to lose it -- the new data key exists only in
the buffer being written, so a file truncated there is not recoverable with any
identity. L</How a file is written> has the consequences of the C<rename>.
Key rotation is recommended periodically for security, or when removing
a recipient's access.
The rotated file keeps the C<sops> section it had, apart from what a new data
key necessarily replaces. Its encryption rules, C<mac_only_encrypted> and any
field this distribution does not model -- C<shamir_threshold> and whatever a
later sops adds -- are carried over; the wrapped data keys, the MAC and
C<lastmodified> are regenerated. Until 0.003 none of that was carried: rotate
called L</encrypt>, which built fresh metadata with the defaults, so a file
that customised any of it was rewritten under the default rules.
The values keep their C<type:> labels too, and C<type:float> on a B<whole
number> is the one that did not. Rotation re-encrypts every leaf, so each label
is re-derived from the scalar the decryption produced, and until 0.003 that
scalar came back carrying Perl's public C<SVf_IOK> whenever the plaintext was
integral: a C<whole: 2.0> that sops itself had written was rotated back out as
lib/File/SOPS.pm view on Meta::CPAN
to and an interactive user in front of it -- neither of which a library method
can assume.
=head3 Editing re-keys the file
Unlike C<sops edit>, which keeps the document's data key and only rewrites the
values, this method decrypts and encrypts, so the file comes back with a
B<new data key> -- the wrapped copies in the C<sops> section change on every
edit, and so does the diff. Nothing is lost by it: the age recipients and the
encryption policy (the rules, C<mac_only_encrypted>, and any C<sops> field this
distribution does not model) are carried over exactly as L</rotate> carries
them.
What it does mean is that C<edit> refuses the same files L</rotate> refuses: a
document whose C<sops> section also holds C<pgp>, C<kms>, C<gcp_kms>,
C<azure_kv>, C<hc_vault> or C<key_groups> material cannot be re-encrypted for
those recipients here, and dropping them silently would revoke their access
while reporting success. See L</Files rotate refuses>.
It stops on the other of rotate's files too, and B<before the editor is
opened>: a document holding an encrypted value at a path its own encryption
rule says is not encrypted fails its MAC in the L</decrypt> this method does
first. See L</A rule that does not cover what is encrypted stops on the MAC>.
Stopping after the editor had run would throw away what was just typed, for a
defect that was in the file before it started.
Key B<order> is not preserved either: the plaintext handed to the editor is
emitted from a Perl hash, so it comes out sorted whatever order the encrypted
file had. That is the same emitter L</decrypt_file> uses -- the format
handler's C<emit>, which is also what the encrypted document is written with --
and it is the order this distribution writes documents in anyway, so it does
not affect the MAC.
C<ignore_mac> is passed through to L</decrypt>; editing a file you could not
verify re-signs whatever it contained, so prefer to fail. C<max_stanzas> is
passed through to L</decrypt> as well, and defaults to 64.
A dotenv or INI document whose encrypted slot holds a I<plaintext> comment
C<carp>s on the L</decrypt> this does first (see L</A comment in a list comes
back as a C<File::SOPS::Comment>>); re-keying the file then re-encrypts that
comment into a C<type:comment> leaf.
=head3 What the round trip through the editor keeps, and what it does not
The document the editor sees is B<plaintext>, so anything a value knew that its
plaintext spelling does not say is gone by the time it comes back. What comes
back is parsed exactly as any other YAML file would be -- there is no second,
gentler parse for text this method wrote itself.
That is enough for a B<plain YAML infinity>, because the plaintext really does
say it: C<.inf> written plain is a float to go-yaml and to L</decrypt_file>'s
output alike, so a document sops wrote with a bare C<.inf> in an unencrypted
slot survives an edit and comes back with the wire byte-identical. Before
0.003 it did not -- editing any other key in such a file died with the leaf
refused, B<and the edit was destroyed with it>, because the temporary file is
already gone by then. See L</A plain YAML infinity is the float go-yaml reads>
and k123.
It is B<not> enough for a non-finite float in an B<encrypted> slot, and that
one is still wrong: such a leaf decrypts to a real Perl infinity, whose only
plaintext spelling from this emitter is a bare C<Inf> / C<-Inf> / C<NaN> --
tokens go-yaml reads as B<strings>. The editor is shown C<Inf>, the string
C<Inf> comes back, and the leaf is re-encrypted as a C<type:str>. C<sops edit>
keeps it a C<type:float>, measured. The file is written and nothing is said, so
this is the one place C<edit> can still lose a value quietly. Open as k134.
A C<data_key =E<gt> $bytes> argument would close the gap -- pass the
existing data key through and this method stops re-keying -- but it puts
raw key material on the public API, which is a real decision (and
probably an ADR) rather than a refactor. It will be worth doing once a
backend other than age exists (k39): today the refusal only fires
on documents this distribution could not have produced in the first
place.
=head2 creation_rules_for
my %args = File::SOPS->creation_rules_for(file => 'secrets/prod.yaml');
File::SOPS->encrypt_in_place(
file => 'secrets/prod.yaml',
%args,
);
Reads the C<.sops.yaml> that governs a file and returns the L</encrypt>
arguments its first matching creation rule asks for: C<recipients>, plus
whichever of the four encryption rules and C<mac_only_encrypted> that rule
carries. The returned list is meant to be splatted straight into L</encrypt>,
L</encrypt_file> or L</encrypt_in_place>, which is where the actual encrypting
still happens -- this method decides B<for whom and under which rules>, and
nothing else.
Nothing here reads C<.sops.yaml> implicitly. C<encrypt> and friends still want
their C<recipients> spelled out; this is the one method that will go and look.
=head3 Finding the config file
C<.sops.yaml> is looked for in the directory holding C<file> and then in every
directory above it, up to the filesystem root, and the first one found is used.
Nothing stops the walk earlier -- not a C<.git> directory, not C<$HOME> --
which is what sops does as well (measured on 3.13.3). The name must be exactly
C<.sops.yaml>: a C<.sops.yml> is ignored there and here.
B<This is a deliberate deviation, and it is the one thing here that differs
from the reference implementation.> sops walks up from the B<current working
directory>, not from the file: measured on 3.13.3, C<sops -e a/b/c/secrets.yaml>
run from the top of that tree does not see an C<a/b/.sops.yaml> at all, and
C<sops -e /abs/path/secrets.yaml> run from an unrelated directory reports
C<config file not found> however many config files sit above the file. That is
a sensible rule for a command a person types in the directory they are working
in, and a useless one for a library: the caller's working directory has nothing
to do with the file it was handed, and a daemon whose cwd is C</> would find
nothing at all. The two agree in the ordinary case -- one C<.sops.yaml> at the
top of a repository, the file somewhere underneath it -- and differ only when
config files are nested, where walking up from the file picks the nearer and
more specific one. The measurements and the two rejected alternatives are in
L<docs/adr/0007|https://github.com/Getty/p5-file-sops/blob/main/docs/adr/0007-the-config-search-starts-at-the-file-not-at-the-working-directory.md>.
C<config> names a config file explicitly, and skips the search entirely. It is
the equivalent of sops's C<--config>. Its value is B<not> taken from
C<$SOPS_CONFIG>, which sops does honour: an environment variable that
redirects which public keys a secret gets encrypted to is a reasonable thing
( run in 1.122 second using v1.01-cache-2.11-cpan-60884a8df76 )