The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Exclude data app permission groups from SCIM (#82416)
Timofey Kachalov8d5cb13553af
diff --git a/enterprise/backend/src/metabase_enterprise/scim/v2/api.clj b/enterprise/backend/src/metabase_enterprise/scim/v2/api.cljindex 0eb91439a33..47808e914e5 100644--- a/enterprise/backend/src/metabase_enterprise/scim/v2/api.clj+++ b/enterprise/backend/src/metabase_enterprise/scim/v2/api.clj@@ -144,14 +144,20 @@;;; | User operations |;;; +----------------------------------------------------------------------------------------------------------------++(defn- hidden-group-ids+ "IDs of the groups SCIM never exposes or manages: the static Administrators and All Users groups, and the groups+ data apps own (see [[perms/data-app-group-ids]])."+ []+ (into [(:id (perms/all-users-group)) (:id (perms/admin-group))]+ (perms/data-app-group-ids)))+(mi/define-batched-hydration-method add-scim-user-group-memberships:scim_user_group_memberships"Add to each `user` a list of :user_group_memberships where each item is a map with 2 keys [:name :entity_id]."[users](when (seq users)(let [user-id->memberships (group-by :user_id (scim.db/user-group-memberships (map u/the-id users)- [(:id (perms/all-users-group))- (:id (perms/admin-group))]))+ (hidden-group-ids)))membership->group (fn [membership] (select-keys membership [:name :entity_id]))](for [user users](assoc user :user_group_memberships (->> (user-id->memberships (u/the-id user))@@ -372,9 +378,9 @@(mu/defn ^:private get-group-by-entity-id"Fetches a group by entity ID, or throws a 404. Cannot fetch the Administrators or All Users groups, as these are- static and cannot be managed via SCIM."+ static, nor data-app groups, as Metabase manages their membership itself, so none can be managed via SCIM."[entity-id]- (or (scim.db/scim-group-by-entity-id entity-id [(:id (perms/all-users-group)) (:id (perms/admin-group))])+ (or (scim.db/scim-group-by-entity-id entity-id (hidden-group-ids))(throw-scim-error 404 "Group not found")))(mu/defn ^:private mb-group->scim :- SCIMGroup@@ -417,7 +423,7 @@;; SCIM start-index is 1-indexed, so we need to decrement it hereoffset (if start-index (dec start-index) default-pagination-offset)filter-param (when filter-param (codec/url-decode filter-param))- excluded-ids [(:id perms/all-users-group) (:id perms/admin-group)]+ excluded-ids (hidden-group-ids)group-name (when filter-param (group-filter-name filter-param))groups (scim.db/scim-groups excluded-ids group-name limit offset)results-count (count groups)diff --git a/enterprise/backend/test/metabase_enterprise/scim/v2/api_test.clj b/enterprise/backend/test/metabase_enterprise/scim/v2/api_test.cljindex cbfd870738e..d4c1d0e7bd0 100644--- a/enterprise/backend/test/metabase_enterprise/scim/v2/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/scim/v2/api_test.clj@@ -103,6 +103,15 @@:display "Test Group"}]:meta {:resourceType "User"}}response)))))+ (testing "Data-app group memberships are not exposed"+ (mt/with-temp [:model/User user {:email "[email protected]"}+ :model/PermissionsGroup group {:name "Test Group"}+ :model/PermissionsGroup app-group {:name "App Group" :is_data_app_group true}+ :model/PermissionsGroupMembership _ {:user_id (:id user) :group_id (:id group)}+ :model/PermissionsGroupMembership _ {:user_id (:id user) :group_id (:id app-group)}]+ (let [entity-id (t2/select-one-fn :entity_id :model/User :id (:id user))+ response (scim-client :get 200 (format "ee/scim/v2/Users/%s" entity-id))]+ (is (= ["Test Group"] (map :display (:groups response)))))))(testing "404 is returned when fetching a non-existent user"(scim-client :get 404 (format "ee/scim/v2/Users/%s" (random-uuid))))))@@ -333,58 +342,74 @@:value {"name.familyName" "NonexistentUser"}}]}response (scim-client :patch 404 (format "ee/scim/v2/Users/%s" (random-uuid)) patch-body)](is (= ["urn:ietf:params:scim:api:messages:2.0:Error"] (get response :schemas)))- (is (= "User not found" (get response :detail)))))- (deftest list-groups-test- (with-scim-setup!- (mt/with-temp [:model/PermissionsGroup _group1 {:name "Group 1"}]- (testing "Fetch groups with default pagination"- (let [response (scim-client :get 200 "ee/scim/v2/Groups")]- (is (malli= scim-api/SCIMGroupList response))))- (testing "Fetch groups with custom pagination"- (let [response (scim-client :get 200 (format "ee/scim/v2/Groups?startIndex=%d&count=%d" 1 2))]- (is (= ["urn:ietf:params:scim:api:messages:2.0:ListResponse"] (get response :schemas)))- (is (integer? (get response :totalResults)))- (is (= 1 (get response :startIndex)))- (is (= 2 (get response :itemsPerPage)))- (is (= 2 (count (get response :Resources))))))- (testing "Fetch group by name"- (let [response (scim-client :get 200 (format "ee/scim/v2/Groups?filter=%s"- (codec/url-encode "displayName eq \"Group 1\"")))]- (is (malli= scim-api/SCIMGroupList response))- (is (= 1 (get response :totalResults)))- (is (= 1 (count (get response :Resources))))))- (testing "Fetch non-existent group by name"- (let [response (scim-client :get 200 (format "ee/scim/v2/Groups?filter=%s"- (codec/url-encode "displayName eq \"Fake Group\"")))]- (is (malli= scim-api/SCIMUserList response))- (is (= 0 (get response :totalResults)))- (is (= 0 (count (get response :Resources))))))- (testing "Error if unsupported filter operation is provided"- (scim-client :get 400 (format "ee/scim/v2/Users?filter=%s"- (codec/url-encode "displayName ne \"Group 1\"")))))))))- (deftest fetch-group-test- (with-scim-setup!- (testing "A single group can be fetched in the SCIM format by entity ID with its members"- (mt/with-temp [:model/PermissionsGroup group {:name "Test Group"}- :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id (:id group)}]- (let [entity-id (t2/select-one-fn :entity_id :model/PermissionsGroup :id (:id group))- response (scim-client :get 200 (format "ee/scim/v2/Groups/%s" entity-id))]- (is (malli= scim-api/SCIMGroup response))- (is (=?- {:schemas ["urn:ietf:params:scim:schemas:core:2.0:Group"]- :id entity-id- :displayName "Test Group"- :members [{:value (t2/select-one-fn :entity_id :model/User (mt/user->id :rasta))- :display "[email protected]"}]- :meta {:resourceType "Group"}}- response)))))- (testing "404 is returned when fetching a non-existent group"- (scim-client :get 404 (format "ee/scim/v2/Groups/%s" (random-uuid))))- (testing "404 is returned when fetching the Admin or All Users group"- (let [entity-ids (t2/select-fn-set :entity_id :model/PermissionsGroup- {:where [:in :id #{(:id (perms-group/admin)) (:id (perms-group/all-users))}]})]- (doseq [entity-id entity-ids]- (scim-client :get 404 (format "ee/scim/v2/Groups/%s" entity-id)))))))))+ (is (= "User not found" (get response :detail)))))))))++(deftest list-groups-test+ (with-scim-setup!+ (mt/with-temp [:model/PermissionsGroup _group1 {:name "Group 1"}]+ (testing "Fetch groups with default pagination"+ (let [response (scim-client :get 200 "ee/scim/v2/Groups")]+ (is (malli= scim-api/SCIMGroupList response))))+ (testing "Fetch groups with custom pagination"+ (let [response (scim-client :get 200 (format "ee/scim/v2/Groups?startIndex=%d&count=%d" 1 2))]+ (is (= ["urn:ietf:params:scim:api:messages:2.0:ListResponse"] (get response :schemas)))+ (is (integer? (get response :totalResults)))+ (is (= 1 (get response :startIndex)))+ (is (= 2 (get response :itemsPerPage)))+ (is (= 2 (count (get response :Resources))))))+ (testing "Fetch group by name"+ (let [response (scim-client :get 200 (format "ee/scim/v2/Groups?filter=%s"+ (codec/url-encode "displayName eq \"Group 1\"")))]+ (is (malli= scim-api/SCIMGroupList response))+ (is (= 1 (get response :totalResults)))+ (is (= 1 (count (get response :Resources))))))+ (testing "Fetch non-existent group by name"+ (let [response (scim-client :get 200 (format "ee/scim/v2/Groups?filter=%s"+ (codec/url-encode "displayName eq \"Fake Group\"")))]+ (is (malli= scim-api/SCIMUserList response))+ (is (= 0 (get response :totalResults)))+ (is (= 0 (count (get response :Resources))))))+ (testing "Error if unsupported filter operation is provided"+ (scim-client :get 400 (format "ee/scim/v2/Users?filter=%s"+ (codec/url-encode "displayName ne \"Group 1\""))))+ (testing "Data-app groups are not listed"+ (mt/with-temp [:model/PermissionsGroup app-group {:name "App Group" :is_data_app_group true}]+ (let [app-entity-id (t2/select-one-fn :entity_id :model/PermissionsGroup :id (:id app-group))+ response (scim-client :get 200 "ee/scim/v2/Groups")]+ (is (not (contains? (into #{} (map :id) (:Resources response)) app-entity-id)))+ (is (= (count (:Resources response)) (:totalResults response))))+ (let [response (scim-client :get 200 (format "ee/scim/v2/Groups?filter=%s"+ (codec/url-encode "displayName eq \"App Group\"")))]+ (is (= 0 (get response :totalResults)))+ (is (= 0 (count (get response :Resources))))))))))++(deftest fetch-group-test+ (with-scim-setup!+ (testing "A single group can be fetched in the SCIM format by entity ID with its members"+ (mt/with-temp [:model/PermissionsGroup group {:name "Test Group"}+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id (:id group)}]+ (let [entity-id (t2/select-one-fn :entity_id :model/PermissionsGroup :id (:id group))+ response (scim-client :get 200 (format "ee/scim/v2/Groups/%s" entity-id))]+ (is (malli= scim-api/SCIMGroup response))+ (is (=?+ {:schemas ["urn:ietf:params:scim:schemas:core:2.0:Group"]+ :id entity-id+ :displayName "Test Group"+ :members [{:value (t2/select-one-fn :entity_id :model/User (mt/user->id :rasta))+ :display "[email protected]"}]+ :meta {:resourceType "Group"}}+ response)))))+ (testing "404 is returned when fetching a non-existent group"+ (scim-client :get 404 (format "ee/scim/v2/Groups/%s" (random-uuid))))+ (testing "404 is returned when fetching the Admin or All Users group"+ (let [entity-ids (t2/select-fn-set :entity_id :model/PermissionsGroup+ {:where [:in :id #{(:id (perms-group/admin)) (:id (perms-group/all-users))}]})]+ (doseq [entity-id entity-ids]+ (scim-client :get 404 (format "ee/scim/v2/Groups/%s" entity-id)))))+ (testing "404 is returned when fetching a data-app group"+ (mt/with-temp [:model/PermissionsGroup app-group {:name "App Group" :is_data_app_group true}]+ (let [entity-id (t2/select-one-fn :entity_id :model/PermissionsGroup :id (:id app-group))]+ (scim-client :get 404 (format "ee/scim/v2/Groups/%s" entity-id)))))))(deftest create-group-test(with-scim-setup!@@ -418,7 +443,19 @@(t2/hydrate :members))](is (= new-group-name (:name group)))(is (= 1 (count (:members group))))- (is (= (mt/user->id :crowberto) (-> group :members first :user_id)))))))))+ (is (= (mt/user->id :crowberto) (-> group :members first :user_id)))))))+ (testing "404 is returned when trying to update a data-app group, and its membership is left alone"+ (mt/with-temp [:model/PermissionsGroup app-group {:name "App Group" :is_data_app_group true}+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id (:id app-group)}]+ (let [entity-id (t2/select-one-fn :entity_id :model/PermissionsGroup :id (:id app-group))+ group-update {:schemas ["urn:ietf:params:scim:schemas:core:2.0:Group"]+ :id entity-id+ :displayName "Renamed App Group"+ :members [{:value (t2/select-one-fn :entity_id :model/User :id (mt/user->id :crowberto))}]}]+ (scim-client :put 404 (format "ee/scim/v2/Groups/%s" entity-id) group-update)+ (is (= "App Group" (t2/select-one-fn :name :model/PermissionsGroup :id (:id app-group))))+ (is (= #{(mt/user->id :rasta)}+ (t2/select-fn-set :user_id :model/PermissionsGroupMembership :group_id (:id app-group)))))))))(deftest delete-group-test(with-scim-setup!@@ -435,4 +472,9 @@(let [entity-ids (t2/select-fn-set :entity_id :model/PermissionsGroup{:where [:in :id #{(:id (perms-group/admin)) (:id (perms-group/all-users))}]})](doseq [entity-id entity-ids]- (scim-client :delete 404 (format "ee/scim/v2/Groups/%s" entity-id)))))))+ (scim-client :delete 404 (format "ee/scim/v2/Groups/%s" entity-id)))))+ (testing "404 is returned when trying to delete a data-app group, and it survives"+ (mt/with-temp [:model/PermissionsGroup app-group {:name "App Group" :is_data_app_group true}]+ (let [entity-id (t2/select-one-fn :entity_id :model/PermissionsGroup :id (:id app-group))]+ (scim-client :delete 404 (format "ee/scim/v2/Groups/%s" entity-id))+ (is (t2/exists? :model/PermissionsGroup :id (:id app-group))))))))