Introduction
MI Scripting Toolkit supports three separate authentication schemes. These schemes are listed in the table below, alongside their compatibility with available Granta MI server configurations:- [1] Available on Windows only
Autologon
To use autologon (also known as Integrated Windows Authentication), useSessionBuilder.with_autologon():
NOTE
To use autologon with MI Scripting Toolkit your Granta MI server must be configured in Windows authentication mode.Windows Task Scheduler and autologon
When scheduling scripts to run periodically in a Windows environment, the Task Scheduler is commonly used. It can be configured to set both the timing of the execution and the identity that the script runs as. A Scheduled Task is typically configured to run as a stored identity. The password for the identity is stored as part of the configuration, and when the task is executed, the script is run as that identity. This allows the Python script to authenticate securely with Granta MI. Additional configuration is available in the Triggers and Actions tabs. Triggers define when the script executes. Generally this is specified in terms of the day of the week and the time. Actions define the task itself, which for Python scripts is the execution of a specific executable. In Windows, the Python executable is typically available either withpy or python. If these are not available, specify the full
path to the executable (accessible in Python by running sys.executable).
Some additional recommendations:
- A service principal used as the account in a task must have the permission “Log On As Batch Job”.
- Select the radio button Run whether user is logged on or not, since in general the service principal is not logged in when the task executes.
- Uncheck the Do not store password box, since the session needs to access a remote Granta MI server. This means that if the password changes, it must be re-entered in Task Scheduler.
NOTE
If the Python script is executed within a virtual environment, write a batch or powershell script that first activates the virtual environment before the script is run. In this case, the Actions tab would reference the script, which in turn executes the Python script.Basic authentication
To connect with basic authentication, useSessionBuilder.with_credentials() to specify the username, password,
and optional domain:
WARNING
Basic authentication does not define any encryption of credentials. As a result, it is strongly recommended to use HTTPS when using basic authentication to ensure unencrypted credentials are not sent to the server. Credentials should not be hard coded in scripts, and instead should be provided at runtime. Three common approaches for managing credentials securely are described below. Use the approach which best fits your needs based on interactive or batch execution and other security needs.getpass
The Python standard library includes thegetpass module, which prompts the user for the password in the command
prompt without displaying the entered text. An example of using getpass to prompt the user for the password is shown
below:
Environment variables
Credentials can be injected into the script using environment variables. Theos standard library module provides
access to environment variables, and can be used with SessionBuilder.with_credentials() as follows:
Keyring
Keyring provides simple access to secure credential stores in Windows, Linux, and macOS in Python. Keyring leverages the Windows Credential Locker in Windows and the Secret Service API in Linux. Linux support relies on one of a number of different credential storage methods depending on the Linux distribution. In Ubuntu 22.04, the GNOME Password Manager is used.NOTE
Thekeyring package is installed automatically with the [oidc] extra.
Before proceeding, be aware of the following considerations when using keyring.
keyringitself does not store the credentials, it relies on an external back end. Environments other than Windows and Ubuntu may provide these, but their configuration can be more complex. See the keyring documentation for more details.- In Windows, all credentials stored by a user are visible to that user. As a result, any credentials stored by a shared or service account are visible to everyone with access to that account.
- In Ubuntu, credentials are only accessible if a password was used to log in. If the user was logged in automatically, the credential store must be unlocked before it can be accessed.
keyringhas a plug-in architecture, which is used to provide alternative (and potentially insecure) back ends. However, these require installing additional packages from PyPI and manually selecting an alternative back end.
Installing keyring
Installing keyring is typically done in the same way in both Windows and Ubuntu 22.04, despite the different
credential storage back ends. The package is installed using pip:
keyring package provides a command line interface, which can be accessed by entering keyring on the command
line directly. Confirm the back end in use by running the following on the command line:
keyring fails, thus ensuring that credentials are not inadvertently
stored using an insecure method:
Managing credentials using the command line
The simplest way to store a credential usingkeyring is to use the command line directly.
The following command invokes the keyring Python package on the command line and creates a credential for the
username “granta-admin-user” and service “my-python-script”:
keyring then prompts for the user to enter a password, which is stored securely using either Windows Credential
Locker or Secret Service depending on the platform. To retrieve the stored credential, use the following:
Managing credentials programmatically
It is sometimes useful to store credentials programmatically in Python. This approach is used in MI Scripting Toolkit to securely persist OpenID Connect (OIDC) tokens. The cells below demonstrate how to perform the same operation as above, but in Python:SessionBuilder.with_credentials() without being visible on screen. To access a credential programmatically, use
the following:
NOTE
Since the same persistence backend is used for both the command line tool and Python interface, credentials can be stored using the command line and accessed programmatically. Credentials are updated by setting the password for the same username and service:delete_password function:
OIDC
Open ID Connect (OIDC) authentication is a modern, open approach to authentication which relies on federation to connect disparate systems with shared identities.NOTE
To use OIDC with MI Scripting Toolkit your Granta MI server must be configured in OIDC mode. For more information about supported OIDC IdPs and for configuration and setup documentation, refer to Granta MI Administration and Configuration on Ansys help.NOTE
To use OIDC with MI Scripting Toolkit you must install the[oidc] extra. See Getting started for more details.
Interactive mode
To use OIDC authentication interactively, useSessionBuilder.with_oidc() to obtain a OIDCSessionBuilder
object, and then use OIDCSessionBuilder.with_authorization_code_flow().
The following example loads the default web browser and redirects the user to their OIDC Identity Provider (IdP) login
screen:
OIDCSessionBuilder.with_device_code_flow() instead. The following example
displays a URL and code which are used to authenticate instead:
Batch mode: storing refresh tokens
In most cases, following a successful interactive authentication, the IdP returns both an access token and a refresh token:- Access token: Short lifetime, not transferable, not accessible on the
Sessionobject. - Refresh token: Long lifetime, transferable between scripts, accessible via the
Session.refresh_tokenproperty.
WARNING
Refresh tokens are bearer tokens that grant access to the Granta MI server, and so should be treated as confidential. For unattended use, the refresh token can be stored securely in the OS credential manager and used to login automatically. This approach uses thekeyring library, see the Keyring section above for more details.
Tokens can be securely persisted with the Session.persist_oidc_token() method: