VS Back to Home
Build log · SAP BTP & STRUST

From BTP Service Key to STRUST in One Command

Automating X.509 client certificates for ABAP to SAP Cloud Integration: btp CLI, cf CLI, openssl and sapgenpse in one interactive script, with the usual traps built out.

BTP service key → STRUST SSL client PSE
Creator: Vladimir Savekov (SAP BASIS Consultant) Method: btp/cf CLI + openssl + sapgenpse Read time: ~8 min

Purpose

An ABAP system calls SAP Cloud Integration through a SOAP consumer proxy and authenticates with an X.509 client certificate. On BTP you create a service key with key type Certificate, and SAP generates the certificate and private key for you. The ABAP side needs them as an SSL client PSE in STRUST, assigned to the logical port in SOAMANAGER.

Done by hand, that is a chain of small steps, and every one of them has a trap:

  • The service key JSON stores certificate and key with \n escapes. People delete them instead of turning them into line breaks, and openssl fails.
  • The PSE is built with sapgenpse and SECUDIR. One wrong SECUDIR or the name SAPSSLS.pse, and you overwrite the system's SSL server PSE.
  • The target's leaf certificate gets imported as trusted instead of its CA. It works until the day the server certificate is renewed.
  • Private keys stay behind in home directories, mail and shared folders.
  • Certificates expire, and nobody wrote down when.

Certificates are renewed regularly, so this is a task that comes back. I wanted it done the same way every time.

01

What the script does

Flow of btp_key2pse.sh Service key from btp CLI or cf CLI, checks with openssl, PKCS#12, sapgenpse PSE, get_my_name, then manual import into STRUST and SOAMANAGER. btp CLI subaccount → instance → key cf CLI (fallback) keys of CF instances -k key.json key from the cockpit openssl extract + check key ↔ cert, expiry → PKCS#12 sapgenpse own SECUDIR import_p12 → .pse get_my_name Output <name>.pse target CA .crt summary.txt Manual in SAP GUI STRUST import → SOAMANAGER logical port → test service key never printed, shredded at the end

Swipe to see the whole diagram. One run on the application server as <sid>adm. Only the STRUST import and the logical port stay manual.

1

Proxy and CLI setup

It takes the proxy from -P, K2P_PROXY or the shell, or asks for it. It finds the btp CLI the same way.

2

Pick the key

You get menus for the subaccount, the service instance and the service key. It runs btp login only if you are not logged on.

3

Download

The credentials go straight into a private temp folder. They are never printed, and they are shredded at the end.

4

Checks

Certificate and key are extracted with real line breaks. The script checks that the chain is present, that the key matches the certificate, and that it has not expired, and it warns 30 days before expiry.

5

PKCS#12 → PSE

openssl builds the PKCS#12, with SAP-readable encryption on OpenSSL 3. sapgenpse import_p12 then runs with SECUDIR set to a folder you choose.

6

Verify

sapgenpse get_my_name shows the own certificate of the new PSE.

7

Target CA (optional)

It saves the CA certificates of the Cloud Integration host for STRUST, never the leaf.

8

Summary

A text file with the source of the key, subject, issuer, expiry date and the remaining SAP GUI steps. No secrets.

02

Lesson learned: the btp CLI does not see Cloud Foundry keys

In the first real run the btp CLI listed all service instances of the subaccount, including the Cloud Integration runtime instance, but showed no service keys for it. That instance runs in the Cloud Foundry environment, and its keys are Cloud Foundry service keys. The BTP cockpit shows them, the btp CLI does not.

So the script now has a second route. If the btp CLI returns no keys for the instance, it reads the Cloud Foundry org and API endpoint from the subaccount, runs cf login if needed, and reads the keys through the CF v3 API:

bash
btp --format json list accounts/environment-instance --subaccount <id>   # org + API endpoint
cf curl "/v3/service_instances?names=<instance>&organization_guids=<org>"
cf curl "/v3/service_credential_bindings?type=key&service_instance_guids=<guid>"
cf curl "/v3/service_credential_bindings/<key-guid>/details"           # credentials
i
Space Developer role needed

Reading the key details needs the Space Developer role in that space. The script tells you if Cloud Foundry refuses.

