Skip to content

Commit

Permalink
Merge pull request #15 from sylr/various-things
Browse files Browse the repository at this point in the history
Various things
  • Loading branch information
sylr authored Apr 15, 2021
2 parents e0e8e50 + d8e5937 commit c36bb7f
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions yage.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (f *multiFlag) Set(value string) error {
}

func (f *multiFlag) Type() string {
return "multiFlag"
return "string"
}

func (f *multiFlag) Append(value string) error {
Expand All @@ -64,23 +64,22 @@ Options:
--yaml-discard-notag Does not honour NoTag attribute when decrypting (useful for re-keying).
--rekey Decrypt the input and encrypt it with the given recipients.
In re-keying mode the input and output can be the same file.
In YAML mode it implies --yaml-discard-notag.
In YAML mode it implies --yaml-discard-notag. By default if no -o option
is given yage will overwrite input file.
INPUT defaults to standard input, and OUTPUT defaults to standard output.
RECIPIENT can be an age public key generated by age-keygen ("age1...")
or an SSH public key ("ssh-ed25519 AAAA...", "ssh-rsa AAAA...").
RECIPIENT can be an age public key generated by age-keygen ("age1...") or an SSH public key ("ssh-ed25519 AAAA...", "ssh-rsa AAAA...").
Recipient files contain one or more recipients, one per line. Empty lines
and lines starting with "#" are ignored as comments. "-" may be used to
read recipients from standard input.
Recipient files contain one or more recipients, one per line. Empty lines and lines starting with "#" are ignored as comments.
"-" may be used to read recipients from standard input.
Identity files contain one or more secret keys ("AGE-SECRET-KEY-1..."),
one per line, or an SSH key. Empty lines and lines starting with "#" are
ignored as comments. Multiple key files can be provided, and any unused ones
will be ignored. "-" may be used to read identities from standard input.
Identity files contain one or more secret keys ("AGE-SECRET-KEY-1..."), one per line, or an SSH key. Empty lines and lines
starting with "#" are ignored as comments. Multiple key files can be provided, and any unused ones will be ignored.
"-" may be used to read identities from standard input.
Examples:
Example:
# Generate age key pair
$ age-keygen -o key.txt
Public key: age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p
Expand All @@ -96,7 +95,7 @@ Example:
$ yage --decrypt -i key.txt --yaml config.yaml.age
# Re-key age encrypted YAML
$ yage --rekey --yaml -i key.txt -R ~/.ssh/id_ed25519.pub -R ~/.ssh/id_rsa.pub -o config.yaml.age config.yaml.age
$ yage --rekey --yaml -i key.txt -R ~/.ssh/id_ed25519.pub -R ~/.ssh/id_rsa.pub config.yaml.age
`

// Version can be set at link time to override debug.BuildInfo.Main.Version,
Expand Down Expand Up @@ -139,6 +138,9 @@ func main() {
if versionFlag {
if Version != "" {
fmt.Printf("%s (%s)\n", Version, runtime.Version())
for _, mod := range getModsVersion() {
fmt.Fprintf(os.Stderr, "%s\n", mod)
}
return
}
if buildInfo, ok := debug.ReadBuildInfo(); ok {
Expand Down Expand Up @@ -220,12 +222,15 @@ func main() {

if outputName != "" && outputName != "-" {
overwrite := false
istat, _ := os.Stat(inputName)
ostat, _ := os.Stat(outputName)
if rekeyFlag && istat.Name() == ostat.Name() {
istat, err := os.Stat(inputName)
if err != nil {
logFatalf("Error: failed to open input file %q: %v", inputName, err)
}
ostat, err := os.Stat(outputName)
if rekeyFlag && err == nil && os.SameFile(istat, ostat) {
// in rekey mode we allow to overwrite the input file
overwrite = true
} else if _, err := os.Stat(outputName); err == nil {
} else if err == nil {
logFatalf("Error: output file %q exists", outputName)
}
f := newLazyOpener(outputName, overwrite)
Expand Down Expand Up @@ -547,12 +552,20 @@ func newLazyOpener(name string, overwrite bool) io.WriteCloser {
func (l *lazyOpener) Write(p []byte) (n int, err error) {
if l.f == nil && l.err == nil {
oFlags := os.O_WRONLY | os.O_CREATE
perms := os.FileMode(0660)

if l.overwrite {
stat, err := os.Stat(l.name)
if err != nil {
return 0, err
}
perms = stat.Mode()
oFlags = oFlags | os.O_TRUNC
} else {
oFlags = oFlags | os.O_EXCL
}
l.f, l.err = os.OpenFile(l.name, oFlags, 0666)

l.f, l.err = os.OpenFile(l.name, oFlags, perms)
}
if l.err != nil {
return 0, l.err
Expand All @@ -570,3 +583,17 @@ func (l *lazyOpener) Close() error {
func logFatalf(format string, v ...interface{}) {
_log.Fatalf(format, v...)
}

func getModsVersion() (mods []string) {
info, ok := debug.ReadBuildInfo()

if !ok {
return
}

for _, mod := range info.Deps {
mods = append(mods, fmt.Sprintf("%s: %s", mod.Path, mod.Version))
}

return
}

0 comments on commit c36bb7f

Please sign in to comment.