Skip to content

Commit

Permalink
change config format + validation
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartnelson3 committed Jun 22, 2021
1 parent 4a3c33b commit 4834a7c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 12 deletions.
43 changes: 31 additions & 12 deletions beater/config/java_attacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,42 @@

package config

import "fmt"

// JavaAttacherConfig holds configuration information for running a java
// attacher jarfile.
type JavaAttacherConfig struct {
Enabled bool `config:"enabled"`

IncludeAll bool `config:"include_all"`
IncludePID []string `config:"include_pid"`

IncludeMain []string `config:"include_main"`
IncludeVMArg []string `config:"include_vmarg"`
IncludeUser []string `config:"include_user"`
Enabled bool `config:"enabled"`
DiscoveryRules []map[string]string `config:"discovery_rules"`
Config map[string]string `config:"config"`
}

ExcludeMain []string `config:"exclude_main"`
ExcludeVMArg []string `config:"exclude_vmarg"`
ExcludeUser []string `config:"exclude_user"`
func (j JavaAttacherConfig) setup() error {
if !j.Enabled {
return nil
}
for _, rule := range j.DiscoveryRules {
if len(rule) != 1 {
return fmt.Errorf("unexpected discovery rule format: %v", rule)
}
for flag := range rule {
if _, ok := allowlist[flag]; !ok {
return fmt.Errorf("unrecognized discovery rule: %s", rule)
}
}
}
return nil
}

Config map[string]string `config:"config"`
var allowlist = map[string]struct{}{
"include-all": {},
"include-pid": {},
"include-main": {},
"include-vmarg": {},
"include-user": {},
"exclude-main": {},
"exclude-vmarg": {},
"exclude-user": {},
}

func defaultJavaAttacherConfig() JavaAttacherConfig {
Expand Down
42 changes: 42 additions & 0 deletions beater/config/java_attacher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package config

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestJavaAttacherConfig(t *testing.T) {
discoveryRules := []map[string]string{
map[string]string{"include-pid": "1001"},
map[string]string{"include-main": "main.jar"},
map[string]string{"include-vmarg": "elastic.apm.agent.attach=true"},
map[string]string{"exclude-user": "root"},
}
config := JavaAttacherConfig{
Enabled: true,
DiscoveryRules: discoveryRules,
}

assert.NoError(t, config.setup())

config.DiscoveryRules = append(discoveryRules, map[string]string{"bogus-flag": "dne"})
assert.Error(t, config.setup())
}

0 comments on commit 4834a7c

Please sign in to comment.