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

feat(inputs.win_services): Make service selection case-insensitive #14684

Merged
merged 2 commits into from
Feb 6, 2024
Merged
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
22 changes: 18 additions & 4 deletions plugins/inputs/win_services/win_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io/fs"
"strings"
"syscall"

"golang.org/x/sys/windows"
Expand Down Expand Up @@ -120,11 +121,22 @@ func (*WinServices) SampleConfig() string {
}

func (m *WinServices) Init() error {
var err error
m.servicesFilter, err = filter.NewIncludeExcludeFilter(m.ServiceNames, m.ServiceNamesExcluded)
// For case insensitive comparision (see issue #8796) we need to transform the services
// to lowercase
servicesInclude := make([]string, 0, len(m.ServiceNames))
for _, s := range m.ServiceNames {
servicesInclude = append(servicesInclude, strings.ToLower(s))
}
servicesExclude := make([]string, 0, len(m.ServiceNamesExcluded))
for _, s := range m.ServiceNamesExcluded {
servicesExclude = append(servicesExclude, strings.ToLower(s))
}

f, err := filter.NewIncludeExcludeFilter(servicesInclude, servicesExclude)
if err != nil {
return err
}
m.servicesFilter = f

return nil
}
Expand Down Expand Up @@ -178,9 +190,11 @@ func (m *WinServices) listServices(scmgr WinServiceManager) ([]string, error) {
}

var services []string
for _, n := range names {
for _, name := range names {
// Compare case-insensitive. Use lowercase as we already converted the filter to use it.
n := strings.ToLower(name)
if m.servicesFilter.Match(n) {
services = append(services, n)
services = append(services, name)
}
}

Expand Down
Loading