Skip to content

Commit 7cd8201

Browse files
committed
perf(imports): use lazy-imports for idp plugins
1 parent 3332642 commit 7cd8201

8 files changed

+27
-22
lines changed

redshift_connector/iam_helper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
import typing
33
from enum import Enum
44

5-
import boto3 # type: ignore
6-
import botocore # type: ignore
7-
85
from redshift_connector.config import ClientProtocolVersion
96
from redshift_connector.credentials_holder import CredentialsHolder
107
from redshift_connector.error import InterfaceError
@@ -233,6 +230,9 @@ def set_iam_credentials(info: RedshiftProperty) -> None:
233230
# Calls the AWS SDK methods to return temporary credentials.
234231
# The expiration date is returned as the local time set by the client machines OS.
235232
def set_cluster_credentials(cred_provider: SamlCredentialsProvider, info: RedshiftProperty) -> None:
233+
import boto3 # type: ignore
234+
import botocore # type: ignore
235+
236236
try:
237237
credentials: CredentialsHolder = cred_provider.get_credentials()
238238
client = boto3.client(

redshift_connector/plugin/adfs_credentials_provider.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
import re
33
import typing
44

5-
import bs4 # type: ignore
6-
import requests
7-
85
from redshift_connector.error import InterfaceError
96
from redshift_connector.plugin.saml_credentials_provider import SamlCredentialsProvider
107

@@ -26,6 +23,9 @@ def windows_integrated_authentication(self: "AdfsCredentialsProvider"):
2623
pass
2724

2825
def form_based_authentication(self: "AdfsCredentialsProvider") -> str:
26+
import bs4 # type: ignore
27+
import requests
28+
2929
url: str = "https://{host}:{port}/adfs/ls/IdpInitiatedSignOn.aspx?loginToRp=urn:amazon:webservices".format(
3030
host=self.idp_host, port=str(self.idpPort)
3131
)

redshift_connector/plugin/azure_credentials_provider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import logging
33
import typing
44

5-
import requests
6-
75
from redshift_connector.error import InterfaceError
86
from redshift_connector.plugin.credential_provider_constants import azure_headers
97
from redshift_connector.plugin.saml_credentials_provider import SamlCredentialsProvider
@@ -53,6 +51,8 @@ def get_saml_assertion(self: "AzureCredentialsProvider") -> str:
5351
# Method to initiate a POST request to grab the SAML Assertion from Microsoft Azure
5452
# and convert it to a SAML Response.
5553
def azure_oauth_based_authentication(self: "AzureCredentialsProvider") -> str:
54+
import requests
55+
5656
# endpoint to connect with Microsoft Azure to get SAML Assertion token
5757
url: str = "https://login.microsoftonline.com/{tenant}/oauth2/token".format(tenant=self.idp_tenant)
5858
# headers to pass with POST request

redshift_connector/plugin/browser_azure_credentials_provider.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
import random
55
import socket
66
import typing
7-
import webbrowser
8-
9-
import requests
107

118
from redshift_connector.error import InterfaceError
129
from redshift_connector.plugin.credential_provider_constants import azure_headers
@@ -99,6 +96,8 @@ def fetch_authorization_token(self: "BrowserAzureCredentialsProvider", listen_so
9996
# Initiates the request to the IDP and gets the response body
10097
# Get Base 64 encoded saml assertion from the response body
10198
def fetch_saml_response(self: "BrowserAzureCredentialsProvider", token):
99+
import requests
100+
102101
url: str = "https://login.microsoftonline.com/{tenant}/oauth2/token".format(tenant=self.idp_tenant)
103102
# headers to pass with POST request
104103
headers: typing.Dict[str, str] = azure_headers
@@ -203,6 +202,8 @@ def run_server(
203202

204203
# Opens the default browser with the authorization request to the IDP
205204
def open_browser(self: "BrowserAzureCredentialsProvider", state: str) -> None:
205+
import webbrowser
206+
206207
url: str = (
207208
"https://login.microsoftonline.com/{tenant}"
208209
"/oauth2/authorize"

redshift_connector/plugin/browser_saml_credentials_provider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import socket
55
import typing
66
import urllib.parse
7-
import webbrowser
87

98
from redshift_connector.error import InterfaceError
109
from redshift_connector.plugin.saml_credentials_provider import SamlCredentialsProvider
@@ -92,6 +91,8 @@ def run_server(self: "BrowserSamlCredentialsProvider", listen_port: int, idp_res
9291

9392
# Opens the default browser with the authorization request to the web service.
9493
def open_browser(self: "BrowserSamlCredentialsProvider") -> None:
94+
import webbrowser
95+
9596
url: typing.Optional[str] = self.login_url
9697
if url is None:
9798
raise InterfaceError("the login_url could not be empty")

redshift_connector/plugin/okta_credentials_provider.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
import logging
33
import typing
44

5-
import bs4 # type: ignore
6-
import requests
7-
85
from redshift_connector.error import InterfaceError
96
from redshift_connector.plugin.credential_provider_constants import okta_headers
107
from redshift_connector.plugin.saml_credentials_provider import SamlCredentialsProvider
@@ -35,6 +32,8 @@ def get_saml_assertion(self: "OktaCredentialsProvider") -> str:
3532

3633
# Authenticates users credentials via Okta, return Okta session token.
3734
def okta_authentication(self: "OktaCredentialsProvider") -> str:
35+
import requests
36+
3837
# HTTP Post request to Okta API for session token
3938
url: str = "https://{host}/api/v1/authn".format(host=self.idp_host)
4039
headers: typing.Dict[str, str] = okta_headers
@@ -70,6 +69,9 @@ def okta_authentication(self: "OktaCredentialsProvider") -> str:
7069

7170
# Retrieves SAML assertion from Okta containing AWS roles.
7271
def handle_saml_assertion(self: "OktaCredentialsProvider", okta_session_token: str) -> str:
72+
import bs4 # type: ignore
73+
import requests
74+
7375
url: str = "https://{host}/home/{app_name}/{app_id}?onetimetoken={session_token}".format(
7476
host=self.idp_host, app_name=self.app_name, app_id=self.app_id, session_token=okta_session_token
7577
)

redshift_connector/plugin/ping_credentials_provider.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
import re
33
import typing
44

5-
import bs4 # type: ignore
6-
import requests
7-
85
from redshift_connector.error import InterfaceError
96
from redshift_connector.plugin.saml_credentials_provider import SamlCredentialsProvider
107
from redshift_connector.redshift_property import RedshiftProperty
@@ -23,6 +20,9 @@ def add_parameter(self: "PingCredentialsProvider", info: RedshiftProperty) -> No
2320

2421
# Required method to grab the SAML Response. Used in base class to refresh temporary credentials.
2522
def get_saml_assertion(self: "PingCredentialsProvider") -> str:
23+
import bs4 # type: ignore
24+
import requests
25+
2626
self.check_required_parameters()
2727

2828
if self.partner_sp_id is None:

redshift_connector/plugin/saml_credentials_provider.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
import typing
66
from abc import ABC, abstractmethod
77

8-
import boto3 # type: ignore
9-
import bs4 # type: ignore
10-
118
from redshift_connector.credentials_holder import CredentialsHolder
129
from redshift_connector.error import InterfaceError
1310
from redshift_connector.plugin.credential_provider_constants import SAML_RESP_NAMESPACES
@@ -70,6 +67,9 @@ def get_credentials(self: "SamlCredentialsProvider") -> CredentialsHolder:
7067
return credentials
7168

7269
def refresh(self: "SamlCredentialsProvider") -> None:
70+
import boto3 # type: ignore
71+
import bs4 # type: ignore
72+
7373
try:
7474
# get SAML assertion from specific identity provider
7575
saml_assertion = self.get_saml_assertion()
@@ -78,7 +78,6 @@ def refresh(self: "SamlCredentialsProvider") -> None:
7878
raise InterfaceError(e)
7979
# decode SAML assertion into xml format
8080
doc: bytes = base64.b64decode(saml_assertion)
81-
8281
soup = bs4.BeautifulSoup(doc, "xml")
8382
attrs = soup.findAll("Attribute")
8483
# extract RoleArn adn PrincipleArn from SAML assertion
@@ -180,6 +179,8 @@ def check_required_parameters(self: "SamlCredentialsProvider") -> None:
180179
raise InterfaceError("Missing required property: idp_host")
181180

182181
def read_metadata(self: "SamlCredentialsProvider", doc: bytes) -> CredentialsHolder.IamMetadata:
182+
import bs4 # type: ignore
183+
183184
try:
184185
soup = bs4.BeautifulSoup(doc, "xml")
185186
attrs: typing.Any = []

0 commit comments

Comments
 (0)