|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "### 🛠️ 1. Initialize notebook variables\n", |
| 8 | + "\n", |
| 9 | + "❗️ **Run cells 1 & 2 MANUALLY (not via _Run All_)!**\n", |
| 10 | + "\n", |
| 11 | + "Configures everything that's needed for deployment. \n", |
| 12 | + "\n", |
| 13 | + "👉 **Modify entries under _1) User-defined parameters_ and _3) Define the APIs and their operations and policies_**." |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": null, |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "import utils\n", |
| 23 | + "import time\n", |
| 24 | + "from apimtypes import *\n", |
| 25 | + "\n", |
| 26 | + "# 1) User-defined parameters (change these as needed)\n", |
| 27 | + "rg_location = 'eastus2'\n", |
| 28 | + "index = 1\n", |
| 29 | + "deployment = INFRASTRUCTURE.SIMPLE_APIM\n", |
| 30 | + "tags = ['oauth-3rd-party', 'jwt', 'credential-manager', 'policy-fragment'] # ENTER DESCRIPTIVE TAG(S)\n", |
| 31 | + "api_prefix = 'oauth-3rd-party-' # OPTIONAL: ENTER A PREFIX FOR THE APIS TO REDUCE COLLISION POTENTIAL WITH OTHER SAMPLES\n", |
| 32 | + "# OAuth\n", |
| 33 | + "client_id = 'your-spotify-client-id' # ENTER THE OAUTH CLIENT ID FOR THE BACKEND API\n", |
| 34 | + "client_secret = 'your-spotify-client-secret' # ENTER THE OAUTH CLIENT SECRET FOR THE BACKEND API\n", |
| 35 | + "\n", |
| 36 | + "# 2) Service-defined parameters (please do not change these)\n", |
| 37 | + "rg_name = utils.get_infra_rg_name(deployment, index)\n", |
| 38 | + "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", |
| 39 | + "utils.validate_infrastructure(deployment, supported_infrastructures)\n", |
| 40 | + "sample_folder = \"oauth-3rd-party\"\n", |
| 41 | + "\n", |
| 42 | + "# Set up the signing key for the JWT policy\n", |
| 43 | + "jwt_key_name = f'JwtSigningKey{int(time.time())}'\n", |
| 44 | + "jwt_key_value, jwt_key_value_bytes_b64 = utils.generate_signing_key()\n", |
| 45 | + "utils.print_val('JWT key value', jwt_key_value) # this value is used to create the signed JWT token for requests to APIM\n", |
| 46 | + "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", |
| 47 | + "\n", |
| 48 | + "\n", |
| 49 | + "# 4) Set up the named values\n", |
| 50 | + "nvs: List[NamedValue] = [\n", |
| 51 | + " NamedValue(jwt_key_name, jwt_key_value_bytes_b64, True),\n", |
| 52 | + " NamedValue('MarketingMemberRoleId', Role.MARKETING_MEMBER)\n", |
| 53 | + "]\n", |
| 54 | + "\n", |
| 55 | + "# 5) Define the APIs and their operations and policies\n", |
| 56 | + "\n", |
| 57 | + "# Policies\n", |
| 58 | + "pol_artist_get_xml = utils.read_policy_xml('artist_get.xml', sample_name = sample_folder)\n", |
| 59 | + "\n", |
| 60 | + "# Read the policy XML without modifications - it already uses correct APIM named value format\n", |
| 61 | + "pol_spotify_api_xml = utils.read_and_modify_policy_xml('spotify_api.xml', {\n", |
| 62 | + " 'jwt_signing_key': '{{' + jwt_key_name + '}}', \n", |
| 63 | + " 'marketing_member_role_id': '{{MarketingMemberRoleId}}'\n", |
| 64 | + "}, sample_folder) \n", |
| 65 | + "\n", |
| 66 | + "# Define template parameters for the artists\n", |
| 67 | + "blob_template_parameters = [\n", |
| 68 | + " {\n", |
| 69 | + " \"name\": \"id\",\n", |
| 70 | + " \"description\": \"The Spotify ID of the artist\",\n", |
| 71 | + " \"type\": \"string\",\n", |
| 72 | + " \"required\": True\n", |
| 73 | + " }\n", |
| 74 | + "]\n", |
| 75 | + "\n", |
| 76 | + "# Spotify\n", |
| 77 | + "spotify_artist_get = GET_APIOperation2('artists-get', 'Artists', '/artists/{id}', 'Gets the artist by their ID', pol_artist_get_xml, templateParameters = blob_template_parameters)\n", |
| 78 | + "\n", |
| 79 | + "# APIs Array\n", |
| 80 | + "apis: List[API] = [\n", |
| 81 | + " API(f'{api_prefix}spotify', 'Spotify', f'/{api_prefix}spotify', 'This is the API for interactions with the Spotify REST API', policyXml = pol_spotify_api_xml, operations = [spotify_artist_get], tags = tags),\n", |
| 82 | + "]\n", |
| 83 | + "\n", |
| 84 | + "utils.print_ok('Notebook initialized')" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "markdown", |
| 89 | + "metadata": {}, |
| 90 | + "source": [ |
| 91 | + "### 🚀 2. Create deployment using Bicep\n", |
| 92 | + "\n", |
| 93 | + "Creates the bicep deployment into the previously-specified resource group. A bicep parameters file will be created prior to execution." |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "code", |
| 98 | + "execution_count": null, |
| 99 | + "metadata": {}, |
| 100 | + "outputs": [], |
| 101 | + "source": [ |
| 102 | + "import utils\n", |
| 103 | + "\n", |
| 104 | + "# 1) Define the Bicep parameters with serialized APIs\n", |
| 105 | + "bicep_parameters = {\n", |
| 106 | + " 'apis': {'value': [api.to_dict() for api in apis]},\n", |
| 107 | + " 'namedValues': {'value': [nv.to_dict() for nv in nvs]},\n", |
| 108 | + " 'clientId': {'value': client_id},\n", |
| 109 | + " 'clientSecret': {'value': client_secret}\n", |
| 110 | + "}\n", |
| 111 | + "\n", |
| 112 | + "# 2) Infrastructure must be in place before samples can be layered on top\n", |
| 113 | + "if not utils.does_resource_group_exist(rg_name):\n", |
| 114 | + " 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", |
| 115 | + " raise SystemExit(1)\n", |
| 116 | + "\n", |
| 117 | + "# 3) Run the deployment using the utility function that handles working directory management\n", |
| 118 | + "output = utils.create_bicep_deployment_group_for_sample(sample_folder, rg_name, rg_location, bicep_parameters)\n", |
| 119 | + "\n", |
| 120 | + "# 4) Print a deployment summary, if successful; otherwise, exit with an error\n", |
| 121 | + "if not output.success:\n", |
| 122 | + " raise SystemExit('Deployment failed')\n", |
| 123 | + "\n", |
| 124 | + "if output.success and output.json_data:\n", |
| 125 | + " apim_gateway_url = output.get('apimResourceGatewayURL', 'APIM API Gateway URL')\n", |
| 126 | + " apim_service_name = output.get('apimServiceName', 'APIM Service Name')\n", |
| 127 | + "\n", |
| 128 | + " # TODO: This should be retrieved from an output; however, the format is static.\n", |
| 129 | + " apim_oauth_redirect_url = f'https://authorization-manager.consent.azure-apim.net/redirect/apim/{apim_service_name}'\n", |
| 130 | + " utils.print_val('APIM OAuth Redirect URL', apim_oauth_redirect_url)\n", |
| 131 | + "\n", |
| 132 | + "utils.print_ok('Deployment completed')" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "metadata": {}, |
| 138 | + "source": [ |
| 139 | + "### 🗒️ 3. Authenticate API Management with Spotify\n", |
| 140 | + "\n", |
| 141 | + "❗️ **The following steps are all manual and cannot presently be automated.**\n", |
| 142 | + "\n", |
| 143 | + "We have previously created the _APIM_ application in Spotify and have also set Spotify up in Credential Manager via the just-completed bicep. \n", |
| 144 | + "\n", |
| 145 | + "#### 3.1 Set Redirect URL in Spotify\n", |
| 146 | + "\n", |
| 147 | + "Now that the API Management instance has been created, we need to update the redirect URI for the _APIM_ application in Spotify.\n", |
| 148 | + "\n", |
| 149 | + "1. Open the [Spotify Developer Dashboard](https://developer.spotify.com/dashboard), then click on the _APIM_ application.\n", |
| 150 | + "1. Press _Edit_ and remove the temporary _localhost_ Redirect URI.\n", |
| 151 | + "1. Add the `APIM OAuth Redirect URL` (see output above), then press 'Save`. \n", |
| 152 | + "\n", |
| 153 | + "#### 3.2 Log API Management into Spotify\n", |
| 154 | + "\n", |
| 155 | + "We now need to log the _APIM_ application into Spotify via OAuth 2.0.\n", |
| 156 | + "\n", |
| 157 | + "1. Open the [Azure Portal](https://portal.azure.com) and navigate to your API Management instance.\n", |
| 158 | + "1. Expand the _APIs_ blade and click on _Credential manager_. You should see the `spotify` credential provider name. Click on it.\n", |
| 159 | + "1. Press _Connections_. You should see `spotify-auth` with an `Error` status (\"This connection is not authenticated.\").\n", |
| 160 | + "1. Click on the ellipsis (...) on the right and select _Login_. This should open a dialog with Spotify, asking you to agree for Spotify and APIM to connect. Press _Agree_.\n", |
| 161 | + "1. Back in the Azure Portal, press _Refresh_ to see the `Connected` status.\n" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "markdown", |
| 166 | + "metadata": {}, |
| 167 | + "source": [ |
| 168 | + "### ✅ 4. Verify API Request Success\n", |
| 169 | + "\n", |
| 170 | + "Assert that the deployment was successful by making simple calls to APIM. \n", |
| 171 | + "\n", |
| 172 | + "❗️ If the infrastructure shields APIM and requires a different ingress (e.g. Azure Front Door), the request to the APIM gateway URl will fail by design. Obtain the Front Door endpoint hostname and try that instead." |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": null, |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [], |
| 180 | + "source": [ |
| 181 | + "import utils\n", |
| 182 | + "import json\n", |
| 183 | + "from apimrequests import ApimRequests\n", |
| 184 | + "from apimtesting import ApimTesting\n", |
| 185 | + "from users import UserHelper\n", |
| 186 | + "from authfactory import AuthFactory\n", |
| 187 | + "\n", |
| 188 | + "tests = ApimTesting(\"OAuth 3rd Party (Spotify) Sample Tests\")\n", |
| 189 | + "\n", |
| 190 | + "# 1) Marketing Member Role\n", |
| 191 | + "# Create a JSON Web Token with a payload and sign it with the symmetric key from above.\n", |
| 192 | + "encoded_jwt_token_marketing_member = AuthFactory.create_symmetric_jwt_token_for_user(UserHelper.get_user_by_role(Role.MARKETING_MEMBER), jwt_key_value)\n", |
| 193 | + "print(f'\\nJWT token for Marketing Member:\\n{encoded_jwt_token_marketing_member}') # this value is used to call the APIs via APIM\n", |
| 194 | + "\n", |
| 195 | + "# 2) Issue a direct request to API Management\n", |
| 196 | + "artist_id = '6XpaIBNiVzIetEPCWDvAFP' # Whitney Houston's Spotify Artist ID\n", |
| 197 | + "reqsApim = ApimRequests(apim_gateway_url)\n", |
| 198 | + "reqsApim.headers['Authorization'] = f'Bearer {encoded_jwt_token_marketing_member}'\n", |
| 199 | + "\n", |
| 200 | + "output = reqsApim.singleGet(f'/oauth-3rd-party-spotify/artists/{artist_id}', msg = 'Calling the Spotify Artist API via API Management Gateway URL. Response codes 200 and 403 are both valid depending on the infrastructure used.')\n", |
| 201 | + "artist = json.loads(output)\n", |
| 202 | + "tests.verify(artist['name'], 'Whitney Houston')\n", |
| 203 | + "utils.print_info(f'{artist[\"name\"]} has a popularity rating of {artist[\"popularity\"]} with {artist[\"followers\"][\"total\"]:,} followers on Spotify.')\n", |
| 204 | + "\n", |
| 205 | + "# 2) Issue requests against Front Door.\n", |
| 206 | + "# Check if the infrastructure architecture deployment uses Azure Front Door.\n", |
| 207 | + "utils.print_message('Checking if the infrastructure architecture deployment uses Azure Front Door.', blank_above = True)\n", |
| 208 | + "afd_endpoint_url = utils.get_frontdoor_url(deployment, rg_name)\n", |
| 209 | + "\n", |
| 210 | + "if afd_endpoint_url:\n", |
| 211 | + " artist_id = '2VSHKHBTiXWplO8lxcnUC9' # Taylor Swift's Spotify Artist ID\n", |
| 212 | + " reqsAfd = ApimRequests(afd_endpoint_url)\n", |
| 213 | + " reqsAfd.headers['Authorization'] = f'Bearer {encoded_jwt_token_marketing_member}'\n", |
| 214 | + " output = reqsAfd.singleGet(f'/oauth-3rd-party-spotify/artists/{artist_id}', msg = 'Calling the Spotify Artist API via API Management Gateway URL. Response codes 200 and 403 are both valid depending on the infrastructure used.')\n", |
| 215 | + " artist = json.loads(output)\n", |
| 216 | + " tests.verify(artist['name'], 'Whitney Houston')\n", |
| 217 | + " utils.print_info(f'{artist[\"name\"]} has a popularity rating of {artist[\"popularity\"]} with {artist[\"followers\"][\"total\"]:,} followers on Spotify.')\n", |
| 218 | + "\n", |
| 219 | + "tests.print_summary()\n", |
| 220 | + "\n", |
| 221 | + "utils.print_ok('All done!')" |
| 222 | + ] |
| 223 | + } |
| 224 | + ], |
| 225 | + "metadata": { |
| 226 | + "kernelspec": { |
| 227 | + "display_name": "APIM Samples Python 3.12", |
| 228 | + "language": "python", |
| 229 | + "name": "apim-samples" |
| 230 | + }, |
| 231 | + "language_info": { |
| 232 | + "codemirror_mode": { |
| 233 | + "name": "ipython", |
| 234 | + "version": 3 |
| 235 | + }, |
| 236 | + "file_extension": ".py", |
| 237 | + "mimetype": "text/x-python", |
| 238 | + "name": "python", |
| 239 | + "nbconvert_exporter": "python", |
| 240 | + "pygments_lexer": "ipython3", |
| 241 | + "version": "3.12.10" |
| 242 | + } |
| 243 | + }, |
| 244 | + "nbformat": 4, |
| 245 | + "nbformat_minor": 2 |
| 246 | +} |
0 commit comments