|
18 | 18 | "outputs": [], |
19 | 19 | "source": [ |
20 | 20 | "import utils\n", |
21 | | - "import time\n", |
22 | 21 | "from apimtypes import *\n", |
23 | 22 | "\n", |
24 | 23 | "# 1) User-defined parameters (change these as needed)\n", |
|
29 | 28 | "api_prefix = 'authX-pro-' # OPTIONAL: ENTER A PREFIX FOR THE APIS TO REDUCE COLLISION POTENTIAL WITH OTHER SAMPLES\n", |
30 | 29 | "\n", |
31 | 30 | "# 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", |
35 | 32 | "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", |
42 | 34 | "\n", |
43 | 35 | "# 3) Set up the named values\n", |
44 | 36 | "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", |
46 | 38 | " NamedValue('HRMemberRoleId', Role.HR_MEMBER),\n", |
47 | 39 | " NamedValue('HRAssociateRoleId', Role.HR_ASSOCIATE),\n", |
48 | 40 | " NamedValue('HRAdministratorRoleId', Role.HR_ADMINISTRATOR)\n", |
49 | 41 | "]\n", |
50 | 42 | "\n", |
51 | 43 | "# 4) Set up the policy fragments\n", |
52 | 44 | "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", |
54 | 46 | " 'hr_member_role_id': 'HRMemberRoleId'\n", |
55 | 47 | "}, sample_folder)\n", |
56 | 48 | "\n", |
|
62 | 54 | "\n", |
63 | 55 | "# HR Product with authentication policy, including authorization via a required claim check for HR member role\n", |
64 | 56 | "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", |
66 | 58 | " 'hr_member_role_id': 'HRMemberRoleId'\n", |
67 | 59 | "}, sample_folder)\n", |
68 | 60 | "\n", |
|
125 | 117 | " 'products': {'value': [product.to_dict() for product in products]}\n", |
126 | 118 | "}\n", |
127 | 119 | "\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", |
139 | 122 | "\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", |
141 | 125 | " apim_gateway_url = output.get('apimResourceGatewayURL', 'APIM API Gateway URL')\n", |
142 | 126 | "\n", |
143 | 127 | "utils.print_ok('Deployment completed')" |
|
167 | 151 | "from users import UserHelper\n", |
168 | 152 | "from authfactory import AuthFactory\n", |
169 | 153 | "\n", |
170 | | - "tests = ApimTesting(\"AuthX-Pro Sample Tests\")\n", |
| 154 | + "tests = ApimTesting(\"AuthX-Pro Sample Tests\", sample_folder, deployment)\n", |
171 | 155 | "\n", |
172 | 156 | "# 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", |
173 | 157 | "endpoint_url = utils.test_url_preflight_check(deployment, rg_name, apim_gateway_url)\n", |
174 | 158 | "\n", |
175 | 159 | "# 1) HR Administrator\n", |
176 | 160 | "# 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", |
178 | 162 | "print(f'\\nJWT token for HR Admin:\\n{encoded_jwt_token_hr_admin}') # this value is used to call the APIs via APIM\n", |
179 | 163 | "\n", |
180 | 164 | "# Set up an APIM requests object with the JWT token\n", |
|
196 | 180 | "\n", |
197 | 181 | "# 2) HR Associate\n", |
198 | 182 | "# 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", |
200 | 184 | "print(f'\\nJWT token for HR Associate:\\n{encoded_jwt_token_hr_associate}') # this value is used to call the APIs via APIM\n", |
201 | 185 | "\n", |
202 | 186 | "# Set up an APIM requests object with the JWT token\n", |
|
0 commit comments