Exemple #1
0
 def _get_response(self, cmd, *, json=None):
     if 'ws-auth' in self.prefix:
         # basic authentication
         resp = requests.get(self.prefix + cmd,
                             auth=HTTPBasicAuth(self.user, self.password),
                             json=json,
                             timeout=self.timeout)
     elif 'ws-kerb' in self.prefix:
         # kerberos authentication
         resp = requests.get(self.prefix + cmd,
                             **{
                                 "headers":
                                 KerberosTicket('HTTP@' +
                                                self.host).getAuthHeaders()
                             },
                             json=json,
                             timeout=self.timeout)
     else:
         # no authentication
         resp = requests.get(self.prefix + cmd,
                             json=json,
                             timeout=self.timeout)
     # raise exception if status is not ok
     resp.raise_for_status()
     return resp.json()
Exemple #2
0
 def test_all_runs_with_par_value():
     ws_url = "https://pswww.slac.stanford.edu/ws-kerb/lgbk/lgbk/{experiment_name}/ws/get_runs_matching_params".format(
         experiment_name="xcsdaq13")
     krbheaders = KerberosTicket(
         "*****@*****.**").getAuthHeaders()
     r = requests.post(ws_url,
                       headers=krbheaders,
                       json={
                           "USEG:UND1:3250:KACT": "3.47697",
                           "XCS:R42:EVR:01:TRIG7:BW_TDES": "896924"
                       })
     if r: print(r.json())
Exemple #3
0
def kerberos_headers(serv='*****@*****.**'):
    try:
        krbheaders = KerberosTicket(serv).getAuthHeaders()
    except kerberos.GSSError as err:
        logger.warning(str(err))
        msg = '\nEXIT: CHECK VALIDITI OF KERBEROS TICKET (commands klist, kinit)'
        print(msg)
        sys.exit()
    except err:
        logger.warning(str(err))
        msg = '\nEXIT: UNSPECIFIED ERROR at getting KerberosTicket'
        print(msg)
        sys.exit()
    return krbheaders
 def __init__(self, url=None, use_kerberos=True, user=None, pw=None):
     if use_kerberos:
         self.questionnaire_url = url or self.kerb_url
         self.krbheaders = KerberosTicket(
             "HTTP@" +
             urlparse(self.questionnaire_url).hostname).getAuthHeaders()
         self.rget = partial(requests.get, headers=self.krbheaders)
         self.rpost = partial(requests.post, headers=self.krbheaders)
     else:
         self.questionnaire_url = url or self.wsauth_url
         # Find the login information if not provided
         user = user or getpass.getuser()
         pw = pw or getpass.getpass()
         self.auth = requests.auth.HTTPBasicAuth(user, pw)
         self.rget = partial(requests.get, auth=self.auth)
         self.rpost = partial(requests.post, auth=self.auth)
Exemple #5
0
def get_for_url(url):

    krbheaders = KerberosTicket("HTTP@" +
                                urlparse(url).hostname).getAuthHeaders()
    r = requests_get(
        url, headers=krbheaders
    )  # returns {'success': True, 'value': [<old-style-responce>]}
    if not r:
        msg = '\nget_for_url try to get run times from experiment DB'\
              '\nurl: %s\nstatus_code: %d\ncontent: %s' %(url, r.status_code, r.text)
        must_to_fix_error(msg)

    jdic = r.json()
    if jdic['success']: return jdic['value']
    else:
        must_to_fix_error(
            '\nget_for_url un-successful responce: %s\nfor url: %s' %
            (str(r), url))
