From ec2f67bcefb6bb78d1aedcc76f497243ca7d69e4 Mon Sep 17 00:00:00 2001 From: Sebastian Choren Date: Thu, 25 Jul 2024 18:40:52 -0300 Subject: [PATCH 1/6] add --insecure and --skip-verify flags for agent --- agent/client/client.go | 2 + agent/client/connector.go | 48 ++++++--------------- agent/client/options.go | 12 ++++++ agent/config/config.go | 2 + agent/config/config_test.go | 4 +- agent/config/flags.go | 2 + agent/runner/runner.go | 18 +++++--- agent/runner/session.go | 12 +++++- cli/cmd/start_cmd.go | 6 +++ cli/config/api.go | 35 ++++++++------- cli/config/config.go | 2 + cli/config/configurator.go | 30 ++++++------- examples/tracetest-k6/collector.config.yaml | 6 ++- examples/tracetest-k6/docker-compose.yml | 2 + 14 files changed, 104 insertions(+), 77 deletions(-) diff --git a/agent/client/client.go b/agent/client/client.go index d6c3a136c0..adba88a140 100644 --- a/agent/client/client.go +++ b/agent/client/client.go @@ -40,6 +40,8 @@ type SessionConfig struct { type Client struct { mutex sync.Mutex endpoint string + insecure bool + skipVerify bool conn *grpc.ClientConn config Config sessionConfig *SessionConfig diff --git a/agent/client/connector.go b/agent/client/connector.go index bde4bd0936..22610de926 100644 --- a/agent/client/connector.go +++ b/agent/client/connector.go @@ -4,8 +4,6 @@ import ( "context" "crypto/tls" "fmt" - "net" - "os" "time" "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" @@ -66,14 +64,7 @@ func (c *Client) connect(ctx context.Context) error { ctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() - transportCredentials, err := getTransportCredentialsForEndpoint(c.endpoint) - if err != nil { - return fmt.Errorf("could not get transport credentials: %w", err) - } - - conn, err := grpc.DialContext( - ctx, c.endpoint, - grpc.WithTransportCredentials(transportCredentials), + opts := []grpc.DialOption{ grpc.WithDefaultServiceConfig(retryPolicy), grpc.WithIdleTimeout(0), // disable grpc idle timeout grpc.WithStatsHandler(otelgrpc.NewClientHandler()), @@ -82,6 +73,19 @@ func (c *Client) connect(ctx context.Context) error { propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}), ), )), + } + + if c.insecure { + opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials())) + } else { + opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{ + InsecureSkipVerify: c.skipVerify, + }))) + } + + conn, err := grpc.DialContext( + ctx, c.endpoint, + opts..., ) if err != nil { return fmt.Errorf("could not connect to server: %w", err) @@ -90,27 +94,3 @@ func (c *Client) connect(ctx context.Context) error { c.conn = conn return nil } - -func getTransportCredentialsForEndpoint(endpoint string) (credentials.TransportCredentials, error) { - _, port, err := net.SplitHostPort(endpoint) - if err != nil { - return nil, fmt.Errorf("cannot parse endpoint: %w", err) - } - - tlsCreds := credentials.NewTLS(&tls.Config{ - InsecureSkipVerify: true, - }) - - if os.Getenv("TRACETEST_DEV_FORCE_URL") == "true" { - return tlsCreds, nil - } - - switch port { - case "443": - return tlsCreds, nil - - default: - return insecure.NewCredentials(), nil - } - -} diff --git a/agent/client/options.go b/agent/client/options.go index ef3da08dc4..78fb899cad 100644 --- a/agent/client/options.go +++ b/agent/client/options.go @@ -44,3 +44,15 @@ func WithTracer(tracer trace.Tracer) Option { c.tracer = tracer } } + +func WithInsecure() Option { + return func(c *Client) { + c.insecure = true + } +} + +func WithSkipVerify() Option { + return func(c *Client) { + c.skipVerify = true + } +} diff --git a/agent/config/config.go b/agent/config/config.go index 088a7f6210..6527b13aca 100644 --- a/agent/config/config.go +++ b/agent/config/config.go @@ -17,6 +17,8 @@ type Config struct { ServerURL string `mapstructure:"server_url"` CollectorEndpoint string `mapstructure:"collector_endpoint"` Mode string `mapstructure:"mode"` + Insecure bool `mapstructure:"insecure"` + SkipVerify bool `mapstructure:"skip_verify"` OTLPServer OtlpServer `mapstructure:"otlp_server"` } diff --git a/agent/config/config_test.go b/agent/config/config_test.go index 847d5dd238..1ccf3f23e7 100644 --- a/agent/config/config_test.go +++ b/agent/config/config_test.go @@ -28,7 +28,7 @@ func TestConfigWithEnvs(t *testing.T) { t.Cleanup(func() { os.Unsetenv("TRACETEST_AGENT_NAME") os.Unsetenv("TRACETEST_API_KEY") - os.Unsetenv("TRACETEST_DEV_MODE") + os.Unsetenv("TRACETEST_DEV") os.Unsetenv("TRACETEST_SERVER_URL") os.Unsetenv("TRACETEST_OTLP_SERVER_GRPC_PORT") os.Unsetenv("TRACETEST_OTLP_SERVER_HTTP_PORT") @@ -38,7 +38,7 @@ func TestConfigWithEnvs(t *testing.T) { os.Setenv("TRACETEST_AGENT_NAME", "my-agent-name") os.Setenv("TRACETEST_API_KEY", "my-agent-api-key") os.Setenv("TRACETEST_ENVIRONMENT_ID", "123456") - os.Setenv("TRACETEST_DEV_MODE", "true") + os.Setenv("TRACETEST_DEV", "true") os.Setenv("TRACETEST_SERVER_URL", "https://custom.server.com") os.Setenv("TRACETEST_OTLP_SERVER_GRPC_PORT", "1234") os.Setenv("TRACETEST_OTLP_SERVER_HTTP_PORT", "1235") diff --git a/agent/config/flags.go b/agent/config/flags.go index 9578cf406d..f15718353d 100644 --- a/agent/config/flags.go +++ b/agent/config/flags.go @@ -17,6 +17,8 @@ type Flags struct { Mode Mode LogLevel string CollectorEndpoint string + Insecure bool + SkipVerify bool } func (f Flags) AutomatedEnvironmentCanBeInferred() bool { diff --git a/agent/runner/runner.go b/agent/runner/runner.go index a1d3184f12..f134509586 100644 --- a/agent/runner/runner.go +++ b/agent/runner/runner.go @@ -88,7 +88,7 @@ Once started, Tracetest Agent exposes OTLP ports 4317 and 4318 to ingest traces func (s *Runner) onStartAgent(ctx context.Context, cfg config.Config) { if cfg.AgentApiKey != "" { - err := s.StartAgent(ctx, cfg.AgentEndpoint, cfg.AgentApiKey, cfg.UIEndpoint, cfg.EnvironmentID) + err := s.StartAgent(ctx, cfg, cfg.AgentApiKey, cfg.EnvironmentID) if err != nil { s.ui.Error(err.Error()) } @@ -106,23 +106,27 @@ func (s *Runner) onStartAgent(ctx context.Context, cfg config.Config) { return } - err = s.StartAgent(ctx, cfg.AgentEndpoint, agentToken, cfg.UIEndpoint, "") + err = s.StartAgent(ctx, cfg, agentToken, "") if err != nil { s.ui.Error(err.Error()) } } -func (s *Runner) StartAgent(ctx context.Context, endpoint, agentApiKey, uiEndpoint, environmentID string) error { +func (s *Runner) StartAgent(ctx context.Context, cliConfig config.Config, agentApiKey, environmentID string) error { cfg, err := agentConfig.LoadConfig() + + cfg.Insecure = cliConfig.AllowInsecure + cfg.SkipVerify = cliConfig.SkipVerify + s.logger.Debug("Loaded agent config", zap.Any("config", cfg)) if err != nil { s.logger.Error("Could not load agent config", zap.Error(err)) return err } - if endpoint != "" { - s.logger.Debug("Overriding agent endpoint", zap.String("endpoint", endpoint)) - cfg.ServerURL = endpoint + if cliConfig.AgentEndpoint != "" { + s.logger.Debug("Overriding agent endpoint", zap.String("endpoint", cliConfig.AgentEndpoint)) + cfg.ServerURL = cliConfig.AgentEndpoint } s.logger.Debug("Agent endpoint", zap.String("endpoint", cfg.ServerURL)) @@ -140,7 +144,7 @@ func (s *Runner) StartAgent(ctx context.Context, endpoint, agentApiKey, uiEndpoi if s.mode == agentConfig.Mode_Desktop { s.logger.Debug("Starting agent in desktop mode") - return s.RunDesktopStrategy(ctx, cfg, uiEndpoint) + return s.RunDesktopStrategy(ctx, cfg, cliConfig.UIEndpoint) } s.logger.Debug("Starting agent in verbose mode") diff --git a/agent/runner/session.go b/agent/runner/session.go index 011880fdac..a24fa70a71 100644 --- a/agent/runner/session.go +++ b/agent/runner/session.go @@ -117,11 +117,21 @@ func StartCollector(ctx context.Context, config config.Config, traceCache collec } func newControlPlaneClient(ctx context.Context, config config.Config, traceCache collector.TraceCache, observer event.Observer, logger *zap.Logger, tracer trace.Tracer, meter metric.Meter) (*client.Client, error) { - controlPlaneClient, err := client.Connect(ctx, config.ServerURL, + opts := []client.Option{ client.WithAPIKey(config.APIKey), client.WithAgentName(config.Name), client.WithEnvironmentID(config.EnvironmentID), client.WithLogger(logger), + } + if config.Insecure { + opts = append(opts, client.WithInsecure()) + } + + if config.SkipVerify { + opts = append(opts, client.WithSkipVerify()) + } + controlPlaneClient, err := client.Connect(ctx, config.ServerURL, + opts..., ) if err != nil { observer.Error(err) diff --git a/cli/cmd/start_cmd.go b/cli/cmd/start_cmd.go index 014ee4b581..69923c2878 100644 --- a/cli/cmd/start_cmd.go +++ b/cli/cmd/start_cmd.go @@ -35,6 +35,8 @@ var startCmd = &cobra.Command{ Mode: agentConfig.Mode(startParams.mode), LogLevel: startParams.logLevel, CollectorEndpoint: startParams.collectorEndpoint, + Insecure: startParams.insecure, + SkipVerify: startParams.skipVerify, } // override organization and environment id from context. @@ -89,6 +91,8 @@ func init() { startCmd.Flags().StringVarP(&startParams.mode, "mode", "m", "desktop", "set how the agent will start") startCmd.Flags().StringVarP(&startParams.logLevel, "log-level", "l", "debug", "set the agent log level") startCmd.Flags().StringVarP(&startParams.collectorEndpoint, "collector-endpoint", "", "", "address of the OTel Collector endpoint") + startCmd.Flags().BoolVarP(&startParams.insecure, "insecure", "", false, "allow insecure connections to control plane") + startCmd.Flags().BoolVarP(&startParams.skipVerify, "skip-verify", "", false, "skip verification of the server certificate (allows self signed, for example)") startCmd.Flags().MarkDeprecated("endpoint", "use --server-url instead") startCmd.Flags().MarkShorthandDeprecated("e", "use --server-url instead") @@ -104,4 +108,6 @@ type startParameters struct { mode string logLevel string collectorEndpoint string + insecure bool + skipVerify bool } diff --git a/cli/config/api.go b/cli/config/api.go index d3001cc69a..f57a568250 100644 --- a/cli/config/api.go +++ b/cli/config/api.go @@ -4,7 +4,6 @@ import ( "crypto/tls" "fmt" "net/http" - "os" "strings" "github.com/kubeshop/tracetest/cli/analytics" @@ -26,21 +25,8 @@ func GetAPIClient(cliConfig Config) *openapi.APIClient { config.AddDefaultHeader("x-organization-id", cliConfig.OrganizationID) config.AddDefaultHeader("x-environment-id", cliConfig.EnvironmentID) config.AddDefaultHeader("Authorization", fmt.Sprintf("Bearer %s", cliConfig.Jwt)) - if os.Getenv("TRACETEST_DEV_FORCE_URL") == "true" { - if config.HTTPClient == nil { - config.HTTPClient = http.DefaultClient - } - if config.HTTPClient.Transport == nil { - config.HTTPClient.Transport = http.DefaultTransport - } - - if t, ok := config.HTTPClient.Transport.(*http.Transport); ok { - if t.TLSClientConfig == nil { - t.TLSClientConfig = &tls.Config{} - } - - t.TLSClientConfig.InsecureSkipVerify = true - } + if cliConfig.SkipVerify { + setUnsecureClient(config) } config.Scheme = cliConfig.Scheme @@ -53,3 +39,20 @@ func GetAPIClient(cliConfig Config) *openapi.APIClient { return openapi.NewAPIClient(config) } + +func setUnsecureClient(config *openapi.Configuration) { + if config.HTTPClient == nil { + config.HTTPClient = http.DefaultClient + } + if config.HTTPClient.Transport == nil { + config.HTTPClient.Transport = http.DefaultTransport + } + + if t, ok := config.HTTPClient.Transport.(*http.Transport); ok { + if t.TLSClientConfig == nil { + t.TLSClientConfig = &tls.Config{} + } + + t.TLSClientConfig.InsecureSkipVerify = true + } +} diff --git a/cli/config/config.go b/cli/config/config.go index 57ae20e15c..f5f7c17140 100644 --- a/cli/config/config.go +++ b/cli/config/config.go @@ -23,6 +23,8 @@ var ( ) type Config struct { + AllowInsecure bool `yaml:"allowInsecure,omitempty"` + SkipVerify bool `yaml:"skipVerify,omitempty"` Scheme string `yaml:"scheme"` Endpoint string `yaml:"endpoint"` ServerPath string `yaml:"serverPath,omitempty"` diff --git a/cli/config/configurator.go b/cli/config/configurator.go index 99ca05bb53..25b2211be5 100644 --- a/cli/config/configurator.go +++ b/cli/config/configurator.go @@ -91,6 +91,9 @@ func (c Configurator) Start(ctx context.Context, prev *Config, flags agentConfig return err } + cfg.AllowInsecure = c.flags.Insecure + cfg.SkipVerify = c.flags.SkipVerify + cfg, err, isOSS := c.populateConfigWithVersionInfo(ctx, cfg) if err != nil { c.logger.Error("Could not populate config with version info", zap.Error(err)) @@ -158,14 +161,9 @@ func (c Configurator) createConfig(serverURL string) (Config, error) { return Config{}, err } - if os.Getenv("TRACETEST_DEV_FORCE_URL") != "true" { - if strings.Contains(serverURL, DefaultCloudDomain) { - path = DefaultCloudPath - } else if !strings.HasSuffix(path, "/api") { - path = strings.TrimSuffix(path, "/") + "/api" - } - } else { - c.ui.Warning("Server path overwritten by TRACETEST_DEV_FORCE_URL") + if strings.Contains(serverURL, DefaultCloudDomain) { + endpoint = strings.TrimPrefix(endpoint, "app") + endpoint = "api" + endpoint } return Config{ @@ -190,7 +188,7 @@ func (e invalidServerErr) Render() { e.ui.Error(msg) } -func (c Configurator) populateConfigWithDevConfig(ctx context.Context, cfg *Config) { +func (c Configurator) populateConfigWithDevConfig(_ context.Context, cfg *Config) { cfg.AgentEndpoint = os.Getenv("TRACETEST_DEV_AGENT_ENDPOINT") if cfg.AgentEndpoint == "" { cfg.AgentEndpoint = "localhost:8091" @@ -215,14 +213,14 @@ func (c Configurator) populateConfigWithDevConfig(ctx context.Context, cfg *Conf } func (c Configurator) populateConfigWithVersionInfo(ctx context.Context, cfg Config) (_ Config, _ error, isOSS bool) { - useDevVersion := os.Getenv("TRACETEST_AGENT_DEV_CONFIG") == "true" - if useDevVersion && Version == "dev" { - c.populateConfigWithDevConfig(ctx, &cfg) + // useDevVersion := os.Getenv("TRACETEST_AGENT_DEV_CONFIG") == "true" + // if useDevVersion && Version == "dev" { + // c.populateConfigWithDevConfig(ctx, &cfg) - c.ui.Success("Configured Tracetest CLI in development mode") + // c.ui.Success("Configured Tracetest CLI in development mode") - return cfg, nil, false - } + // return cfg, nil, false + // } client := GetAPIClient(cfg) version, err := getVersionMetadata(ctx, client) @@ -255,7 +253,7 @@ func (c Configurator) populateConfigWithVersionInfo(ctx context.Context, cfg Con func (c Configurator) handleOAuth(ctx context.Context, cfg Config, prev *Config) (Config, error) { if prev != nil && cfg.UIEndpoint == prev.UIEndpoint { c.logger.Debug("Using previous UI endpoint", zap.String("uiEndpoint", cfg.UIEndpoint)) - if prev != nil && prev.Jwt != "" { + if prev.Jwt != "" { c.logger.Debug("Using previous JWT") cfg.Jwt = prev.Jwt cfg.Token = prev.Token diff --git a/examples/tracetest-k6/collector.config.yaml b/examples/tracetest-k6/collector.config.yaml index a1ab056cc6..4b089e3f05 100644 --- a/examples/tracetest-k6/collector.config.yaml +++ b/examples/tracetest-k6/collector.config.yaml @@ -13,10 +13,14 @@ exporters: endpoint: jaeger:14250 tls: insecure: true + otlp/agent: + endpoint: host.docker.internal:4317 + tls: + insecure: true service: pipelines: traces: receivers: [otlp] processors: [batch] - exporters: [jaeger] + exporters: [jaeger, otlp/agent] diff --git a/examples/tracetest-k6/docker-compose.yml b/examples/tracetest-k6/docker-compose.yml index 3de2f4ff88..6b49cf4908 100644 --- a/examples/tracetest-k6/docker-compose.yml +++ b/examples/tracetest-k6/docker-compose.yml @@ -37,6 +37,8 @@ services: - ./collector.config.yaml:/otel-local-config.yaml depends_on: - jaeger + extra_hosts: + - "host.docker.internal:host-gateway" jaeger: image: jaegertracing/all-in-one:latest From 988243b935deab0fd3fe81cf20f51ae6118d2259 Mon Sep 17 00:00:00 2001 From: Sebastian Choren Date: Thu, 25 Jul 2024 19:00:55 -0300 Subject: [PATCH 2/6] fix flag for configure cmd --- cli/cmd/configure_cmd.go | 3 ++- cli/cmd/root.go | 1 + cli/cmd/start_cmd.go | 4 +--- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/cmd/configure_cmd.go b/cli/cmd/configure_cmd.go index d757a24c6a..0032638b58 100644 --- a/cli/cmd/configure_cmd.go +++ b/cli/cmd/configure_cmd.go @@ -24,7 +24,8 @@ var configureCmd = &cobra.Command{ PreRun: setupLogger, Run: WithResultHandler(WithParamsHandler(configParams)(func(ctx context.Context, cmd *cobra.Command, _ []string) (string, error) { flags := agentConfig.Flags{ - CI: configParams.CI, + CI: configParams.CI, + SkipVerify: cliConfig.SkipVerify, } config, err := config.LoadConfig("") diff --git a/cli/cmd/root.go b/cli/cmd/root.go index b73ad1d65f..624bd4588c 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -78,6 +78,7 @@ func init() { rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "config.yml", "config file will be used by the CLI") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "display debug information") rootCmd.PersistentFlags().StringVarP(&overrideEndpoint, "server-url", "s", "", "server url") + rootCmd.PersistentFlags().BoolVarP(&cliConfig.SkipVerify, "skip-verify", "", false, "skip verification of the server certificate (allows self signed, for example)") groups := []*cobra.Group{cmdGroupConfig, cmdGroupResources, cmdGroupMisc} diff --git a/cli/cmd/start_cmd.go b/cli/cmd/start_cmd.go index 69923c2878..7f5a841b42 100644 --- a/cli/cmd/start_cmd.go +++ b/cli/cmd/start_cmd.go @@ -36,7 +36,7 @@ var startCmd = &cobra.Command{ LogLevel: startParams.logLevel, CollectorEndpoint: startParams.collectorEndpoint, Insecure: startParams.insecure, - SkipVerify: startParams.skipVerify, + SkipVerify: cliConfig.SkipVerify, } // override organization and environment id from context. @@ -92,7 +92,6 @@ func init() { startCmd.Flags().StringVarP(&startParams.logLevel, "log-level", "l", "debug", "set the agent log level") startCmd.Flags().StringVarP(&startParams.collectorEndpoint, "collector-endpoint", "", "", "address of the OTel Collector endpoint") startCmd.Flags().BoolVarP(&startParams.insecure, "insecure", "", false, "allow insecure connections to control plane") - startCmd.Flags().BoolVarP(&startParams.skipVerify, "skip-verify", "", false, "skip verification of the server certificate (allows self signed, for example)") startCmd.Flags().MarkDeprecated("endpoint", "use --server-url instead") startCmd.Flags().MarkShorthandDeprecated("e", "use --server-url instead") @@ -109,5 +108,4 @@ type startParameters struct { logLevel string collectorEndpoint string insecure bool - skipVerify bool } From e0c8a3491acb301bf880549cf4c472caf2b9ad6b Mon Sep 17 00:00:00 2001 From: Sebastian Choren Date: Mon, 29 Jul 2024 11:22:54 -0300 Subject: [PATCH 3/6] fix skipverify for resources --- cli/cmd/configure_cmd.go | 2 +- cli/cmd/resources.go | 9 ++++++++- cli/cmd/root.go | 4 +++- cli/cmd/start_cmd.go | 2 +- cli/config/configurator.go | 2 +- cli/pkg/resourcemanager/client.go | 8 +++++++- 6 files changed, 21 insertions(+), 6 deletions(-) diff --git a/cli/cmd/configure_cmd.go b/cli/cmd/configure_cmd.go index 0032638b58..10fc8d125d 100644 --- a/cli/cmd/configure_cmd.go +++ b/cli/cmd/configure_cmd.go @@ -25,7 +25,7 @@ var configureCmd = &cobra.Command{ Run: WithResultHandler(WithParamsHandler(configParams)(func(ctx context.Context, cmd *cobra.Command, _ []string) (string, error) { flags := agentConfig.Flags{ CI: configParams.CI, - SkipVerify: cliConfig.SkipVerify, + SkipVerify: skipVerify, } config, err := config.LoadConfig("") diff --git a/cli/cmd/resources.go b/cli/cmd/resources.go index 1aa127b14d..f4d58d69a5 100644 --- a/cli/cmd/resources.go +++ b/cli/cmd/resources.go @@ -403,13 +403,20 @@ func setupResources() { extraHeaders.Set("x-environment-id", cliConfig.EnvironmentID) extraHeaders.Set("Authorization", fmt.Sprintf("Bearer %s", cliConfig.Jwt)) + // if cliConfig has SkipVerify set to true, use that value. + // otherwise use the value from the flag + if cliConfig.SkipVerify { + skipVerify = true + } + // To avoid a ciruclar reference initialization when setting up the registry and its resources, // we create the resources with a pointer to an unconfigured HTTPClient. // When each command is run, this function is run in the PreRun stage, before any of the actual `Run` code is executed // We take this chance to configure the HTTPClient with the correct URL and headers. // To make this configuration propagate to all the resources, we need to replace the pointer to the HTTPClient. // For more details, see https://github.com/kubeshop/tracetest/pull/2832#discussion_r1245616804 - hc := resourcemanager.NewHTTPClient(fmt.Sprintf("%s%s", cliConfig.URL(), cliConfig.Path()), extraHeaders) + + hc := resourcemanager.NewHTTPClient(fmt.Sprintf("%s%s", cliConfig.URL(), cliConfig.Path()), extraHeaders, skipVerify) *httpClient = *hc } diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 624bd4588c..94462675fd 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -73,12 +73,14 @@ var ( } ) +var skipVerify bool + func init() { rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "", fmt.Sprintf("output format [%s]", outputFormatsString)) rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "config.yml", "config file will be used by the CLI") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "display debug information") rootCmd.PersistentFlags().StringVarP(&overrideEndpoint, "server-url", "s", "", "server url") - rootCmd.PersistentFlags().BoolVarP(&cliConfig.SkipVerify, "skip-verify", "", false, "skip verification of the server certificate (allows self signed, for example)") + rootCmd.PersistentFlags().BoolVarP(&skipVerify, "skip-verify", "", false, "skip verification of the server certificate (allows self signed, for example)") groups := []*cobra.Group{cmdGroupConfig, cmdGroupResources, cmdGroupMisc} diff --git a/cli/cmd/start_cmd.go b/cli/cmd/start_cmd.go index 7f5a841b42..c6d612c7b8 100644 --- a/cli/cmd/start_cmd.go +++ b/cli/cmd/start_cmd.go @@ -36,7 +36,7 @@ var startCmd = &cobra.Command{ LogLevel: startParams.logLevel, CollectorEndpoint: startParams.collectorEndpoint, Insecure: startParams.insecure, - SkipVerify: cliConfig.SkipVerify, + SkipVerify: skipVerify, } // override organization and environment id from context. diff --git a/cli/config/configurator.go b/cli/config/configurator.go index 25b2211be5..34acf8ef71 100644 --- a/cli/config/configurator.go +++ b/cli/config/configurator.go @@ -411,7 +411,7 @@ func SetupHttpClient(cfg Config) *resourcemanager.HTTPClient { extraHeaders.Set("x-environment-id", "default") } - return resourcemanager.NewHTTPClient(fmt.Sprintf("%s%s", cfg.URL(), cfg.Path()), extraHeaders) + return resourcemanager.NewHTTPClient(fmt.Sprintf("%s%s", cfg.URL(), cfg.Path()), extraHeaders, cfg.SkipVerify) } func GetTokenClaims(tokenString string) (jwt.MapClaims, error) { diff --git a/cli/pkg/resourcemanager/client.go b/cli/pkg/resourcemanager/client.go index 56dd0842ff..d5883dfa0f 100644 --- a/cli/pkg/resourcemanager/client.go +++ b/cli/pkg/resourcemanager/client.go @@ -2,6 +2,7 @@ package resourcemanager import ( "context" + "crypto/tls" "fmt" "io" "net/http" @@ -32,7 +33,7 @@ type HTTPClient struct { extraHeaders http.Header } -func NewHTTPClient(baseURL string, extraHeaders http.Header) *HTTPClient { +func NewHTTPClient(baseURL string, extraHeaders http.Header, skipVerify bool) *HTTPClient { return &HTTPClient{ client: http.Client{ // this function avoids blindly followin redirects. @@ -42,6 +43,11 @@ func NewHTTPClient(baseURL string, extraHeaders http.Header) *HTTPClient { CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: skipVerify, + }, + }, }, baseURL: baseURL, extraHeaders: extraHeaders, From bb915c6c0c82dfeb19915c66841c2ef7b12da7ba Mon Sep 17 00:00:00 2001 From: Sebastian Choren Date: Mon, 29 Jul 2024 12:23:51 -0300 Subject: [PATCH 4/6] fix tests --- agent/client/workflow_listen_for_ds_connection_tests_test.go | 2 +- agent/client/workflow_listen_for_otlp_connection_tests_test.go | 2 +- agent/client/workflow_listen_for_poll_requests_test.go | 2 +- agent/client/workflow_listen_for_trigger_requests_test.go | 2 +- agent/client/workflow_send_ds_connection_result_test.go | 2 +- agent/client/workflow_send_otlp_connection_result_test.go | 2 +- agent/client/workflow_send_trace_test.go | 2 +- agent/client/workflow_send_trigger_response_test.go | 2 +- agent/client/workflow_shutdown_test.go | 2 +- agent/client/workflow_startup_test.go | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/agent/client/workflow_listen_for_ds_connection_tests_test.go b/agent/client/workflow_listen_for_ds_connection_tests_test.go index d0efb0d1bc..0fea4186c0 100644 --- a/agent/client/workflow_listen_for_ds_connection_tests_test.go +++ b/agent/client/workflow_listen_for_ds_connection_tests_test.go @@ -17,7 +17,7 @@ func TestDataStoreConnectionTestWorkflow(t *testing.T) { server := mocks.NewGrpcServer() defer server.Stop() - client, err := client.Connect(ctx, server.Addr()) + client, err := client.Connect(ctx, server.Addr(), client.WithInsecure()) require.NoError(t, err) var receivedConnectionTestRequest *proto.DataStoreConnectionTestRequest diff --git a/agent/client/workflow_listen_for_otlp_connection_tests_test.go b/agent/client/workflow_listen_for_otlp_connection_tests_test.go index 64ad665782..38a34f035f 100644 --- a/agent/client/workflow_listen_for_otlp_connection_tests_test.go +++ b/agent/client/workflow_listen_for_otlp_connection_tests_test.go @@ -17,7 +17,7 @@ func TestOtlpConnectionTestWorkflow(t *testing.T) { server := mocks.NewGrpcServer() defer server.Stop() - client, err := client.Connect(ctx, server.Addr()) + client, err := client.Connect(ctx, server.Addr(), client.WithInsecure()) require.NoError(t, err) var receivedConnectionTestRequest *proto.OTLPConnectionTestRequest diff --git a/agent/client/workflow_listen_for_poll_requests_test.go b/agent/client/workflow_listen_for_poll_requests_test.go index a629798f70..8026a7100a 100644 --- a/agent/client/workflow_listen_for_poll_requests_test.go +++ b/agent/client/workflow_listen_for_poll_requests_test.go @@ -17,7 +17,7 @@ func TestPollWorkflow(t *testing.T) { server := mocks.NewGrpcServer() defer server.Stop() - client, err := client.Connect(ctx, server.Addr()) + client, err := client.Connect(ctx, server.Addr(), client.WithInsecure()) require.NoError(t, err) var receivedPollingRequest *proto.PollingRequest diff --git a/agent/client/workflow_listen_for_trigger_requests_test.go b/agent/client/workflow_listen_for_trigger_requests_test.go index 117ae78e66..153b7eddfc 100644 --- a/agent/client/workflow_listen_for_trigger_requests_test.go +++ b/agent/client/workflow_listen_for_trigger_requests_test.go @@ -17,7 +17,7 @@ func TestTriggerWorkflow(t *testing.T) { server := mocks.NewGrpcServer() defer server.Stop() - client, err := client.Connect(ctx, server.Addr()) + client, err := client.Connect(ctx, server.Addr(), client.WithInsecure()) require.NoError(t, err) var receivedTrigger *proto.TriggerRequest diff --git a/agent/client/workflow_send_ds_connection_result_test.go b/agent/client/workflow_send_ds_connection_result_test.go index 76573b1664..e9bd56593c 100644 --- a/agent/client/workflow_send_ds_connection_result_test.go +++ b/agent/client/workflow_send_ds_connection_result_test.go @@ -15,7 +15,7 @@ func TestDataStoreConnectionResult(t *testing.T) { server := mocks.NewGrpcServer() defer server.Stop() - client, err := client.Connect(context.Background(), server.Addr()) + client, err := client.Connect(context.Background(), server.Addr(), client.WithInsecure()) require.NoError(t, err) err = client.Start(context.Background()) diff --git a/agent/client/workflow_send_otlp_connection_result_test.go b/agent/client/workflow_send_otlp_connection_result_test.go index c533649f8f..c7ea16d334 100644 --- a/agent/client/workflow_send_otlp_connection_result_test.go +++ b/agent/client/workflow_send_otlp_connection_result_test.go @@ -16,7 +16,7 @@ func TestOTLPConnectionResultTrace(t *testing.T) { server := mocks.NewGrpcServer() defer server.Stop() - client, err := client.Connect(context.Background(), server.Addr()) + client, err := client.Connect(context.Background(), server.Addr(), client.WithInsecure()) require.NoError(t, err) err = client.Start(context.Background()) diff --git a/agent/client/workflow_send_trace_test.go b/agent/client/workflow_send_trace_test.go index 4446564117..84e5e6769e 100644 --- a/agent/client/workflow_send_trace_test.go +++ b/agent/client/workflow_send_trace_test.go @@ -15,7 +15,7 @@ func TestSendTrace(t *testing.T) { server := mocks.NewGrpcServer() defer server.Stop() - client, err := client.Connect(context.Background(), server.Addr()) + client, err := client.Connect(context.Background(), server.Addr(), client.WithInsecure()) require.NoError(t, err) err = client.Start(context.Background()) diff --git a/agent/client/workflow_send_trigger_response_test.go b/agent/client/workflow_send_trigger_response_test.go index 7dac798da3..7564ffd871 100644 --- a/agent/client/workflow_send_trigger_response_test.go +++ b/agent/client/workflow_send_trigger_response_test.go @@ -15,7 +15,7 @@ func TestSendTriggerResult(t *testing.T) { server := mocks.NewGrpcServer() defer server.Stop() - client, err := client.Connect(context.Background(), server.Addr()) + client, err := client.Connect(context.Background(), server.Addr(), client.WithInsecure()) require.NoError(t, err) err = client.Start(context.Background()) diff --git a/agent/client/workflow_shutdown_test.go b/agent/client/workflow_shutdown_test.go index 26df95b3e1..463e83ac6a 100644 --- a/agent/client/workflow_shutdown_test.go +++ b/agent/client/workflow_shutdown_test.go @@ -17,7 +17,7 @@ func TestShutdownFlow(t *testing.T) { server := mocks.NewGrpcServer() defer server.Stop() - client, err := client.Connect(ctx, server.Addr()) + client, err := client.Connect(ctx, server.Addr(), client.WithInsecure()) require.NoError(t, err) var called bool = false diff --git a/agent/client/workflow_startup_test.go b/agent/client/workflow_startup_test.go index 3fcc645c72..9bc25aa7b7 100644 --- a/agent/client/workflow_startup_test.go +++ b/agent/client/workflow_startup_test.go @@ -15,7 +15,7 @@ func TestStartupFlow(t *testing.T) { server := mocks.NewGrpcServer() defer server.Stop() - client, err := client.Connect(context.Background(), server.Addr()) + client, err := client.Connect(context.Background(), server.Addr(), client.WithInsecure()) require.NoError(t, err) err = client.Start(context.Background()) From be818a8dd0cff3743ce5193da0171f615138027f Mon Sep 17 00:00:00 2001 From: Sebastian Choren Date: Mon, 29 Jul 2024 15:59:50 -0300 Subject: [PATCH 5/6] fix tests --- agent/Makefile | 2 +- agent/workers/poller_test.go | 6 +++--- agent/workers/trigger_test.go | 2 +- cli/config/configurator.go | 12 ++++++------ 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/agent/Makefile b/agent/Makefile index 9e59de2a8f..7678c5c17a 100644 --- a/agent/Makefile +++ b/agent/Makefile @@ -21,4 +21,4 @@ clean-proto: @rm -f proto/*.go test: - go test -timeout 150s -coverprofile=coverage.out ./... + go test -timeout 30s -coverprofile=coverage.out ./... diff --git a/agent/workers/poller_test.go b/agent/workers/poller_test.go index 8b3437a208..33968d8a33 100644 --- a/agent/workers/poller_test.go +++ b/agent/workers/poller_test.go @@ -23,7 +23,7 @@ func TestPollerWorker(t *testing.T) { ctx := ContextWithTracingEnabled() controlPlane := mocks.NewGrpcServer() - client, err := client.Connect(ctx, controlPlane.Addr()) + client, err := client.Connect(ctx, controlPlane.Addr(), client.WithInsecure()) require.NoError(t, err) pollerWorker := workers.NewPollerWorker(client, workers.WithPollerStoppableProcessRunner(workers.NewProcessStopper().RunStoppableProcess)) @@ -123,7 +123,7 @@ func TestPollerWorkerWithInmemoryDatastore(t *testing.T) { ctx := context.Background() controlPlane := mocks.NewGrpcServer() - client, err := client.Connect(ctx, controlPlane.Addr()) + client, err := client.Connect(ctx, controlPlane.Addr(), client.WithInsecure()) require.NoError(t, err) cache := collector.NewTraceCache() @@ -186,7 +186,7 @@ func TestPollerWithInvalidDataStore(t *testing.T) { ctx := context.Background() controlPlane := mocks.NewGrpcServer() - client, err := client.Connect(ctx, controlPlane.Addr()) + client, err := client.Connect(ctx, controlPlane.Addr(), client.WithInsecure()) require.NoError(t, err) pollerWorker := workers.NewPollerWorker(client, workers.WithPollerStoppableProcessRunner(workers.NewProcessStopper().RunStoppableProcess)) diff --git a/agent/workers/trigger_test.go b/agent/workers/trigger_test.go index 713d69c797..77c91f3976 100644 --- a/agent/workers/trigger_test.go +++ b/agent/workers/trigger_test.go @@ -20,7 +20,7 @@ func setupTriggerWorker(t *testing.T) (*mocks.GrpcServerMock, collector.TraceCac controlPlane := mocks.NewGrpcServer() cache := collector.NewTraceCache() - client, err := client.Connect(context.Background(), controlPlane.Addr()) + client, err := client.Connect(context.Background(), controlPlane.Addr(), client.WithInsecure()) require.NoError(t, err) triggerWorker := workers.NewTriggerWorker( diff --git a/cli/config/configurator.go b/cli/config/configurator.go index 34acf8ef71..3b4fbe9f3d 100644 --- a/cli/config/configurator.go +++ b/cli/config/configurator.go @@ -213,14 +213,14 @@ func (c Configurator) populateConfigWithDevConfig(_ context.Context, cfg *Config } func (c Configurator) populateConfigWithVersionInfo(ctx context.Context, cfg Config) (_ Config, _ error, isOSS bool) { - // useDevVersion := os.Getenv("TRACETEST_AGENT_DEV_CONFIG") == "true" - // if useDevVersion && Version == "dev" { - // c.populateConfigWithDevConfig(ctx, &cfg) + useDevVersion := os.Getenv("TRACETEST_AGENT_DEV_CONFIG") == "true" + if useDevVersion && Version == "dev" { + c.populateConfigWithDevConfig(ctx, &cfg) - // c.ui.Success("Configured Tracetest CLI in development mode") + c.ui.Success("Configured Tracetest CLI in development mode") - // return cfg, nil, false - // } + return cfg, nil, false + } client := GetAPIClient(cfg) version, err := getVersionMetadata(ctx, client) From 4534c230896cfaeb14dd4edabd407a5a34fd9ffa Mon Sep 17 00:00:00 2001 From: Sebastian Choren Date: Mon, 29 Jul 2024 16:28:51 -0300 Subject: [PATCH 6/6] revert test timeout --- agent/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agent/Makefile b/agent/Makefile index 7678c5c17a..9e59de2a8f 100644 --- a/agent/Makefile +++ b/agent/Makefile @@ -21,4 +21,4 @@ clean-proto: @rm -f proto/*.go test: - go test -timeout 30s -coverprofile=coverage.out ./... + go test -timeout 150s -coverprofile=coverage.out ./...