UnboundLocalError when getting config from environment vars only

Bug #1841038 reported by Andreas Scheuring
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
oslo.config
Fix Released
Medium
Stephen Finucane

Bug Description

Getting the following error under the following conditions
* Option set via env variable AND
* option *not* defined in the config file AND
* the validation of its value fails (e.g. as the defined regex does not match).

It works as expected when a the option is defined in the config file. More details in the reproduce section

```
Traceback (most recent call last):
  File "test.py", line 19, in <module>
    print(cfg.CONF.group.foo)
  File "/home/nch7fe/gitt/osn2-na-api/.venv/py27/local/lib/python2.7/site-packages/oslo_config/cfg.py", line 3130, in __getattr__
    return self._conf._get(name, self._group)
  File "/home/nch7fe/gitt/osn2-na-api/.venv/py27/local/lib/python2.7/site-packages/oslo_config/cfg.py", line 2627, in _get
    value, loc = self._do_get(name, group, namespace)
  File "/home/nch7fe/gitt/osn2-na-api/.venv/py27/local/lib/python2.7/site-packages/oslo_config/cfg.py", line 2697, in _do_get
    opt.name, alt_loc, str(ve))
UnboundLocalError: local variable 'alt_loc' referenced before assignment
```

Reproduce
=========

Save the following script "test.py"

```
from __future__ import print_function

from oslo_config import cfg
from os import environ

g = cfg.OptGroup("group")
foo = cfg.StrOpt("foo", regex="^[a-z].*$")

cfg.CONF.register_opt(foo, group=g)

environ["OS_GROUP__FOO"] = "abc"
cfg.CONF()
print("### Correct value set via env variable")
print(cfg.CONF.group.foo)

environ["OS_GROUP__FOO"] = "ABC"
cfg.CONF()
print("### Invalid value set via env variable")
print(cfg.CONF.group.foo)

del environ["OS_GROUP__FOO"]
cfg.CONF()
print("### Invalid value from conf file")
print(cfg.CONF.group.foo)

```

store the following config file "config.cfg"

```
[group]
foo=BAR

```

Now run the script

`python test.py --config-file config.cfg`
-> as expected, getting a `ConfigFileValueError`

now clear the config.cfg file and run the command again

echo "" > config.cfg

`python test.py --config-file config.cfg`
-> Result: `UnboundLocalError: local variable 'alt_loc' referenced before assignment`
-> Expectation: `oslo_config.cfg.ConfigFileValueError: Value for option foo from LocationInfo(location=<Locations.user: (4, True)>, detail='/home/nch7fe/gitt/osn2-na-api/test.cfg') is not valid: Value 'ABC' doesn't match regex '^[a-z].*$'`

Version
-------
python 2.7.15rc1
oslo.config==6.11.0

description: updated
description: updated
Changed in oslo.config:
importance: Undecided → Medium
status: New → Confirmed
assignee: nobody → Stephen Finucane (stephenfinucane)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to oslo.config (master)

Fix proposed to branch: master
Review: https://review.opendev.org/724880

Changed in oslo.config:
status: Confirmed → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to oslo.config (master)

Reviewed: https://review.opendev.org/724880
Committed: https://git.openstack.org/cgit/openstack/oslo.config/commit/?id=2c11e9dd8221325a1071d42c069dc6c806d3ce60
Submitter: Zuul
Branch: master

commit 2c11e9dd8221325a1071d42c069dc6c806d3ce60
Author: Stephen Finucane <email address hidden>
Date: Fri May 1 16:45:04 2020 +0100

    Resolve UnboundLocalError

    As discussed in bug 1841038, configuring a StrOpt with a regex parameter
    and providing an invalid environment variable will result in an ugly
    stacktrace due to an undefined 'alt_loc' variable. Resolve this by
    creating said variable.

    Change-Id: Id0fb6efdc435a2bd88d5dfea2b4720df453fbfec
    Signed-off-by: Stephen Finucane <email address hidden>
    Closes-Bug: #1841038

Changed in oslo.config:
status: In Progress → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.