-
Notifications
You must be signed in to change notification settings - Fork 98
/
signers.clj
172 lines (151 loc) · 6.73 KB
/
signers.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
;; Copyright (c) Cognitect, Inc.
;; All rights reserved.
(ns ^:skip-wiki cognitect.aws.signers
"Impl, don't call directly."
(:require [clojure.string :as str]
[cognitect.aws.client :as client]
[cognitect.aws.service :as service]
[cognitect.aws.util :as util])
(:import [java.net URI]
[java.net URLDecoder]))
(set! *warn-on-reflection* true)
(defn uri-encode
"Escape (%XX) special characters in the string `s`.
Letters, digits, and the characters `_-~.` are never encoded.
The optional string `safe` specifies extra characters to not encode."
[^String s & [safe]]
(when s
(let [safe-chars (->> [\_ \- \~ \.]
(concat (set safe))
(map byte)
set)
builder (StringBuilder.)]
(doseq [b (.getBytes s "UTF-8")]
(.append builder
(if (or (<= (byte \A) b (byte \Z))
(<= (byte \a) b (byte \z))
(<= (byte \0) b (byte \9))
(contains? safe-chars b))
(char b)
(format "%%%02X" b))))
(.toString builder))))
(defn credential-scope
[{:keys [region service] :as auth-info} request]
(str/join "/" [(->> (get-in request [:headers "x-amz-date"])
(util/parse-date util/x-amz-date-format)
(util/format-date util/x-amz-date-only-format))
region
service
"aws4_request"]))
(defn- canonical-method
[{:keys [request-method]}]
(-> request-method name str/upper-case))
(defn- canonical-uri
[{:keys [uri]}]
(let [encoded-path (-> uri
(str/replace #"//+" "/") ; (URI.) throws Exception on '//'.
(str/replace #"\s" "%20"); (URI.) throws Exception on space.
(URI.)
(.normalize)
(.getPath)
(uri-encode "/"))]
(if (.isEmpty ^String encoded-path)
"/"
encoded-path)))
(defn- canonical-query-string
[{:keys [uri query-string]}]
(let [qs (or query-string (second (str/split uri #"\?")))]
(when-not (str/blank? qs)
(->> (str/split qs #"&")
(map #(str/split % #"=" 2))
;; TODO (dchelimsky 2019-01-30) decoding first because sometimes
;; it's already been encoding. Look into avoiding that!
(map (fn [kv] (map #(uri-encode (URLDecoder/decode %)) kv)))
(sort (fn [[k1 v1] [k2 v2]]
(if (= k1 k2)
(compare v1 v2)
(compare k1 k2))))
(map (fn [[k v]] (str k "=" v)))
(str/join "&")))))
(defn- canonical-headers
[{:keys [headers]}]
(reduce-kv (fn [m k v]
(assoc m (str/lower-case k) (-> v str/trim (str/replace #"\s+" " "))))
(sorted-map)
headers))
(defn- canonical-headers-string
[request]
(->> (canonical-headers request)
(map (fn [[k v]] (str k ":" v "\n")))
(str/join "")))
(defn signed-headers
[request]
(->> (canonical-headers request)
keys
(str/join ";")))
(defn hash-payload
[{:keys [body] :as request}]
(util/hex-encode (util/sha-256 body)))
(defn canonical-request
[{:keys [headers body content-length] :as request}]
(str/join "\n" [(canonical-method request)
(canonical-uri request)
(canonical-query-string request)
(canonical-headers-string request)
(signed-headers request)
(or (get headers "x-amz-content-sha256")
(hash-payload request))]))
(defn string-to-sign
[request auth-info]
(let [bytes (.getBytes ^String (canonical-request request))]
(str/join "\n" ["AWS4-HMAC-SHA256"
(get-in request [:headers "x-amz-date"])
(credential-scope auth-info request)
(util/hex-encode (util/sha-256 bytes))])))
(defn signing-key
[request {:keys [secret-access-key region service] :as auth-info}]
(-> (.getBytes (str "AWS4" secret-access-key) "UTF-8")
(util/hmac-sha-256 (->> (get-in request [:headers "x-amz-date"])
(util/parse-date util/x-amz-date-format)
(util/format-date util/x-amz-date-only-format)))
(util/hmac-sha-256 region)
(util/hmac-sha-256 service)
(util/hmac-sha-256 "aws4_request")))
(defn signature
[auth-info request]
(util/hex-encode
(util/hmac-sha-256 (signing-key request auth-info)
(string-to-sign request auth-info))))
(defn v4-sign-http-request
[service region credentials http-request & {:keys [content-sha256?]}]
(let [{:keys [:aws/access-key-id :aws/secret-access-key :aws/session-token]} credentials
auth-info {:access-key-id access-key-id
:secret-access-key secret-access-key
:service (or (service/signing-name service)
(service/endpoint-prefix service))
:region (if (and (-> service :metadata :globalEndpoint)
(not= "s3" (service/service-name service)))
"us-east-1"
(name region))}
req-with-token (-> http-request
(update :headers
#(cond-> % session-token (assoc "x-amz-security-token" session-token))))]
(-> req-with-token
(update :headers
#(cond-> %
content-sha256? (assoc "x-amz-content-sha256" (hash-payload req-with-token))
:always (assoc "authorization"
(format "AWS4-HMAC-SHA256 Credential=%s/%s, SignedHeaders=%s, Signature=%s"
(:access-key-id auth-info)
(credential-scope auth-info req-with-token)
(signed-headers req-with-token)
(signature auth-info req-with-token))))))))
(defmethod client/sign-http-request "v4"
[service region credentials http-request]
(v4-sign-http-request service region credentials http-request))
(defmethod client/sign-http-request "s3"
[service region credentials http-request]
(v4-sign-http-request service region credentials http-request :content-sha256? true))
(defmethod client/sign-http-request "s3v4"
[service region credentials http-request]
(v4-sign-http-request service region credentials http-request :content-sha256? true))