Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[cmr.common.services.errors :as errors]
[cmr.common.util :as common-util]
[cmr.transmit.config :as transmit-config]
[cmr.transmit.launchpad-user-cache :as launchpad-user-cache]
[cmr.transmit.tokens :as tokens]))

;; TODO - remove legacy token check after legacy token retirement
Expand Down Expand Up @@ -58,6 +59,22 @@
"Write operations are disabled. Both Launchpad SAML and IDFed JWT authentication are turned off."
"You do not have permission to perform that action."))

(defn verify-write-token-with-edl
"Validates the request's JWT with EDL, whatever local-edl-verification says, unless the
edl-validate-write-tokens emergency switch is off. With local verification on, the local
signature and expiry check runs first, so forged or expired JWTs are rejected without asking
EDL. Write handlers call it through validate-write-token or directly. Throws if the token is
rejected."
[request-context]
(let [token (:token request-context)]
(when (and token
(not (transmit-config/echo-system-token? token))
(common-util/is-jwt-token? token)
(transmit-config/edl-validate-write-tokens))
(when (transmit-config/local-edl-verification)
(tokens/verify-edl-token-locally token))
(launchpad-user-cache/get-edl-token-user request-context token))))

(defn validate-write-token
"Validates token is authorized for write operations. Level 4 JWT tokens require NON_NASA_DRAFT_USER ACL."
([request-context]
Expand All @@ -84,7 +101,10 @@
(errors/throw-service-error :unauthorized (build-error-message token-info)))

(when (:requires-draft-acl? token-info)
(acl/verify-non-nasa-draft-permission request-context :update :provider-object provider-id)))))))
(acl/verify-non-nasa-draft-permission request-context :update :provider-object provider-id))))
;; Local checks first, so tokens they reject never cost an EDL call.
(verify-write-token-with-edl request-context)
nil)))

(defn validate-read-token
"Validates token is valid for read operations."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
(ns cmr.common-app.test.launchpad-token-validation-test
(:require
[clojure.test :refer [deftest is testing]]
[clojure.test :refer [deftest is testing use-fixtures]]
[cmr.common-app.api.launchpad-token-validation :as lt-validation]
[cmr.common-app.config :as config]
[cmr.transmit.config :as transmit-config]
[cmr.transmit.launchpad-user-cache :as launchpad-user-cache]
[cmr.transmit.tokens :as tokens]
[cmr.acl.core :as acl]
[cmr.common.util :as util]))

(use-fixtures :each (fn [f]
(with-redefs [launchpad-user-cache/get-edl-token-user (constantly {:uid "edl-user"})
tokens/verify-edl-token-locally (constantly "local-user")]
(f))))

(defn mock-context [token]
{:token token})

Expand Down Expand Up @@ -186,3 +192,37 @@
(is (nil? (lt-validation/validate-write-token
(mock-context "SYSTEM-TOKEN")
"PROV1"))))))

