Get SSL details thorugh portalpy

2480
11
Jump to solution
12-14-2016 08:16 AM
ADITYAKUMAR1
Occasional Contributor III

Hi team,

  I am working on portal.py. I am trying to get the details of SSL certficates which the portal is using. 

 I have tried with some code but nothing is working. Any suggestion on this will be helpful.

import sys
sys.path.append('..')   
import portalpy     
import csv   
import ssl, socket  
portalUrl           = "https://xxx-xxx.xx.xx.com:7443/arcgis"
portalAdminUser     = "xxxx"
portalAdminPassword = "xxxx"        
portal = portalpy.Portal(portalUrl, portalAdminUser,portalAdminPassword)     
users = portal.search_users('(role:account_admin OR role:account_publisher  OR role:account_user)')   
activeUser = portal.logged_in_user()      
print activeUser['email']     
ctx = ssl.create_default_context()      
s = ctx.wrap_socket(socket.socket(),server_hostname='http://xxx-xxx.xxxx.com/arcgis/home/')     
s.connect((portal, 443))     
cert = s.getpeercert()      
print cert

Thanks

Aditya Kumar

Tags (2)
0 Kudos
11 Replies
JoshuaBixby
MVP Esteemed Contributor

If you are using 2.7.9 or later, the examples already provided will get you the information you want.  The cert information is returned as a dictionary containing a mix of tuple, Unicode, string, and long; so you just need to explore the structure of that dictionary to know which keys and values to work with.

Using GeoNet as an example:

>>> import ssl, socket
>>> 
>>> hostname = 'geonet.esri.com'
>>> ctx = ssl.create_default_context()
>>> s = ctx.wrap_socket(socket.socket(), server_hostname=hostname)
>>> s.connect((hostname, 443))
>>> cert = s.getpeercert()
>>> 
>>> props = []
>>> props.append(('Valid from', cert['notBefore']))
>>> props.append(('Valid to', cert['notAfter']))
>>> props.append(('Serial number', cert['serialNumber']))
>>> 
>>> for name,value in props:
...     print "{:<20}{}".format(name, value)
...     
Valid from          Sep 30 00:00:00 2016 GMT
Valid to            Nov 29 23:59:59 2017 GMT
Serial number       616FD344BDA2F974F351E1F18CFE8881
>>> 
RebeccaStrauch__GISP
MVP Emeritus

Joshua,

I was able to run this in the Pro python window, but had to add ( ) around the print string.  Based on things I've read on one of Dan's /blogs/dan_patterson/2016/05/09/the-links?sr=search&searchId=a1012d53-da1f-4df7-b109-573165a00e62&se...‌ blogs, that is a requirement for the 3.x python flavors.

So, yours is the correct answer, but here it is with the change

import ssl, socket
hostname = 'geonet.esri.com'

ctx = ssl.create_default_context()
s = ctx.wrap_socket(socket.socket(), server_hostname=hostname)
s.connect((hostname, 443))
cert = s.getpeercert()

props = []
props.append(('Valid from', cert['notBefore']))
props.append(('Valid to', cert['notAfter']))
props.append(('Serial number', cert['serialNumber']))

for name,value in props:
     print ("{:<20}{}".format(name, value))

I was able to successfully run this against my own hostname.  thanks.