Skip to content

Commit 985b20b

Browse files
committed
CMR-11116: pr comments
1 parent 5761448 commit 985b20b

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

system-int-test/test/cmr/system_int_test/misc/launchpad_user_cache.clj

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
[cmr.system-int-test.utils.ingest-util :as ingest]
1313
[cmr.system-int-test.utils.metadata-db-util :as mdb]
1414
[cmr.system-int-test.utils.url-helper :as url]
15-
[cmr.transmit.config :as transmit-config]))
15+
[cmr.transmit.config :as transmit-config])
16+
(:import
17+
(org.apache.commons.codec.digest DigestUtils)))
1618

1719
(use-fixtures :each (join-fixtures
1820
[(ingest/reset-fixture {"provguid1" "PROV1" "provguid2" "PROV2"})
@@ -21,7 +23,7 @@
2123
(deftest launchpad-user-cache-test
2224
(testing "launchpad cache initial value"
2325
(let [launchpad-token (echo-util/login-with-launchpad-token (system/context) "user1")
24-
token-key (hash launchpad-token)]
26+
token-key (keyword (DigestUtils/sha256Hex launchpad-token))]
2527
(is (empty? (cache-util/list-cache-keys (url/ingest-read-caches-url) "launchpad-user" transmit-config/mock-echo-system-token)))
2628
(let [concept (data-umm-c/collection-concept {})
2729
{:keys [concept-id revision-id]} (ingest/ingest-concept concept {:token launchpad-token})]
@@ -84,27 +86,42 @@
8486
(testing "Transient errors (429, 504) are not cached"
8587
(let [resp (ingest/ingest-concept (data-umm-c/collection-concept {}) {:token "ABC-429-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"})]
8688
(is (= 429 (:status resp)))
87-
(is (some #(re-find #"Rate limit exceeded" %) (:errors resp))))
89+
(is (some #(re-find #"Launchpad rate limit exceeded" %) (:errors resp))))
8890

8991
(is (empty? (cache-util/list-cache-keys (url/ingest-read-caches-url) "launchpad-user" transmit-config/mock-echo-system-token))))
9092

9193
(testing "Gateway timeout errors (504) are not cached"
9294
(let [resp (ingest/ingest-concept (data-umm-c/collection-concept {}) {:token "ABC-504-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"})]
9395
(is (= 504 (:status resp)))
94-
(is (some #(re-find #"Gateway timeout" %) (:errors resp))))
96+
(is (some #(re-find #"\(gateway timeout\)" %) (:errors resp))))
9597

9698
(is (empty? (cache-util/list-cache-keys (url/ingest-read-caches-url) "launchpad-user" transmit-config/mock-echo-system-token)))))
9799

98100
(deftest non-transient-errors-are-cached-test
99101
(testing "Non-transient errors are cached for 5 minutes"
100102
(let [token "ABC-INV-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
101-
token-key (hash token)]
103+
token-key (keyword (DigestUtils/sha256Hex token))]
102104
(let [resp (ingest/ingest-concept (data-umm-c/collection-concept {}) {:token token})]
103105
(is (= 401 (:status resp))))
104106

107+
;; Verify the error is cached
105108
(is (seq (cache-util/list-cache-keys (url/ingest-read-caches-url) "launchpad-user" transmit-config/mock-echo-system-token)))
109+
(let [cached-value (cache-util/get-cache-value
110+
(url/ingest-read-caches-url)
111+
"launchpad-user"
112+
token-key
113+
transmit-config/mock-echo-system-token
114+
200)]
115+
(is (false? (:valid cached-value)))
116+
(is (:error-message cached-value))
117+
(is (:expiration-time cached-value)))
106118

119+
;; Advance past the 5 minute cache expiration
107120
(dev-sys-util/advance-time! 301)
108121

122+
;; After expiration, revalidation fails and cache is evicted (not re-cached)
109123
(let [resp (ingest/ingest-concept (data-umm-c/collection-concept {}) {:token token})]
110-
(is (= 401 (:status resp)))))))
124+
(is (= 401 (:status resp))))
125+
126+
;; Verify the cache is now empty after failed revalidation
127+
(is (empty? (cache-util/list-cache-keys (url/ingest-read-caches-url) "launchpad-user" transmit-config/mock-echo-system-token))))))

transmit-lib/src/cmr/transmit/launchpad_user_cache.clj

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
[cmr.common.time-keeper :as time-keeper]
1010
[cmr.common.util :as common-util]
1111
[cmr.transmit.config :as transmit-config]
12-
[cmr.transmit.urs :as urs]))
12+
[cmr.transmit.urs :as urs])
13+
(:import
14+
(org.apache.commons.codec.digest DigestUtils)))
1315

1416
(def launchpad-user-cache-key
1517
"The cache key for a launchpad token cache."
@@ -53,7 +55,7 @@
5355
:error-type error-type
5456
:expiration-time (t/plus (time-keeper/now) (t/minutes (transmit-config/invalid-token-timeout)))})))))]
5557
(if-let [cache (cache/context->cache context launchpad-user-cache-key)]
56-
(let [cache-key (keyword (str (hash token)))
58+
(let [cache-key (keyword (DigestUtils/sha256Hex token))
5759
token-info (cache/get-value cache cache-key get-launchpad-user-fn)]
5860
(if (t/before? (:expiration-time token-info) (time-keeper/now))
5961
;; Cache entry expired (after 5 min for errors, or token lifetime for valid tokens)
@@ -65,13 +67,11 @@
6567
(do
6668
(cache/set-value cache cache-key fresh-result)
6769
fresh-result)
68-
(do
69-
(cache/set-value cache cache-key fresh-result)
70-
(errors/throw-service-error
71-
(or (:error-type fresh-result) :unauthorized)
72-
(or (:error-message fresh-result)
73-
(format "Invalid Launchpad token (partially redacted) [%s]"
74-
(common-util/scrub-token token))))))))
70+
(errors/throw-service-error
71+
(or (:error-type fresh-result) :unauthorized)
72+
(or (:error-message fresh-result)
73+
(format "Invalid Launchpad token (partially redacted) [%s]"
74+
(common-util/scrub-token token)))))))
7575
;; Cache entry still valid - return cached result or throw cached error
7676
(if (:valid token-info)
7777
token-info

0 commit comments

Comments
 (0)