Skip to content

Commit bc80215

Browse files
authored
[BREAKING] FEAT: Enable Entra ID Authentication across the codebase (#1124)
1 parent 14a3bc1 commit bc80215

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+812
-183
lines changed

.env_example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# This is an example of the .env file. Copy to .env and fill in your secrets.
2+
# Note that if you are using Entra authentication for certain Azure resources (use_entra_auth = True in PyRIT),
3+
# keys for those resources are not needed.
24

35

46
###################################
@@ -164,6 +166,8 @@ AZURE_STORAGE_ACCOUNT_SAS_TOKEN="xxxxx"
164166

165167
AZURE_SPEECH_REGION = "eastus2"
166168
AZURE_SPEECH_KEY = "xxxxx"
169+
# Resource ID is needed when using Entra authentication
170+
AZURE_SPEECH_RESOURCE_ID = "xxxxx"
167171

168172
AZURE_CONTENT_SAFETY_API_KEY="xxxxx"
169173
AZURE_CONTENT_SAFETY_API_ENDPOINT="https://xxxxx.cognitiveservices.azure.com/"

doc/code/scoring/1_azure_content_safety_scorers.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
"In order to use this API, you need to configure a few environment variables:\n",
1414
"\n",
1515
"- AZURE_CONTENT_SAFETY_API_ENDPOINT: The endpoint for the Azure Content Safety API\n",
16-
"- AZURE_CONTENT_SAFETY_API_KEY: The API key for the Azure Content Safety API (if not using AAD Auth)\n",
16+
"- AZURE_CONTENT_SAFETY_API_KEY: The API key for the Azure Content Safety API (if not using Entra Auth)\n",
1717
"\n",
18-
"As an alternative to key-based authentication, you may set `use_aad_auth=True` and use identity-based authentication.\n",
18+
"As an alternative to key-based authentication, you may set `use_entra_auth=True` and use identity-based authentication.\n",
1919
"\n",
2020
"Note that this api returns a value between 0 and 7. This is different from likert scales, which return a value between 1 and 5. Because both are `float_scale` scores, these values are all normalized to floating point values between 0.0 and 1.0 and can be directly compared. This is sometimes interesting as an operator e.g. if there are scenarios where a `SelfAskLikertScorer` and `AzureContentFilterScorer` produce very different values.\n",
2121
"\n",
@@ -52,9 +52,9 @@
5252
"\n",
5353
"# Set up the Azure Content Filter\n",
5454
"azure_content_filter = AzureContentFilterScorer(\n",
55-
" # Comment out either api_key or use_aad_auth\n",
55+
" # Comment out either api_key or use_entra_auth\n",
5656
" api_key=os.environ.get(\"AZURE_CONTENT_SAFETY_API_KEY\"),\n",
57-
" # use_aad_auth=True,\n",
57+
" # use_entra_auth=True,\n",
5858
" endpoint=os.environ.get(\"AZURE_CONTENT_SAFETY_API_ENDPOINT\"),\n",
5959
")\n",
6060
"\n",

doc/code/scoring/1_azure_content_safety_scorers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
# In order to use this API, you need to configure a few environment variables:
2323
#
2424
# - AZURE_CONTENT_SAFETY_API_ENDPOINT: The endpoint for the Azure Content Safety API
25-
# - AZURE_CONTENT_SAFETY_API_KEY: The API key for the Azure Content Safety API (if not using AAD Auth)
25+
# - AZURE_CONTENT_SAFETY_API_KEY: The API key for the Azure Content Safety API (if not using Entra Auth)
2626
#
27-
# As an alternative to key-based authentication, you may set `use_aad_auth=True` and use identity-based authentication.
27+
# As an alternative to key-based authentication, you may set `use_entra_auth=True` and use identity-based authentication.
2828
#
2929
# Note that this api returns a value between 0 and 7. This is different from likert scales, which return a value between 1 and 5. Because both are `float_scale` scores, these values are all normalized to floating point values between 0.0 and 1.0 and can be directly compared. This is sometimes interesting as an operator e.g. if there are scenarios where a `SelfAskLikertScorer` and `AzureContentFilterScorer` produce very different values.
3030
#
@@ -43,9 +43,9 @@
4343

4444
# Set up the Azure Content Filter
4545
azure_content_filter = AzureContentFilterScorer(
46-
# Comment out either api_key or use_aad_auth
46+
# Comment out either api_key or use_entra_auth
4747
api_key=os.environ.get("AZURE_CONTENT_SAFETY_API_KEY"),
48-
# use_aad_auth=True,
48+
# use_entra_auth=True,
4949
endpoint=os.environ.get("AZURE_CONTENT_SAFETY_API_ENDPOINT"),
5050
)
5151

doc/code/scoring/prompt_shield_scorer.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@
106106
"initialize_pyrit(memory_db_type=IN_MEMORY)\n",
107107
"\n",
108108
"pst = PromptShieldTarget()\n",
109+
"# For an Azure Content Safety endpoint with Entra ID authentication enabled, use the following command instead. Make sure to run `az login` first.\n",
110+
"# target = PromptShieldTarget(use_entra_auth=True)\n",
109111
"\n",
110112
"scorer = PromptShieldScorer(prompt_shield_target=pst)\n",
111113
"\n",
@@ -148,7 +150,7 @@
148150
"name": "python",
149151
"nbconvert_exporter": "python",
150152
"pygments_lexer": "ipython3",
151-
"version": "3.12.11"
153+
"version": "3.11.9"
152154
}
153155
},
154156
"nbformat": 4,

