packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | /// Secrets read from files (`NAME_FILE`), the way container secrets are |
| 2 | /// mounted (ADR 0081): each of the names an app gives comes from its |
| 3 | /// variable or from the file its `_FILE` variable names, never both. One |
| 4 | /// trailing newline is dropped. Read once in `main`: |
| 5 | /// |
| 6 | /// let env = fn name { os.env name } |
| 7 | /// use files <- result.try (secrets.read ["DATABASE_URL", "APP_ROOT_KEY"] env file.read_text) |
| 8 | /// let setting = fn name { secrets.lookup files env name } |
| 9 | /// |
| 10 | /// `lookup` then answers a name from its file when one was read, from the |
| 11 | /// environment otherwise. |
| 12 | |
| 13 | import std/http |
| 14 | import std/list |
| 15 | import std/option |
| 16 | import std/result |
| 17 | import std/string |
| 18 | @test import std/test.{assert_equal} |
| 19 | |
| 20 | /// Each of `names` that has a `_FILE` variable, with the content of the |
| 21 | /// file it names; an error when a name is set both ways or its file |
| 22 | /// cannot be read. |
| 23 | pub fn read :: |
| 24 | names: List<String> |
| 25 | env: fn(String) -> Option<String> / <e> |
| 26 | reader: fn(String) -> Result<String, String> / <e> |
| 27 | -> Result<List<(String, String)>, String> / <e> |
| 28 | { |
| 29 | list.try_map names fn name { from_file env reader name } |
| 30 | |> result.map option.values |
| 31 | } |
| 32 | |
| 33 | pub fn lookup :: |
| 34 | files: List<(String, String)> |
| 35 | env: fn(String) -> Option<String> / <e> |
| 36 | name: String |
| 37 | -> Option<String> / <e> |
| 38 | { |
| 39 | match http.field files name { Some(value) -> Some(value), None -> env name } |
| 40 | } |
| 41 | |
| 42 | fn from_file :: |
| 43 | env: fn(String) -> Option<String> / <e> |
| 44 | reader: fn(String) -> Result<String, String> / <e> |
| 45 | name: String |
| 46 | -> Result<Option<(String, String)>, String> / <e> |
| 47 | { |
| 48 | match (env name, env "{name}_FILE") |
| 49 | { (_, None) -> Ok(None) |
| 50 | , (Some(_), Some(_)) -> Error("{name} and {name}_FILE are both set: keep one") |
| 51 | , (None, Some(path)) -> match reader path |
| 52 | { Ok(content) -> Ok(Some((name, without_newline content))) |
| 53 | , Error(why) -> Error("{name}_FILE: cannot read {path}: {why}") |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn without_newline :: content: String -> String |
| 59 | { |
| 60 | let size = string.length content |
| 61 | if string.ends_with content "\r\n" |
| 62 | { |
| 63 | string.slice content 0 (size - 2) |
| 64 | } |
| 65 | else if string.ends_with content "\n" |
| 66 | { |
| 67 | string.slice content 0 (size - 1) |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | content |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | @test |
| 76 | fn a_secret_is_read_from_its_file_without_one_trailing_newline :: |
| 77 | { |
| 78 | assert_equal |
| 79 | ( resolved |
| 80 | [ ("DATABASE_URL_FILE", "/run/secrets/database-url") |
| 81 | , ("NEWSLETTER_ROOT_KEY_FILE", "/run/secrets/root-key") |
| 82 | , ("NEWSLETTER_PEPPER_FILE", "/run/secrets/pepper") |
| 83 | ] |
| 84 | [ ("/run/secrets/database-url", "postgres://news@db/news\n") |
| 85 | , ("/run/secrets/root-key", "a2V5\r\n") |
| 86 | , ("/run/secrets/pepper", "cGVwcGVy\n\n") |
| 87 | ] |
| 88 | ) |
| 89 | Ok( [ ("DATABASE_URL", "postgres://news@db/news") |
| 90 | , ("NEWSLETTER_ROOT_KEY", "a2V5") |
| 91 | , ("NEWSLETTER_PEPPER", "cGVwcGVy\n") |
| 92 | ] |
| 93 | ) |
| 94 | } |
| 95 | |
| 96 | fn on_disk :: |
| 97 | files: List<(String, String)> |
| 98 | -> fn(String) -> Result<String, String> |
| 99 | { |
| 100 | fn path |
| 101 | { |
| 102 | http.field files path |> option.to_result "No such file or directory" |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | fn resolved :: |
| 107 | pairs: List<(String, String)> |
| 108 | files: List<(String, String)> |
| 109 | -> Result<List<(String, String)>, String> |
| 110 | { |
| 111 | read |
| 112 | [ "DATABASE_URL" |
| 113 | , "NEWSLETTER_ROOT_KEY" |
| 114 | , "NEWSLETTER_PEPPER" |
| 115 | , "SMTP2GO_API_KEY" |
| 116 | ] |
| 117 | (http.field pairs) |
| 118 | (on_disk files) |
| 119 | } |
| 120 | |
| 121 | @test |
| 122 | fn only_the_names_given_are_read_from_files :: |
| 123 | { |
| 124 | assert_equal |
| 125 | ( resolved |
| 126 | [("SMTP2GO_API_KEY_FILE", "/k"), ("NEWSLETTER_PUBLIC_URL_FILE", "/u")] |
| 127 | [("/k", "api-key"), ("/u", "https://news.example")] |
| 128 | ) |
| 129 | Ok([("SMTP2GO_API_KEY", "api-key")]) |
| 130 | } |
| 131 | |
| 132 | @test |
| 133 | fn a_variable_set_both_ways_is_refused :: |
| 134 | { |
| 135 | assert_equal |
| 136 | ( resolved |
| 137 | [ ("NEWSLETTER_PEPPER", "cGVwcGVy") |
| 138 | , ("NEWSLETTER_PEPPER_FILE", "/run/secrets/pepper") |
| 139 | ] |
| 140 | [("/run/secrets/pepper", "cGVwcGVy")] |
| 141 | ) |
| 142 | Error("NEWSLETTER_PEPPER and NEWSLETTER_PEPPER_FILE are both set: keep one") |
| 143 | } |
| 144 | |
| 145 | @test |
| 146 | fn a_file_that_cannot_be_read_is_refused :: |
| 147 | { |
| 148 | assert_equal |
| 149 | (resolved [("NEWSLETTER_ROOT_KEY_FILE", "/run/secrets/gone")] []) |
| 150 | Error( "NEWSLETTER_ROOT_KEY_FILE: cannot read /run/secrets/gone: No such file or directory" |
| 151 | ) |
| 152 | } |
| 153 | |
| 154 | @test |
| 155 | fn a_file_stands_in_for_its_variable_and_nothing_else :: |
| 156 | { |
| 157 | let env = http.field [("DATABASE_URL", "postgres://env"), ("PORT", "8080")] |
| 158 | let files = [("NEWSLETTER_ROOT_KEY", "a2V5")] |
| 159 | assert_equal (lookup files env "NEWSLETTER_ROOT_KEY") Some("a2V5") |
| 160 | assert_equal (lookup files env "DATABASE_URL") Some("postgres://env") |
| 161 | assert_equal (lookup files env "PORT") Some("8080") |
| 162 | assert_equal (lookup files env "NEWSLETTER_PEPPER") None |
| 163 | } |