03

Using it

Interactive

bash
./btp_key2pse.sh

It asks for everything it doesn't know: proxy, btp folder, subaccount, instance, key, PSE file name, temp folder and SECUDIR folder.

Command line

bash
./btp_key2pse.sh -N -O -a "<subaccount>" -i <instance> -b <service-key> \
                 -n <PSE-name> -w /tmp -s ~/btp_pse -t <cpi-runtime-host>

Site defaults

The script contains no customer values. Put yours once into <sid>adm's profile or a small wrapper:

env
export K2P_PROXY=<proxy-host>:<port>
export K2P_BTP_DIR=<folder with btp and cf>

Options

OptionMeaning
-a / -i / -bSubaccount (name, subdomain or GUID), service instance, service key
-k fileUse a key JSON downloaded from the cockpit instead of the CLIs
-nPSE file name. The STRUST identity is chosen at import.
-w / -sTemp folder (only its own sub-folder is shredded) / SECUDIR folder for sapgenpse
-t hostSave the target's CA certificates for STRUST
-NNo password on the PKCS#12 and no PIN on the PSE
-OOverwrite an existing PSE without asking (otherwise: overwrite / new name / quit)
-R auto|sm|cfWhere to read keys: btp CLI, Cloud Foundry, or automatic
-P / -B / -C / -UProxy, btp folder, cf folder, btp CLI server URL
-g / -SGlobal account subdomain for btp login / log on with SSO
04

Safety built in

  • Refuses system PSE names (SAPSSLS, SAPSSLC, SAPSYS …), any /usr/sap/*/sec folder, the session's own SECUDIR and any folder that already holds system PSEs.
  • A re-generated PSE is built under a temporary name. It replaces the old file only after sapgenpse succeeded, so a failed run leaves the working PSE alone.
  • Credentials, private key and PKCS#12 live only in a private temp folder (umask 077) and are shredded on exit, also on errors and Ctrl-C.
  • Passwords and PINs are typed in or left empty. They are never passed as options and never land in shell history.
  • For STRUST it saves the target's CA, never the leaf certificate.
05

The manual part

1

Transfer

Copy the .pse to your PC via a secure transfer.

2

STRUST identity

Environment → SSL Client Identities → create the identity (for a renewal, export the current PSE first).

3

Import

Change mode → PSE → Import → PSE → Save as → SSL Client → your identity. Check that all application servers are green.

4

Trust the target

Same node: import the target CA → Add to Certificate List → Save.

5

SOAMANAGER

Logical port → Consumer Security → X.509 SSL Client Certificate → your identity.

6

Test

Ping Web Service and one real call: the ping proves TLS, only a real call proves the certificate is accepted. Then delete the .pse from the PC and the server.

06

Is this the best way?

It is the right way for SAP-generated certificates (service key type Certificate): quick, no CA process, and the script removes the manual risks.

The alternative is key type External Certificate. There you create the key pair and the certificate request in STRUST, have it signed by a CA, and register only the certificate on BTP. The private key then never leaves the ABAP system.

!
The catch with External Certificate

The chain must end in a root CA that SAP BTP trusts, so self-signed certificates are rejected. If your security policy requires keys to be created on the system that uses them, it is still the cleaner long-term pattern.

07

Practical tips

i
Copying the script to the server

Use scp/WinSCP. If you paste it, use cat > btp_key2pse.sh + paste + Ctrl-D, or :set paste in vi first. Plain vi auto-indent shifts every line and breaks the embedded Python. Then check the md5 below.

!
csh users

Many <sid>adm accounts use csh as login shell. The script runs in bash through its shebang, but multi-line commands pasted at a csh prompt fail. Start bash first.

  • Needs openssl, sapgenpse, and python3 or RHEL's /usr/libexec/platform-python (Python 3.6 is enough).
  • Write the expiry date from the summary into your calendar. Renewal is the same run, and saving the new PSE as the same STRUST identity keeps the logical ports unchanged.
  • Run it on a non-production system first.
08

Download

Download btp_key2pse.sh Version 2.4.0 · bash · md5 46d735c846b72925b9c7ac389d864e25
Provided as is, without warranty. Test it on a non-production system first.