Exemple #6
0
    def authenticate(self, user=None, pw=None):
        """
        Authorize use of the ELog

        If neither a username or password is supplied a Kerberos ticket will be
        used to authenticate the use of the ELog. Otherwise basic HTTPBasicAuth
        will be used.

        Parameters
        ----------
        user : str, optional
            Username

        pw : str, optional
            Password. If not supplied, a prompt will launch requesting the
            password in a safe manner

        Returns
        -------
        auth: dict
            Keywords necessary to properly authenticate a ``requests.get``
        """
        auth = dict()
        logger.debug("Creating authentication information ...")
        if user:
            # Create authentication URL and params for HTTP
            logger.debug("Using HTTP authorization ...")
            url = self._base_url + '/ws-auth'
            if not pw:
                pw = getpass.getpass()
            auth['auth'] = HTTPBasicAuth(user, pw)
        else:
            user = getpass.getuser()
            # Create authentication URL and params for Kerberos
            logger.debug("Using Kerberos ...")
            url = self._base_url + '/ws-kerb'
            host = urlparse(url).hostname
            auth['headers'] = KerberosTicket('HTTP@' + host).getAuthHeaders()
        # Store internal authentication information
        self._url = url
        self._auth = auth
        self._user = user
        return auth
Exemple #7
0
    def __init__(self, url=None, use_kerberos=True, user=None, pw=None):
        if use_kerberos:
            if KerberosTicket is None:
                raise RuntimeError(
                    'Kerberos-based authentication unavailable.  '
                    'Please install krtc.')

            self.questionnaire_url = url or self.kerb_url
            self.krbheaders = KerberosTicket(
                "HTTP@" +
                urlparse(self.questionnaire_url).hostname).getAuthHeaders()
            self.rget = partial(requests.get, headers=self.krbheaders)
            self.rpost = partial(requests.post, headers=self.krbheaders)
        else:
            self.questionnaire_url = url or self.wsauth_url
            # Find the login information if not provided
            user = user or getpass.getuser()
            pw = pw or getpass.getpass()
            self.auth = requests.auth.HTTPBasicAuth(user, pw)
            self.rget = partial(requests.get, auth=self.auth)
            self.rpost = partial(requests.post, auth=self.auth)
Exemple #8
0
from urllib.parse import urlparse

URL_ENV = os.environ.get('LCLS_CALIB_HTTP', None)
URL = 'https://pswww.slac.stanford.edu/calib_ws' if URL_ENV is None else URL_ENV
URL_KRB = 'https://pswww.slac.stanford.edu/ws-kerb/calib_ws/'
HOST = 'psdbdev01'  # 'psdb-dev' # 'psanaphi103'
PORT = 9306  # 27017
USERNAME = '******'
USERPW = USERNAME[:5]
DBNAME_PREFIX = 'cdb_'
DETNAMESDB = '%sdetnames' % DBNAME_PREFIX
MAX_DETNAME_SIZE = 55
OPER = 'pcds'

try:
    KRBHEADERS = KerberosTicket("HTTP@" +
                                urlparse(URL_KRB).hostname).getAuthHeaders()
    #except kerberos.GSSError as e:
except Exception as e:
    #msg = e.message if hasattr(e, 'message') else str(e)
    #print('Exception:', msg)
    #sys.exit('Fix kerberos ticket - use command kinit')
    KRBHEADERS = None

TSFORMAT = '%Y-%m-%dT%H:%M:%S%z'  # e.g. 2018-02-07T08:40:28-0800
TSFORMAT_SHORT = '%Y%m%d%H%M%S'  # e.g. 20180207084028

# Enumerated and named parameters

PEDESTALS = 0
PIXEL_STATUS = 1
PIXEL_RMS = 2
Exemple #9
0
#!/usr/bin/env python
"""
Sample for posting to the calibration service using a web service and kerberos authentication.
Make sure we have a kerberos ticket.

2019-07-27 Murali Shankar - version 1 
"""

import requests
import json
from krtc import KerberosTicket
from urllib.parse import urlparse

ws_url = "https://pswww.slac.stanford.edu/ws-kerb/calib_ws/"
krbheaders = KerberosTicket("HTTP@" +
                            urlparse(ws_url).hostname).getAuthHeaders()

# Create a new document in the collection test_coll in the database test_db.
resp = requests.post(ws_url + "test_db/test_coll/",
                     headers=krbheaders,
                     json={"calib_count": 1})
print(resp.text)
new_id = resp.json()["_id"]

# Update an existing document
resp = requests.put(ws_url + "test_db/test_coll/" + new_id,
                    headers=krbheaders,
                    json={"calib_count": 2})
print(resp.text)

