-
Notifications
You must be signed in to change notification settings - Fork 1
/
providers.clj
106 lines (84 loc) · 3.21 KB
/
providers.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
(ns tech.io.providers
(:require [tech.config.core :as config]
[com.stuartsierra.component :as c]
[tech.io.protocols :as io-prot]
[tech.io.base :as base]))
(defn caching-provider
[cache-dir options]
(require 'tech.io.cache)
((resolve 'tech.io.cache/create-file-cache)
(or cache-dir (config/get-config :tech-io-cache-dir))
options))
(defn redirect-provider
[redirect-dir options]
(require 'tech.io.redirect)
((resolve 'tech.io.redirect/create-file-provider)
(or redirect-dir (config/get-config :tech-io-redirect-dir))
options))
(def ^:dynamic ^:private *default-vault-auth-providers*
(atom {}))
(defn vault-auth-provider
[vault-path options]
(require 'tech.io.auth)
(let [map-key [vault-path options]
providers @*default-vault-auth-providers*]
(if-let [provider (get providers map-key)]
provider
(do
;;only allow this once, nil->provider only valid transition
(compare-and-set! *default-vault-auth-providers*
providers
(assoc providers map-key
((resolve 'tech.io.auth/vault-aws-auth-provider)
(or vault-path (config/get-config :tech-vault-aws-path))
options)))
;;start is idempotent
(swap! *default-vault-auth-providers* update map-key c/start)
(recur vault-path options)))))
(defn shutdown-providers!
[]
(let [providers @*default-vault-auth-providers*]
(compare-and-set! *default-vault-auth-providers* providers {})
(->> providers
(map (comp c/stop second))
dorun)
:ok))
(defn wrap-provider
"Works like middlewear. But the providers have to implement it themselves; most
general purpose (cache, redirect, auth) implement it via their src-provider member."
([prov-inner prov-outer]
(assoc prov-outer
:src-provider prov-inner))
([] nil))
(defn provider-seq->wrapped-providers
"Given a sequence of providers, wrap them such that the first
provider is the outer provider. This means that data will travel
through the sequence in a left-to-right or top-to-bottom order.
Returns the outer provider or nil of seq is empty"
[provider-seq]
(->> provider-seq
reverse
(reduce wrap-provider)))
(def default-provider
(memoize
(fn []
(->> [[:tech-io-vault-auth #(vault-auth-provider nil {})]
[:tech-io-cache-local #(caching-provider nil {})]
[:tech-io-redirect-local #(redirect-provider nil {})]]
(map (fn [[config-key provider-fn]]
(when (config/get-config config-key)
(provider-fn))))
(remove nil?)
provider-seq->wrapped-providers))))
(defmethod io-prot/url-parts->provider :s3
[& args]
(require 'tech.io.s3)
((resolve 'tech.io.s3/s3-provider) (if-not (empty? (config/get-config :tech-aws-endpoint))
{:tech.aws/endpoint (config/get-config :tech-aws-endpoint)}
{})))
(defmethod io-prot/url-parts->provider :default
[& args]
(Object.))
(defmethod io-prot/url-parts->provider :file
[url-parts]
(base/parts->file url-parts))