Using Policies

Objective

After completing this lesson, you will be able to use Policies.

Usage of Policies

In this lesson, we will cover the following topics:

  • What are Policies?
  • Policy types
  • Apply prebuilt Policies using the Policy Designer
  • Use predefined Policies

What are Policies?

SAP API Management provides capabilities to define the behavior of an API by using Policies. A Policy is a program that executes a specific function at runtime. They offer the flexibility to add common functionalities on an API without having to code them individually each time.

Policies provide features to secure APIs, control the API traffic, and transform message formats. You can also customize the behavior of an API by adding scripts and attaching them to policies.

You can apply a policy on the request or response stream. You can also specify if it is applicable on the proxy endpoint or the target endpoint. For information on the types of policies supported by API Management, see Policy Types.

You can use the following types of policies:

  • Predefined policy templates at SAP Business Accelerator Hub.
  • Prebuilt policies within the Policy Editor.

Policy types

The following is the list of prebuilt policies supported by API Management:

  • Access Control
  • Access Entity
  • Assign Message
  • Basic Authentication
  • Extract variables
  • Invalidate Cache
  • JavaScript
  • JSON to XML
  • Key Value Map Operations
  • Lookup Cache
  • Message Logging Policy
  • OAuth v2.0
  • OAuth v2.0 GET
  • OAuth v2.0 SET
  • Populate Cache
  • Python Script
  • Quota
  • Raise Fault
  • Reset Quota
  • Service Callout
  • Spike Arrest
  • SAML Assertion Policy
  • SOAP Message Validation Policy
  • Verify API Key
  • XML to JSON
  • XSL Transform
  • XML Threat Protection
  • Regular Expression Protection
  • JSON Threat Protection
  • Response Cache
  • Statistics Collector Policy

Read more here: Policy Types

Apply Prebuilt Policies Using the Policy Designer

To use one of the available policies, it is necessary first to consider where the policy will work. The policy editor offers the following options in the request and response:

The graphic shows the flow process of policies and endpoints in a request and response.

Policies can also be used for all calls (PostClientFlows, resources), so you do not select a PostClientFlow. In the following example, there are two PostClientFlows CatalogCollection and ServiceCollection. The policies are used for all PostClientFlows because none have been specially selected.

The graphic shows two PostClientFlows - CatalogCollection and ServiceCollecction in the Flow menu.

Security - Policies

SAP Business Technology Platform, API Management offers many out-of-the-box API security policies based on the Open Web Application Security Project (OWASP). API security best practices can be customized for your enterprise requirements.

There is a blog series that showcases the security policies from SAP Business Technology Platform, API Management to secure and protect the enterprise APIs, as shown in the following figure, SAP Cloud Platform API Management.

The graphic shows a blog series of security policies in the SAP Cloud Platform API Management. The link to the blog series is provided in the following text.

You will find the blog series here: SAP Cloud Platform API Management – API Security Best Practices Blog Series

Logging and Monitoring Policies

The Message Logging policy lets you send syslog messages to third-party log management services, such as Splunk, SumoLogic, Loggly, or similar log management services.

A blog with the Message Logging Policy and Splunk can be found here: Splunk – Part 1 : SAP APIM Logging & Monitoring | SAP Blogs

A blog with the Message Logging Policy and Loggly can be found here: Part 7 – API Security Best Practices – Log all API interactions | SAP Blogs

Use Predefined Policies

There are predefined sets of policies for specific applications. They can be found in the SAP Business Accelerator Hub.

Navigate to https://api.sap.com/ to Categoriesand select the resource APIs.

Screenshot of the SAP API Business Hub. Choose Categories > APIs.

Under the Policy Template tab SAP Business Accelerator Hub, you will find over 20 policy templates for immediate use.

Screenshot of the Policy Templates tab where you can choose from 20 policy templates.

Import a Policy Template from SAP Business Accelerator Hub

Search and find the Performance_Traceability policy template at SAP Business Accelerator Hub. Choose the Performance_Traceability tile. You will find the content at the Flow Type.

