Skip to content
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

Allow GET from non-admins on data source resource #4992

Merged
merged 6 commits into from
Jul 1, 2020
Merged
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
8 changes: 6 additions & 2 deletions client/app/pages/queries/QueryView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import routeWithUserSession from "@/components/ApplicationArea/routeWithUserSess
import EditInPlace from "@/components/EditInPlace";
import Parameters from "@/components/Parameters";

import DataSource from "@/services/data-source";
import { ExecutionStatus } from "@/services/query-result";
import useQueryResultData from "@/lib/useQueryResultData";

Expand All @@ -22,7 +23,6 @@ import QueryExecutionMetadata from "./components/QueryExecutionMetadata";

import useVisualizationTabHandler from "./hooks/useVisualizationTabHandler";
import useQueryExecute from "./hooks/useQueryExecute";
import useQueryDataSources from "./hooks/useQueryDataSources";
import useUpdateQueryDescription from "./hooks/useUpdateQueryDescription";
import useQueryFlags from "./hooks/useQueryFlags";
import useQueryParameters from "./hooks/useQueryParameters";
Expand All @@ -35,7 +35,7 @@ import "./QueryView.less";

function QueryView(props) {
const [query, setQuery] = useState(props.query);
const { dataSource } = useQueryDataSources(query);
const [dataSource, setDataSource] = useState();
const queryFlags = useQueryFlags(query, dataSource);
const [parameters, areParametersDirty, updateParametersDirtyFlag] = useQueryParameters(query);
const [selectedVisualization, setSelectedVisualization] = useVisualizationTabHandler(query.visualizations);
Expand Down Expand Up @@ -81,6 +81,10 @@ function QueryView(props) {
document.title = query.name;
}, [query.name]);

useEffect(() => {
DataSource.get({ id: query.data_source_id }).then(setDataSource);
}, [query.data_source_id]);

return (
<div
className={cx("query-page-wrapper", {
Expand Down
8 changes: 4 additions & 4 deletions client/app/pages/queries/components/QueryMetadata.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isFunction } from "lodash";
import { isFunction, has } from "lodash";
import React from "react";
import PropTypes from "prop-types";
import cx from "classnames";
Expand Down Expand Up @@ -41,7 +41,7 @@ export default function QueryMetadata({ query, dataSource, layout, onEditSchedul
</div>
</div>
<div className="query-metadata-space" />
{dataSource && (
{has(dataSource, "name") && has(dataSource, "type") && (
<div className="query-metadata-item">
Data Source:
<img src={`${IMG_ROOT}/${dataSource.type}.png`} width="20" alt={dataSource.type} />
Expand Down Expand Up @@ -88,8 +88,8 @@ QueryMetadata.propTypes = {
schedule: PropTypes.object,
}).isRequired,
dataSource: PropTypes.shape({
type: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
type: PropTypes.string,
name: PropTypes.string,
}),
onEditSchedule: PropTypes.func,
};
Expand Down
17 changes: 13 additions & 4 deletions redash/handlers/data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@ def get(self):


class DataSourceResource(BaseResource):
@require_admin
kravets-levko marked this conversation as resolved.
Show resolved Hide resolved
def get(self, data_source_id):
Copy link
Member

Choose a reason for hiding this comment

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

It should still require some permission to view the data source details, because otherwise this API will be available when using the query API key...

Also, when the user doesn't have a list_data_sources permission it's very likely the admin wouldn't want them to see the details of the data source anyway.

Let's keep this new implementation, but still require list_data_sources permission here for now (ideally we would use a new permission, but this is a bit of a hassle to add).

Copy link
Collaborator

Choose a reason for hiding this comment

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

I suggested the same but late a bit 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

Having "list_data_sources" doesn't fix the problem :(

Also, when the user doesn't have a list_data_sources permission it's very likely the admin wouldn't want them to see the details of the data source anyway

I had that in mind, that's why I put the "all" only when the user is an admin.

ds = data_source.to_dict(all=self.current_user.has_permission("admin"))

Basically we show exactly the same information we would show in the list resource if the user is not an admin.

I see your point anyway, just that if felt ok as the user would actually be "listing data sources". WDYT about only returning the { view_only: true/false } when the user doesn't have the list permission?

Copy link
Member Author

Choose a reason for hiding this comment

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

4958ec4 with "returning the { view_only: true/false } when the user doesn't have the list permission"

Copy link
Member

Choose a reason for hiding this comment

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

I had that in mind, that's why I put the "all" only when the user is an admin.

By details I meant even the name or type.

data_source = models.DataSource.get_by_id_and_org(
data_source_id, self.current_org
data_source = get_object_or_404(
models.DataSource.get_by_id_and_org, data_source_id, self.current_org
)
require_access(data_source, self.current_user, view_only)

ds = {}
if self.current_user.has_permission("list_data_sources"):
# if it's a non-admin, limit the information
ds = data_source.to_dict(all=self.current_user.has_permission("admin"))

# add view_only info, required for frontend permissions
ds["view_only"] = all(
project(data_source.groups, self.current_user.group_ids).values()
)
ds = data_source.to_dict(all=True)
self.record_event(
{"action": "view", "object_id": data_source_id, "object_type": "datasource"}
)
Expand Down
39 changes: 39 additions & 0 deletions tests/handlers/test_data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,45 @@ def test_returns_403_for_non_admin(self):
self.assertEqual(rv.status_code, 403)


class TestDataSourceResourceGet(BaseTestCase):
def setUp(self):
super(TestDataSourceResourceGet, self).setUp()
self.path = "/api/data_sources/{}".format(self.factory.data_source.id)

def test_returns_all_data_for_admins(self):
admin = self.factory.create_admin()
rv = self.make_request("get", self.path, user=admin)
self.assertEqual(rv.status_code, 200)
self.assertIn("view_only", rv.json)
self.assertIn("options", rv.json)

def test_returns_only_view_only_for_users_without_list_permissions(self):
group = self.factory.create_group(permissions=[])
data_source = self.factory.create_data_source(group=group, view_only=True)
user = self.factory.create_user(group_ids=[group.id])

rv = self.make_request(
"get", "/api/data_sources/{}".format(data_source.id), user=user
)
self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.json, {"view_only": True})

def test_returns_limited_data_for_non_admin_in_the_default_group(self):
user = self.factory.create_user()
self.assertTrue(user.has_permission("list_data_sources"))

rv = self.make_request("get", self.path, user=user)
self.assertEqual(rv.status_code, 200)
self.assertNotIn("options", rv.json)
self.assertIn("view_only", rv.json)

def test_returns_403_for_non_admin_in_group_without_permission(self):
group = self.factory.create_group()
user = self.factory.create_user(group_ids=[group.id])
rv = self.make_request("get", self.path, user=user)
self.assertEqual(rv.status_code, 403)


class TestDataSourceResourcePost(BaseTestCase):
def setUp(self):
super(TestDataSourceResourcePost, self).setUp()
Expand Down