Skip to content

Commit ac994bc

Browse files
Default azure credential auth is made as the default
1 parent 5df6e02 commit ac994bc

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

AzureFunctions/km-charts-function/function_app.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,6 @@ def get_db_connection():
2121
password = os.environ.get("SQLDB_PASSWORD")
2222

2323
# Attempt connection using Username & Password
24-
try:
25-
conn = pyodbc.connect(
26-
f"DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password}",
27-
timeout=5
28-
)
29-
logging.info("Connected using Username & Password")
30-
return conn
31-
except pyodbc.Error as e:
32-
print(f"Failed with Username & Password: {str(e)}")
33-
34-
# If first attempt fails, try Azure Default Credential
3524
try:
3625
credential = DefaultAzureCredential()
3726

@@ -50,10 +39,16 @@ def get_db_connection():
5039
)
5140

5241
logging.info("Connected using Default Azure Credential")
42+
5343
return conn
54-
except Exception as e:
44+
except pyodbc.Error as e:
5545
logging.error(f"Failed with Default Credential: {str(e)}")
56-
return None # Return None if both attempts fail
46+
conn = pyodbc.connect(
47+
f"DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password}",
48+
timeout=5
49+
)
50+
logging.info("Connected using Username & Password")
51+
return conn
5752

5853
# add post methods - filters will come in the body (request.body), if body is not empty, update the where clause in the query
5954
@app.route(route="get_metrics", methods=["GET","POST"], auth_level=func.AuthLevel.ANONYMOUS)

AzureFunctions/km-rag-function/function_app.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,6 @@ def get_db_connection():
4949
password = os.environ.get("SQLDB_PASSWORD")
5050

5151
# Attempt connection using Username & Password
52-
try:
53-
conn = pyodbc.connect(
54-
f"DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password}",
55-
timeout=5
56-
)
57-
logging.info("Connected using Username & Password")
58-
return conn
59-
except pyodbc.Error as e:
60-
print(f"Failed with Username & Password: {str(e)}")
61-
62-
# If first attempt fails, try Azure Default Credential
6352
try:
6453
credential = DefaultAzureCredential()
6554

@@ -78,10 +67,17 @@ def get_db_connection():
7867
)
7968

8069
logging.info("Connected using Default Azure Credential")
70+
8171
return conn
82-
except Exception as e:
72+
except pyodbc.Error as e:
8373
logging.error(f"Failed with Default Credential: {str(e)}")
84-
return None # Return None if both attempts fail
74+
conn = pyodbc.connect(
75+
f"DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password}",
76+
timeout=5
77+
)
78+
79+
logging.info("Connected using Username & Password")
80+
return conn
8581

8682
class ChatWithDataPlugin:
8783
@kernel_function(name="Greeting", description="Respond to any greeting or general questions")

0 commit comments

Comments
 (0)