| 1 | package mainscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/ |
| 2 | |
| 3 | import ( |
| 4 | "errorsscip-go gomod github.com/golang/go/src . errors/" |
| 5 | "flagscip-go gomod github.com/golang/go/src . flag/" |
| 6 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 7 | "ioscip-go gomod github.com/golang/go/src . io/" |
| 8 | "logscip-go gomod github.com/golang/go/src . log/" |
| 9 | "osscip-go gomod github.com/golang/go/src . os/" |
| 10 | "runtime/debugscip-go gomod github.com/golang/go/src . `runtime/debug`/" |
| 11 | "strconvscip-go gomod github.com/golang/go/src . strconv/" |
| 12 | "stringsscip-go gomod github.com/golang/go/src . strings/" |
| 13 | |
| 14 | "filippo.io/agescip-go gomod filippo.io/age . `filippo.io/age`/" |
| 15 | "filippo.io/age/pluginscip-go gomod filippo.io/age . `filippo.io/age/plugin`/" |
| 16 | ) |
| 17 | |
| 18 | const usagescip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/usage. = `age-plugin-batchpass is an age plugin that enables non-interactive |
| 19 | passphrase-based encryption and decryption using environment variables. |
| 20 | |
| 21 | WARNING: IN 90% OF CASES, YOU DON'T NEED THIS PLUGIN. |
| 22 | |
| 23 | This functionality is not built into the age CLI because most applications |
| 24 | should use native keys instead of scripting passphrase-based encryption. |
| 25 | |
| 26 | Humans are notoriously bad at remembering and generating strong passphrases. |
| 27 | age uses scrypt to partially mitigate this, which is necessarily very slow. |
| 28 | |
| 29 | If a computer will be doing the remembering anyway, you can and should use |
| 30 | native keys instead. There is no need to manage separate public and private |
| 31 | keys, you encrypt directly to the private key: |
| 32 | |
| 33 | $ age-keygen -o key.txt |
| 34 | $ age -e -i key.txt file.txt > file.txt.age |
| 35 | $ age -d -i key.txt file.txt.age > file.txt |
| 36 | |
| 37 | Likewise, you can store a native identity string in an environment variable |
| 38 | or through your CI secrets manager and use it to encrypt and decrypt files |
| 39 | non-interactively: |
| 40 | |
| 41 | $ export AGE_SECRET=$(age-keygen) |
| 42 | $ age -e -i <(echo "$AGE_SECRET") file.txt > file.txt.age |
| 43 | $ age -d -i <(echo "$AGE_SECRET") file.txt.age > file.txt |
| 44 | |
| 45 | The age CLI also natively supports passphrase-encrypted identity files, so you |
| 46 | can use that functionality to non-interactively encrypt multiple files such that |
| 47 | you will be able to decrypt them later by entering the same passphrase: |
| 48 | |
| 49 | $ age-keygen -pq | age -p -o encrypted-identity.txt |
| 50 | Public key: age1pq1cd[... 1950 more characters ...] |
| 51 | Enter passphrase (leave empty to autogenerate a secure one): |
| 52 | age: using autogenerated passphrase "eternal-erase-keen-suffer-fog-exclude-huge-scorpion-escape-scrub" |
| 53 | $ age -r age1pq1cd[... 1950 more characters ...] file.txt > file.txt.age |
| 54 | $ age -d -i encrypted-identity.txt file.txt.age > file.txt |
| 55 | Enter passphrase for identity file "encrypted-identity.txt": |
| 56 | |
| 57 | Finally, when using this plugin care should be taken not to let the password be |
| 58 | persisted in the shell history or leaked to other users on multi-user systems. |
| 59 | |
| 60 | Usage: |
| 61 | |
| 62 | $ AGE_PASSPHRASE=password age -e -j batchpass file.txt > file.txt.age |
| 63 | |
| 64 | $ AGE_PASSPHRASE=password age -d -j batchpass file.txt.age > file.txt |
| 65 | |
| 66 | Alternatively, you can use AGE_PASSPHRASE_FD to read the passphrase from |
| 67 | a file descriptor. Trailing newlines are stripped from the file contents. |
| 68 | |
| 69 | When encrypting, you can set AGE_PASSPHRASE_WORK_FACTOR to adjust the scrypt |
| 70 | work factor (between 1 and 30, default 18). Higher values are more secure |
| 71 | but slower. The age CLI refuses to decrypt files above 22, and so does this |
| 72 | plugin unless AGE_PASSPHRASE_MAX_WORK_FACTOR is set to at least the same value. |
| 73 | |
| 74 | When decrypting, you can set AGE_PASSPHRASE_MAX_WORK_FACTOR to change the |
| 75 | maximum scrypt work factor accepted (between 1 and 30, default 22). This can |
| 76 | be used to avoid very slow decryptions, or to allow slower ones.` |
| 77 | |
| 78 | // Version can be set at link time to override debug.BuildInfo.Main.Version when |
| 79 | // building manually without git history. It should look like "v1.2.3". |
| 80 | var Versionscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/Version. string |
| 81 | |
| 82 | func mainscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/main().() { |
| 83 | flagscip-go gomod github.com/golang/go/src . flag/.Usagescip-go gomod github.com/golang/go/src . flag/Usage. = func() { fmtscip-go gomod github.com/golang/go/src . fmt/.Fprintfscip-go gomod github.com/golang/go/src . fmt/Fprintf().(osscip-go gomod github.com/golang/go/src . os/.Stderrscip-go gomod github.com/golang/go/src . os/Stderr., "%s\n", usagescip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/usage.) } |
| 84 | |
| 85 | p, err := pluginscip-go gomod filippo.io/age . `filippo.io/age/plugin`/.Newscip-go gomod filippo.io/age . `filippo.io/age/plugin`/New().("batchpass") |
| 86 | if err != nil { |
| 87 | logscip-go gomod github.com/golang/go/src . log/.Fatalscip-go gomod github.com/golang/go/src . log/Fatal().(err) |
| 88 | } |
| 89 | p.RegisterFlagsscip-go gomod filippo.io/age . `filippo.io/age/plugin`/Plugin#RegisterFlags().(nil) |
| 90 | |
| 91 | versionFlag := flagscip-go gomod github.com/golang/go/src . flag/.Boolscip-go gomod github.com/golang/go/src . flag/Bool().("version", false, "print the version") |
| 92 | flagscip-go gomod github.com/golang/go/src . flag/.Parsescip-go gomod github.com/golang/go/src . flag/Parse().() |
| 93 | |
| 94 | if *versionFlag { |
| 95 | if buildInfo, ok := debugscip-go gomod github.com/golang/go/src . `runtime/debug`/.ReadBuildInfoscip-go gomod github.com/golang/go/src . `runtime/debug`/ReadBuildInfo().(); ok && Versionscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/Version. == "" { |
| 96 | Versionscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/Version. = buildInfo.Mainscip-go gomod github.com/golang/go/src . `runtime/debug`/BuildInfo#Main..Versionscip-go gomod github.com/golang/go/src . `runtime/debug`/Module#Version. |
| 97 | } |
| 98 | fmtscip-go gomod github.com/golang/go/src . fmt/.Printlnscip-go gomod github.com/golang/go/src . fmt/Println().(Versionscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/Version.) |
| 99 | return |
| 100 | } |
| 101 | |
| 102 | p.HandleIdentityAsRecipientscip-go gomod filippo.io/age . `filippo.io/age/plugin`/Plugin#HandleIdentityAsRecipient().(func(data []byte) (agescip-go gomod filippo.io/age . `filippo.io/age`/.Recipientscip-go gomod filippo.io/age . `filippo.io/age`/Recipient#, error) { |
| 103 | if len(data) != 0 { |
| 104 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("batchpass identity does not take any payload") |
| 105 | } |
| 106 | pass, err := passphrasescip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/passphrase().() |
| 107 | if err != nil { |
| 108 | return nil, err |
| 109 | } |
| 110 | r, err := agescip-go gomod filippo.io/age . `filippo.io/age`/.NewScryptRecipientscip-go gomod filippo.io/age . `filippo.io/age`/NewScryptRecipient().(pass) |
| 111 | if err != nil { |
| 112 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to create scrypt recipient: %v", err) |
| 113 | } |
| 114 | if envWorkFactor := osscip-go gomod github.com/golang/go/src . os/.Getenvscip-go gomod github.com/golang/go/src . os/Getenv().("AGE_PASSPHRASE_WORK_FACTOR"); envWorkFactor != "" { |
| 115 | workFactor, err := strconvscip-go gomod github.com/golang/go/src . strconv/.Atoiscip-go gomod github.com/golang/go/src . strconv/Atoi().(envWorkFactor) |
| 116 | if err != nil { |
| 117 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("invalid AGE_PASSPHRASE_WORK_FACTOR: %v", err) |
| 118 | } |
| 119 | if workFactor > 30 || workFactor < 1 { |
| 120 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("AGE_PASSPHRASE_WORK_FACTOR must be between 1 and 30") |
| 121 | } |
| 122 | r.SetWorkFactorscip-go gomod filippo.io/age . `filippo.io/age`/ScryptRecipient#SetWorkFactor().(workFactor) |
| 123 | } |
| 124 | return r, nil |
| 125 | }) |
| 126 | p.HandleIdentityscip-go gomod filippo.io/age . `filippo.io/age/plugin`/Plugin#HandleIdentity().(func(data []byte) (agescip-go gomod filippo.io/age . `filippo.io/age`/.Identityscip-go gomod filippo.io/age . `filippo.io/age`/Identity#, error) { |
| 127 | if len(data) != 0 { |
| 128 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("batchpass identity does not take any payload") |
| 129 | } |
| 130 | pass, err := passphrasescip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/passphrase().() |
| 131 | if err != nil { |
| 132 | return nil, err |
| 133 | } |
| 134 | maxWorkFactor := 0 |
| 135 | if envMaxWorkFactor := osscip-go gomod github.com/golang/go/src . os/.Getenvscip-go gomod github.com/golang/go/src . os/Getenv().("AGE_PASSPHRASE_MAX_WORK_FACTOR"); envMaxWorkFactor != "" { |
| 136 | maxWorkFactor, err = strconvscip-go gomod github.com/golang/go/src . strconv/.Atoiscip-go gomod github.com/golang/go/src . strconv/Atoi().(envMaxWorkFactor) |
| 137 | if err != nil { |
| 138 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("invalid AGE_PASSPHRASE_MAX_WORK_FACTOR: %v", err) |
| 139 | } |
| 140 | if maxWorkFactor > 30 || maxWorkFactor < 1 { |
| 141 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("AGE_PASSPHRASE_MAX_WORK_FACTOR must be between 1 and 30") |
| 142 | } |
| 143 | } |
| 144 | return &batchpassIdentityscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/batchpassIdentity#{passwordscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/batchpassIdentity#password.: pass, maxWorkFactorscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/batchpassIdentity#maxWorkFactor.: maxWorkFactor}, nil |
| 145 | }) |
| 146 | osscip-go gomod github.com/golang/go/src . os/.Exitscip-go gomod github.com/golang/go/src . os/Exit().(p.Mainscip-go gomod filippo.io/age . `filippo.io/age/plugin`/Plugin#Main().()) |
| 147 | } |
| 148 | |
| 149 | type batchpassIdentityscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/batchpassIdentity# struct { |
| 150 | passwordscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/batchpassIdentity#password. string |
| 151 | maxWorkFactorscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/batchpassIdentity#maxWorkFactor. int |
| 152 | } |
| 153 | |
| 154 | func (i *batchpassIdentityscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/batchpassIdentity#) Unwrapscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/batchpassIdentity#Unwrap().(stanzas []*agescip-go gomod filippo.io/age . `filippo.io/age`/.Stanzascip-go gomod filippo.io/age . `filippo.io/age`/Stanza#) ([]byte, error) { |
| 155 | for _, s := range stanzas { |
| 156 | if s.Typescip-go gomod filippo.io/age . `filippo.io/age`/Stanza#Type. == "scrypt" && len(stanzas) != 1 { |
| 157 | return nil, errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("an scrypt recipient must be the only one") |
| 158 | } |
| 159 | } |
| 160 | if len(stanzas) != 1 || stanzas[0].Typescip-go gomod filippo.io/age . `filippo.io/age`/Stanza#Type. != "scrypt" { |
| 161 | // Don't fallback to other identities, this plugin should mostly be used |
| 162 | // in isolation, from the CLI. |
| 163 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("file is not passphrase-encrypted") |
| 164 | } |
| 165 | ii, err := agescip-go gomod filippo.io/age . `filippo.io/age`/.NewScryptIdentityscip-go gomod filippo.io/age . `filippo.io/age`/NewScryptIdentity().(i.passwordscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/batchpassIdentity#password.) |
| 166 | if err != nil { |
| 167 | return nil, err |
| 168 | } |
| 169 | if i.maxWorkFactorscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/batchpassIdentity#maxWorkFactor. != 0 { |
| 170 | ii.SetMaxWorkFactorscip-go gomod filippo.io/age . `filippo.io/age`/ScryptIdentity#SetMaxWorkFactor().(i.maxWorkFactorscip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/batchpassIdentity#maxWorkFactor.) |
| 171 | } |
| 172 | fileKey, err := ii.Unwrapscip-go gomod filippo.io/age . `filippo.io/age`/ScryptIdentity#Unwrap().(stanzas) |
| 173 | if errorsscip-go gomod github.com/golang/go/src . errors/.Isscip-go gomod github.com/golang/go/src . errors/Is().(err, agescip-go gomod filippo.io/age . `filippo.io/age`/.ErrIncorrectIdentityscip-go gomod filippo.io/age . `filippo.io/age`/ErrIncorrectIdentity.) { |
| 174 | // ScryptIdentity returns ErrIncorrectIdentity to make it possible to |
| 175 | // try multiple passphrases from the API. If a user is invoking this |
| 176 | // plugin, it's safe to say they expect it to be the only mechanism to |
| 177 | // decrypt a passphrase-protected file. |
| 178 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("incorrect passphrase") |
| 179 | } |
| 180 | return fileKey, err |
| 181 | } |
| 182 | |
| 183 | func passphrasescip-go gomod filippo.io/age . `filippo.io/age/cmd/age-plugin-batchpass`/passphrase().() (string, error) { |
| 184 | envPASSPHRASE := osscip-go gomod github.com/golang/go/src . os/.Getenvscip-go gomod github.com/golang/go/src . os/Getenv().("AGE_PASSPHRASE") |
| 185 | envFD := osscip-go gomod github.com/golang/go/src . os/.Getenvscip-go gomod github.com/golang/go/src . os/Getenv().("AGE_PASSPHRASE_FD") |
| 186 | if envPASSPHRASE != "" && envFD != "" { |
| 187 | return "", fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("AGE_PASSPHRASE and AGE_PASSPHRASE_FD are mutually exclusive") |
| 188 | } |
| 189 | if envPASSPHRASE == "" && envFD == "" { |
| 190 | return "", fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("either AGE_PASSPHRASE or AGE_PASSPHRASE_FD must be set") |
| 191 | } |
| 192 | |
| 193 | if envPASSPHRASE != "" { |
| 194 | return envPASSPHRASE, nil |
| 195 | } |
| 196 | |
| 197 | fd, err := strconvscip-go gomod github.com/golang/go/src . strconv/.Atoiscip-go gomod github.com/golang/go/src . strconv/Atoi().(envFD) |
| 198 | if err != nil { |
| 199 | return "", fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("invalid AGE_PASSPHRASE_FD: %v", err) |
| 200 | } |
| 201 | f := osscip-go gomod github.com/golang/go/src . os/.NewFilescip-go gomod github.com/golang/go/src . os/NewFile().(uintptr(fd), "AGE_PASSPHRASE_FD") |
| 202 | if f == nil { |
| 203 | return "", fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to open file descriptor %d", fd) |
| 204 | } |
| 205 | defer f.Closescip-go gomod github.com/golang/go/src . os/File#Close().() |
| 206 | const maxPassphraseSize = 1024 * 1024 // 1 MiB |
| 207 | b, err := ioscip-go gomod github.com/golang/go/src . io/.ReadAllscip-go gomod github.com/golang/go/src . io/ReadAll().(ioscip-go gomod github.com/golang/go/src . io/.LimitReaderscip-go gomod github.com/golang/go/src . io/LimitReader().(f, maxPassphraseSize+1)) |
| 208 | if err != nil { |
| 209 | return "", fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to read passphrase from fd %d: %v", fd, err) |
| 210 | } |
| 211 | if len(b) > maxPassphraseSize { |
| 212 | return "", fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("passphrase from fd %d is too long", fd) |
| 213 | } |
| 214 | return stringsscip-go gomod github.com/golang/go/src . strings/.TrimRightscip-go gomod github.com/golang/go/src . strings/TrimRight().(string(b), "\r\n"), nil |
| 215 | } |