Forum Discussion

giovanni_favara's avatar
5 years ago

How to manage different auth profiles per environments in a REST api project

Hello everybody,

i have the task to create tests in a Scrum team, which should then run in different environments (Dev, Release, Master). The speciality is, that I have to test REST services in the Azure Cloud. It is common to use a Key Vault in the Cloud, which contains the Secrets/Tokens for the different services. To access the Key Vault, it is necessary to make an OAuth 2.0 authentication. For each environment there is a Key Vault and accordingly different credentials for authentication.


The tests runs in a CD pipelines and the environment is passed as a parameter. How can I tell ReadyAPI to use a specific authentication profile for each environment?


Unfortunately, it is not possible in Azure to specify the password for "Managed Identities". The password is generated automatically, so this option is not available.


In this forum there is a solution ( that works with project properties. For a basic authentication I would accept this solution. Since the authentication is done via "OAuth 2.0 Azure" I would like to find a solution where the "Auth-Manager" of ReadyAPI is used. Does anyone have an idea?


Thanks in advance


  • giovanni_favara's avatar
    5 years ago

    Hi New2API ,

    I have now found a way to select the Auth-Profile depending on the environment. Here is my solution.


    // prefix from profile name
    def ENV
    ENV = "test environment is >>$ENV<<..."
    // profile name suffix
    def profileSuffix = "OAuth2Azure"
    def authEntry = context.getCurrentStep().testCase.testSuite.project.getAuthRepository().getEntry(ENV + profileSuffix);

    Thanks for your help.

9 Replies

  • New2API's avatar
    Frequent Contributor

    giovanni_favara , I am not sure how to answer this specific to Azure. However, Combination of AuthProfiles and Environment should work seamlessly. I have implemented this to get OAuth2.0 token for different environments.


    Step1: Create authProfiles for different environments say,





    Step 2: Create environments and assign OAuth2.0 profiles as shown below


    3. Implemented an eventhandler script to refresh access token:


    //## Get Project ##//
    def project = testRunner.testCase.testSuite.getProject() 
    //## Get TestStep ##//
    def TestStep = context.getCurrentStep()
    //## Get oAuth Profile Name ##//
    def oAuthProfileName
    def oAuthProfile 
    def TokenStatus
    def oAuthClientFacade
    def currentToken
    def ENV
    def i = 0
    if (TestStep.config.type == 'restrequest'){
    											//## get current environment ##//
    	 "Running GetAccessToken event script..."
    											ENV =
    	 "test environment is $ENV..."
    										   //## Get oAuth profile name list ##//
    										   def oAuthProfilelist = project.getOAuth2ProfileContainer().getOAuth2ProfileNameList() 
    															   oAuthProfileName = oAuthProfilelist[i]
    								 "oAuth Profile name is:  $oAuthProfileName"
    																									  // "oAuth Profile for $ENV is:  $oAuthProfileName"
    										   //## Get Token Status ##//
    										   oAuthProfile = project.getAuthRepository().getEntry("$oAuthProfileName")
    										   TokenStatus = oAuthProfile.accessTokenStatus.toString()
    			 "Access Token Status for $oAuthProfileName is:  $TokenStatus"
    										   //## Get Access Token from the server if expired ##//  
    										   if ((TokenStatus != 'RETRIEVED_FROM_SERVER') || (currentToken == oAuthProfile.getAccessToken())) {
    																										 "Retrieving token from the auth server..."
    																																			 oAuthClientFacade = new OltuOAuth2ClientFacade(TokenType.ACCESS)
    																																			 oAuthClientFacade.requestAccessToken(oAuthProfile, true)
    																										 "Access Token Status is " + oAuthProfile.accessTokenStatus 

     Above script will retrieve access token from specific auth profile based on the selected environment.


    Hope this helps!




    • giovanni_favara's avatar

      Hi New2API ,

      thx for your post. What kind of event you use for your script? I tried with "SubmitListener.beforeSubmit" and with "RequestFilter.filterRequest" but I get an error:
      Mon Jan 13 15:35:43 CET 2020: ERROR: Error in RequestFilter.filterRequest
      Mon Jan 13 15:35:43 CET 2020: ERROR: An error occurred [Error in RequestFilter.filterRequest], see error log for details


      Caused by: groovy.lang.MissingPropertyException: No such property: testRunner for class: Script3



      Event scripts cannot be debugged. Do you have a tip on how to check event-scripts?




      • New2API's avatar
        Frequent Contributor

        giovanni_favara, since I am specifically checking if my test step is REST step, I am using  TestRunListener.beforeStep event.


        Event handlers do support testRunner. So, you shouldn't be getting that error.