Skip to content

Commit

Permalink
add java attacher config (#5483)
Browse files Browse the repository at this point in the history
* add java attacher config
  • Loading branch information
stuartnelson3 authored Jun 24, 2021
1 parent 9cf78f5 commit 92f0452
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions beater/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type Config struct {
Sampling SamplingConfig `config:"sampling"`
DataStreams DataStreamsConfig `config:"data_streams"`
DefaultServiceEnvironment string `config:"default_service_environment"`
JavaAttacherConfig JavaAttacherConfig `config:"java_attacher"`

Pipeline string

Expand Down Expand Up @@ -199,5 +200,6 @@ func DefaultConfig() *Config {
Sampling: defaultSamplingConfig(),
DataStreams: defaultDataStreamsConfig(),
AgentAuth: defaultAgentAuth(),
JavaAttacherConfig: defaultJavaAttacherConfig(),
}
}
60 changes: 60 additions & 0 deletions beater/config/java_attacher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 "fmt"

// JavaAttacherConfig holds configuration information for running a java
// attacher jarfile.
type JavaAttacherConfig struct {
Enabled bool `config:"enabled"`
DiscoveryRules []map[string]string `config:"discovery_rules"`
Config map[string]string `config:"config"`
}

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
}

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

func defaultJavaAttacherConfig() JavaAttacherConfig {
return JavaAttacherConfig{Enabled: false}
}
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 92f0452

Please sign in to comment.