cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Highlighted
Occasional Contributor

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...) 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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Occasional Contributor

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

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.

View solution in original post

9 REPLIES 9
Highlighted
Frequent Contributor

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

@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

 

AuthProfiles.png

 

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

Environments.pngEnvironments#2.png

Environments#3.png

 

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.

 

Highlighted
Occasional Contributor

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

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

 

Highlighted
Frequent Contributor

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

@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.

 

 

Occasional Contributor

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

Hi @New2API ,

I have analyzed the error. The error probably occurs because the script assumes the normal oAuth2.0. But I use the version of oAuth 2.0 for Azure. Other libraries must be included. I am currently in the process of rebuilding your script.

Thanks for your support.

Highlighted
Frequent Contributor

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

@giovanni_favara, for azure, below is the suggestion from smartbear help.

If you are using OAuth 2.0 Azure authentication, replace the OltuOAuth2ClientFacade class in the script with the OltuOAuth2AzureClientFacade class.

 

Thanks!

Highlighted
Occasional Contributor

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

Hi @New2API ,

before I solve the problem with Azure, I have to solve another one.

 

There are no profiles found. The array is empty.

def oAuthProfilelist = project.getOAuth2ProfileContainer().getOAuth2ProfileNameList() 

This script works and shows the profiles.

def authRepository = testRunner.getTestCase().getProject().getAuthRepository()
def entrylist = authRepository.getEntryList()

entrylist.each(){ entry -> 
	log.info('Entry "' + entry.getName() + '" class = ' + entry.getClass())
}

I use ReadyAPI 3.0.0 and have also tried it with 2.8.0. Do you have an idea why no profile can be determined?

 

Thanks for your quick and useful answers.

Highlighted
Frequent Contributor

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

@giovanni_favara, not sure why authprofile list would be empty. I tried below script against my project setting and I am getting values.

//GET PROJECT
def project = testRunner.testCase.testSuite.getProject() 
def oAuthProfilelist = project.getOAuth2ProfileContainer().getOAuth2ProfileNameList()

oAuthProfilelist.each{
	                 log.info it
}
Highlighted
Occasional Contributor

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

Hi @New2API ,

the code works by you because you have (normal) OAuth2.0-Profiles. The code doesn't works by OAuth2.0-Azure-Profiles.

I found the class 

 OAuth2AzureProfile

(https://support.smartbear.com/readyapi/apidocs/soapui/com/eviware/soapui/impl/rest/OAuth2AzureProfil...) but at moment I have no idea how use can this class. I'm  searching a way now how I can use this or a other class.

 

Thanks.

Highlighted
Occasional Contributor

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

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.

View solution in original post