(deftest verify-write-token-with-edl-test
(let [checked (atom nil)]
(with-redefs [util/is-jwt-token? (fn [token] (boolean (re-find #"^JWT-" token)))
tokens/get-jwt-claims mock-get-jwt-claims
transmit-config/local-edl-verification (constantly true)
tokens/verify-edl-token-locally (fn [token]
(or (:uid (mock-get-jwt-claims token))
(throw (ex-info "bad signature" {:type :unauthorized}))))
launchpad-user-cache/get-edl-token-user (fn [_ token] (reset! checked token) {:uid "u1"})]
(testing "JWTs on writes are validated with EDL even with local verification on"
(lt-validation/verify-write-token-with-edl (mock-context "JWT-L5"))
(is (= "JWT-L5" @checked)))
(testing "other tokens are not checked"
(reset! checked nil)
(lt-validation/verify-write-token-with-edl (mock-context "LAUNCHPAD-TOKEN"))
(is (nil? @checked)))
(testing "a JWT that fails the local check is rejected without asking EDL"
(is (thrown? clojure.lang.ExceptionInfo
(lt-validation/verify-write-token-with-edl (mock-context "JWT-FORGED"))))
(is (nil? @checked)))
(testing "the emergency switch turns the check off"
(with-redefs [transmit-config/edl-validate-write-tokens (constantly false)]
(lt-validation/verify-write-token-with-edl (mock-context "JWT-L5"))
(is (nil? @checked))))
(testing "a token that passes the local checks but EDL rejects fails the write"
(with-redefs [tokens/get-jwt-claims mock-get-jwt-claims
config/launchpad-token-enforced (constantly true)
config/enable-idfed-jwt-authentication (constantly true)
config/required-assurance-level (constantly 4)
launchpad-user-cache/get-edl-token-user
(fn [_ _] (throw (ex-info "Token does not exist" {:type :unauthorized})))]
(is (thrown-with-msg? clojure.lang.ExceptionInfo #"Token does not exist"
(lt-validation/validate-write-token (mock-context "JWT-L5") "PROV1"))))))))
4 changes: 3 additions & 1 deletion common-lib/src/cmr/common/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1050,10 +1050,12 @@
Note: Similar code exists at gov.nasa.echo.kernel.service.authentication."
[raw-token]
(let [BEARER "Bearer "
raw-token (string/trim raw-token)
token (if (string/starts-with? raw-token BEARER)
(subs raw-token (count BEARER))
raw-token)]
(if (some? (re-find #"[A-Za-z0-9=_-]+\.[A-Za-z0-9=_-]+\.[:A-Za-z0-9=_-]+" token))
;; Anchored; an unanchored search is quadratic on long tokens.
(if (re-matches #"[A-Za-z0-9=_-]+\.[A-Za-z0-9=_-]+\.[:A-Za-z0-9=_-]+" token)
(let [token-parts (string/split token #"\.")
token-header (first token-parts)
header-raw (try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
false false
true true true
false] util/is-jwt-token?))
(testing "JWT detection matches the whole token"
(is (true? (util/is-jwt-token? (str token-jwt "\n"))))
(is (false? (util/is-jwt-token? (str token-jwt " junk"))))
(is (false? (util/is-jwt-token? (str token-jwt ".x")))))
(testing "Is token a legacy token?"
(test-group [true
true true true true
Expand Down
4 changes: 4 additions & 0 deletions ingest-app/src/cmr/ingest/api/provider.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
[clojure.string :as string]
[cmr.acl.core :as acl]
[cmr.common-app.api.enabled :as common-enabled]
[cmr.common-app.api.launchpad-token-validation :as lt-validation]
[cmr.common.generics :as gcom]
[cmr.common.mime-types :as mt]
[cmr.common.services.errors :as srvc-errors]
Expand Down Expand Up @@ -145,6 +146,7 @@
(POST "/" {:keys [request-context body headers]}
(acl/verify-ingest-management-permission request-context :update)
(common-enabled/validate-write-enabled request-context "ingest")
(lt-validation/verify-write-token-with-edl request-context)
(one-result->response-map
(provider-service/create-provider request-context
(validate-and-prepare-provider-concept
Expand All @@ -171,6 +173,7 @@
headers :headers}
(acl/verify-ingest-management-permission request-context :update)
(common-enabled/validate-write-enabled request-context "ingest")
(lt-validation/verify-write-token-with-edl request-context)
(one-result->response-map
(provider-service/update-provider request-context
(validate-and-prepare-provider-concept
Expand All @@ -182,6 +185,7 @@
headers :headers}
(acl/verify-ingest-management-permission request-context :update)
(common-enabled/validate-write-enabled request-context "ingest")
(lt-validation/verify-write-token-with-edl request-context)
(provider-service/verify-empty-provider request-context provider-id headers)
(one-result->response-map
(provider-service/delete-provider request-context provider-id)))
Expand Down
4 changes: 3 additions & 1 deletion ingest-app/src/cmr/ingest/api/subscriptions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[clojure.string :as string]
[cmr.acl.core :as acl]
[cmr.common-app.api.enabled :as common-enabled]
[cmr.common-app.api.launchpad-token-validation :as lt-validation]
[cmr.common.concepts :as concepts]
[cmr.common.log :refer [info warn]]
[cmr.common.services.errors :as errors]
Expand Down Expand Up @@ -180,7 +181,8 @@
(defn- common-ingest-checks
"Common checks needed before starting to process an ingest operation"
[context]
(common-enabled/validate-write-enabled context "ingest"))
(common-enabled/validate-write-enabled context "ingest")
(lt-validation/verify-write-token-with-edl context))

(defn- verify-coll-modification-permission
"Verifies the current user has been granted permission to modify collection subscription.
Expand Down
3 changes: 3 additions & 0 deletions search-app/src/cmr/search/api/association.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(:require
[cheshire.core :as json]
[cmr.common-app.api.enabled :as common-enabled]
[cmr.common-app.api.launchpad-token-validation :as lt-validation]
[cmr.common.log :refer (info)]
[cmr.common.mime-types :as mt]
[cmr.common.util :as util]
Expand Down Expand Up @@ -65,6 +66,7 @@
provider(s) of the collection(s) in order for the association(s) to be successful."
[context headers body concept-type concept-id]
(common-enabled/validate-write-enabled context "search")
(lt-validation/verify-write-token-with-edl context)
(validate-association-content-type headers)
(info (format "Associate %s [%s] on collections: %s by client: %s."
(name concept-type) concept-id body (:client-id context)))
Expand All @@ -82,6 +84,7 @@
collections in the request body."
[context headers body concept-type concept-id]
(common-enabled/validate-write-enabled context "search")
(lt-validation/verify-write-token-with-edl context)
(validate-association-content-type headers)
(info (format "Dissociating %s [%s] from collections: %s by client: %s."
(name concept-type) concept-id body (:client-id context)))
Expand Down
2 changes: 2 additions & 0 deletions search-app/src/cmr/search/api/community_usage_metrics.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
(:require
[cheshire.core :as json]
[cmr.acl.core :as acl]
[cmr.common-app.api.launchpad-token-validation :as lt-validation]
[cmr.common-app.api.routes :as cr]
[cmr.common.mime-types :as mt]
[cmr.search.services.community-usage-metrics.metrics-service :as metrics-service]
Expand All @@ -21,6 +22,7 @@
"Processes a community usage update request"
[context headers params body]
(acl/verify-ingest-management-permission context :update)
(lt-validation/verify-write-token-with-edl context)
(mt/extract-header-mime-type #{mt/csv} headers "content-type" true)
(let [result (metrics-service/update-community-usage context params body)
status-code (if (= 1 (:revision-id result)) 201 200)]
Expand Down
3 changes: 3 additions & 0 deletions search-app/src/cmr/search/api/generic_association.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(:require
[cheshire.core :as json]
[cmr.common-app.api.enabled :as common-enabled]
[cmr.common-app.api.launchpad-token-validation :as lt-validation]
[cmr.common.concepts :as common-concepts]
[cmr.common.log :refer (info)]
[cmr.common.mime-types :as mt]
Expand Down Expand Up @@ -50,6 +51,7 @@
provider(s) of the concepts in order for the association(s) to be successful."
[context headers body concept-id revision-id]
(common-enabled/validate-write-enabled context "search")
(lt-validation/verify-write-token-with-edl context)
(validate-association-content-type headers)
(info (format "Associate concept [%s] revision [%s] on concepts: %s by client: %s."
concept-id revision-id body (:client-id context)))
Expand All @@ -63,6 +65,7 @@
concepts in the request body."
[context headers body concept-id revision-id]
(common-enabled/validate-write-enabled context "search")
(lt-validation/verify-write-token-with-edl context)
(validate-association-content-type headers)
(info (format "Dissociating concept [%s] revision [%s] from concepts: %s by client: %s."
concept-id revision-id body (:client-id context)))
Expand Down
2 changes: 2 additions & 0 deletions search-app/src/cmr/search/api/humanizer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(:require
[cheshire.core :as json]
[cmr.acl.core :as acl]
[cmr.common-app.api.launchpad-token-validation :as lt-validation]
[cmr.common-app.api.routes :as cr]
[cmr.common.mime-types :as mt]
[cmr.common.util :as util]
Expand All @@ -28,6 +29,7 @@
"Processes a humanizer update request."
[context headers body]
(acl/verify-ingest-management-permission context :update)
(lt-validation/verify-write-token-with-edl context)
(validate-humanizer-content-type headers)
(hv/validate-humanizer-json body)
(let [result (humanizer-service/update-humanizers context body)
Expand Down
8 changes: 8 additions & 0 deletions search-app/src/cmr/search/api/tags.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[clojure.string :as string]
[cmr.acl.core :as acl]
[cmr.common-app.api.enabled :as common-enabled]
[cmr.common-app.api.launchpad-token-validation :as lt-validation]
[cmr.common.log :refer (debug)]
[cmr.search.api.association :as assoc]
[cmr.common.mime-types :as mt]
Expand Down Expand Up @@ -68,6 +69,7 @@
[context headers body]
(verify-tag-modification-permission context :create)
(common-enabled/validate-write-enabled context "search")
(lt-validation/verify-write-token-with-edl context)
(validate-tag-content-type headers)
(let [result (tagging-service/create-tag context body)
status-code (if (= 1 (:revision-id result)) 201 200)]
Expand All @@ -83,13 +85,15 @@
[context headers body tag-key]
(verify-tag-modification-permission context :update)
(common-enabled/validate-write-enabled context "search")
(lt-validation/verify-write-token-with-edl context)
(validate-tag-content-type headers)
(tag-api-response (tagging-service/update-tag context tag-key body)))

(defn delete-tag
"Deletes the tag with the given tag-key."
[context tag-key]
(common-enabled/validate-write-enabled context "search")
(lt-validation/verify-write-token-with-edl context)
(verify-tag-modification-permission context :delete)
(tag-api-response (tagging-service/delete-tag context tag-key)))

Expand All @@ -98,6 +102,7 @@
[context headers body tag-key]
(verify-tag-modification-permission context :update)
(common-enabled/validate-write-enabled context "search")
(lt-validation/verify-write-token-with-edl context)
(validate-tag-content-type headers)
(debug (format "Tagging [%s] on collections: %s by client: %s."
tag-key body (:client-id context)))
Expand All @@ -110,6 +115,7 @@
[context headers body tag-key]
(verify-tag-modification-permission context :update)
(common-enabled/validate-write-enabled context "search")
(lt-validation/verify-write-token-with-edl context)
(validate-tag-content-type headers)
(debug (format "Dissociating tag [%s] from collections: %s by client: %s."
tag-key body (:client-id context)))
Expand All @@ -122,6 +128,7 @@
[context headers body tag-key]
(verify-tag-modification-permission context :update)
(common-enabled/validate-write-enabled context "search")
(lt-validation/verify-write-token-with-edl context)
(validate-tag-content-type headers)
(debug (format "Tagging [%s] on collections by query: %s by client: %s."
tag-key body (:client-id context)))
Expand All @@ -132,6 +139,7 @@
[context headers body tag-key]
(verify-tag-modification-permission context :update)
(common-enabled/validate-write-enabled context "search")
(lt-validation/verify-write-token-with-edl context)
(validate-tag-content-type headers)
(debug (format "Dissociating tag [%s] from collections by query: %s by client: %s."
tag-key body (:client-id context)))
Expand Down
13 changes: 12 additions & 1 deletion transmit-lib/src/cmr/transmit/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,18 @@

(declare local-edl-verification)
(defconfig local-edl-verification
"Controls when cmr uses the EDL public key to locally verify JWT tokens."
"Controls when cmr uses the EDL public key to locally verify JWT tokens. When false, JWTs are
validated with EDL instead, which also catches tokens revoked in EDL. Writes in ingest (including
providers and subscriptions), access control, and search (tags, associations, humanizers,
community usage metrics) validate JWTs with EDL unless edl-validate-write-tokens is off; admin
maintenance routes do not."
{:type Boolean
:default true})

(declare edl-validate-write-tokens)
(defconfig edl-validate-write-tokens
"Emergency switch for the EDL check on writes. When false, JWTs on writes are verified only as
local-edl-verification says, the same as reads."
{:type Boolean
:default true})

Expand Down
Loading
Loading