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

Grant Tests Implementation #62

Merged
merged 6 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 10 additions & 1 deletion .github/scripts/smoke_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ EOF
# Main
test_type=$1
test_ssl="${6:-false}"
profiles_path=~/.dbt/profiles.yml
profiles_path=~/.dbt/profiles.yml
temp_path=~/.dbt/temp_profiles.yml

if [ -f $profiles_path ]; then
mv $profiles_path $temp_path
fi

if [ $test_type == softwareUP ]; then
test_user=$2
Expand Down Expand Up @@ -173,3 +178,7 @@ elif [ $test_type == cloud ]; then
test_cloud
fi

if [ -f $temp_path ]; then
mv $temp_path $profiles_path
fi

38 changes: 33 additions & 5 deletions dbt/adapters/dremio/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
from dbt.adapters.sql import SQLAdapter
from dbt.adapters.dremio import DremioConnectionManager
from dbt.adapters.dremio.relation import DremioRelation

from typing import Dict

from typing import List
from typing import Optional
from dbt.adapters.base.relation import BaseRelation

from dbt.adapters.sql.impl import DROP_RELATION_MACRO_NAME
from dbt.events import AdapterLogger

logger = AdapterLogger("dremio")
Expand Down Expand Up @@ -66,9 +66,13 @@ def create_schema(self, relation: DremioRelation) -> None:
self.connections.create_catalog(database, schema)

def drop_schema(self, relation: DremioRelation) -> None:
database = relation.database
schema = relation.schema
self.connections.drop_catalog(database, schema)
if relation.type == "table":
self.execute_macro(DROP_RELATION_MACRO_NAME, kwargs={"relation": relation})

else:
database = relation.database
schema = relation.schema
self.connections.drop_catalog(database, schema)

