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

Add support for extra parameters to samba client #16115

Merged
merged 8 commits into from
Jun 6, 2021
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
24 changes: 24 additions & 0 deletions airflow/providers/samba/hooks/samba.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,36 @@ def __init__(self, samba_conn_id: str = default_conn_name) -> None:
self.conn = self.get_connection(samba_conn_id)

def get_conn(self) -> SambaClient:
"""
Return a samba client object.

You can provide optional parameters in the extra fields of
your connection.

Below is an inexhaustive list of these parameters:

`logdir`
Base directory name for log/debug files.

`kerberos`
Try to authenticate with kerberos.

`workgroup`
Set the SMB domain of the username.

`netbios_name`
This option allows you to override the NetBIOS name that
Samba uses for itself.

For additional details, see `smbclient.SambaClient`.
"""
samba = SambaClient(
server=self.conn.host,
share=self.conn.schema,
username=self.conn.login,
ip=self.conn.host,
password=self.conn.password,
**self.conn.extra_dejson,
Copy link
Member

Choose a reason for hiding this comment

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

Is the list of fields exhaustive? I feel we should do some sanitising instead of passing the whole JSON object directly in.

Also :param is for function arguments and should not be used for extra keys. These descriptions should live somewhere else.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@uranusjr fixed in 0c8cca7 – the list is inexhaustive (mentioned now) and I have reformatted as definition list.

The :param stuff was token from the Oracle hook, but I guess that is a bad example :-)

By the way, I have since realized that the underlying Python library that supports SambaHook is basically abandonware and not particularly useful – it breaks quite a few expectations if you look at the actual smbclient functionality.

Copy link
Member

Choose a reason for hiding this comment

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

By the way, I have since realized that the underlying Python library that supports SambaHook is basically abandonware and not particularly useful

Thanks for taising this. It would be worthwhile to open an issue for this to migrate to another library, I think. I don’t have much experience dealing with SMB with Python, but pysmb seems to be a popular choice.

Copy link
Contributor

Choose a reason for hiding this comment

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

There is an open issue: #14054

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@uranusjr so I think we can perhaps go ahead and consider merging this request and then I'll submit another request to migrate to pysmb.

I have already done some testing and it seems very well suited.

Copy link
Member

Choose a reason for hiding this comment

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

Yup LGTM. Could you close-reopen the PR to trigger CI?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@uranusjr done

Copy link
Contributor

@eladkal eladkal Jun 3, 2021

Choose a reason for hiding this comment

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

@malthe
Can you please rebase to latest main (former master branch)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eladkal done!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eladkal looks about ready now, checks have run.

)
return samba

Expand Down
2 changes: 2 additions & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ Naik
Namenode
Namespace
Neo4j
NetBIOS
Nextdoor
Nones
NotFound
Expand Down Expand Up @@ -882,6 +883,7 @@ img
imgcat
impyla
incompliancies
inexhaustive
infile
infoType
infoTypes
Expand Down
18 changes: 13 additions & 5 deletions tests/providers/samba/hooks/test_samba.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,39 @@
# specific language governing permissions and limitations
# under the License.

import json
import unittest
from unittest import mock
from unittest.mock import call

import pytest
import smbclient

from airflow.exceptions import AirflowException
from airflow.models import Connection
from airflow.providers.samba.hooks.samba import SambaHook

connection = Connection(host='ip', schema='share', login='username', password='password')
connection = Connection(
host='ip',
schema='share',
login='username',
password='password',
extra=json.dumps({'workgroup': 'workgroup'}),
)


class TestSambaHook(unittest.TestCase):
def test_get_conn_should_fail_if_conn_id_does_not_exist(self):
with pytest.raises(AirflowException):
SambaHook('conn')

@mock.patch('airflow.providers.samba.hooks.samba.SambaClient')
@mock.patch('airflow.hooks.base.BaseHook.get_connection')
def test_get_conn(self, get_conn_mock):
def test_get_conn(self, get_conn_mock, get_client_mock):
get_conn_mock.return_value = connection
hook = SambaHook('samba_default')

assert isinstance(hook.get_conn(), smbclient.SambaClient)
conn = hook.get_conn()
assert str(get_client_mock.mock_calls[0].workgroup) == "workgroup"
assert conn is get_client_mock()
get_conn_mock.assert_called_once_with('samba_default')

@mock.patch('airflow.providers.samba.hooks.samba.SambaHook.get_conn')
Expand Down