|
| 1 | +--- |
| 2 | +auto_validation: true |
| 3 | +time: 20 |
| 4 | +tags: [ tutorial>advanced, tutorial>free-tier, software-product>sap-integration-suite, software-product>sap-cloud-integration, software-product>sap-api-management, software-product>sap-business-technology-platform ] |
| 5 | +primary_tag: software-product>sap-api-management |
| 6 | +parser: v2 |
| 7 | +author_name: Mariajose Martinez |
| 8 | +author_profile: https://github.com/mariajosesap |
| 9 | +--- |
| 10 | +# Configure SAP API Management policies to avoid CORS issues and generate an API Key |
| 11 | +<!-- description --> Learn how to set up API Management policies to avoid CORS issues when calling the API endpoint from SAP AppGyver, also you'll learn how to apply security policies to enable the API consumption via an API Key. |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | + - You have a SAP BTP account or trial account with access to the SAP Integration Suite. |
| 16 | + - You have completed the previous tutorial [Send SMS using SAP Cloud Integration while consuming a Twilio API](btp-integration-suite-integral-cpi-urlencoded). |
| 17 | + |
| 18 | + |
| 19 | +## You will learn |
| 20 | + |
| 21 | + - How to set up API Management policies to avoid CORS issues when calling the API endpoint. |
| 22 | + - How to apply security policies to enable the API consumption via an API Key. |
| 23 | + |
| 24 | +### Set up the policy to avoid CORS issues |
| 25 | + |
| 26 | +1. Go to your SAP API Portal (in the main SAP Integration Suite portal). |
| 27 | + |
| 28 | +2. Create an API and select URL. Here you're going to paste your IFlow endpoint from past tutorials. Give it a name: `APIBestRunDemo`. Give it a path: `/https/salesOrder`, and service type `REST`. |
| 29 | + |
| 30 | +  |
| 31 | + |
| 32 | +3. After creating it, go to the Proxy Endpoint tab and add the following in this order, and save it: |
| 33 | + |
| 34 | +  |
| 35 | + |
| 36 | +4. Now, go to Policies (if it doesn't show up in the top-right of your screen, click on the 3 dots). |
| 37 | + |
| 38 | + Here you'll add the required policies to avoid CORS issues while calling this API from SAP AppGyver and configure the API Key policy later on. |
| 39 | + |
| 40 | +5. On the Edit Mode, start adding the Policies as followed: |
| 41 | + |
| 42 | + In the PostFlow inside the Proxy Endpoint configuration, add an `Assign Message` policy as an `OutgoingResponse` and name it `setCORS`. Like this: |
| 43 | + |
| 44 | +  |
| 45 | + |
| 46 | +6. Copy and paste this script (read message on line 5): |
| 47 | + |
| 48 | + <!-- cpes-file db/schema.cds --> |
| 49 | + ```XML |
| 50 | + <!-- This policy can be used to create or modify the standard HTTP request and response messages --> |
| 51 | + <AssignMessage async="false" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'> |
| 52 | + <Add> |
| 53 | + <Headers> |
| 54 | + <!-- Set up this header, however if you come across to this error: " 'Access-Control-Allow-Origin' header contains multiple values, but only one is allowed when executing the test on AppGyver ", then remove it and try it again: <Header name="Access-Control-Allow-Origin">*</Header> --> |
| 55 | + <Header name="Access-Control-Allow-Headers">set-cookie, origin, accept, maxdataserviceversion, x-csrf-token, authorization, dataserviceversion, accept-language, x-http-method, content-type, X-Requested-With, apikey</Header> |
| 56 | + <Header name="Access-Control-Max-Age">3628800</Header> |
| 57 | + <Header name="Access-Control-Allow-Methods">GET, PUT, POST, DELETE</Header> |
| 58 | + <Header name="Access-Control-Expose-Headers">set-cookie, x-csrf-token, x-http-method</Header> |
| 59 | + </Headers> |
| 60 | + </Add> |
| 61 | + <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables> |
| 62 | + <AssignTo createNew="false" type="response">response</AssignTo> |
| 63 | + </AssignMessage> |
| 64 | + ``` |
| 65 | + >Note: notice that you're already adding "apikey" as a header in the policy. |
| 66 | + |
| 67 | + You should look it like this: |
| 68 | + |
| 69 | +  |
| 70 | + |
| 71 | +7. In the ProxyEndpoint click on `+` to add one. Name it `preflight` and in the condition string paste: `request.verb == "OPTIONS"`. Like this: |
| 72 | + |
| 73 | +  |
| 74 | + |
| 75 | + This is all you need to avoid CORS issues when calling our API from SAP AppGyver. |
| 76 | + |
| 77 | +### Create a Key Value Map for your SAP Cloud Integration Credentials |
| 78 | + |
| 79 | +1. Before adding the needed policies, create first a Key Value Map with your CPI credentials (to access the Cloud Integration Platform API endpoint). You will need this credential to reference it in the API Key Policy. |
| 80 | + |
| 81 | + Go to Configure (click on the tool icon) -> go to the `Key Value Maps` tab and create one. |
| 82 | + |
| 83 | + |
| 84 | +  |
| 85 | + |
| 86 | +2. Put `CPICredentials` as the name and declare your CPI credentials (username and password). Check the `Encrypt Key Value Map` box. |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +  |
| 91 | + |
| 92 | +### Add the Verify API Key Policy |
| 93 | + |
| 94 | +Go back to your API policies. |
| 95 | + |
| 96 | +In the TargetEndpoint, add 3 policies in the PreFlow. |
| 97 | + |
| 98 | +1. Add a Key Value Map Operations policy, name it and leave it as a Incoming Request. This is needed to get the CPI Credentials created before, as a Key Value Map. Like this: |
| 99 | + |
| 100 | +  |
| 101 | + |
| 102 | + Copy and Paste this script (you will be referencing 'CPICredentials' with the mapIdentifier parameter): |
| 103 | + |
| 104 | + <!-- cpes-file db/schema.cds --> |
| 105 | + ```XML |
| 106 | + <KeyValueMapOperations mapIdentifier="CPICredentials" continueOnError="false" enabled="true" xmlns="http://www.sap.com/apimgmt"> |
| 107 | + <!-- Read parameter with key "username" and assign its value to private variable BasicAuthUsername--> |
| 108 | + <Get assignTo="private.BasicAuthUsername" index='1'> |
| 109 | + <Key><Parameter>username</Parameter></Key> |
| 110 | + </Get> |
| 111 | + <!-- Read parameter with key "password" and assign its value to private variable BasicAuthPassword--> |
| 112 | + <Get assignTo="private.BasicAuthPassword" index='1'> |
| 113 | + <Key><Parameter>password</Parameter></Key> |
| 114 | + </Get> |
| 115 | + <Scope>environment</Scope> |
| 116 | + </KeyValueMapOperations> |
| 117 | + ``` |
| 118 | + Like this: |
| 119 | + |
| 120 | +  |
| 121 | + |
| 122 | +2. Add a Basic Authentication policy, name it and leave it with the `IncomeRequest` stream. Like this: |
| 123 | + |
| 124 | +  |
| 125 | + |
| 126 | + Copy and paste this script: |
| 127 | + |
| 128 | + <!-- cpes-file db/schema.cds --> |
| 129 | + ```XML |
| 130 | + <BasicAuthentication async='true' continueOnError='false' enabled='true' xmlns='http://www.sap.com/apimgmt'> |
| 131 | + <!-- Operation can be Encode or Decode --> |
| 132 | + <Operation>Encode</Operation> |
| 133 | + <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables> |
| 134 | + <!-- for Encode, User element can be used to dynamically populate the user value --> |
| 135 | + <User ref='private.BasicAuthUsername'></User> |
| 136 | + <!-- for Encode, Password element can be used to dynamically populate the password value --> |
| 137 | + <Password ref='private.BasicAuthPassword'></Password> |
| 138 | + |
| 139 | + <!-- Assign to is used to assign the encoded value of username and password to a variable. This should not be used if the operation is Decode --> |
| 140 | + <AssignTo createNew="true">request.header.Authorization</AssignTo> |
| 141 | + </BasicAuthentication> |
| 142 | + ``` |
| 143 | + |
| 144 | + You should look it like this: |
| 145 | + |
| 146 | +  |
| 147 | + |
| 148 | +3. Add a API Key verification policy to request the API Key every time the API is called (you'll later need to create the API Product and subscribe to the application to get the API Key). Add the `Verify API Key` policy, same in the PreFlow. Like this: |
| 149 | + |
| 150 | +  |
| 151 | + |
| 152 | + <!-- cpes-file db/schema.cds --> |
| 153 | + ```XML |
| 154 | + <!--Specify in the APIKey element where to look for the variable containing the api key--> |
| 155 | + <VerifyAPIKey async='true' continueOnError='false' enabled='true' |
| 156 | + xmlns='http://www.sap.com/apimgmt'> |
| 157 | + <APIKey ref='request.header.ApiKey'/> |
| 158 | + </VerifyAPIKey> |
| 159 | + ``` |
| 160 | + |
| 161 | + This should be the first policy to be triggered. Make sure it is set it up as the first one (you can change the order with the arrows in the top-right). Like this: |
| 162 | + |
| 163 | +  |
| 164 | + |
| 165 | +4. Click on Update, Save and Deploy your API project. |
| 166 | + |
| 167 | +  |
| 168 | + |
| 169 | +### Create an API Product and Subscribe to the Application |
| 170 | + |
| 171 | +1. Now you need to create the API Product. Go to the pencil icon (develop), click on the `Products` tab and create one. Put this name to your Product `APIBestRunDemoProduct`. |
| 172 | + |
| 173 | +  |
| 174 | + |
| 175 | +3. Reference your API to your Product. |
| 176 | + |
| 177 | +  |
| 178 | + |
| 179 | +4. Publish your Product, click on `Publish`. |
| 180 | + |
| 181 | +5. Then, go to the API Business Hub Enterprise portal. |
| 182 | + |
| 183 | +  |
| 184 | + |
| 185 | + You should be able to see it in your SAP API Business Hub: |
| 186 | + |
| 187 | +  |
| 188 | + |
| 189 | +6. Enter the API product and subscribe it by creating a new Application: |
| 190 | + |
| 191 | +  |
| 192 | + |
| 193 | + Name it `APIBestRunDemoApplication`, like this: |
| 194 | + |
| 195 | +  |
| 196 | + |
| 197 | + |
| 198 | +7. Here's where you can get your API Key, crucial to authenticate to this API endpoint when consuming its service. |
| 199 | + |
| 200 | +  |
| 201 | + |
| 202 | +Now you've successfully created policies to avoid CORS issues and enable an API Key to authenticate and consume your API endpoint from SAP Integration Suite. |
| 203 | + |
| 204 | +### Add additional tasks into your Integration Flow |
| 205 | + |
| 206 | +Right now, as following the whole exercises, the IFlow is returning us the SMS message sent by consuming the Twilio API. But for the purpose of the exercise with SAP AppGyver, you're going to use as the return message, mainly the receipt URL when the payment transaction is successfully done with Stripe. For this, you need to save this message in the IFlow and retrieve it at the end of the IFlow. |
| 207 | + |
| 208 | +Remember the previous tutorial about "Set up Write, Filter and Get Tasks in the Integration Flow…" for filtering the payment data and later calling the SAP C4C OData service? well, you are going to do the same, but in this case to retrieve the receipt URL from the Stripe request response. |
| 209 | + |
| 210 | +1. Go back to SAP Cloud Integration and to your Integration Flow. Add a Write task after the Stripe Connector request reply, and configure the task as following: |
| 211 | + |
| 212 | +  |
| 213 | + |
| 214 | +2. And now add a Get task at the end of the IFlow to retrieve this returned message: |
| 215 | + |
| 216 | +  |
| 217 | + |
| 218 | + Save and deploy your IFlow. |
| 219 | + |
| 220 | +Now, it's time to set up the integration with SAP AppGyver. Check out the next and final tutorial of this series: [Integrate SAP AppGyver with SAP Integration Suite](btp-integration-suite-integral-appgyver). |
| 221 | + |
| 222 | +### Check your knowledge |
| 223 | + |
| 224 | +### One more time, check your knowledge |
0 commit comments