-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for ALLOWED_CLIENT_IDS (#9)
* Added support for ALLOWED_CLIENT_IDS * Review feedback * Support for loading secrets from files specified by env vars. * Prevent exceptions during request, correctly set json headers * Log gateway url * Refactored config secret file loading * Last review feedback * Review feedback --------- Co-authored-by: Joost Diepenmaat <[email protected]>
- Loading branch information
Showing
6 changed files
with
230 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
;; This file is part of eduhub-validator-service | ||
;; | ||
;; Copyright (C) 2022 SURFnet B.V. | ||
;; | ||
;; This program is free software: you can redistribute it and/or | ||
;; modify it under the terms of the GNU Affero General Public License | ||
;; as published by the Free Software Foundation, either version 3 of | ||
;; the License, or (at your option) any later version. | ||
;; | ||
;; This program is distributed in the hope that it will be useful, but | ||
;; WITHOUT ANY WARRANTY; without even the implied warranty of | ||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
;; Affero General Public License for more details. | ||
;; | ||
;; You should have received a copy of the GNU Affero General Public | ||
;; License along with this program. If not, see | ||
;; <https://www.gnu.org/licenses/>. | ||
|
||
(ns nl.surf.eduhub.validator.service.config | ||
(:require [clojure.java.io :as io] | ||
[clojure.string :as str] | ||
[nl.jomco.envopts :as envopts])) | ||
|
||
(def opt-specs | ||
{:gateway-url ["URL of gateway" :str | ||
:in [:gateway-url]] | ||
:gateway-basic-auth-user ["Basic auth username of gateway" :str | ||
:in [:gateway-basic-auth :user]] | ||
:gateway-basic-auth-pass ["Basic auth password of gateway" :str | ||
:in [:gateway-basic-auth :pass]] | ||
:allowed-client-ids ["Comma separated list of allowed SurfCONEXT client ids." :str | ||
:in [:allowed-client-ids]] | ||
:surf-conext-client-id ["SurfCONEXT client id for validation service" :str | ||
:in [:introspection-basic-auth :user]] | ||
:surf-conext-client-secret ["SurfCONEXT client secret for validation service" :str | ||
:in [:introspection-basic-auth :pass]] | ||
:surf-conext-introspection-endpoint ["SurfCONEXT introspection endpoint" :str | ||
:in [:introspection-endpoint-url]] | ||
:ooapi-version ["Ooapi version to pass through to gateway" :str | ||
:in [:ooapi-version]]}) | ||
|
||
(defn- file-secret-loader-reducer [env-map value-key] | ||
(let [file-key (keyword (str (name value-key) "-file")) | ||
path (file-key env-map)] | ||
(cond | ||
(nil? path) | ||
env-map | ||
|
||
(not (.exists (io/file path))) | ||
(throw (ex-info (str "ENV var contains filename that does not exist: " path) | ||
{:filename path, :env-path file-key})) | ||
|
||
(value-key env-map) | ||
(throw (ex-info "ENV var contains secret both as file and as value" | ||
{:env-path [value-key file-key]})) | ||
|
||
:else | ||
(assoc env-map value-key (str/trim (slurp path)))))) | ||
|
||
;; These ENV keys may alternatively have a form in which the secret is contained in a file. | ||
;; These ENV keys have a -file suffix, e.g.: gateway-basic-auth-pass-file | ||
(def env-keys-with-alternate-file-secret | ||
[:gateway-basic-auth-user :gateway-basic-auth-pass :surf-conext-client-id :surf-conext-client-secret]) | ||
|
||
(defn load-config-from-env [env-map] | ||
(-> (reduce file-secret-loader-reducer env-map env-keys-with-alternate-file-secret) | ||
(envopts/opts opt-specs))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
;; This file is part of eduhub-validator-service | ||
;; | ||
;; Copyright (C) 2022 SURFnet B.V. | ||
;; | ||
;; This program is free software: you can redistribute it and/or | ||
;; modify it under the terms of the GNU Affero General Public License | ||
;; as published by the Free Software Foundation, either version 3 of | ||
;; the License, or (at your option) any later version. | ||
;; | ||
;; This program is distributed in the hope that it will be useful, but | ||
;; WITHOUT ANY WARRANTY; without even the implied warranty of | ||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
;; Affero General Public License for more details. | ||
;; | ||
;; You should have received a copy of the GNU Affero General Public | ||
;; License along with this program. If not, see | ||
;; <https://www.gnu.org/licenses/>. | ||
|
||
(ns nl.surf.eduhub.validator.service.config-test | ||
(:require [clojure.test :refer [deftest is]] | ||
[nl.surf.eduhub.validator.service.config :as config] | ||
[nl.surf.eduhub.validator.service.main :as main]) | ||
(:import [clojure.lang ExceptionInfo] | ||
[java.io File])) | ||
|
||
(def app (main/wrap-validator main/app-routes {})) | ||
|
||
(def default-env {:allowed-client-ids "default", | ||
:gateway-basic-auth-pass "default", | ||
:gateway-url "default", | ||
:ooapi-version "default", | ||
:surf-conext-client-id "default", | ||
:surf-conext-client-secret "default", | ||
:surf-conext-introspection-endpoint "default"}) | ||
|
||
(def default-expected-value {:allowed-client-ids "default", | ||
:gateway-url "default", | ||
:ooapi-version "default", | ||
:gateway-basic-auth {:pass "default", :user "john200"}, | ||
:introspection-basic-auth {:pass "default", :user "default"}, | ||
:introspection-endpoint-url "default"}) | ||
|
||
(defn- test-env [env] | ||
(config/load-config-from-env (merge default-env env))) | ||
|
||
(deftest missing-secret | ||
(is (= {:gateway-basic-auth-user "missing"} | ||
(last (test-env {}))))) | ||
|
||
(deftest only-value-secret | ||
(let [env {:gateway-basic-auth-user "john200"}] | ||
(is (= [default-expected-value] | ||
(test-env env))))) | ||
|
||
(deftest only-file-secret | ||
(let [path (.getAbsolutePath (File/createTempFile "test-secret" ".txt")) | ||
env {:gateway-basic-auth-user-file path}] | ||
(spit path "john200") | ||
(is (= [default-expected-value] | ||
(test-env env))))) | ||
|
||
(deftest only-file-secret-file-missing | ||
(let [env {:gateway-basic-auth-user-file "missing-file"}] | ||
(is (thrown? ExceptionInfo (test-env env))))) | ||
|
||
(deftest both-types-of-secret-specified | ||
(let [path (.getAbsolutePath (File/createTempFile "test-secret" ".txt")) | ||
env {:gateway-basic-auth-user "john200" | ||
:gateway-basic-auth-user-file path}] | ||
(spit path "john200") | ||
(is (thrown? ExceptionInfo (test-env env))))) |