Skip to content

Commit c2219ee

Browse files
Refactoring (#65)
1 parent 94a3e0f commit c2219ee

File tree

13 files changed

+246
-204
lines changed

13 files changed

+246
-204
lines changed

infrastructure/afd-apim-pe/create.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
"from apimtesting import ApimTesting\n",
188188
"\n",
189189
"reqs = ApimRequests(apim_gateway_url)\n",
190-
"tests = ApimTesting(\"AFD-APIM-PE Tests (Pre-Lockdown)\")\n",
190+
"tests = ApimTesting(\"AFD-APIM-PE Tests (Pre-Lockdown)\", deployment, deployment)\n",
191191
"\n",
192192
"utils.print_message('Calling Hello World (Root) API via API Management Gateway URL. Expect 200 (if run before disabling API Management public network access).')\n",
193193
"output = reqs.singleGet('/')\n",
@@ -255,7 +255,7 @@
255255
"\n",
256256
"reqsApim = ApimRequests(apim_gateway_url)\n",
257257
"reqsAfd = ApimRequests(afd_endpoint_url)\n",
258-
"tests = ApimTesting(\"AFD-APIM-PE Tests (Post-Lockdown)\")\n",
258+
"tests = ApimTesting(\"AFD-APIM-PE Tests (Post-Lockdown)\", deployment, deployment)\n",
259259
"\n",
260260
"# 1) Unsuccessful call to APIM Gateway URL (should fail with 403 Forbidden)\n",
261261
"output = reqsApim.singleGet('/', msg = '1) Calling Hello World (Root) API via API Management Gateway URL. Expect 403 as APIM public access is disabled now.')\n",

infrastructure/apim-aca/create.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
"from apimtesting import ApimTesting\n",
131131
"\n",
132132
"reqs = ApimRequests(apim_gateway_url)\n",
133-
"tests = ApimTesting(\"APIM-ACA Tests\")\n",
133+
"tests = ApimTesting(\"APIM-ACA Tests\", deployment, deployment)\n",
134134
"\n",
135135
"output = reqs.singleGet('/', msg = 'Calling Hello World (Root) API')\n",
136136
"tests.verify(output, 'Hello World from API Management!')\n",

infrastructure/simple-apim/create.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
"from apimtesting import ApimTesting\n",
113113
"\n",
114114
"reqs = ApimRequests(apim_gateway_url)\n",
115-
"tests = ApimTesting(\"Simple APIM Tests\")\n",
115+
"tests = ApimTesting(\"Simple APIM Tests\", deployment, deployment)\n",
116116
"\n",
117117
"output = reqs.singleGet('/', msg = 'Calling Hello World (Root) API')\n",
118118
"tests.verify(output, 'Hello World from API Management!')\n",

samples/_TEMPLATE/create.ipynb

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030
"api_prefix = '' # OPTIONAL: ENTER A PREFIX FOR THE APIS TO REDUCE COLLISION POTENTIAL WITH OTHER SAMPLES\n",
3131
"\n",
3232
"# 2) Service-defined parameters (please do not change these)\n",
33-
"rg_name = utils.get_infra_rg_name(deployment, index)\n",
34-
"supported_infrastructures = [] # ENTER SUPPORTED INFRASTRUCTURES HERE, e.g., [INFRASTRUCTURE.AFD_APIM_PE, INFRASTRUCTURE.AFD_APIM_FE]\n",
35-
"utils.validate_infrastructure(deployment, supported_infrastructures)\n",
36-
"sample_folder = \"_TEMPLATE\" # ENTER THE SAMPLE FOLDER NAME HERE, e.g., '_TEMPLATE' \n",
33+
"rg_name = utils.get_infra_rg_name(deployment, index)\n",
34+
"sample_folder = \"_TEMPLATE\"\n",
35+
"nb_helper = utils.NotebookHelper(sample_folder, rg_name, rg_location, deployment, [INFRASTRUCTURE.SIMPLE_APIM])\n",
3736
"\n",
3837
"# 3) Define the APIs and their operations and policies\n",
3938
"\n",
@@ -74,19 +73,11 @@
7473
" 'apis': {'value': [api.to_dict() for api in apis]}\n",
7574
"}\n",
7675
"\n",
77-
"# 2) Infrastructure must be in place before samples can be layered on top\n",
78-
"if not utils.does_resource_group_exist(rg_name):\n",
79-
" utils.print_error(f'The specified infrastructure resource group and its resources must exist first. Please check that the user-defined parameters above are correctly referencing an existing infrastructure. If it does not yet exist, run the desired infrastructure in the /infra/ folder first.')\n",
80-
" raise SystemExit(1)\n",
76+
"# 2) Deploy the bicep template\n",
77+
"output = nb_helper.deploy_bicep(bicep_parameters)\n",
8178
"\n",
82-
"# 3) Run the deployment using the utility function that handles working directory management\n",
83-
"output = utils.create_bicep_deployment_group_for_sample('_TEMPLATE', rg_name, rg_location, bicep_parameters)\n",
84-
"\n",
85-
"# 4) Print a deployment summary, if successful; otherwise, exit with an error\n",
86-
"if not output.success:\n",
87-
" raise SystemExit('Deployment failed')\n",
88-
"\n",
89-
"if output.success and output.json_data:\n",
79+
"if output.json_data:\n",
80+
" apim_name = output.get('apimServiceName', 'APIM Service Name')\n",
9081
" apim_gateway_url = output.get('apimResourceGatewayURL', 'APIM API Gateway URL')\n",
9182
"\n",
9283
"utils.print_ok('Deployment completed')"

samples/authX-pro/create.ipynb

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"outputs": [],
1919
"source": [
2020
"import utils\n",
21-
"import time\n",
2221
"from apimtypes import *\n",
2322
"\n",
2423
"# 1) User-defined parameters (change these as needed)\n",
@@ -29,28 +28,21 @@
2928
"api_prefix = 'authX-pro-' # OPTIONAL: ENTER A PREFIX FOR THE APIS TO REDUCE COLLISION POTENTIAL WITH OTHER SAMPLES\n",
3029
"\n",
3130
"# 2) Service-defined parameters (please do not change these)\n",
32-
"rg_name = utils.get_infra_rg_name(deployment, index)\n",
33-
"supported_infrastructures = [INFRASTRUCTURE.SIMPLE_APIM, INFRASTRUCTURE.AFD_APIM_PE, INFRASTRUCTURE.APIM_ACA] # ENTER SUPPORTED INFRASTRUCTURES HERE, e.g., [INFRASTRUCTURE.AFD_APIM_PE, INFRASTRUCTURE.AFD_APIM_FE]\n",
34-
"utils.validate_infrastructure(deployment, supported_infrastructures)\n",
31+
"rg_name = utils.get_infra_rg_name(deployment, index)\n",
3532
"sample_folder = \"authX-pro\"\n",
36-
"\n",
37-
"# Set up the signing key for the JWT policy\n",
38-
"jwt_key_name = f'JwtSigningKey{int(time.time())}'\n",
39-
"jwt_key_value, jwt_key_value_bytes_b64 = utils.generate_signing_key()\n",
40-
"utils.print_val('JWT key value', jwt_key_value) # this value is used to create the signed JWT token for requests to APIM\n",
41-
"utils.print_val('JWT key value (base64)', jwt_key_value_bytes_b64) # this value is used in the APIM validate-jwt policy's issuer-signing-key attribute \n",
33+
"nb_helper = utils.NotebookHelper(sample_folder, rg_name, rg_location, deployment, [INFRASTRUCTURE.AFD_APIM_PE, INFRASTRUCTURE.APIM_ACA, INFRASTRUCTURE.SIMPLE_APIM], True)\n",
4234
"\n",
4335
"# 3) Set up the named values\n",
4436
"nvs: List[NamedValue] = [\n",
45-
" NamedValue(jwt_key_name, jwt_key_value_bytes_b64, True),\n",
37+
" NamedValue(nb_helper.jwt_key_name, nb_helper.jwt_key_value_bytes_b64, True),\n",
4638
" NamedValue('HRMemberRoleId', Role.HR_MEMBER),\n",
4739
" NamedValue('HRAssociateRoleId', Role.HR_ASSOCIATE),\n",
4840
" NamedValue('HRAdministratorRoleId', Role.HR_ADMINISTRATOR)\n",
4941
"]\n",
5042
"\n",
5143
"# 4) Set up the policy fragments\n",
5244
"pf_authx_hr_member_xml = utils.read_policy_xml('pf-authx-hr-member.xml', {\n",
53-
" 'jwt_signing_key': jwt_key_name,\n",
45+
" 'jwt_signing_key': nb_helper.jwt_key_name,\n",
5446
" 'hr_member_role_id': 'HRMemberRoleId'\n",
5547
"}, sample_folder)\n",
5648
"\n",
@@ -62,7 +54,7 @@
6254
"\n",
6355
"# HR Product with authentication policy, including authorization via a required claim check for HR member role\n",
6456
"hr_product_xml = utils.read_policy_xml('hr_product.xml', {\n",
65-
" 'jwt_signing_key': jwt_key_name, \n",
57+
" 'jwt_signing_key': nb_helper.jwt_key_name, \n",
6658
" 'hr_member_role_id': 'HRMemberRoleId'\n",
6759
"}, sample_folder)\n",
6860
"\n",
@@ -125,19 +117,11 @@
125117
" 'products': {'value': [product.to_dict() for product in products]}\n",
126118
"}\n",
127119
"\n",
128-
"# 2) Infrastructure must be in place before samples can be layered on top\n",
129-
"if not utils.does_resource_group_exist(rg_name):\n",
130-
" utils.print_error(f'The specified infrastructure resource group and its resources must exist first. Please check that the user-defined parameters above are correctly referencing an existing infrastructure. If it does not yet exist, run the desired infrastructure in the /infra/ folder first.')\n",
131-
" raise SystemExit(1)\n",
132-
"\n",
133-
"# 3) Run the deployment using the utility function that handles working directory management\n",
134-
"output = utils.create_bicep_deployment_group_for_sample(sample_folder, rg_name, rg_location, bicep_parameters)\n",
135-
"\n",
136-
"# 4) Print a deployment summary, if successful; otherwise, exit with an error\n",
137-
"if not output.success:\n",
138-
" raise SystemExit('Deployment failed')\n",
120+
"# 2) Deploy the bicep template\n",
121+
"output = nb_helper.deploy_bicep(bicep_parameters)\n",
139122
"\n",
140-
"if output.success and output.json_data:\n",
123+
"if output.json_data:\n",
124+
" apim_name = output.get('apimServiceName', 'APIM Service Name')\n",
141125
" apim_gateway_url = output.get('apimResourceGatewayURL', 'APIM API Gateway URL')\n",
142126
"\n",
143127
"utils.print_ok('Deployment completed')"
@@ -167,14 +151,14 @@
167151
"from users import UserHelper\n",
168152
"from authfactory import AuthFactory\n",
169153
"\n",
170-
"tests = ApimTesting(\"AuthX-Pro Sample Tests\")\n",
154+
"tests = ApimTesting(\"AuthX-Pro Sample Tests\", sample_folder, deployment)\n",
171155
"\n",
172156
"# Preflight: Check if the infrastructure architecture deployment uses Azure Front Door. If so, assume that APIM is not directly accessible and use the Front Door URL instead.\n",
173157
"endpoint_url = utils.test_url_preflight_check(deployment, rg_name, apim_gateway_url)\n",
174158
"\n",
175159
"# 1) HR Administrator\n",
176160
"# Create a JSON Web Token with a payload and sign it with the symmetric key from above.\n",
177-
"encoded_jwt_token_hr_admin = AuthFactory.create_symmetric_jwt_token_for_user(UserHelper.get_user_by_role(Role.HR_ADMINISTRATOR), jwt_key_value)\n",
161+
"encoded_jwt_token_hr_admin = AuthFactory.create_symmetric_jwt_token_for_user(UserHelper.get_user_by_role(Role.HR_ADMINISTRATOR), nb_helper.jwt_key_value)\n",
178162
"print(f'\\nJWT token for HR Admin:\\n{encoded_jwt_token_hr_admin}') # this value is used to call the APIs via APIM\n",
179163
"\n",
180164
"# Set up an APIM requests object with the JWT token\n",
@@ -196,7 +180,7 @@
196180
"\n",
197181
"# 2) HR Associate\n",
198182
"# Create a JSON Web Token with a payload and sign it with the symmetric key from above.\n",
199-
"encoded_jwt_token_hr_associate = AuthFactory.create_symmetric_jwt_token_for_user(UserHelper.get_user_by_role(Role.HR_ASSOCIATE), jwt_key_value)\n",
183+
"encoded_jwt_token_hr_associate = AuthFactory.create_symmetric_jwt_token_for_user(UserHelper.get_user_by_role(Role.HR_ASSOCIATE), nb_helper.jwt_key_value)\n",
200184
"print(f'\\nJWT token for HR Associate:\\n{encoded_jwt_token_hr_associate}') # this value is used to call the APIs via APIM\n",
201185
"\n",
202186
"# Set up an APIM requests object with the JWT token\n",

samples/authX/create.ipynb

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,16 @@
3030
"api_prefix = 'authX-' # OPTIONAL: ENTER A PREFIX FOR THE APIS TO REDUCE COLLISION POTENTIAL WITH OTHER SAMPLES\n",
3131
"\n",
3232
"# 2) Service-defined parameters (please do not change these)\n",
33-
"rg_name = utils.get_infra_rg_name(deployment, index)\n",
34-
"supported_infrastructures = [INFRASTRUCTURE.SIMPLE_APIM, INFRASTRUCTURE.AFD_APIM_PE, INFRASTRUCTURE.APIM_ACA] # ENTER SUPPORTED INFRASTRUCTURES HERE, e.g., [INFRASTRUCTURE.AFD_APIM_PE, INFRASTRUCTURE.AFD_APIM_FE]\n",
35-
"utils.validate_infrastructure(deployment, supported_infrastructures)\n",
33+
"rg_name = utils.get_infra_rg_name(deployment, index)\n",
3634
"sample_folder = \"authX\"\n",
37-
"\n",
38-
"# Set up the signing key for the JWT policy\n",
39-
"jwt_key_name = f'JwtSigningKey{int(time.time())}'\n",
40-
"jwt_key_value, jwt_key_value_bytes_b64 = utils.generate_signing_key()\n",
41-
"utils.print_val('JWT key value', jwt_key_value) # this value is used to create the signed JWT token for requests to APIM\n",
42-
"utils.print_val('JWT key value (base64)', jwt_key_value_bytes_b64) # this value is used in the APIM validate-jwt policy's issuer-signing-key attribute \n",
35+
"nb_helper = utils.NotebookHelper(sample_folder, rg_name, rg_location, deployment, [INFRASTRUCTURE.AFD_APIM_PE, INFRASTRUCTURE.APIM_ACA, INFRASTRUCTURE.SIMPLE_APIM], True)\n",
4336
"\n",
4437
"# 3) Define the APIs and their operations and policies\n",
4538
"\n",
4639
"# Policies\n",
4740
"# Named values must be set up a bit differently as they need to have two surrounding curly braces\n",
4841
"hr_all_operations_xml = utils.read_policy_xml('hr_all_operations.xml', sample_name = sample_folder).format(\n",
49-
" jwt_signing_key = '{{' + jwt_key_name + '}}', \n",
42+
" jwt_signing_key = '{{' + nb_helper.jwt_key_name + '}}', \n",
5043
" hr_member_role_id = '{{HRMemberRoleId}}'\n",
5144
")\n",
5245
"hr_get_xml = utils.read_policy_xml('hr_get.xml', sample_name = sample_folder).format(\n",
@@ -67,7 +60,7 @@
6760
"\n",
6861
"# 4) Set up the named values\n",
6962
"nvs: List[NamedValue] = [\n",
70-
" NamedValue(jwt_key_name, jwt_key_value_bytes_b64, True),\n",
63+
" NamedValue(nb_helper.jwt_key_name, nb_helper.jwt_key_value_bytes_b64, True),\n",
7164
" NamedValue('HRMemberRoleId', Role.HR_MEMBER),\n",
7265
" NamedValue('HRAssociateRoleId', Role.HR_ASSOCIATE),\n",
7366
" NamedValue('HRAdministratorRoleId', Role.HR_ADMINISTRATOR)\n",
@@ -99,19 +92,11 @@
9992
" 'namedValues': {'value': [nv.to_dict() for nv in nvs]}\n",
10093
"}\n",
10194
"\n",
102-
"# 2) Infrastructure must be in place before samples can be layered on top\n",
103-
"if not utils.does_resource_group_exist(rg_name):\n",
104-
" utils.print_error(f'The specified infrastructure resource group and its resources must exist first. Please check that the user-defined parameters above are correctly referencing an existing infrastructure. If it does not yet exist, run the desired infrastructure in the /infra/ folder first.')\n",
105-
" raise SystemExit(1)\n",
106-
"\n",
107-
"# 3) Run the deployment using the utility function that handles working directory management\n",
108-
"output = utils.create_bicep_deployment_group_for_sample(sample_folder, rg_name, rg_location, bicep_parameters)\n",
109-
"\n",
110-
"# 4) Print a deployment summary, if successful; otherwise, exit with an error\n",
111-
"if not output.success:\n",
112-
" raise SystemExit('Deployment failed')\n",
95+
"# 2) Deploy the bicep template\n",
96+
"output = nb_helper.deploy_bicep(bicep_parameters)\n",
11397
"\n",
114-
"if output.success and output.json_data:\n",
98+
"if output.json_data:\n",
99+
" apim_name = output.get('apimServiceName', 'APIM Service Name')\n",
115100
" apim_gateway_url = output.get('apimResourceGatewayURL', 'APIM API Gateway URL')\n",
116101
"\n",
117102
"utils.print_ok('Deployment completed')"
@@ -141,14 +126,14 @@
141126
"from users import UserHelper\n",
142127
"from authfactory import AuthFactory\n",
143128
"\n",
144-
"tests = ApimTesting(\"AuthX Sample Tests\")\n",
129+
"tests = ApimTesting(\"AuthX Sample Tests\", sample_folder, deployment)\n",
145130
"\n",
146131
"# Preflight: Check if the infrastructure architecture deployment uses Azure Front Door. If so, assume that APIM is not directly accessible and use the Front Door URL instead.\n",
147132
"endpoint_url = utils.test_url_preflight_check(deployment, rg_name, apim_gateway_url)\n",
148133
"\n",
149134
"# 1) HR Administrator\n",
150135
"# Create a JSON Web Token with a payload and sign it with the symmetric key from above.\n",
151-
"encoded_jwt_token_hr_admin = AuthFactory.create_symmetric_jwt_token_for_user(UserHelper.get_user_by_role(Role.HR_ADMINISTRATOR), jwt_key_value)\n",
136+
"encoded_jwt_token_hr_admin = AuthFactory.create_symmetric_jwt_token_for_user(UserHelper.get_user_by_role(Role.HR_ADMINISTRATOR), nb_helper.jwt_key_value)\n",
152137
"print(f'\\nJWT token for HR Admin:\\n{encoded_jwt_token_hr_admin}') # this value is used to call the APIs via APIM\n",
153138
"\n",
154139
"# Set up an APIM requests object with the JWT token\n",
@@ -164,7 +149,7 @@
164149
"\n",
165150
"# 2) HR Associate\n",
166151
"# Create a JSON Web Token with a payload and sign it with the symmetric key from above.\n",
167-
"encoded_jwt_token_hr_associate = AuthFactory.create_symmetric_jwt_token_for_user(UserHelper.get_user_by_role(Role.HR_ASSOCIATE), jwt_key_value)\n",
152+
"encoded_jwt_token_hr_associate = AuthFactory.create_symmetric_jwt_token_for_user(UserHelper.get_user_by_role(Role.HR_ASSOCIATE), nb_helper.jwt_key_value)\n",
168153
"print(f'\\nJWT token for HR Associate:\\n{encoded_jwt_token_hr_associate}') # this value is used to call the APIs via APIM\n",
169154
"\n",
170155
"# Set up an APIM requests object with the JWT token\n",

0 commit comments

Comments
 (0)