Skip to content

Commit

Permalink
Merge pull request #7 from DaSenf1860/msaladdition
Browse files Browse the repository at this point in the history
Msaladdition
  • Loading branch information
DaSenf1860 authored Jul 29, 2024
2 parents 5edd1a4 + a03d048 commit a36eea3
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 17 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ They are designed to automate your Fabric processes.

This SDK helps to interact with the Fabric APIs in a more Pythonic way.
Additionally it brings some extra features like:
- Authentication is handled for you (currently Azure CLI Authentication, Authentication from a Microsoft Fabric notebook and Service Principal Authentication are supported)
- Authentication is handled for you, the following is supported:
- Azure CLI Authentication
- Authentication from a Microsoft Fabric notebook
- Service Principal Authentication
- MSALConfidentialClientApplicationAuthentication
- Waiting for completion of long running operations
- Retry logic when hitting the API rate limits
- Referencing objects by name instead of ID
Expand Down
6 changes: 4 additions & 2 deletions msfabricpysdkcore/adminapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
class FabricClientAdmin(FabricClient):
"""FabricClientAdmin class to interact with Fabric Admin APIs"""

def __init__(self, tenant_id = None, client_id = None, client_secret = None) -> None:
def __init__(self, tenant_id = None, client_id = None, client_secret = None,
username = None, password = None) -> None:
"""Initialize FabricClientAdmin object"""
super().__init__(scope="https://api.fabric.microsoft.com/.default",
tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
tenant_id=tenant_id, client_id=client_id, client_secret=client_secret,
username=username, password=password)


def long_running_operation(self, response_headers):
Expand Down
28 changes: 28 additions & 0 deletions msfabricpysdkcore/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import requests
from abc import abstractmethod
from azure.identity import AzureCliCredential
import msal
from msfabricpysdkcore.util import logger
import logging
try:
Expand Down Expand Up @@ -98,3 +99,30 @@ def get_token(self):
return token


class MSALConfidentialClientApplicationAuthentication(FabricAuth):

def __init__(self, tenant_id, client_id, client_secret, username, password, scope):
super().__init__(scope)

self._logger.info("Using Microsoft Authentication Library (MSAL) ConfidentialClientApplication for authentication")

self.username = username
self.password = password
self.scopes = [scope]

authority = f"https://login.microsoftonline.com/{tenant_id}"

self.app = msal.ConfidentialClientApplication(
client_id=client_id,
client_credential=client_secret,
authority=authority,
)

def get_token(self):
result = self.app.acquire_token_for_client(scopes=self.scopes)
result = self.app.acquire_token_by_username_password(
username=self.username,
password=self.password,
scopes=self.scopes,
)
return result["access_token"]
23 changes: 16 additions & 7 deletions msfabricpysdkcore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,44 @@
import requests
import json

from msfabricpysdkcore.auth import FabricAuthClient, FabricServicePrincipal, FabricSparkUtilsAuthentication
from msfabricpysdkcore.auth import FabricAuthClient, FabricServicePrincipal, FabricSparkUtilsAuthentication, MSALConfidentialClientApplicationAuthentication
from msfabricpysdkcore.util import logger

class FabricClient():
"""FabricClient class to interact with Fabric API"""

_logger: logging.Logger

def __init__(self, scope, tenant_id = None, client_id = None, client_secret = None, silent=None) -> None:
def __init__(self, scope, tenant_id = None, client_id = None, client_secret = None, username = None, password = None, silent=None) -> None:
"""Initialize FabricClient object"""

self._logger = logger.getChild(__name__)

self.tenant_id = tenant_id if tenant_id else os.getenv("FABRIC_TENANT_ID")
self.client_id = client_id if client_id else os.getenv("FABRIC_CLIENT_ID")
self.client_secret = client_secret if client_secret else os.getenv("FABRIC_CLIENT_SECRET")
self.username = username if username else os.getenv("FABRIC_USERNAME")
self.password = password if password else os.getenv("FABRIC_PASSWORD")
self.scope = scope
#self.scope = "https://api.fabric.microsoft.com/.default"

if self.client_id is None or self.client_secret is None or self.tenant_id is None:
try:
self.auth = FabricSparkUtilsAuthentication(self.scope)
except:
self.auth = FabricAuthClient(self.scope)
else:
self.auth = FabricServicePrincipal(scope= self.scope,
tenant_id = self.tenant_id,
client_id = self.client_id,
client_secret = self.client_secret)
if username and password:
self.auth = MSALConfidentialClientApplicationAuthentication(tenant_id = self.tenant_id,
client_id = self.client_id,
client_secret = self.client_secret,
username = self.username,
password = self.password,
scope = self.scope)
else:
self.auth = FabricServicePrincipal(scope= self.scope,
tenant_id = self.tenant_id,
client_id = self.client_id,
client_secret = self.client_secret)

if silent is not None:
warn("The 'silent' parameter is deprecated and will be removed in a future version.", DeprecationWarning, stacklevel=2)
Expand Down
7 changes: 5 additions & 2 deletions msfabricpysdkcore/coreapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
class FabricClientCore(FabricClient):
"""FabricClientCore class to interact with Fabric Core APIs"""

def __init__(self, tenant_id = None, client_id = None, client_secret = None, silent=None) -> None:
def __init__(self, tenant_id = None, client_id = None, client_secret = None,
username = None, password = None, silent=None) -> None:
"""Initialize FabricClientCore object"""
super().__init__(scope="https://api.fabric.microsoft.com/.default",
tenant_id=tenant_id,
client_id=client_id,
client_secret=client_secret)
client_secret=client_secret,
username=username,
password=password)
if silent is not None:
warn("The 'silent' parameter is deprecated and will be removed in a future version.", DeprecationWarning, stacklevel=2)

Expand Down
7 changes: 5 additions & 2 deletions msfabricpysdkcore/fabric_azure_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

class FabricAzureClient(FabricClient):

def __init__(self, tenant_id=None, client_id=None, client_secret=None, silent=None) -> None:
def __init__(self, tenant_id=None, client_id=None, client_secret=None,
username = None, password = None, silent=None) -> None:
super().__init__(scope = "https://management.azure.com/.default",
tenant_id = tenant_id,
client_id = client_id,
client_secret = client_secret,)
client_secret = client_secret,
username = username,
password = password)

if silent is not None:
warn("The 'silent' parameter is deprecated and will be removed in a future version.", DeprecationWarning, stacklevel=2)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "msfabricpysdkcore"
version = "0.1.4"
version = "0.1.5"
dynamic = ["dependencies"]
authors = [
{ name="Andreas Rederer"},
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/release_notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 0.1.5

- added MSALConfidentialClientApplicationAuthentication

## 0.1.4

### Bug Fixes
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
requests >= 2.31.0
azure-identity >= 1.15.0
azure-identity >= 1.15.0
msal >= 1.28.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='msfabricpysdkcore',
version='1.4',
version='1.5',
packages=find_packages(),
install_requires=[
'requests>=2.30.0',
Expand Down

0 comments on commit a36eea3

Please sign in to comment.