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

add java attacher config #5483

Merged
merged 12 commits into from
Jun 24, 2021
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())
}