Skip to content

Commit

Permalink
Revert "Support credential profiles (#76)" (#77)
Browse files Browse the repository at this point in the history
This reverts commit 036e753.
  • Loading branch information
wangzlei authored Nov 3, 2020
1 parent 036e753 commit bedb04c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 49 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ Usage: X-Ray [options]
5. -b --bind Overrides default UDP address (127.0.0.1:2000).
6. -t --bind-tcp Overrides default TCP address (127.0.0.1:2000).
7. -r --role-arn Assume the specified IAM role to upload segments to a different account.
8. -s --profile Use the credentials associated with the specified profile.
9. -c --config Load a configuration file from the specified path.
10. -f --log-file Output logs to the specified file path.
11. -l --log-level Log level, from most verbose to least: dev, debug, info, warn, error, prod (default).
12. -p --proxy-address Proxy address through which to upload segments.
13. -v --version Show AWS X-Ray daemon version.
14. -h --help Show this screen
8. -c --config Load a configuration file from the specified path.
9. -f --log-file Output logs to the specified file path.
10. -l --log-level Log level, from most verbose to least: dev, debug, info, warn, error, prod (default).
11. -p --proxy-address Proxy address through which to upload segments.
12. -v --version Show AWS X-Ray daemon version.
13. -h --help Show this screen

## Build

Expand Down
5 changes: 1 addition & 4 deletions cmd/tracing/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ var socketConnection string
var cpuProfile string
var memProfile string
var roleArn string
var profile string
var receiveBufferSize int
var daemonProcessBufferMemoryMB int
var logFile string
Expand Down Expand Up @@ -122,7 +121,6 @@ func initCli(configFile string) (*cli.Flag, *cfg.Config) {
defaultUDPAddress = cnfg.Socket.UDPAddress
defaultTCPAddress = cnfg.Socket.TCPAddress
defaultRoleARN = cnfg.RoleARN
defaultProfile = cnfg.Profile
defaultLocalMode = cnfg.LocalMode
defaultRegion = cnfg.Region
defaultResourceARN = cnfg.ResourceARN
Expand All @@ -137,7 +135,6 @@ func initCli(configFile string) (*cli.Flag, *cfg.Config) {
flag.StringVarF(&udpAddress, "bind", "b", defaultUDPAddress, "Overrides default UDP address (127.0.0.1:2000).")
flag.StringVarF(&tcpAddress, "bind-tcp", "t", defaultTCPAddress, "Overrides default TCP address (127.0.0.1:2000).")
flag.StringVarF(&roleArn, "role-arn", "r", defaultRoleARN, "Assume the specified IAM role to upload segments to a different account.")
flag.StringVarF(&profile, "profile", "s", defaultProfile, "Use the credentials associated with the specified profile.")
flag.StringVarF(&configFilePath, "config", "c", "", "Load a configuration file from the specified path.")
flag.StringVarF(&logFile, "log-file", "f", defaultLogPath, "Output logs to the specified file path.")
flag.StringVarF(&logLevel, "log-level", "l", defaultLogLevel, "Log level, from most verbose to least: dev, debug, info, warn, error, prod (default).")
Expand Down Expand Up @@ -195,7 +192,7 @@ func initDaemon(config *cfg.Config) *Daemon {
if config.Endpoint != "" {
log.Debugf("Using Endpoint read from Config file: %s", config.Endpoint)
}
awsConfig, session := conn.GetAWSConfigSession(&conn.Conn{}, config, roleArn, profile, regionFlag, noMetadata)
awsConfig, session := conn.GetAWSConfigSession(&conn.Conn{}, config, roleArn, regionFlag, noMetadata)
log.Infof("Using region: %v", aws.StringValue(awsConfig.Region))

log.Debugf("ARN of the AWS resource running the daemon: %v", resourceARN)
Expand Down
2 changes: 0 additions & 2 deletions pkg/cfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ LocalMode: false
ResourceARN: ""
# Assume an IAM role to upload segments to a different account.
RoleARN: ""
# Use the profile credentials from credentials file
Profile: ""
# Disable TLS certificate verification.
NoVerifySSL: false
# Upload segments to AWS X-Ray through a proxy.
Expand Down
4 changes: 0 additions & 4 deletions pkg/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ type Config struct {
// IAM role to upload segments to a different account.
RoleARN string `yaml:"RoleARN"`

// Profile name to use from the credentials file
Profile string `yaml:"Profile"`

// Enable or disable TLS certificate verification.
NoVerifySSL *bool `yaml:"NoVerifySSL"`

Expand Down Expand Up @@ -366,7 +363,6 @@ func merge(configFile string) *Config {
userConfig.TotalBufferSizeMB = getIntValue(userConfig.TotalBufferSizeMB, DefaultConfig().TotalBufferSizeMB)
userConfig.ResourceARN = getStringValue(userConfig.ResourceARN, DefaultConfig().ResourceARN)
userConfig.RoleARN = getStringValue(userConfig.RoleARN, DefaultConfig().RoleARN)
userConfig.Profile = getStringValue(userConfig.Profile, DefaultConfig().Profile)
userConfig.Concurrency = getIntValue(userConfig.Concurrency, DefaultConfig().Concurrency)
userConfig.Endpoint = getStringValue(userConfig.Endpoint, DefaultConfig().Endpoint)
userConfig.Region = getStringValue(userConfig.Region, DefaultConfig().Region)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cfg/cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ Version: 2`
}

func TestValidConfigArray(t *testing.T) {
validString := []string{"TotalBufferSizeMB", "Concurrency", "Endpoint", "Region", "Socket.UDPAddress", "Socket.TCPAddress", "ProxyServer.IdleConnTimeout", "ProxyServer.MaxIdleConnsPerHost", "ProxyServer.MaxIdleConns", "Logging.LogRotation", "Logging.LogLevel", "Logging.LogPath", "LocalMode", "ResourceARN", "RoleARN", "Profile", "NoVerifySSL", "ProxyAddress", "Version"}
validString := []string{"TotalBufferSizeMB", "Concurrency", "Endpoint", "Region", "Socket.UDPAddress", "Socket.TCPAddress", "ProxyServer.IdleConnTimeout", "ProxyServer.MaxIdleConnsPerHost", "ProxyServer.MaxIdleConns", "Logging.LogRotation", "Logging.LogLevel", "Logging.LogPath", "LocalMode", "ResourceARN", "RoleARN", "NoVerifySSL", "ProxyAddress", "Version"}
testString := validConfigArray()
if len(validString) != len(testString) {
t.Fatalf("Unexpect test array length. Got %v but should be %v", len(testString), len(validString))
Expand Down
22 changes: 8 additions & 14 deletions pkg/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ package conn

import (
"crypto/tls"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"time"
"encoding/json"
"io/ioutil"
"strings"

"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
Expand All @@ -34,7 +34,7 @@ import (
)

type connAttr interface {
newAWSSession(roleArn string, profile string, region string) *session.Session
newAWSSession(roleArn string, region string) *session.Session
getEC2Region(s *session.Session) (string, error)
}

Expand Down Expand Up @@ -133,7 +133,7 @@ func getRegionFromECSMetadata() string {
}

// GetAWSConfigSession returns AWS config and session instances.
func GetAWSConfigSession(cn connAttr, c *cfg.Config, roleArn string, profile string, region string, noMetadata bool) (*aws.Config, *session.Session) {
func GetAWSConfigSession(cn connAttr, c *cfg.Config, roleArn string, region string, noMetadata bool) (*aws.Config, *session.Session) {
var s *session.Session
var err error
var awsRegion string
Expand Down Expand Up @@ -161,7 +161,7 @@ func GetAWSConfigSession(cn connAttr, c *cfg.Config, roleArn string, profile str
log.Errorf("Cannot fetch region variable from config file, environment variables, ecs metadata, or ec2 metadata.")
os.Exit(1)
}
s = cn.newAWSSession(roleArn, profile, awsRegion)
s = cn.newAWSSession(roleArn, awsRegion)

config := &aws.Config{
Region: aws.String(awsRegion),
Expand Down Expand Up @@ -202,17 +202,11 @@ func ProxyServerTransport(config *cfg.Config) *http.Transport {
return transport
}

func (c *Conn) newAWSSession(roleArn string, profile string, region string) *session.Session {
func (c *Conn) newAWSSession(roleArn string, region string) *session.Session {
var s *session.Session
var err error
if roleArn == "" {
if profile == "" {
s = getDefaultSession()
} else {
s, err = session.NewSession(&aws.Config{
Credentials: credentials.NewSharedCredentials("", profile),
})
}
s = getDefaultSession()
} else {
stsCreds := getSTSCreds(region, roleArn)

Expand Down
28 changes: 11 additions & 17 deletions pkg/conn/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *mockConn) getEC2Region(s *session.Session) (string, error) {
return ec2Region, nil
}

func (c *mockConn) newAWSSession(roleArn string, profile string, region string) *session.Session {
func (c *mockConn) newAWSSession(roleArn string, region string) *session.Session {
return c.sn
}

Expand All @@ -79,10 +79,9 @@ func TestEC2Session(t *testing.T) {
m.On("getEC2Region", nil).Return("").Once()
var expectedSession *session.Session
roleARN := ""
profile := ""
expectedSession, _ = session.NewSession()
m.sn = expectedSession
cfg, s := GetAWSConfigSession(m, cfg.DefaultConfig(), roleARN, profile, "", false)
cfg, s := GetAWSConfigSession(m, cfg.DefaultConfig(), roleARN, "", false)
assert.Equal(t, s, expectedSession, "Expect the session object is not overridden")
assert.Equal(t, *cfg.Region, ec2Region, "Region value fetched from ec2-metadata service")
fmt.Printf("Logs: %v", log.Logs)
Expand All @@ -100,10 +99,9 @@ func TestRegionEnv(t *testing.T) {
var m = &mockConn{}
var expectedSession *session.Session
roleARN := ""
profile := ""
expectedSession, _ = session.NewSession()
m.sn = expectedSession
cfg, s := GetAWSConfigSession(m, cfg.DefaultConfig(), roleARN, profile, "", true)
cfg, s := GetAWSConfigSession(m, cfg.DefaultConfig(), roleARN, "", true)
assert.Equal(t, s, expectedSession, "Expect the session object is not overridden")
assert.Equal(t, *cfg.Region, region, "Region value fetched from environment")
assert.True(t, strings.Contains(log.Logs[1], fmt.Sprintf("Fetch region %v from environment variables", region)))
Expand All @@ -116,10 +114,9 @@ func TestRegionArgument(t *testing.T) {
var m = &mockConn{}
var expectedSession *session.Session
roleARN := ""
profile := ""
expectedSession, _ = session.NewSession()
m.sn = expectedSession
cfg, s := GetAWSConfigSession(m, cfg.DefaultConfig(), roleARN, profile, region, true)
cfg, s := GetAWSConfigSession(m, cfg.DefaultConfig(), roleARN, region, true)
assert.Equal(t, s, expectedSession, "Expect the session object is not overridden")
assert.Equal(t, *cfg.Region, region, "Region value fetched from the environment")
assert.True(t, strings.Contains(log.Logs[1], fmt.Sprintf("Fetch region %v from commandline/config file", region)))
Expand All @@ -132,12 +129,11 @@ func TestNoRegion(t *testing.T) {
var m = &mockConn{}
var expectedSession *session.Session
roleARN := ""
profile := ""
expectedSession, _ = session.NewSession()
m.sn = expectedSession

if os.Getenv(envFlag) == "1" {
GetAWSConfigSession(m, cfg.DefaultConfig(), roleARN, profile, region, true) // exits because no region found
GetAWSConfigSession(m, cfg.DefaultConfig(), roleARN, region, true) // exits because no region found
return
}

Expand Down Expand Up @@ -203,16 +199,15 @@ func TestValidECSRegion(t *testing.T) {
}

// getRegionFromECSMetadata() returns an empty string if ECS metadata related env is not set
func TestNoECSMetadata(t *testing.T) {
func TestNoECSMetadata(t *testing.T){
env := stashEnv()
defer popEnv(env)
testString := getRegionFromECSMetadata()

assert.EqualValues(t, "", testString)
}

// getRegionFromECSMetadata() throws an error and returns an empty string when ECS metadata file cannot be parsed as valid JSON
func TestInvalidECSMetadata(t *testing.T) {
func TestInvalidECSMetadata(t *testing.T){
metadataFile := "][foobar})("
setupTestFile(metadataFile)
env := stashEnv()
Expand All @@ -230,7 +225,7 @@ func TestInvalidECSMetadata(t *testing.T) {
}

// getRegionFromECSMetadata() throws an error and returns an empty string when ECS metadata file cannot be opened
func TestMissingECSMetadataFile(t *testing.T) {
func TestMissingECSMetadataFile(t *testing.T){
metadataFile := "foobar"
setupTestFile(metadataFile)
env := stashEnv()
Expand All @@ -253,12 +248,11 @@ func TestErrEC2(t *testing.T) {
m.On("getEC2Region", nil).Return("Error").Once()
var expectedSession *session.Session
roleARN := ""
profile := ""
expectedSession, _ = session.NewSession()
m.sn = expectedSession
envFlag := "NO_REGION"
if os.Getenv(envFlag) == "1" {
GetAWSConfigSession(m, cfg.DefaultConfig(), roleARN, profile, "", false)
GetAWSConfigSession(m, cfg.DefaultConfig(), roleARN, "", false)
return
}

Expand Down Expand Up @@ -299,13 +293,13 @@ func TestLoadEnvConfigCreds(t *testing.T) {
os.Setenv(k, v)
}
c := &Conn{}
cfg := c.newAWSSession("", "", "")
cfg := c.newAWSSession("", "")
value, err := cfg.Config.Credentials.Get()

assert.Nil(t, err, "Expect no error")
assert.Equal(t, cases.Val, value, "Expect the credentials value to match")

cfgA := c.newAWSSession("ROLEARN", "", "TEST")
cfgA := c.newAWSSession("ROLEARN", "TEST")
valueA, _ := cfgA.Config.Credentials.Get()

assert.Equal(t, "", valueA.AccessKeyID, "Expect the value to be empty")
Expand Down

0 comments on commit bedb04c

Please sign in to comment.