Select to view content in your preferred language

Obscure password in a script

3590
3
Jump to solution
05-28-2020 06:15 AM
MKF62
by
Frequent Contributor

I'm trying to automate a process of checking to see if my server services are running every morning. I'm following this example script, but I need to store my credentials in the script as I will be running it in the early morning hours and will not be sitting there to input my credentials every time it runs. Hard-coding my username and password into the script seems like a bad idea since it will be connecting to the internet by making web requests, so how do I go about obscuring or encrypting it?

This will be a standalone python script run using windows task scheduler. Working with ArcMap and Enterprise 10.7, python 2.7.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
MKF62
by
Frequent Contributor

Thanks, I was able to implement two solutions and both work equally well. I went with keyring because it seems more secure than just creating a separate python module to import into the script to get the username/password from. I'll document exactly what I did for the sake of clarity when others come across this question.

Option #1: Create a separate python module

Basically, you have two scripts. One .py file with your code doing whatever you want upon login, the other .py file with your username and password:

#credentials.py

username = MyUserName
password = MyPassword

Then import that module into your code that's running whatever you want:

import arcpy
from credentials import username, password

#Log into whatever with username and password variables to do stuff
‍‍‍‍‍‍‍‍‍‍‍‍‍

Option #2: Use keyring

Pip install keyring into the ArcGIS python interpreter. Open Credentials Manager (windows) on your computer from the control panel. Go to Windows Credentials > Add Generic Credential. Fill out the internet or network address with any name (you'll reference it as a service name in your python script). Fill out the username and password with the appropriate credentials for whatever application you want your python script to login to. In your python script that utilizes these credentials:

import arcpy, keyring

#The username should be what you entered as a username in the credentials manager
username = 'MyUserName'
#The first argument is whatever you entered as the internet/network address in credentials manager
password = keyring.get_password('AGSServicesStatus', username)‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

3 Replies
JoshuaBixby
MVP Esteemed Contributor

Basically the same question was asked a couple weeks ago, see https://community.esri.com/thread/253235-save-portal-password-securely .

MKF62
by
Frequent Contributor

Thanks, I was able to implement two solutions and both work equally well. I went with keyring because it seems more secure than just creating a separate python module to import into the script to get the username/password from. I'll document exactly what I did for the sake of clarity when others come across this question.

Option #1: Create a separate python module

Basically, you have two scripts. One .py file with your code doing whatever you want upon login, the other .py file with your username and password:

#credentials.py

username = MyUserName
password = MyPassword

Then import that module into your code that's running whatever you want:

import arcpy
from credentials import username, password

#Log into whatever with username and password variables to do stuff
‍‍‍‍‍‍‍‍‍‍‍‍‍

Option #2: Use keyring

Pip install keyring into the ArcGIS python interpreter. Open Credentials Manager (windows) on your computer from the control panel. Go to Windows Credentials > Add Generic Credential. Fill out the internet or network address with any name (you'll reference it as a service name in your python script). Fill out the username and password with the appropriate credentials for whatever application you want your python script to login to. In your python script that utilizes these credentials:

import arcpy, keyring

#The username should be what you entered as a username in the credentials manager
username = 'MyUserName'
#The first argument is whatever you entered as the internet/network address in credentials manager
password = keyring.get_password('AGSServicesStatus', username)‍‍‍‍‍‍‍‍‍‍‍‍
NickHarvey
Frequent Contributor

Hi All - I found that a previous version of keyring is already installed in Pro (see pic).  Is it necessary to Pip install keyring if Pro is available on the machine where the script will be executed?   I tried cloning the environment, but couldn't update the keyring version (will come back to that).  Anyway, for simplicity sake (apart from the version) is there an issue with using this already-available package? 

NickHarvey_2-1636491546278.png

 

 

0 Kudos