Skip to content

(TK-497) Allow requiring authorization via config setting #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions documentation/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ metrics: {
# Default is true.
enabled: false

# Enable or disable authorization for metrics endpoint
# Default is true.
require-auth: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meant to be nested under the jolokia section?

Copy link
Author

@stelcodes stelcodes Oct 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice catch! Would it actually make more sense to nest it under jolokia?


# Configure any of the settings listed at:
# https://jolokia.org/reference/html/agents.html#war-agent-installation
servlet-init-params: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@
{:authorized true :message ""}))]
(if auth-check-fn
(log/info "Metrics access control using trapperkeeper-authorization is enabled.")
(log/warn "Metrics access control using trapperkeeper-authorization is disabled. Add the authorization service to the trapperkeeper bootstrap configuration file to enable it."))
(log/warn (str "Metrics access control using trapperkeeper-authorization is disabled. "
"To enable it, add the authorization service to the trapperkeeper bootstrap "
"configuration file and set the trapperkeeper-metrics config setting "
"metrics.metrics-webservice.jolokia.require-auth to true (default).")))
(proxy [AgentServlet] []
;; NOTE: An alternative to this method override would be to use defrecord
;; to create a class that can be set as `:logHandlerClass` in the servlet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

(def JolokiaApiConfig
{(schema/optional-key :enabled) schema/Bool
(schema/optional-key :require-auth) schema/Bool
(schema/optional-key :servlet-init-params) jolokia/JolokiaConfig})

(def MbeansApiConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,21 @@
options (if (nil? server)
{:servlet-init-params config}
{:servlet-init-params config :server-id (keyword server)})
auth-service (tk-services/maybe-get-service this :AuthorizationService)
auth-check-fn (if auth-service (partial tk-auth/authorization-check auth-service))]
(add-servlet-handler
(jolokia/create-servlet auth-check-fn)
route
options)))
require-auth? (get-in-config [:metrics :metrics-webservice :jolokia :require-auth] true)]
(if-not require-auth?
(add-servlet-handler (jolokia/create-servlet nil) route options)
(try
(let [auth-service (tk-services/get-service this :AuthorizationService)
auth-check-fn (partial tk-auth/authorization-check auth-service)
servlet (jolokia/create-servlet auth-check-fn)]
(add-servlet-handler servlet route options))
;; Fail fast when auth is required but the auth service could not load
(catch IllegalArgumentException e
(log/error "metrics.metrics-webservice.jolokia.require-auth set to true (default)"
"but trapperkeeper-authorization service was not found. To disable"
"authorization change this setting to false.")
(throw e))))))


context)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

(def metrics-service-config
{:metrics {:server-id "localhost"
:metrics-webservice {:jolokia {:require-auth false}}
:registries {:pl.test.reg {:reporters {:jmx {:enabled true}}}
:pl.other.reg {:reporters {:jmx {:enabled true}}}}}
:webserver {:port 8180
Expand Down Expand Up @@ -226,15 +227,31 @@
(is (= 403 (get body "status")))))))))

(deftest metrics-service-with-tk-auth
(testing "tk-auth works when included in bootstrap"
(with-app-with-config
app
(conj services authorization-service/authorization-service)
(merge metrics-service-config auth-config ssl-webserver-config)
(let [resp (http-client/get "https://localhost:8180/metrics/v2/list" ssl-opts)]
(is (= 200 (:status resp))))
(let [resp (http-client/get "https://localhost:8180/metrics/v2" ssl-opts)]
(is (= 403 (:status resp)))))))
(let [config-with-require-auth
(-> (merge metrics-service-config auth-config ssl-webserver-config)
(assoc-in [:metrics :metrics-webservice :jolokia :require-auth] true))]

(testing "tk-auth works when included in bootstrap"
(with-app-with-config
app
(conj services authorization-service/authorization-service)
;; This test requires authentication so we need to override the require-auth
;; option that is set to false in metrics-service-config
config-with-require-auth
(let [resp (http-client/get "https://localhost:8180/metrics/v2/list" ssl-opts)]
(is (= 200 (:status resp))))
(let [resp (http-client/get "https://localhost:8180/metrics/v2" ssl-opts)]
(is (= 403 (:status resp))))))

(testing "exception occurs when require-auth option is true but auth service was not found"
;; This test is like the previous, except the auth service is not listed as a dependency
(is (thrown-with-msg? IllegalArgumentException
#"service ':AuthorizationService' does not exist"
(with-app-with-config
app
;; The auth service is purposefully left out here
services
config-with-require-auth))))))

(deftest metrics-v1-endpoint-disabled-by-default
(testing "metrics/v1 is disabled by default, returns 404"
Expand Down