# Delete database and collection
Exemple #10
0
def getUxiDict(run):
    ##
    # get timestamp range for xtc file (???)
    ##
    xtcBeginTime = None
    xtcEndTime = None
    args_url = "https://pswww.slac.stanford.edu/ws-kerb/lgbk/"
    ws_url = args_url + "/lgbk/{0}/ws/runs".format('xcsx31116')
    krbheaders = KerberosTicket("HTTP@" +
                                urlparse(ws_url).hostname).getAuthHeaders()
    r = requests.get(ws_url, headers=krbheaders)
    runDict = (r.json()["value"])
    #print(json.dumps(r.json()["value"], sort_keys=True, indent=4))
    xtcEndTime = -1
    for thisRun in runDict:
        if thisRun['num'] == int(run):
            #print(thisRun['num'], thisRun['begin_time'], thisRun['end_time'])
            xtcBeginTimeStr = (thisRun['begin_time'].split('+')[0]).replace(
                'T', ' ')
            xtcEndTimeStr = (thisRun['end_time'].split('+')[0]).replace(
                'T', ' ')
            xtcBeginTime = int(
                (datetime.strptime(xtcBeginTimeStr, '%Y-%m-%d %H:%M:%S') -
                 datetime.utcfromtimestamp(0)).total_seconds())
            xtcEndTime = int(
                (datetime.strptime(xtcEndTimeStr, '%Y-%m-%d %H:%M:%S') -
                 datetime.utcfromtimestamp(0)).total_seconds())

    if xtcEndTime < 0:
        print(
            'something went wrong with getting the xtc time from the run database'
        )
        return None

    ##
    # put some UXI stuff here.
    ##
    uxiPath = '/reg/d/psdm/xcs/xcsx31116/usrdaq/uxi/'
    #start with actual run first.
    fname = 'uxi_x311_%04d.dat' % int(run)
    fnamePre = 'uxi_x311_%04d.dat' % (int(run) - 1)
    if int(run) == 19:  #yes, this is special....
        fnamePre = 'uxi_x311_%04d.dat' % (int(run) - 2)
    fnamePost = 'uxi_x311_%04d.dat' % (int(run) + 1)

    print('look for uxi: ', uxiPath + fname)
    minUxi = None
    maxUxi = None
    print('xtc begin: ', xtcBeginTime, xtcEndTime)

    uxiDict = {}
    try:
        configDict, minUxi, maxUxi = fillUxiDict(uxiPath + fname, xtcBeginTime,
                                                 xtcEndTime, uxiDict)
    except:
        pass

    #print 'DEBUG times: ',xtcBeginTime,minUxi
    if minUxi is None:
        configDict, tmp, tmp2 = fillUxiDict(uxiPath + fnamePre, xtcBeginTime,
                                            xtcEndTime, uxiDict)
    if xtcBeginTime < minUxi and os.path.isfile(uxiPath + fnamePre):
        print('checking the run before....')
        tmp1, tmp2, tmp3 = fillUxiDict(uxiPath + fnamePre, xtcBeginTime,
                                       xtcEndTime, uxiDict)

    if xtcEndTime > maxUxi and os.path.isfile(uxiPath + fnamePost):
        print('checking the run after....')
        tmp1, tmp2, tmp3 = fillUxiDict(uxiPath + fnamePost, xtcBeginTime,
                                       xtcEndTime, uxiDict)

    if uxiDict == {}:
        return uxiDict, None
    fidmask = int('0x1ffff', 16)
    uxiSecs = []
    uxiNsecs = []
    uxiFids = []
    for tsec, tnsec, tfid in zip(uxiDict['lcls_ts_secs'],
                                 uxiDict['lcls_ts_necs'],
                                 uxiDict['lcls_ts_high']):
        uxiSecs.append(np.int64(tsec))
        uxiNsecs.append(np.int64(tnsec))
        uxiFids.append(int(tfid) & int(fidmask))

    uxiDict['lcls_ts_secs'] = np.array(uxiSecs)
    uxiDict['lcls_ts_necs'] = np.array(uxiNsecs)
    uxiDict['lcls_ts_fids'] = np.array(uxiFids)

    return uxiDict, configDict