Issue Details (XML | Word | Printable)

Key: PYFB-69
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Pavel Cisar
Reporter: Pavel Zotov
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Firebird driver for Python

Can not connect to FB services if set ISC_USER & ISC_PASSWORD by os.environ[ ... ]

Created: 06/Dec/16 05:24 PM   Updated: 15/Feb/17 12:04 PM
Component/s: None
Affects Version/s: 1.6.1
Fix Version/s: 1.7


 Description  « Hide
test #1
#####

C:\>set isc_user
ISC_USER=SYSDBA

C:\>set isc_password
ISC_PASSWORD=masterke

C:\>python
ActivePython 2.7.8.10 (ActiveState Software Inc.) based on
Python 2.7.8 (default, Jul 2 2014, 19:50:44) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from fdb import services
>>> print( services.connect(host='localhost').get_home_directory() )
C:\MIX\firebird\fb40\
>>> ^Z

(OK, we able to connect to FB service manager using ISC_* environment variables that are assigned in OS command prompt)

test #2
#####

C:\>set isc_user=
C:\>set isc_password=

C:\>python
...
>>> import os
>>> from fdb import services
>>>
>>> os.environ["ISC_USER"] = 'SYSDBA'
>>> os.environ["ISC_PASSWORD"] = 'masterke'
>>> print( services.connect(host='localhost').get_home_directory() )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\fdb\services.py", line 186, in connect
    raise fdb.ProgrammingError('A password is required to use'
fdb.fbcore.ProgrammingError: A password is required to use the Services Manager.

Seems that inside python we can not use env. variables ? But they *are* defined:

>>> print os.environ.get('ISC_USER')
SYSDBA
>>> print os.environ.get('ISC_PASSWORD')
masterke


 All   Comments   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Philippe Makowski added a comment - 07/Dec/16 07:38 AM
read https://docs.python.org/2/library/os.html#process-parameters

you need putenv to change environment variables

Pavel Zotov added a comment - 07/Dec/16 08:18 AM
Philippe, thank you for reply.
But why I did not encounter this problem before, when lot of tests like following were implemented:

import os
import subprocess

os.environ["ISC_USER"] = user_name
os.environ["ISC_PASSWORD"] = user_password

db_file="$(DATABASE_LOCATION)bugs.core_4337.fdb"

fn_nul = open(os.devnull, 'w')
subprocess.call([ "fbsvcmgr", "localhost:service_mgr",
                  "action_properties", "prp_write_mode", "prp_wm_async",
                  "dbname", db_file ],
                  stdout = fn_nul,
                  stderr = subprocess.STDOUT
               )
fn_nul.close()
...

- ?

Pavel Cisar added a comment - 07/Dec/16 11:24 AM
It's not FDB problem, because FDB handles ISC_USER & ISC_PASSWORD in service calls just fine - if it's properly set before. Setting values to os.environ may not always work, see https://docs.python.org/2/library/os.html#os.environ

Pavel Zotov added a comment - 07/Dec/16 04:45 PM
> FDB handles ISC_USER & ISC_PASSWORD in service calls just fine - if it's properly set before.
> Setting values to os.environ may not always work, see https://docs.python.org/2/library/os.html#os.environ

Setting values *DOES* work correctly, either via os.environ[] or os.putenv().
The problem is exactly in services.connect() function.

Pease see attached screen an following samples:

case-1: using os.putenv()
######

C:\>set isc_user=

C:\>set isc_password=

C:\>python
ActivePython 2.7.8.10 (ActiveState Software Inc.) based on
Python 2.7.8 (default, Jul 2 2014, 19:50:44) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import subprocess
>>> from fdb import services
>>> subprocess.call(['fbsvcmgr','localhost:service_mgr','info_server_version','info_implementation'])
Your user name and password are not defined. Ask your database administrator to set up a Firebird login.
1
>>> os.putenv('ISC_USER','SYSDBA')
>>> os.putenv('ISC_PASSWORD','masterkey')
>>> subprocess.call(['fbsvcmgr','localhost:service_mgr','info_server_version','info_implementation'])
Server version: WI-T4.0.0.463 Firebird 4.0 Unstable
Server implementation: Firebird/Windows/Intel/i386
0
>>> print( services.connect(host='localhost').get_home_directory() )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\fdb\services.py", line 186, in connect
    raise fdb.ProgrammingError('A password is required to use'
fdb.fbcore.ProgrammingError: A password is required to use the Services Manager.
>>>


case-2: using os.environ[...] = ...
######

C:\>set isc_user=

C:\>set isc_password=

C:\>python
ActivePython 2.7.8.10 (ActiveState Software Inc.) based on
Python 2.7.8 (default, Jul 2 2014, 19:50:44) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import subprocess
>>> from fdb import services
>>> subprocess.call(['fbsvcmgr','localhost:service_mgr','info_server_version','info_implementation'])
Your user name and password are not defined. Ask your database administrator to set up a Firebird login.
1
>>> os.environ['ISC_USER']='SYSDBA'
>>> os.environ['ISC_PASSWORD']='masterkey'
>>> subprocess.call(['fbsvcmgr','localhost:service_mgr','info_server_version','info_implementation'])
Server version: WI-T4.0.0.463 Firebird 4.0 Unstable
Server implementation: Firebird/Windows/Intel/i386
0
>>> print( services.connect(host='localhost').get_home_directory() )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\fdb\services.py", line 186, in connect
    raise fdb.ProgrammingError('A password is required to use'
fdb.fbcore.ProgrammingError: A password is required to use the Services Manager.
>>>

Pavel Zotov added a comment - 07/Dec/16 04:47 PM
Unfortunately, one can not attach file in this branch of tracker.
See screenshot here (if needed)

https://yadi.sk/i/xXc1durh32UqTU

Pavel Cisar added a comment - 08/Dec/16 10:48 AM
Well, I see where the problem is. FDB services.connect() reads environment variables on loading into memory (when you import fdb into python), not on call to it. So if you define environment variables at OS shell level or before fdb is imported, it works as expected. See:

> python
Python 2.7.12 (default, Jul 01 2016, 15:34:22) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ.get('ISC_PASSWORD')
>>> os.environ['ISC_PASSWORD'] = 'masterkey'
>>> os.environ.get('ISC_PASSWORD')
'masterkey'
>>> import fdb
>>> s = fdb.services.connect(host='localhost')
>>> s.get_home_directory()
'/opt/firebird/'
>>>

Because there is a clear anomaly how fdb.connect and fdb.services.connect handle environment variables, I'll fix services.connect to read environment on call, like fdb.connect/create_database does.