Skip to content

Commit 5a287a4

Browse files
committed
fix FileIsReadableValidator and add proper tests (#434)
fixes #433 closes #434
1 parent 307a2d5 commit 5a287a4

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
[Check the diff](https://github.com/elastic/apm-agent-python/compare/v4.2.0...master)
5+
* fixed an issue with the certificate pinning feature introduced in 4.2.0 (#433, #434)
6+
37
## v4.2.0
48
[Check the diff](https://github.com/elastic/apm-agent-python/compare/v4.1.0...v4.2.0)
59

elasticapm/conf/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ def __call__(self, value, field_name):
172172
raise ConfigurationError("{} does not exist".format(value), field_name)
173173
elif not os.path.isfile(value):
174174
raise ConfigurationError("{} is not a file".format(value), field_name)
175-
elif not os.access(value):
176-
raise ConfigurationError("{} is not accessible".format(value), field_name)
175+
elif not os.access(value, os.R_OK):
176+
raise ConfigurationError("{} is not readable".format(value), field_name)
177177
return value
178178

179179

tests/config/tests.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,17 @@
3131
from __future__ import absolute_import
3232

3333
import logging
34+
import os
35+
import platform
36+
import stat
3437

3538
import mock
39+
import pytest
3640

3741
from elasticapm.conf import (
3842
Config,
43+
ConfigurationError,
44+
FileIsReadableValidator,
3945
RegexValidator,
4046
_BoolConfigValue,
4147
_ConfigBase,
@@ -221,3 +227,35 @@ class MyConfig(_ConfigBase):
221227

222228
c = MyConfig({"CHAIN": "x"})
223229
assert c.chained == "XX"
230+
231+
232+
def test_file_is_readable_validator_not_exists(tmpdir):
233+
validator = FileIsReadableValidator()
234+
with pytest.raises(ConfigurationError) as e:
235+
validator(tmpdir.join("doesnotexist").strpath, "path")
236+
assert "does not exist" in e.value.args[0]
237+
238+
239+
def test_file_is_readable_validator_not_a_file(tmpdir):
240+
validator = FileIsReadableValidator()
241+
with pytest.raises(ConfigurationError) as e:
242+
validator(tmpdir.strpath, "path")
243+
assert "is not a file" in e.value.args[0]
244+
245+
246+
@pytest.mark.skipif(platform.system() == "Windows", reason="os.access() doesn't seem to work as we expect on Windows")
247+
def test_file_is_readable_validator_not_readable(tmpdir):
248+
p = tmpdir.join("nonreadable")
249+
p.write("")
250+
os.chmod(p.strpath, stat.S_IWRITE)
251+
validator = FileIsReadableValidator()
252+
with pytest.raises(ConfigurationError) as e:
253+
validator(p.strpath, "path")
254+
assert "is not readable" in e.value.args[0]
255+
256+
257+
def test_file_is_readable_validator_all_good(tmpdir):
258+
p = tmpdir.join("readable")
259+
p.write("")
260+
validator = FileIsReadableValidator()
261+
assert validator(p.strpath, "path") == p.strpath

0 commit comments

Comments
 (0)