doc/code/scoring/prompt_shield_scorer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# extension: .py
66
# format_name: percent
77
# format_version: '1.3'
8-
# jupytext_version: 1.17.3
8+
# jupytext_version: 1.17.2
99
# ---
1010

1111
# %% [markdown]
@@ -48,6 +48,8 @@
4848
initialize_pyrit(memory_db_type=IN_MEMORY)
4949

5050
pst = PromptShieldTarget()
51+
# For an Azure Content Safety endpoint with Entra ID authentication enabled, use the following command instead. Make sure to run `az login` first.
52+
# target = PromptShieldTarget(use_entra_auth=True)
5153

5254
scorer = PromptShieldScorer(prompt_shield_target=pst)
5355

doc/code/targets/1_openai_chat_target.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
"\n",
7070
"\n",
7171
"target = OpenAIChatTarget()\n",
72+
"# For an AzureOpenAI endpoint with Entra ID authentication enabled, use the following command instead. Make sure to run `az login` first.\n",
73+
"# target = OpenAIChatTarget(use_entra_auth=True)\n",
7274
"\n",
7375
"attack = PromptSendingAttack(objective_target=target)\n",
7476
"\n",
@@ -117,7 +119,7 @@
117119
"name": "python",
118120
"nbconvert_exporter": "python",
119121
"pygments_lexer": "ipython3",
120-
"version": "3.12.9"
122+
"version": "3.11.9"
121123
}
122124
},
123125
"nbformat": 4,

doc/code/targets/1_openai_chat_target.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131

3232
target = OpenAIChatTarget()
33+
# For an AzureOpenAI endpoint with Entra ID authentication enabled, use the following command instead. Make sure to run `az login` first.
34+
# target = OpenAIChatTarget(use_entra_auth=True)
3335

3436
attack = PromptSendingAttack(objective_target=target)
3537

doc/code/targets/5_multi_modal_targets.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@
118118
"objective = \"Give me an image of a raccoon pirate as a Spanish baker in Spain\"\n",
119119
"\n",
120120
"img_prompt_target = OpenAIDALLETarget()\n",
121+
"# For an AzureOpenAI endpoint with Entra ID authentication enabled, use the following command instead. Make sure to run `az login` first.\n",
122+
"# target = OpenAIChatTarget(use_entra_auth=True)\n",
121123
"\n",
122124
"image_scorer = SelfAskTrueFalseScorer(\n",
123125
" chat_target=OpenAIChatTarget(),\n",
@@ -1108,7 +1110,7 @@
11081110
"name": "python",
11091111
"nbconvert_exporter": "python",
11101112
"pygments_lexer": "ipython3",
1111-
"version": "3.12.11"
1113+
"version": "3.11.9"
11121114
}
11131115
},
11141116
"nbformat": 4,

doc/code/targets/5_multi_modal_targets.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
# extension: .py
66
# format_name: percent
77
# format_version: '1.3'
8-
# jupytext_version: 1.17.3
8+
# jupytext_version: 1.17.2
9+
# kernelspec:
10+
# display_name: pyrit-dev
11+
# language: python
12+
# name: python3
913
# ---
1014

1115
# %% [markdown]
@@ -35,6 +39,8 @@
3539
objective = "Give me an image of a raccoon pirate as a Spanish baker in Spain"
3640

3741
img_prompt_target = OpenAIDALLETarget()
42+
# For an AzureOpenAI endpoint with Entra ID authentication enabled, use the following command instead. Make sure to run `az login` first.
43+
# target = OpenAIChatTarget(use_entra_auth=True)
3844

3945
image_scorer = SelfAskTrueFalseScorer(
4046
chat_target=OpenAIChatTarget(),

doc/code/targets/8_openai_responses_target.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
"initialize_pyrit(memory_db_type=IN_MEMORY)\n",
5656
"\n",
5757
"target = OpenAIResponseTarget()\n",
58+
"# For an AzureOpenAI endpoint with Entra ID authentication enabled, use the following command instead. Make sure to run `az login` first.\n",
59+
"# target = OpenAIResponseTarget(use_entra_auth=True)\n",
5860
"\n",
5961
"attack = PromptSendingAttack(objective_target=target)\n",
6062
"\n",
@@ -243,7 +245,7 @@
243245
"name": "python",
244246
"nbconvert_exporter": "python",
245247
"pygments_lexer": "ipython3",
246-
"version": "3.12.11"
248+
"version": "3.11.9"
247249
}
248250
},
249251
"nbformat": 4,

0 commit comments

Comments
 (0)