/// Secrets read from files (`NAME_FILE`), the way container secrets are /// mounted (ADR 0081): each of the names an app gives comes from its /// variable or from the file its `_FILE` variable names, never both. One /// trailing newline is dropped. Read once in `main`: /// /// let env = fn name { os.env name } /// use files <- result.try (secrets.read ["DATABASE_URL", "APP_ROOT_KEY"] env file.read_text) /// let setting = fn name { secrets.lookup files env name } /// /// `lookup` then answers a name from its file when one was read, from the /// environment otherwise. import std/http import std/list import std/option import std/result import std/string @test import std/test.{assert_equal} /// Each of `names` that has a `_FILE` variable, with the content of the /// file it names; an error when a name is set both ways or its file /// cannot be read. pub fn read :: names: List env: fn(String) -> Option / reader: fn(String) -> Result / -> Result, String> / { list.try_map names fn name { from_file env reader name } |> result.map option.values } pub fn lookup :: files: List<(String, String)> env: fn(String) -> Option / name: String -> Option / { match http.field files name { Some(value) -> Some(value), None -> env name } } fn from_file :: env: fn(String) -> Option / reader: fn(String) -> Result / name: String -> Result, String> / { match (env name, env "{name}_FILE") { (_, None) -> Ok(None) , (Some(_), Some(_)) -> Error("{name} and {name}_FILE are both set: keep one") , (None, Some(path)) -> match reader path { Ok(content) -> Ok(Some((name, without_newline content))) , Error(why) -> Error("{name}_FILE: cannot read {path}: {why}") } } } fn without_newline :: content: String -> String { let size = string.length content if string.ends_with content "\r\n" { string.slice content 0 (size - 2) } else if string.ends_with content "\n" { string.slice content 0 (size - 1) } else { content } } @test fn a_secret_is_read_from_its_file_without_one_trailing_newline :: { assert_equal ( resolved [ ("DATABASE_URL_FILE", "/run/secrets/database-url") , ("NEWSLETTER_ROOT_KEY_FILE", "/run/secrets/root-key") , ("NEWSLETTER_PEPPER_FILE", "/run/secrets/pepper") ] [ ("/run/secrets/database-url", "postgres://news@db/news\n") , ("/run/secrets/root-key", "a2V5\r\n") , ("/run/secrets/pepper", "cGVwcGVy\n\n") ] ) Ok( [ ("DATABASE_URL", "postgres://news@db/news") , ("NEWSLETTER_ROOT_KEY", "a2V5") , ("NEWSLETTER_PEPPER", "cGVwcGVy\n") ] ) } fn on_disk :: files: List<(String, String)> -> fn(String) -> Result { fn path { http.field files path |> option.to_result "No such file or directory" } } fn resolved :: pairs: List<(String, String)> files: List<(String, String)> -> Result, String> { read [ "DATABASE_URL" , "NEWSLETTER_ROOT_KEY" , "NEWSLETTER_PEPPER" , "SMTP2GO_API_KEY" ] (http.field pairs) (on_disk files) } @test fn only_the_names_given_are_read_from_files :: { assert_equal ( resolved [("SMTP2GO_API_KEY_FILE", "/k"), ("NEWSLETTER_PUBLIC_URL_FILE", "/u")] [("/k", "api-key"), ("/u", "https://news.example")] ) Ok([("SMTP2GO_API_KEY", "api-key")]) } @test fn a_variable_set_both_ways_is_refused :: { assert_equal ( resolved [ ("NEWSLETTER_PEPPER", "cGVwcGVy") , ("NEWSLETTER_PEPPER_FILE", "/run/secrets/pepper") ] [("/run/secrets/pepper", "cGVwcGVy")] ) Error("NEWSLETTER_PEPPER and NEWSLETTER_PEPPER_FILE are both set: keep one") } @test fn a_file_that_cannot_be_read_is_refused :: { assert_equal (resolved [("NEWSLETTER_ROOT_KEY_FILE", "/run/secrets/gone")] []) Error( "NEWSLETTER_ROOT_KEY_FILE: cannot read /run/secrets/gone: No such file or directory" ) } @test fn a_file_stands_in_for_its_variable_and_nothing_else :: { let env = http.field [("DATABASE_URL", "postgres://env"), ("PORT", "8080")] let files = [("NEWSLETTER_ROOT_KEY", "a2V5")] assert_equal (lookup files env "NEWSLETTER_ROOT_KEY") Some("a2V5") assert_equal (lookup files env "DATABASE_URL") Some("postgres://env") assert_equal (lookup files env "PORT") Some("8080") assert_equal (lookup files env "NEWSLETTER_PEPPER") None }