Skip to content

Commit

Permalink
Add a permissions option to logging.files for all beats (elastic#4428)
Browse files Browse the repository at this point in the history
(cherry picked from commit fecbdf4)
  • Loading branch information
atoulme authored and andrewkroh committed Oct 13, 2017
1 parent 29f2667 commit dd882fc
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ https://github.com/elastic/beats/compare/v5.5.3...v5.6.0[View commits]
*Affecting all Beats*
- Add option to the import_dashboards script to load the dashboards via Kibana API. {pull}4682[4682]
- Add `logging.files` `permissions` option. {pull}4295[4295]
*Filebeat*
Expand Down
3 changes: 3 additions & 0 deletions filebeat/filebeat.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1017,3 +1017,6 @@ logging.files:
# Number of rotated log files to keep. Oldest files will be deleted first.
#keepfiles: 7

# The permissions mask to apply when rotating log files. The default value is 0600.
# Must be a valid Unix-style file permissions mask expressed in octal notation.
#permissions: 0600
3 changes: 3 additions & 0 deletions heartbeat/heartbeat.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -852,3 +852,6 @@ logging.files:
# Number of rotated log files to keep. Oldest files will be deleted first.
#keepfiles: 7

# The permissions mask to apply when rotating log files. The default value is 0600.
# Must be a valid Unix-style file permissions mask expressed in octal notation.
#permissions: 0600
3 changes: 3 additions & 0 deletions libbeat/_meta/config.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,6 @@ logging.files:
# Number of rotated log files to keep. Oldest files will be deleted first.
#keepfiles: 7

# The permissions mask to apply when rotating log files. The default value is 0600.
# Must be a valid Unix-style file permissions mask expressed in octal notation.
#permissions: 0600
16 changes: 16 additions & 0 deletions libbeat/docs/loggingconfig.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ logging.files:
path: /var/log/mybeat
name: mybeat.log
keepfiles: 7
permissions: 0600
------------------------------------------------------------------------------

TIP: In addition to setting logging options in the config file, you can modify
Expand Down Expand Up @@ -124,6 +125,21 @@ The number of most recent rotated log files to keep on disk. Older files are
deleted during log rotation. The default value is 7. The `keepfiles` options has to be
in the range of 2 to 1024 files.

===== files.permissions

The permissions mask to apply when rotating log files. The default value is 0600. The
`permissions` value must be a valid Unix-style file permissions mask expressed
in octal notation. In YAML, numbers in octal notation must start with '0'.

Examples:

* 0600: give read and write access to the file owner, and no access to all
others (default).
* 0644: give read and write access to the file owner, and read access to all
others.
* 0664: give read and write access to the file owner and members of the group
associated with the file, as well as read access to all other users.

==== Logging Format

The logging format is different for each logging type:
Expand Down
20 changes: 16 additions & 4 deletions libbeat/logp/file_rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type FileRotator struct {
Name string
RotateEveryBytes *uint64
KeepFiles *int
Permissions *uint32

current *os.File
currentSize uint64
Expand All @@ -42,7 +43,7 @@ func (rotator *FileRotator) CreateDirectory() error {

func (rotator *FileRotator) CheckIfConfigSane() error {
if len(rotator.Name) == 0 {
return fmt.Errorf("File logging requires a name for the file names")
return fmt.Errorf("file logging requires a name for the file names")
}
if rotator.KeepFiles == nil {
rotator.KeepFiles = new(int)
Expand All @@ -54,7 +55,11 @@ func (rotator *FileRotator) CheckIfConfigSane() error {
}

if *rotator.KeepFiles < 2 || *rotator.KeepFiles >= RotatorMaxFiles {
return fmt.Errorf("The number of files to keep should be between 2 and %d", RotatorMaxFiles-1)
return fmt.Errorf("the number of files to keep should be between 2 and %d", RotatorMaxFiles-1)
}

if rotator.Permissions != nil && (*rotator.Permissions > uint32(os.ModePerm)) {
return fmt.Errorf("the permissions mask %d is invalid", *rotator.Permissions)
}
return nil
}
Expand Down Expand Up @@ -134,7 +139,7 @@ func (rotator *FileRotator) Rotate() error {

if rotator.FileExists(fileNo + 1) {
// next file exists, something is strange
return fmt.Errorf("File %s exists, when rotating would overwrite it", rotator.FilePath(fileNo+1))
return fmt.Errorf("file %s exists, when rotating would overwrite it", rotator.FilePath(fileNo+1))
}

err := os.Rename(path, rotator.FilePath(fileNo+1))
Expand All @@ -145,7 +150,7 @@ func (rotator *FileRotator) Rotate() error {

// create the new file
path := rotator.FilePath(0)
current, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
current, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.FileMode(rotator.getPermissions()))
if err != nil {
return err
}
Expand All @@ -158,3 +163,10 @@ func (rotator *FileRotator) Rotate() error {

return nil
}

func (rotator *FileRotator) getPermissions() uint32 {
if rotator.Permissions == nil {
return 0600
}
return *rotator.Permissions
}
13 changes: 13 additions & 0 deletions libbeat/logp/file_rotator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,17 @@ func TestConfigSane(t *testing.T) {
}
assert.NotNil(t, rotator.CheckIfConfigSane())

perms := uint32(0544)
rotator = FileRotator{
Name: "test2",
Permissions: &perms,
}
assert.Nil(t, rotator.CheckIfConfigSane())

perms = uint32(077777)
rotator = FileRotator{
Name: "test2",
Permissions: &perms,
}
assert.NotNil(t, rotator.CheckIfConfigSane())
}
3 changes: 3 additions & 0 deletions metricbeat/metricbeat.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -965,3 +965,6 @@ logging.files:
# Number of rotated log files to keep. Oldest files will be deleted first.
#keepfiles: 7

# The permissions mask to apply when rotating log files. The default value is 0600.
# Must be a valid Unix-style file permissions mask expressed in octal notation.
#permissions: 0600
3 changes: 3 additions & 0 deletions packetbeat/packetbeat.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1108,3 +1108,6 @@ logging.files:
# Number of rotated log files to keep. Oldest files will be deleted first.
#keepfiles: 7

# The permissions mask to apply when rotating log files. The default value is 0600.
# Must be a valid Unix-style file permissions mask expressed in octal notation.
#permissions: 0600
3 changes: 3 additions & 0 deletions winlogbeat/winlogbeat.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,6 @@ logging.files:
# Number of rotated log files to keep. Oldest files will be deleted first.
#keepfiles: 7

# The permissions mask to apply when rotating log files. The default value is 0600.
# Must be a valid Unix-style file permissions mask expressed in octal notation.
#permissions: 0600

0 comments on commit dd882fc

Please sign in to comment.