Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

feat: audit for cloud add on purchase (#63021)

Filipe Silvad6c7c03ebe27
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 6957d58af40..46a1456592b 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -1605,6 +1605,7 @@
{:team "Cloud"
:api #{metabase-enterprise.cloud-add-ons.api}
:uses #{api
+ events
premium-features
util
enterprise/harbormaster}}
diff --git a/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj b/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj
index 39feba5b0b1..4ef628f119d 100644
--- a/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj
@@ -5,6 +5,7 @@
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.api.routes.common :refer [+auth]]
+ [metabase.events.core :as events]
[metabase.premium-features.core :as premium-features]
[metabase.util.i18n :refer [deferred-tru]]
[metabase.util.log :as log]))
@@ -40,10 +41,9 @@
:else
(try
- (hm.client/call :change-add-ons
- :upsert-add-ons [{:product-type product-type}]
- :metabase-user (-> (select-keys @api/*current-user* [:email :id])
- (update-vals str)))
+ (let [add-on {:product-type product-type}]
+ (events/publish-event! :event/cloud-add-on-purchase {:details {:add-on add-on}, :user-id api/*current-user-id*})
+ (hm.client/call :change-add-ons :upsert-add-ons [add-on]))
(premium-features/clear-cache)
{:status 200 :body {}}
(catch Exception e
diff --git a/enterprise/backend/test/metabase_enterprise/cloud_add_ons/api_test.clj b/enterprise/backend/test/metabase_enterprise/cloud_add_ons/api_test.clj
index 5a8548ec2ba..ba528f0998f 100644
--- a/enterprise/backend/test/metabase_enterprise/cloud_add_ons/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/cloud_add_ons/api_test.clj
@@ -35,7 +35,7 @@
{:terms_of_service true})))))
(testing "when all conditions are met"
(mt/with-temp [:model/User user {:is_superuser true}]
- (mt/with-premium-features #{:hosting :offer-metabase-ai}
+ (mt/with-premium-features #{:hosting :offer-metabase-ai :audit-app}
;; FIXME: With `(mt/with-temporary-setting-values [token-status {:store-users [{:email (:email user)}]}])`,
;; `(premium-features/token-status)` still returns `nil`; thus resort to `with-redefs`:
(with-redefs [premium-features/token-status (constantly {:store-users [{:email (:email user)}]})]
@@ -64,4 +64,8 @@
(is (not-empty @store-api-calls)
"Store API was called")
(is (not-empty @clear-token-cache-calls)
- "Token cache was cleared"))))))))))
+ "Token cache was cleared")
+ (testing "audit log entry generated"
+ (let [{:keys [details user_id]} (mt/latest-audit-log-entry "cloud-add-on-purchase")]
+ (is (= (:id user) user_id))
+ (is (= {:add-on {:product-type "metabase-ai"}} details)))))))))))))
diff --git a/src/metabase/audit_app/events/audit_log.clj b/src/metabase/audit_app/events/audit_log.clj
index acd4a8a7dd5..955c02b2d32 100644
--- a/src/metabase/audit_app/events/audit_log.clj
+++ b/src/metabase/audit_app/events/audit_log.clj
@@ -267,3 +267,10 @@
(methodical/defmethod events/publish-event! ::permissions-group-membership-event
[topic event]
(audit-log/record-event! topic event))
+
+(derive ::cloud-add-on-event ::event)
+(derive :event/cloud-add-on-purchase ::cloud-add-on-event)
+
+(methodical/defmethod events/publish-event! ::cloud-add-on-event
+ [topic event]
+ (audit-log/record-event! topic event))