cancel
Showing results for 
Search instead for 
Did you mean: 

BTP CPI. get Security Material in groovyscript problem

03-18-2023 11:18 AM
wkpk Participant
3173 views 2 comments Go to solution
0 Likes
Subscribe

I would like to connect and use Google api of openconnector through CPI.


I used an http adapter on the sender, and I succeeded in hardcoding and deploying the authorization of header through groovyscript and calling it from POSTMAN.

Next, hard coding seems to have a management problem, so i tried to change it to Security Material... But it's not working.

To be exact, the API that gets security material information does not seem to work well.

this is my Security Material

*The User value is the User value of OPENCONNECTOR.
In Password, the organization and element of OPENCONNECTOR are concatenated as comma.



this is my GroovyScript :
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import com.sap.it.api.ITApiFactory;
import com.sap.it.api.mapping.ValueMappingApi
import com.sap.it.api.securestore.SecureStoreService
import com.sap.it.api.securestore.UserCredential
import com.sap.it.api.securestore.exception.SecureStoreException

def Message processData(Message message) {
    def secureStorageService =  ITApiFactory.getService(SecureStoreService.class, null)
    def credentials  = secureStorageService.getUserCredential('GoogleDrive')
    
    def user = credentials.getUser()
    def password = credentials.getPassword()
    def (organization, element) = password.split(',')
    
    String authorization ="User ${user}, Organization ${organization}, Element ${element}"
    message.setHeader("Authorization", 'authorization ')
    return message;
}

this is error :


How can solve this problem?
0 Likes

Accepted Solutions (1)

Accepted Solutions (1)

wkpk
Participant

I solved the problem.

I changed it to a secure parameter and changed the Getusercrendental and getpassword strings, and it works well

Answers (1)

Answers (1)

Chris_XI
Discoverer
0 Likes

Hi, 
I tried the same, but as I want to have user and password secured, I did not want to switch to Secure Parameter. 
With this script I am able to read out also User Credentials.

I like to add Debug infos....not necessary, but nice to have.  

 

import com.sap.gateway.ip.core.customdev.util.Message
import com.sap.it.api.ITApiFactory
import com.sap.it.api.securestore.SecureStoreService

def Message processData(Message message) {
    def map = message.getProperties()
    def debugCounter = map.keySet().findAll { it.startsWith("Debug_") }.size() + 1

    try {
        // Retrieve SecureStoreService instance (with null as the second argument)
        SecureStoreService secureStoreService = ITApiFactory.getService(SecureStoreService.class, null)
        if (secureStoreService == null) {
            message.setProperty("Debug_${debugCounter++}", "Error: SecureStoreService could not be instantiated.")
            return message
        }

        // Access stored user credentials
        def credential = secureStoreService.getUserCredential("Name_of_Credential")
        if (credential == null) {
            message.setProperty("Debug_${debugCounter++}", "Error: No credentials found for Credentials.")
            return message
        }

        // Extract username and password
        String username = credential.getUsername()
        String password = new String(credential.getPassword()) // Convert char array to String

        // Store debugging information
        message.setProperty("Debug_${debugCounter++}", "Successfully retrieved credentials from Secure Store.")
        message.setProperty("Username", username)
        message.setProperty("Password", password) 

    } catch (Exception e) {
        message.setProperty("Debug_${debugCounter++}", "Exception while accessing Secure Store: " + e.getMessage())
    }

    return message
}