loadCertificatesFromZip.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import sys
  2. import os
  3. import pathlib
  4. import zipfile
  5. import json
  6. if len(sys.argv)<2:
  7. print("Use {}/scripts/runPython.sh {} file.zip".\
  8. format(os.path.expanduser('~'),sys.argv[0]))
  9. sys.exit(0)
  10. scriptPath=pathlib.Path(__file__)
  11. upOne=str(scriptPath.parents[1])
  12. sys.path.insert(0,upOne)
  13. import labkeyInterface
  14. setupDir=os.path.join(os.path.expanduser('~'),'.labkey')
  15. if not os.path.isdir(setupDir):
  16. os.mkdir(setupDir)
  17. zpath=pathlib.Path(sys.argv[1])
  18. user=zpath.stem
  19. userDir=os.path.join(setupDir,user)
  20. if not os.path.isdir(userDir):
  21. os.mkdir(userDir)
  22. #unzip .key, and .crt files to userDir
  23. caName=None
  24. with zipfile.ZipFile(sys.argv[1],'r') as zipObj:
  25. zipObj.extract(user+'.crt',userDir)
  26. zipObj.extract(user+'.key',userDir)
  27. zipList=zipObj.namelist()
  28. for f in zipList:
  29. if f.find('CA')>-1:
  30. caName=f
  31. zipObj.extract(f,userDir)
  32. #create and dump a config object
  33. SSLconfig={}
  34. SSLconfig['user']=os.path.join(userDir,user+'.crt')
  35. SSLconfig['key']=os.path.join(userDir,user+'.key')
  36. SSLconfig['ca']=os.path.join(userDir,caName)
  37. connectionConfig={}
  38. connectionConfig['SSL']=SSLconfig
  39. connectionConfig['host']='https://merlin.fmf.uni-lj.si'
  40. labkeyConfig={}
  41. labkeyConfig['user']='guest'
  42. labkeyConfig['password']='notUsed'
  43. connectionConfig['labkey']=labkeyConfig
  44. #generate a testNetwork.json file
  45. nfile=os.path.join(setupDir,'testNetwork.json')
  46. with open(nfile,'w') as f:
  47. json.dump(connectionConfig,f,indent=3)
  48. net=labkeyInterface.labkeyInterface()
  49. net.init(nfile)
  50. csrf=net.getCSRF()
  51. if csrf==None:
  52. print("Failed")
  53. else:
  54. print("Success: {}".format(csrf))
  55. print("Done")