Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flare] Warn user if not root #1509

Merged
merged 1 commit into from
Apr 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ def parent_func(): agent.start_event = False
print "If you think it's not normal please get in touch with Datadog Support"

elif 'flare' == command:
Flare.check_user_rights()
case_id = int(args[1]) if len(args) > 1 else None
f = Flare(True, case_id)
f.collect()
Expand Down
2 changes: 1 addition & 1 deletion packaging/centos/datadog-agent.init
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ case "$1" in

flare)
shift
su $AGENTUSER -c "$AGENTPATH flare $@"
$AGENTPATH flare $@
exit $?
;;

Expand Down
2 changes: 1 addition & 1 deletion packaging/debian/datadog-agent.init
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ case "$1" in

flare)
shift
su $AGENTUSER -c "$AGENTPATH flare $@"
$AGENTPATH flare $@
exit $?
;;

Expand Down
9 changes: 5 additions & 4 deletions tests/test_flare.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ def test_init(self, mock_config, mock_version, mock_tempdir, mock_strftime):
@mock.patch('utils.flare.get_config', side_effect=get_mocked_config)
def test_upload_with_case(self, mock_config, mock_tempdir, mock_stfrtime, mock_version, mock_requests):
f = Flare(case_id=1337)
f._ask_for_email = lambda: 'test@example.com'

assert not mock_requests.called
f.upload(confirmation=False)
f.upload()
assert mock_requests.called
args, kwargs = mock_requests.call_args_list[0]
self.assertEqual(
Expand All @@ -73,7 +74,7 @@ def test_upload_with_case(self, mock_config, mock_tempdir, mock_stfrtime, mock_v
os.path.join(get_mocked_temp(), "datadog-agent-1.tar.bz2")
)
self.assertEqual(kwargs['data']['case_id'], 1337)
self.assertEqual(kwargs['data']['email'], '')
self.assertEqual(kwargs['data']['email'], 'test@example.com')
assert kwargs['data']['hostname']

@mock.patch('utils.flare.requests.post', return_value=FakeResponse())
Expand All @@ -86,7 +87,7 @@ def test_upload_no_case(self, mock_config, mock_tempdir, mock_stfrtime, mock_ver
f._ask_for_email = lambda: 'test@example.com'

assert not mock_requests.called
f.upload(confirmation=False)
f.upload()
assert mock_requests.called
args, kwargs = mock_requests.call_args_list[0]
self.assertEqual(
Expand All @@ -108,7 +109,7 @@ def test_endpoint(self, mock_config, mock_temp, mock_stfrtime):
f = Flare()
f._ask_for_email = lambda: None
try:
f.upload(confirmation=False)
f.upload()
raise Exception('Should fail before')
except Exception, e:
self.assertEqual(str(e), "Your request is incorrect: Invalid inputs: 'API key unknown'")
28 changes: 21 additions & 7 deletions utils/flare.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Flare(object):
APIKEY_REGEX = re.compile('^api_key: *\w+(\w{5})$')
REPLACE_APIKEY = r'api_key: *************************\1'
COMPRESSED_FILE = 'datadog-agent-{0}.tar.bz2'
# We limit to 10MB arbitrary
# We limit to 10MB arbitrarily
MAX_UPLOAD_SIZE = 10485000
TIMEOUT = 60

Expand All @@ -82,6 +82,20 @@ def __init__(self, cmdline=False, case_id=None):
self._hostname = get_hostname(config)
self._prefix = "datadog-{0}".format(self._hostname)

# On Unix system, check that the user is root (to call supervisorctl & status)
# Otherwise emit a warning, and ask for confirmation
@staticmethod
def check_user_rights():
if Platform.is_unix() and not os.geteuid() == 0:
log.warning("You are not root, some information won't be collected")
choice = raw_input('Are you sure you want to continue [y/N]? ').lower()
if choice not in ['yes', 'y']:
print 'Aborting'
sys.exit(1)
else:
log.warn('Your user has to have at least read access'
' to the logs and conf files of the agent')

# Collect all conf and logs files and compress them
def collect(self):
if not self._api_key:
Expand All @@ -103,10 +117,10 @@ def collect(self):
self._tar.close()

# Upload the tar file
def upload(self, confirmation=True):
def upload(self):
self._check_size()

if confirmation:
if self._cmdline:
self._ask_for_confirmation()

email = self._ask_for_email()
Expand Down Expand Up @@ -314,8 +328,8 @@ def _pip_freeze(self):
# Check if the file is not too big before upload
def _check_size(self):
if os.path.getsize(self._tar_path) > self.MAX_UPLOAD_SIZE:
log.info('{0} won\'t be uploaded, its size is too important.\n'
'You can send it directly to support by mail.')
log.info("{0} won't be uploaded, its size is too important.\n"
"You can send it directly to support by mail.")
sys.exit(1)

# Function to ask for confirmation before upload
Expand All @@ -328,8 +342,8 @@ def _ask_for_confirmation(self):

# Ask for email if needed
def _ask_for_email(self):
if self._case_id:
return ''
# We ask everytime now, as it is also the 'id' to check
# that the case is the good one if it exists
return raw_input('Please enter your email: ').lower()

# Print output (success/error) of the request
Expand Down