Skip to content

Commit

Permalink
Update config translation to respect common config profile and creds …
Browse files Browse the repository at this point in the history
…file (#447)

(cherry picked from commit e1860d3)
  • Loading branch information
sky333999 committed Aug 18, 2023
1 parent c5c0f62 commit e2ef915
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This common-config is used to configure items used for both ssm and cloudwatch access


## Configuration for shared credential.
## Default credential strategy will be used if it is absent here:
## Instance role is used for EC2 case by default.
## AmazonCloudWatchAgent profile is used for onPremise case by default.
[credentials]
shared_credential_profile = "default"
shared_credential_file = "/root/.aws/credentials"

## Configuration for proxy.
## System-wide environment-variable will be read if it is absent here.
## i.e. HTTP_PROXY/http_proxy; HTTPS_PROXY/https_proxy; NO_PROXY/no_proxy
## Note: system-wide environment-variable is not accessible when using ssm run-command.
## Absent in both here and environment-variable means no proxy will be used.
#[proxy]
# http_proxy = "http://127.0.0.1:3280"
# https_proxy = "https://127.0.0.1:3280"
# no_proxy = "254.1.1.1"

#[ssl]
# ca_bundle_path = "/etc/test/ca_bundle.pem"
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exporters:
max_retries: 2
no_verify_ssl: false
num_workers: 8
profile: ""
profile: "default"
proxy_address: ""
raw_log: true
region: us-east-1
Expand All @@ -27,7 +27,7 @@ exporters:
role_arn: ""
sending_queue:
queue_size: 1000
shared_credentials_file: []
shared_credentials_file: [ "/root/.aws/credentials" ]
awsemf/containerinsights:
detailed_metrics: false
dimension_rollup_option: NoDimensionRollup
Expand Down Expand Up @@ -159,8 +159,8 @@ exporters:
enabled: true
retain_initial_value_of_delta_metric: false
role_arn: ""
profile: ""
shared_credentials_file: [ ]
profile: "default"
shared_credentials_file: [ "/root/.aws/credentials" ]
version: "0"
extensions: {}
processors:
Expand Down Expand Up @@ -195,8 +195,8 @@ receivers:
request_timeout_seconds: 0
resource_arn: ""
role_arn: ""
profile: ""
shared_credentials_file: []
profile: "default"
shared_credentials_file: [ "/root/.aws/credentials" ]
tcplog/emf_logs:
attributes: {}
encoding: utf-8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ receivers:
request_timeout_seconds: 0
resource_arn: ""
role_arn: ""
profile: ""
shared_credentials_file: []
profile: "AmazonCloudWatchAgent"
shared_credentials_file: [ "fake-path" ]
service:
extensions: []
pipelines:
Expand Down
4 changes: 2 additions & 2 deletions translator/tocwconfig/sampleConfig/trace_config_linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ exporters:
request_timeout_seconds: 30
resource_arn: ""
role_arn: ""
profile: ""
shared_credentials_file: []
profile: "default"
shared_credentials_file: [ "/root/.aws/credentials" ]
telemetry:
enabled: true
include_metadata: true
Expand Down
4 changes: 2 additions & 2 deletions translator/tocwconfig/sampleConfig/trace_config_windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ exporters:
request_timeout_seconds: 30
resource_arn: ""
role_arn: ""
profile: ""
shared_credentials_file: []
profile: "default"
shared_credentials_file: [ "/root/.aws/credentials" ]
telemetry:
enabled: true
include_metadata: true
Expand Down
8 changes: 5 additions & 3 deletions translator/tocwconfig/tocwconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestBaseContainerInsightsConfig(t *testing.T) {

func TestEmfAndKubernetesConfig(t *testing.T) {
resetContext(t)
readCommonConfig(t, "./sampleConfig/commonConfig/withCredentials.toml")
context.CurrentContext().SetRunInContainer(true)
t.Setenv(config.HOST_NAME, "host_name_from_env")
t.Setenv(config.HOST_IP, "127.0.0.1")
Expand Down Expand Up @@ -168,6 +169,7 @@ func TestLogOnlyConfig(t *testing.T) {

func TestTraceConfig(t *testing.T) {
resetContext(t)
readCommonConfig(t, "./sampleConfig/commonConfig/withCredentials.toml")
expectedEnvVars := map[string]string{}
checkTranslation(t, "trace_config", "linux", expectedEnvVars, "_linux")
checkTranslation(t, "trace_config", "darwin", expectedEnvVars, "_linux")
Expand All @@ -182,7 +184,7 @@ func TestConfigWithEnvironmentVariables(t *testing.T) {

func TestStandardConfigWithCommonConfig(t *testing.T) {
resetContext(t)
readCommonConfig(t)
readCommonConfig(t, "./sampleConfig/commonConfig/withCredentialsProxySsl.toml")
expectedEnvVars := map[string]string{
"AWS_CA_BUNDLE": "/etc/test/ca_bundle.pem",
"HTTPS_PROXY": "https://127.0.0.1:3280",
Expand Down Expand Up @@ -260,10 +262,10 @@ func checkTranslationForPaths(t *testing.T, jsonFilePath string, expectedTomlFil
verifyToYamlTranslation(t, input, expectedYamlFilePath, tokenReplacements...)
}

func readCommonConfig(t *testing.T) {
func readCommonConfig(t *testing.T, commonConfigFilePath string) {
ctx := context.CurrentContext()
cfg := commonconfig.New()
data, _ := os.ReadFile("./sampleConfig/commonConfigTest.toml")
data, _ := os.ReadFile(commonConfigFilePath)
require.NoError(t, cfg.Parse(bytes.NewReader(data)))
ctx.SetCredentials(cfg.CredentialsMap())
ctx.SetProxy(cfg.ProxyMap())
Expand Down
12 changes: 5 additions & 7 deletions translator/translate/otel/exporter/awsemf/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"go.opentelemetry.io/collector/exporter"
"gopkg.in/yaml.v3"

"github.com/aws/amazon-cloudwatch-agent/translator/config"
"github.com/aws/amazon-cloudwatch-agent/translator/context"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/agent"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
)
Expand Down Expand Up @@ -79,11 +77,11 @@ func (t *translator) Translate(c *confmap.Conf) (component.Config, error) {
}
}
cfg.AWSSessionSettings.Region = agent.Global_Config.Region
if context.CurrentContext().Mode() == config.ModeOnPrem || context.CurrentContext().Mode() == config.ModeOnPremise {
if profile, ok := agent.Global_Config.Credentials[agent.Profile_Key]; ok {
cfg.AWSSessionSettings.Profile = fmt.Sprintf("%v", profile)
cfg.AWSSessionSettings.SharedCredentialsFile = []string{fmt.Sprintf("%v", agent.Global_Config.Credentials[agent.CredentialsFile_Key])}
}
if profileKey, ok := agent.Global_Config.Credentials[agent.Profile_Key]; ok {
cfg.AWSSessionSettings.Profile = fmt.Sprintf("%v", profileKey)
}
if credentialsFileKey, ok := agent.Global_Config.Credentials[agent.CredentialsFile_Key]; ok {
cfg.AWSSessionSettings.SharedCredentialsFile = []string{fmt.Sprintf("%v", credentialsFileKey)}
}

if isEcs(c) {
Expand Down
6 changes: 6 additions & 0 deletions translator/translate/otel/exporter/awsxray/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func (t *translator) Translate(conf *confmap.Conf) (component.Config, error) {
}
cfg.RoleARN = getRoleARN(conf)
cfg.Region = getRegion(conf)
if profileKey, ok := agent.Global_Config.Credentials[agent.Profile_Key]; ok {
cfg.AWSSessionSettings.Profile = fmt.Sprintf("%v", profileKey)
}
if credentialsFileKey, ok := agent.Global_Config.Credentials[agent.CredentialsFile_Key]; ok {
cfg.AWSSessionSettings.SharedCredentialsFile = []string{fmt.Sprintf("%v", credentialsFileKey)}
}
if endpointOverride, ok := common.GetString(conf, common.ConfigKey(common.TracesKey, common.EndpointOverrideKey)); ok {
cfg.Endpoint = endpointOverride
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ func (t *translator) Translate(c *confmap.Conf) (component.Config, error) {
}
}

if profile, ok := agent.Global_Config.Credentials[agent.Profile_Key]; ok {
cfg.AWSSessionSettings.Profile = fmt.Sprintf("%v", profile)
cfg.AWSSessionSettings.SharedCredentialsFile = []string{fmt.Sprintf("%v", agent.Global_Config.Credentials[agent.CredentialsFile_Key])}
if profileKey, ok := agent.Global_Config.Credentials[agent.Profile_Key]; ok {
cfg.AWSSessionSettings.Profile = fmt.Sprintf("%v", profileKey)
}
if credentialsFileKey, ok := agent.Global_Config.Credentials[agent.CredentialsFile_Key]; ok {
cfg.AWSSessionSettings.SharedCredentialsFile = []string{fmt.Sprintf("%v", credentialsFileKey)}
}
cfg.AWSSessionSettings.RoleARN = agent.Global_Config.Role_arn
if c.IsSet(roleArnPathKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package awscontainerinsight

import (
"errors"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -79,6 +80,12 @@ func (t *translator) Translate(conf *confmap.Conf) (component.Config, error) {
cfg.CollectionInterval = common.GetOrDefaultDuration(conf, intervalKeyChain, defaultMetricsCollectionInterval)
cfg.ContainerOrchestrator = configuredService.Value
cfg.AWSSessionSettings.Region = agent.Global_Config.Region
if profileKey, ok := agent.Global_Config.Credentials[agent.Profile_Key]; ok {
cfg.AWSSessionSettings.Profile = fmt.Sprintf("%v", profileKey)
}
if credentialsFileKey, ok := agent.Global_Config.Credentials[agent.CredentialsFile_Key]; ok {
cfg.AWSSessionSettings.SharedCredentialsFile = []string{fmt.Sprintf("%v", credentialsFileKey)}
}

if configuredService.Value == eks {
if err := t.setClusterName(conf, cfg); err != nil {
Expand Down

0 comments on commit e2ef915

Please sign in to comment.