def timestamp_add_sql(
self, add_to: str, number: int = 1, interval: str = "hour"
Expand Down Expand Up @@ -104,6 +108,30 @@ def get_rows_different_sql(

return sql

def standardize_grants_dict(self, grants_table: agate.Table) -> dict:
"""Translate the result of `show grants` (or equivalent) to match the
grants which a user would configure in their project.

Ideally, the SQL to show grants should also be filtering:
filter OUT any grants TO the current user/role (e.g. OWNERSHIP).
If that's not possible in SQL, it can be done in this method instead.

:param grants_table: An agate table containing the query result of
the SQL returned by get_show_grant_sql
:return: A standardized dictionary matching the `grants` config
:rtype: dict
"""
grants_dict: Dict[str, List[str]] = {}
for row in grants_table:
# Just needed to change these two values to match Dremio cols
grantee = row["grantee_id"]
privilege = row["privilege"]
if privilege in grants_dict.keys():
grants_dict[privilege].append(grantee)
else:
grants_dict.update({privilege: [grantee]})
return grants_dict


COLUMNS_EQUAL_SQL = """
with diff_count as (
Expand Down
45 changes: 45 additions & 0 deletions dbt/include/dremio/macros/adapters/apply_grants.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*Copyright (C) 2022 Dremio Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

{%- macro dremio__support_multiple_grantees_per_dcl_statement() -%}
{{ return(False) }}
{%- endmacro -%}

{% macro dremio__get_show_grant_sql(relation) %}
{%- if relation.type == 'table' -%}
{%- set relation_without_double_quotes = target.datalake ~ '.' ~ target.root_path ~ '.' ~ relation.identifier-%}
{%- else -%}
{%- set relation_without_double_quotes = relation.database ~ '.' ~ relation.schema ~ '.' ~ relation.identifier-%}
{%- endif %}

SELECT privilege, grantee_id
FROM sys.privileges
WHERE privileges.object_id='{{ relation_without_double_quotes }}'
{% endmacro %}

{%- macro dremio__get_grant_sql(relation, privilege, grantees) -%}
grant {{ privilege }} on {{relation.type}} {{ relation }} to user {{ grantees | join(', ') }}
{%- endmacro -%}

{%- macro default__get_revoke_sql(relation, privilege, grantees) -%}
revoke {{ privilege }} on {{ relation.type }} {{ relation }} from user {{ grantees | join(', ') }}
{%- endmacro -%}

{% macro dremio__call_dcl_statements(dcl_statement_list) %}
{% for dcl_statement in dcl_statement_list %}
{% call statement('grants') %}
{{ dcl_statement }};
{% endcall %}
{% endfor %}
{% endmacro %}
22 changes: 0 additions & 22 deletions dbt/include/dremio/macros/adapters/schema.sql

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ limitations under the License.*/
{%- set raw_on_schema_change = config.get('on_schema_change', validator=validation.any[basestring]) or 'ignore' -%}
{%- set on_schema_change = incremental_validate_on_schema_change(raw_on_schema_change) -%}
{%- set full_refresh_mode = (should_full_refresh()) -%}
{% set grant_config = config.get('grants') %}

{{ run_hooks(pre_hooks) }}

Expand Down Expand Up @@ -67,6 +68,8 @@ limitations under the License.*/
{{ apply_twin_strategy(target_relation) }}

{% do persist_docs(target_relation, model) %}

{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}

{{ run_hooks(post_hooks) }}

Expand Down
3 changes: 3 additions & 0 deletions dbt/include/dremio/macros/materializations/seed/seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.*/
schema=schema,
database=database,
type='table') -%}
{% set grant_config = config.get('grants') %}

{{ run_hooks(pre_hooks) }}

Expand Down Expand Up @@ -51,6 +52,8 @@ limitations under the License.*/

{% do persist_docs(target_relation, model) %}

{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}

{{ run_hooks(post_hooks) }}

{{ return({'relations': [target_relation]})}}
Expand Down
3 changes: 3 additions & 0 deletions dbt/include/dremio/macros/materializations/table/table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.*/
schema=schema,
database=database,
type='table') -%}
{% set grant_config = config.get('grants') %}
{{ run_hooks(pre_hooks) }}

-- setup: if the target relation already exists, drop it
Expand All @@ -41,6 +42,8 @@ limitations under the License.*/

{% do persist_docs(target_relation, model) %}

{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}

{{ run_hooks(post_hooks) }}

{{ return({'relations': [target_relation]})}}
Expand Down
4 changes: 4 additions & 0 deletions dbt/include/dremio/macros/materializations/view/view.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ limitations under the License.*/
{%- set target_relation = api.Relation.create(
identifier=identifier, schema=schema, database=database, type='view') -%}

{% set grant_config = config.get('grants') %}

{{ run_hooks(pre_hooks) }}

-- If there's a table with the same name and we weren't told to full refresh,
Expand All @@ -40,6 +42,8 @@ limitations under the License.*/

{{ enable_default_reflection() }}

{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}

{{ run_hooks(post_hooks) }}

{{ return({'relations': [target_relation]}) }}
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ def dbt_profile_target():
"password": os.getenv("DREMIO_PASSWORD"),
"datalake": os.getenv("DREMIO_DATALAKE"),
"use_ssl": False,
"database": os.getenv("DREMIO_DATABASE"),
}
14 changes: 14 additions & 0 deletions tests/functional/adapter/basic/test_adapter_methods.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (C) 2022 Dremio Corporation

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
from dbt.tests.adapter.basic.test_adapter_methods import BaseAdapterMethod
from dbt.tests.adapter.basic.test_adapter_methods import models__upstream_sql
Expand Down
19 changes: 17 additions & 2 deletions tests/functional/adapter/basic/test_base_mat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import pytest
from dbt.tests.adapter.basic.test_base import BaseSimpleMaterializations

# Copyright (C) 2022 Dremio Corporation

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from tests.functional.adapter.utils.test_utils import (
relation_from_name,
check_relations_equal,
Expand All @@ -16,12 +31,12 @@
)
from tests.functional.adapter.utils.test_utils import DATALAKE

# Unable to insert variable into docstring, so "rav-test" is hardcoded
# Unable to insert variable into docstring, so "dbt_test_source" is hardcoded
schema_base_yml = """
version: 2
sources:
- name: raw
database: "rav-test"
database: "dbt_test_source"
schema: "{{ target.schema }}"
tables:
- name: seed
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/adapter/basic/test_docs_generate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (C) 2022 Dremio Corporation

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
import os
from tests.functional.adapter.utils.test_utils import (
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/adapter/basic/test_empty.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (C) 2022 Dremio Corporation

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from dbt.tests.adapter.basic.test_empty import BaseEmpty


Expand Down
14 changes: 14 additions & 0 deletions tests/functional/adapter/basic/test_ephemeral.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (C) 2022 Dremio Corporation

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
from dbt.tests.adapter.basic.test_ephemeral import BaseEphemeral
from tests.functional.adapter.utils.test_utils import DATALAKE
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/adapter/basic/test_generic_tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (C) 2022 Dremio Corporation

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
from dbt.tests.adapter.basic.test_generic_tests import BaseGenericTests
from tests.functional.adapter.utils.test_utils import DATALAKE
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/adapter/basic/test_incremental.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (C) 2022 Dremio Corporation

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
from dbt.tests.adapter.basic.test_incremental import BaseIncremental
from tests.functional.adapter.utils.test_utils import DATALAKE
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/adapter/basic/test_singular_ephemeral.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (C) 2022 Dremio Corporation

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
from dbt.tests.adapter.basic.test_singular_tests_ephemeral import (
BaseSingularTestsEphemeral,
Expand Down
Loading