12345678module remote910import rand11import os12131415pub struct Object {16pub:17 found bool18 body []u819 tag string20}2122232425pub interface Backend {26mut:27 get(key string) !Object28 list(prefix string) ![]string29 list_dirs(prefix string) ![]string30 put_if_absent(key string, body []u8) !bool31 put_if_match(key string, body []u8, tag string) !bool32 delete(key string) !33}3435pub const probe_key = 'koh-probe'3637383940pub fn check(mut backend Backend) ! {41 payload := 'koh probe'.bytes()42 43 44 key := '${probe_key}-${rand.hex(16)}'45 if !backend.put_if_absent(key, payload)! {46 return error('the remote refused to store a new object')47 }48 defer {49 50 backend.delete(key) or {}51 }52 if backend.put_if_absent(key, payload)! {53 return error('the remote overwrote an existing object — conditional writes are not working')54 }55 probe := backend.get(key)!56 if !probe.found || probe.body != payload {57 return error('the remote did not give back what was written')58 }59 60 backend.delete(key)!61}626364pub fn tls_roots(url string) !string {65 mut roots := ''66 if url.starts_with('https://') {67 for path in [os.getenv('SSL_CERT_FILE'), '/etc/ssl/cert.pem',68 '/etc/ssl/certs/ca-certificates.crt', '/etc/ssl/certs/ca-bundle.crt',69 '/etc/pki/tls/certs/ca-bundle.crt', '/etc/ssl/ca-bundle.pem'] {70 if path != '' && os.is_file(path) {71 roots = path72 break73 }74 }75 if roots == '' {76 return error('no system CA bundle found — set SSL_CERT_FILE to your trusted CA bundle')77 }78 }79 return roots80}