The following is an example with these two items:

  • Flow Type: ProxyEndPoint PreFlow
  • Content: JavaScript file
Screenshot of the Performance_Traceability policy template. The Flow Type and Content items are highlighted in red.

To download the complete policies, choose the Download button in the upper right corner and save the *.zip file locally to your computer.

Choose Download.

Switch to the Develop view and choose the Policy Templates tab.

Choose the Policy Templates tab.

Then, import the previous locally stored policy template through the Import button.

In the end, the Performance_Traceability template is now imported into the SAP Business Accelerator Hub.

To place the policy template, navigate to the API in which you want to use the policy, and navigate to the Policy Editor. Choose Edit so that the Policy Template button becomes active.

Choose Edit so that the Policy Template button becomes active.

Now, choose the Apply button to import the policy template. Then select the previously imported policy template and choose Apply.

Choose Apply to import the policy template.
Select the imported policy template and choose Apply.

The policy template has been imported and inserted into the corresponding flow.

Screenshot shows the policy template has been imported and inserted into the corresponding flow.

After the update, save and redeploy, the policy template will be active.

Summary

SAP API Management provides capabilities to define the behavior of an API by using policies. These capabilities can be used in both the request and the response. There are policies for the transformation of the payload and calls to external, for example, to log in using OAuth 2.0 and much more. In particular, the security policies are useful. SAP offers federal policies and policy templates for certain use cases. They can be easily imported.

Add Policies for Basic Authentication Against the S/4HANA Demo System

Business Scenario

To use the interfaces in API management, authentication against the source interface is necessary, which is accomplished through a policy implementation. The creation of connections and artifacts is indicated with red markings in the following component diagram.S/4HANA Proxy

Task Flow

In this exercise, you will perform the following tasks:

  1. Add the Message Policy.
  2. Add the Basic Authentication Policy.
  3. Test your policies.
  4. Monitor your API calls.

Prerequisites

You have a functioning API proxy as described in previous exercises.

Explore the API, Policies, and More at SAP Business Accelerator Hub

Business Scenario

Exploring the SAP Business Accelerator Hub to identify available APIs, policies, and other artifacts enables you to expedite your integrations, extensions, and innovations.

Task Flow

In this exercise, you will perform the following tasks:

  1. Log on to the SAP Business Accelerator Hub (https://api.sap.com).
  2. Explore the policies.
  3. Explore the APIs at the SAP Business Accelerator Hub.

Prerequisites

You can do the following exercise even without access to SAP Integration Suite.

Exercise Outcome

Gain a comprehensive understanding of the SAP Business Accelerator Hub and its extensive collection of available APIs for your utilization.

What will you learn from this exercise?

By exploring the SAP Business Accelerator Hub, you can discover and analyze API Management policies, allowing you to determine their suitability for various purposes based on your needs and objectives.

Task 1: Log on to SAP Business Accelerator Hub

Steps

  1. Log on to the SAP Business Accelerator Hub.

    1. Open the link: https://api.sap.com

      Screenshot of the SAP Business Accelerator Hub.
    2. Log in using the Login button to try out the APIs.

    3. Navigate to ExploreAPIs.

      To explore the SAP Business Accelerator Hub, choose Explore > APIs.

Task 2: Explore the APIs at SAP Business Accelerator Hub

Steps

  1. Explore the APIs.

    1. Navigate back to https://api.sap.comExploreAPIs.

    2. Choose ODATA V2.

    3. In the search bar, enter Purchase Order and choose Enter.

      You see a tile with the name: Purchase Order.Choose ODATA V2, enter purchase order, and choose Enter.

    4. Choose the Purchase Order tile. You will find a lot of information there.

      Choose Try Out.

      Note

      The Purchase Order ODATA V2 is marked as deprecated, but don't worry it works, so try it out.
    5. Choose the Try Out button.

      Choose Run.
    6. On the left side, find Purchase OrderGET /APurchaseOrder and choose it. On top of the page, you see the chosen context, /A_PurchaseOrder.

    7. Choose the Run button.

      You get an HTTP Status Code 200 and a filled Response Body and Response Header.