def getUsernameFromCRIC(proxyFileName=None): """ Retrieve username from CRIC by doing a query to https://cms-cric.cern.ch/api/accounts/user/query/?json&preset=whoami using the users proxy. args: proxyfile : string : the full patch to the file containing the user proxy """ ## Path to certificates. capath = os.environ[ 'X509_CERT_DIR'] if 'X509_CERT_DIR' in os.environ else "/etc/grid-security/certificates" # Path to user proxy if not proxyFileName: proxyFileName = getUserProxy() if not proxyFileName: msg = "Can't find user proxy file" raise UsernameException(msg) ## Retrieve user info from CRIC. Note the curl must be executed in same env. (i.e. CMSSW) as crab queryCmd = "curl -sS --capath %s --cert %s --key %s 'https://cms-cric.cern.ch/api/accounts/user/query/?json&preset=whoami'" %\ (capath, proxyFileName, proxyFileName) stdout, stderr, rc = execute_command(queryCmd) if rc or not stdout: msg = "Error contacting CRIC." msg += "\nDetails follow:" msg += "\n Executed command: %s" % (queryCmd) msg += "\n Stdout:\n %s" % (str(stdout).replace( '\n', '\n ')) msg += "\n Stderr:\n %s" % (str(stderr).replace( '\n', '\n ')) raise UsernameException(msg) ## Extract the username from the above command output. parseCmd = "echo '%s' | tr ':,' '\n' | grep -A1 login | tail -1 | tr -d ' \n\"'" % ( str(stdout)) username, stderr, rc = execute_command(parseCmd) if username == 'null' or not username: msg = "Failed to retrieve username from CRIC." msg += "\nDetails follow:" msg += "\n Executed command: %s" % (queryCmd) msg += "\n Stdout:\n %s" % (str(stdout).replace( '\n', '\n ')) msg += "\n Parsed username: %s" % (username) msg += "\n%sNote%s: Make sure you have the correct certificate mapped in your CERN account page" % ( colors.BOLD, colors.NORMAL) msg += " (you can check what is the certificate you currently have mapped" msg += " by looking at CERN Certificatiom Authority page." msg += "\nFor instructions on how to map a certificate, see " msg += "\n https://twiki.cern.ch/twiki/bin/view/CMSPublic/UsernameForCRAB#Adding_your_DN_to_your_profile" raise UsernameException(msg) return username
def getUsernameFromSiteDB(): """ Retrieve username from SiteDB by doing a query to https://cmsweb.cern.ch/sitedb/data/prod/whoami using the users proxy. """ scram_cmd = "which scram >/dev/null 2>&1 && eval `scram unsetenv -sh`" ## Check if there is a proxy. cmd = scram_cmd + "; voms-proxy-info" process = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True) stdout, stderr = process.communicate() if process.returncode or not stdout: msg = "Aborting the attempt to retrieve username from SiteDB." msg += "\nDetails follow:" msg += "\n Error executing command: %s" % (cmd) msg += "\n Stdout:\n %s" % (str(stdout).replace('\n', '\n ')) msg += "\n Stderr:\n %s" % (str(stderr).replace('\n', '\n ')) raise ProxyException(msg) ## Check if proxy is valid. #proxyTimeLeft = [x[x.find(':')+2:] for x in stdout.split('\n') if 'timeleft' in x][0] cmd = scram_cmd + "; voms-proxy-info -timeleft" process = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True) stdout, stderr = process.communicate() if process.returncode or not stdout: msg = "Aborting the attempt to retrieve username from SiteDB." msg += "\nDetails follow:" msg += "\n Error executing command: %s" % (cmd) msg += "\n Stdout:\n %s" % (str(stdout).replace('\n', '\n ')) msg += "\n Stderr:\n %s" % (str(stderr).replace('\n', '\n ')) raise ProxyException(msg) proxyTimeLeft = str(stdout).replace('\n','') if int(proxyTimeLeft) < 60: msg = "Aborting the attempt to retrieve username from SiteDB." msg += "\nProxy time left = %s seconds. Please renew your proxy." % (proxyTimeLeft) raise ProxyException(msg) ## Retrieve proxy file location. cmd = scram_cmd + "; voms-proxy-info -path" process = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True) stdout, stderr = process.communicate() if process.returncode or not stdout: msg = "Aborting the attempt to retrieve username from SiteDB." msg += "\nDetails follow:" msg += "\n Error executing command: %s" % (cmd) msg += "\n Stdout:\n %s" % (str(stdout).replace('\n', '\n ')) msg += "\n Stderr:\n %s" % (str(stderr).replace('\n', '\n ')) raise ProxyException(msg) proxyFileName = str(stdout).replace('\n','') ## Path to certificates. capath = os.environ['X509_CERT_DIR'] if 'X509_CERT_DIR' in os.environ else "/etc/grid-security/certificates" ## Retrieve user info from SiteDB. queryCmd = "curl -s --capath %s --cert %s --key %s 'https://cmsweb.cern.ch/sitedb/data/prod/whoami'" % (capath, proxyFileName, proxyFileName) process = subprocess.Popen(queryCmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True) stdout, stderr = process.communicate() if process.returncode or not stdout: msg = "Error contacting SiteDB." msg += "\nDetails follow:" msg += "\n Executed command: %s" % (queryCmd) msg += "\n Stdout:\n %s" % (str(stdout).replace('\n', '\n ')) msg += "\n Stderr:\n %s" % (str(stderr).replace('\n', '\n ')) raise UsernameException(msg) ## Extract the username from the above command output. parseCmd = "echo '%s' | tr ':,' '\n' | grep -A1 login | tail -1 | tr -d ' \n\"'" % (str(stdout)) process = subprocess.Popen(parseCmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True) username, stderr = process.communicate() if username == 'null' or not username: msg = "Failed to retrieve username from SiteDB. Your DN does not seem to be registered in SiteDB." msg += "\nDetails follow:" msg += "\n Executed command: %s" % (queryCmd) msg += "\n Stdout:\n %s" % (str(stdout).replace('\n', '\n ')) msg += "\n Parsed username: %s" % (username) msg += "\n%sNote%s: Make sure you have the correct certificate mapped in SiteDB" % (colors.BOLD, colors.NORMAL) msg += " (you can check what is the certificate you currently have mapped in SiteDB" msg += " by searching for your name in https://cmsweb.cern.ch/sitedb/prod/people)." msg += " For instructions on how to map a certificate in SiteDB, see https://twiki.cern.ch/twiki/bin/viewauth/CMS/SiteDBForCRAB." raise UsernameException(msg) return username
def getUsernameFromSiteDB(): """ Retrieve username from SiteDB by doing a query to https://cmsweb.cern.ch/sitedb/data/prod/whoami using the users proxy. """ scram_cmd = "which scram >/dev/null 2>&1 && eval `scram unsetenv -sh`" ## Check if there is a proxy. cmd = scram_cmd + "; voms-proxy-info" process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout, stderr = process.communicate() if process.returncode or not stdout: msg = "Aborting the attempt to retrieve username from SiteDB." msg += "\nError executing command: %s" % (cmd) msg += "\n Stdout:\n %s" % (str(stdout).replace('\n', '\n ')) msg += "\n Stderr:\n %s" % (str(stderr).replace('\n', '\n ')) raise ProxyException(msg) ## Check if proxy is valid. #proxyTimeLeft = [x[x.find(':')+2:] for x in stdout.split('\n') if 'timeleft' in x][0] cmd = scram_cmd + "; voms-proxy-info -timeleft" process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout, stderr = process.communicate() if process.returncode or not stdout: msg = "Aborting the attempt to retrieve username from SiteDB." msg += "\nError executing command: %s" % (cmd) msg += "\n Stdout:\n %s" % (str(stdout).replace('\n', '\n ')) msg += "\n Stderr:\n %s" % (str(stderr).replace('\n', '\n ')) raise ProxyException(msg) proxyTimeLeft = stdout.replace('\n', '') if int(proxyTimeLeft) < 60: msg = "Aborting the attempt to retrieve username from SiteDB." msg += "\nProxy time left = %s seconds. Please renew your proxy." % ( proxyTimeLeft) raise ProxyException(msg) ## Retrieve proxy file location. cmd = scram_cmd + "; voms-proxy-info -path" process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout, stderr = process.communicate() if process.returncode or not stdout: msg = "Aborting the attempt to retrieve username from SiteDB." msg += "\nError executing command: %s" % (cmd) msg += "\n Stdout:\n %s" % (str(stdout).replace('\n', '\n ')) msg += "\n Stderr:\n %s" % (str(stderr).replace('\n', '\n ')) raise ProxyException(msg) proxyFileName = stdout.replace('\n', '') ## Path to certificates. capath = os.environ[ 'X509_CERT_DIR'] if 'X509_CERT_DIR' in os.environ else "/etc/grid-security/certificates" ## Retrieve user info from SiteDB. cmd = "curl -s --capath %s --cert %s --key %s 'https://cmsweb.cern.ch/sitedb/data/prod/whoami'" % ( capath, proxyFileName, proxyFileName) process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout, stderr = process.communicate() if process.returncode or not stdout: msg = "Unable to retrieve username from SiteDB." msg += "\nError executing command: %s" % (cmd) msg += "\n Stdout:\n %s" % (str(stdout).replace('\n', '\n ')) msg += "\n Stderr:\n %s" % (str(stderr).replace('\n', '\n ')) raise UsernameException(msg) ## Extract the username from the above command output. dictresult = literal_eval(stdout.replace('\n', '')) if len(dictresult.get('result', [])) != 1 or 'login' not in dictresult['result'][0]: msg = "Unable to extract username from SiteDB." msg += "\nUnexpected output format from command: %s" % (cmd) msg += "\n Stdout:\n %s" % (str(stdout).replace('\n', '\n ')) raise UsernameException(msg) username = dictresult['result'][0]['login'] if username == "null" or not username: msg = "SiteDB returned %s login username." % ("'null'" if username == "null" else "no") msg += "\nExecuted command: %s" % (cmd) msg += "\n Stdout:\n %s" % (str(stdout).replace('\n', '\n ')) raise UsernameException(msg) return username