Forum Discussion

giovanni_favara's avatar
giovanni_favara
Contributor
5 years ago
Solved

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 (https://community.smartbear.com/t5/SoapUI-Pro/How-to-manage-different-auth-profiles-per-environments-for/td-p/112428) 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
    giovanni_favara
    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 = testRunner.testCase.testSuite.project.activeEnvironment.name
    log.info "test environment is >>$ENV<<..."
    // profile name suffix
    def profileSuffix = "OAuth2Azure"
    def authEntry = context.getCurrentStep().testCase.testSuite.project.getAuthRepository().getEntry(ENV + profileSuffix);
    
    log.info authEntry.name

    Thanks for your help.

9 Replies

  • New2API's avatar
    New2API
    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,

     qaAuthProfile

    devAuthProfile

     

     

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

     

    3. Implemented an eventhandler script to refresh access token:

     

    import com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade
    import com.eviware.soapui.support.editor.inspectors.auth.TokenType
    import com.eviware.soapui.impl.rest.OAuth2Profile.*
    
    //## 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 ##//
    											log.info "Running GetAccessToken event script..."
    											ENV = testRunner.testCase.testSuite.project.activeEnvironment.name
    											log.info "test environment is $ENV..."
    										   
    										   
    										   //## Get oAuth profile name list ##//
    										   def oAuthProfilelist = project.getOAuth2ProfileContainer().getOAuth2ProfileNameList() 
    										   
    										   oAuthProfilelist.each{
    															   oAuthProfileName = oAuthProfilelist[i]
    															   log.info "oAuth Profile name is:  $oAuthProfileName"
    
    															   if(oAuthProfileName.contains("$ENV")){
    																									  //log.info "oAuth Profile for $ENV is:  $oAuthProfileName"
    															   return                                        
    															   }
    															   i++
    															   
    										   }
    																 
    										   //## Get Token Status ##//
    										   oAuthProfile = project.getAuthRepository().getEntry("$oAuthProfileName")
    										   TokenStatus = oAuthProfile.accessTokenStatus.toString()
    										   log.info "Access Token Status for $oAuthProfileName is:  $TokenStatus"
    										   Thread.sleep(5000)
    										  
    										   //## Get Access Token from the server if expired ##//  
    										   if ((TokenStatus != 'RETRIEVED_FROM_SERVER') || (currentToken == oAuthProfile.getAccessToken())) {
    																																			 log.info "Retrieving token from the auth server..."
    																																			 oAuthClientFacade = new OltuOAuth2ClientFacade(TokenType.ACCESS)
    																																			 oAuthClientFacade.requestAccessToken(oAuthProfile, true)
    																																			 log.info "Access Token Status is " + oAuthProfile.accessTokenStatus 
    																																			 Thread.sleep(6000)                                                                                                               
    										   }
    										   
    }
    	

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

     

    Hope this helps!

     

    thanks.

     

    • giovanni_favara's avatar
      giovanni_favara
      Contributor

      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: com.eviware.soapui.support.scripting.ScriptException: 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?

       

      TIA

       

      • New2API's avatar
        New2API
        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.