parameters: ()
return_statement: return new_key
docstring: Return a secret key that is common across all sessions
docstring_summary: Return a secret key that is common across all sessions
docstring_tokens: Return a secret key that is common across all sessions
function: def generate_secret_key():
      """
      Return a secret key that is common across all sessions
      """
      logger = logging.getLogger(os.path.basename(sys.argv[0]))
  
      if not htcondor:
          logger.warning("HTCondor module is missing will use a non-persistent WSGI session key")
          return os.urandom(16)
  
      cred_dir = htcondor.param.get("SEC_CREDENTIAL_DIRECTORY_OAUTH",
                      htcondor.param.get("SEC_CREDENTIAL_DIRECTORY",
                          "/var/lib/condor/oauth_credentials"))
      keyfile = os.path.join(cred_dir, "wsgi_session_key")
  
      # Create the secret key file, if possible, with read-only permissions, if it doesn't exist
      try:
          os.close(os.open(keyfile, os.O_CREAT | os.O_EXCL | os.O_RDWR, stat.S_IRUSR))
      except OSError as os_error:
          # An exception will be thrown if the file already exists, and that's fine and good.
          if not (os_error.errno == errno.EEXIST):
              logger.warning("Unable to access WSGI session key at %s (%s);  will use a non-persistent key.", keyfile, str(os_error))
              return os.urandom(16)
  
      # Open the secret key file.
      try:
          with open(keyfile, 'rb') as f:
              current_key = f.read(24)
      except IOError as e:
          logger.warning("Unable to access WSGI session key at %s (%s); will use a non-persistent key.", keyfile, str(e))
          return os.urandom(16)
  
      # Make sure the key string isn't empty or truncated.
      if len(current_key) >= 16:
          logger.debug("Using the persistent WSGI session key")
          return current_key
  
      # We are responsible for generating the keyfile for this webapp to use.
      new_key = os.urandom(24)
      try:
          # Use atomic output so the file is only ever read-only
          atomic_output(new_key, keyfile, stat.S_IRUSR)
          logger.info("Successfully created a new persistent WSGI session key for scitokens-credmon application at %s.", keyfile)
      except Exception as e:
          logger.exception("Failed to atomically create a new persistent WSGI session key at %s (%s); will use a transient one.", keyfile, str(e))
          return new_key
      return new_key
function_tokens: def generate_secret_key ( ) : logger = logging . getLogger ( os . path . basename ( sys . argv [ 0 ] ) ) if not htcondor : logger . warning ( "HTCondor module is missing will use a non-persistent WSGI session key" ) return os . urandom ( 16 ) cred_dir = htcondor . param . get ( "SEC_CREDENTIAL_DIRECTORY_OAUTH" , htcondor . param . get ( "SEC_CREDENTIAL_DIRECTORY" , "/var/lib/condor/oauth_credentials" ) ) keyfile = os . path . join ( cred_dir , "wsgi_session_key" ) # Create the secret key file, if possible, with read-only permissions, if it doesn't exist try : os . close ( os . open ( keyfile , os . O_CREAT | os . O_EXCL | os . O_RDWR , stat . S_IRUSR ) ) except OSError as os_error : # An exception will be thrown if the file already exists, and that's fine and good. if not ( os_error . errno == errno . EEXIST ) : logger . warning ( "Unable to access WSGI session key at %s (%s);  will use a non-persistent key." , keyfile , str ( os_error ) ) return os . urandom ( 16 ) # Open the secret key file. try : with open ( keyfile , 'rb' ) as f : current_key = f . read ( 24 ) except IOError as e : logger . warning ( "Unable to access WSGI session key at %s (%s); will use a non-persistent key." , keyfile , str ( e ) ) return os . urandom ( 16 ) # Make sure the key string isn't empty or truncated. if len ( current_key ) >= 16 : logger . debug ( "Using the persistent WSGI session key" ) return current_key # We are responsible for generating the keyfile for this webapp to use. new_key = os . urandom ( 24 ) try : # Use atomic output so the file is only ever read-only atomic_output ( new_key , keyfile , stat . S_IRUSR ) logger . info ( "Successfully created a new persistent WSGI session key for scitokens-credmon application at %s." , keyfile ) except Exception as e : logger . exception ( "Failed to atomically create a new persistent WSGI session key at %s (%s); will use a transient one." , keyfile , str ( e ) ) return new_key return new_key