Solution: How to copy a template via API and provide scriptUserName and ScriptUsernamePassword without having it in cleartext?

Problem: if you want to copy a template, e.g.from one folder to another, since later version of release you get an error, if in the template the propery “Run Automated Task as User” and the according pwd is set:
Code as in Jython Script Task:

    from com.xebialabs.xlrelease.domain.folder import Folder

    folder = f  olderApi.find("MyFolderName",1)
    templates = folderApi.getTemplates(folder.id)
    # Pick a first template
    template = templates[0]
    # Give it a new title
    template.title = "new template"
    templateApi.createTemplate(template,folder.id)

Runtime Error you get is:
Exception during execution:
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Could not accept encrypted password in at line number 10
Reason
This was introduced some version back in Release due to security requirements.

Suboptimal Solution 1

               from com.xebialabs.xlrelease.domain.folder import Folder
               folder = folderApi.find("myFoldername",1)
               templates = folderApi.getTemplates(folder.id)
               # Pick a first template
               template = templates[0]
               # Give it a new title
               template.title = "new template"
               template.scriptUsername=“admin”
               template.scriptUserPassword="admin"
               templateApi.createTemplate(template,folder.id)

Why is this “suboptimal”: the password is visible in clear text in the script.
How can you avoid this:
https://docs.xebialabs.com/v.10.0/release/how-to/store-encrypted-passwords/

  1. Create a pwd in your servers conf directory in the file:
    XL_RELEASE_SERVER_HOME/conf/xl-release-server.conf

 myScriptRunner.Password=TopSecret123!!

  1. Restart XLR Server and the pwd will be encrypted.

  2. Now, change the code in your jython script as follows:

from com.xebialabs.xlrelease.domain.folder import Folder
from com.xebialabs.xlrelease.config import XlrConfig
from com.xebialabs.deployit.util import PasswordEncrypter

folder = folderApi.find(“MyFolderName”,1)
myEncryptedPwd = XlrConfig.getInstance().getConfig(“myScriptRunner.Password”);
myDecryptedPassword = PasswordEncrypter.getInstance().decrypt(myEncryptedPwd);
templates = folderApi.getTemplates(folder.id)
#Pick a first template
template = templates[0]
#Give it a new title
template.title = “new template name”
template.scriptUsername=“admin”
template.scriptUserPassword=myDecryptedPassword
templateApi.createTemplate(template,folder.id)

Finally ,the pwd is encryped in the file system on the XLR Server and not visible in the script and your template is working after copying it without the need for setting Script User name and password manually.