Follow me on Instagram Follow

Integrating Postman with OPA - Fixing empty body error (with screenshots)

How to integrate Postman with OPA and fix the issue of empty body response in postman

Implementing Attribute-Based Access Control (ABAC) Using Open Policy Agent (OPA)

OPA is available for Windows and Linux. In this post we will see the whole process of implementing ABAC using Rego and OPA in Windows. Requirements for this are given below

Requirements

  1. OPA software
  2. VSCode
  3. Postman website - for API testing
  4. Postman desktop client

Setting up Environment - Downloading required softwares

  1. OPA software. You can download OPA software by following this link
  2. Once software is installed we will add this to path for easy access via terminal. To add to path follow this
    • Go to system environment
    • Click on environment variables --> Path
    • Then double click on path-->New-->Then paste the address where opa.exe is saved
    • Verify if OPA is successfully added to path by using this
      opa version
    • If you see such output in the terminal you have successfully added the OPA to path

    Writing rego code to manage policy

  3. Since we have added OPA to path now we have to write the code in Rego. We can use VScode or any other application like notepad++. Here I am using VSCode. Open VS code and create a file named policy.rego Make sure it ends with .rego
  4. Write a simple policy such that if use is manager allow him else block. For this we will write simple code.
    package hr_system
    
    default allow = false
    # Manager
    allow{
        input.user == "manager"
        }

    To download complete code of project click here

  5. Our next step is to test this. There are two ways to test if our policy is working.
    1. Using local machine
    2. Using postman
    We are using postman so that it meets the requirements.
  6. Setting up for postman

    • Naviagte to postman website and in the top right click on Signup
    • On the left side click on New --> HTTP
    • Now you will have something like this. Stop here and navigate back to VScode

    In VScode press Ctrl + backtick (the key below ESC key) to open the terminal in VScode. Make sure you are in terminal tab

    Starting the server

  7. Paste the following code in the termianl to start the server
  8. opa run --server
  9. Navigate back to the postman dashboard and change the request type GET to POST and use the following address in the field: http://localhost:8181/v1/data/hr_system/allow
  10. below this field click on raw and select JSON as input. and paste the following JSON code there
    {
        "input":{
            "role":"manager"
        }
    }
    
    Then click on send.

    You will see the error to download the postman desktop agent. Simply download it and install it.

  11. When you intall the postman desktop agent successfully. Go to VSCode and open a new terminal. Make sure that opa server is always running
  12. When new terminal paste the following command there. This command uploads the policy to the OPA server so that you do not get empty body response.
    url -X PUT --data-binary @policy.rego http://localhost:8181/v1/policies/hr_system

    If successful you will see {} in return

    Whenever you make changes in policy.rego file you have to rerun the above command to update the policy in OPA Server. Make sure to save the code before running this command else content won't be updated

  13. Navigate back to the postman dashboard and again Send the request

    You will get the response saying { "result": true }

Hope this post helps. That's all for today. Leave a comment if you need help at any step☺️

Post a Comment

Leave your opinion or any doubt about this article And if you have any queries please feel free to ask. Don't try to spam, our team reviews every comment.
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.