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

Start Filebeat Service failed on argument test when it executed by non-root user #4333

Closed
dreampuf opened this issue May 17, 2017 · 11 comments
Closed
Labels

Comments

@dreampuf
Copy link
Contributor

From the code, the argument test will be failed on test function. Cause the checker.
https://github.com/elastic/beats/blob/master/dev-tools/packer/platforms/centos/init.j2#L45

+ /usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml -path.home /usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat -e -configtest
filebeat2017/05/17 11:02:41.103321 beat.go:339: CRIT Exiting: error loading config file: config file ("/etc/filebeat/filebeat.yml") must be owned by the beat user (uid=0) or root
Exiting: error loading config file: config file ("/etc/filebeat/filebeat.yml") must be owned by the beat user (uid=0) or root

For confirmed bugs, please report:

  • Version: 5.3.2
  • Operating System: CentOS5
  • Steps to Reproduce:
    • Install the latest filebeat yum install filebeat
    • config the file permission of binary and configs chown monitor:monitor /etc/filebeat/filebeat.yml /usr/share/filebeat/bin/filebeat
    • Lauch the filebeat service. /etc/init.d/filebeat start
@andrewkroh
Copy link
Member

This looks like the expected behavior to me. See the documentation here. Why are you changing the owner of the binary and config file?

@dreampuf
Copy link
Contributor Author

@andrewkroh I want to execute the filebeat with a non-root user.

@andrewkroh
Copy link
Member

andrewkroh commented May 17, 2017

Changing the owner of the file won't cause it to be executed as a different user. You also need to modify the init.d script to change the user that the process executes as. The script will probably require multiple changes to execute the beat as a user other than root. It's not something that's easily configurable today.

@andrewkroh
Copy link
Member

I think you would need to add -u user -g group to the wrapperopts at https://github.com/elastic/beats/blob/master/dev-tools/packer/platforms/centos/init.j2#L31.

The arguments are described here for daemon process: https://github.com/tsg/go-daemon/blob/master/god.c#L35-L36

In addition, the test() func will need modified to execute as the correct user. Or you could just have the test not check the file permissions by using -strict.perms=false.

@dreampuf
Copy link
Contributor Author

dreampuf commented May 17, 2017

@andrewkroh sorry for something missed.
I had a patch for it.

--- packages/output_orig/etc/init.d/filebeat	2017-05-17 20:53:36.000000000 +0800
+++ packages/output/etc/init.d/filebeat	2017-05-17 20:55:44.000000000 +0800
@@ -27,8 +27,13 @@ pidfile=${PIDFILE-/var/run/filebeat.pid}
 agent=${BEATS_AGENT-/usr/share/filebeat/bin/filebeat}
 args="-c /etc/filebeat/filebeat.yml -path.home /usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat"
 test_args="-e -configtest"
-wrapper="/usr/share/filebeat/bin/filebeat-god"
-wrapperopts="-r / -n -p $pidfile"
+if grep -q -i "release 7" /etc/redhat-release; then
+    wrapper="/usr/share/filebeat/bin/filebeat-god"
+else
+    wrapper="/usr/share/filebeat/bin/filebeat-god-el5"
+fi
+beat_user="monitor"
+wrapperopts="-r / -n -p $pidfile -u $beat_user"
 RETVAL=0

 # Source function library.
@@ -42,7 +47,7 @@ if status | grep -q -- '-p' 2>/dev/null;
 fi

 test() {
-	$agent $args $test_args
+	runuser -s /bin/bash $beat_user -c "$agent $args $test_args"
 }

 start() {
diff -urpN packages/output_orig/lib/systemd/system/filebeat.service packages/output/lib/systemd/system/filebeat.service
--- packages/output_orig/lib/systemd/system/filebeat.service	2017-05-17 20:53:36.000000000 +0800
+++ packages/output/lib/systemd/system/filebeat.service	2017-05-17 20:54:10.000000000 +0800
@@ -6,6 +6,7 @@ After=network-online.target

 [Service]
 ExecStart=/usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml -path.home /usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat
+User=monitor
 Restart=always

 [Install]

I'm not asking to implement it totally(it's just my practice to execute beats from non-root user). What I need is fixing the test function in /etc/init.d/beats incompatible to non-root user's case.

@dreampuf
Copy link
Contributor Author

Haven't seen your latest comment. Anyway thanks for the response so quickly.
Do you mind I send a merge request about the test function?

@andrewkroh
Copy link
Member

I think it would be ideal to have the init script source a file like /etc/default/{{.beat_name}} if it exists. That file can optionally set BEAT_USER=someuser. Then make the init script to use $BEAT_USER in the wrapperopts and in the runuser command.

Do you mind I send a merge request about the test function?

I wouldn't want to merge a change only for the test function. I would prefer a slightly more complete change to make it possible to runas a custom user.

@dreampuf
Copy link
Contributor Author

@andrewkroh I created an MR. Do you have a chance to check it? #4340

@hadivarp
Copy link

hadivarp commented Aug 7, 2017

@andrewkroh I have exact same problem as you describe but even I change permission noting change and also I add -u user to -g group as you said but still noting change do you have any Idea what should I do ?

this is what it show me each time :

filebeat2017/08/07 04:04:32.717139 beat.go:339: CRIT Exiting: error loading config file: config file ("filebeat.yml") must be owned by the beat user (uid=0) or root
Exiting: error loading config file: config file ("filebeat.yml") must be owned by the beat user (uid=0) or root

@dreampuf dreampuf closed this as completed Mar 7, 2018
@socieboy
Copy link

I think you would need to add -u user -g group to the wrapperopts at https://github.com/elastic/beats/blob/master/dev-tools/packer/platforms/centos/init.j2#L31.

The arguments are described here for daemon process: https://github.com/tsg/go-daemon/blob/master/god.c#L35-L36

In addition, the test() func will need modified to execute as the correct user. Or you could just have the test not check the file permissions by using -strict.perms=false.

Where exactly you use or place the -stric.perms=false?

@longpan
Copy link

longpan commented Jun 22, 2021

I think you would need to add -u user -g group to the wrapperopts at https://github.com/elastic/beats/blob/master/dev-tools/packer/platforms/centos/init.j2#L31.
The arguments are described here for daemon process: https://github.com/tsg/go-daemon/blob/master/god.c#L35-L36
In addition, the test() func will need modified to execute as the correct user. Or you could just have the test not check the file permissions by using -strict.perms=false.

Where exactly you use or place the -stric.perms=false?

Add "-strict.perms=false" in filebeat.service. It exact work for me.

image

`[Unit]
Description=Filebeat sends log files to Logstash or directly to Elasticsearch.
Documentation=https://www.elastic.co/products/beats/filebeat
Wants=network-online.target
After=network-online.target

[Service]

Environment="BEAT_LOG_OPTS=-e"
Environment="BEAT_CONFIG_OPTS=-c /etc/filebeat/filebeat.yml"
Environment="BEAT_PATH_OPTS=-path.home /usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat"
ExecStart=/usr/share/filebeat/bin/filebeat $BEAT_LOG_OPTS $BEAT_CONFIG_OPTS $BEAT_PATH_OPTS -strict.perms=false
Restart=always

[Install]
WantedBy=multi-user.target`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants