Skip to content

[VC-43793] Add debug roundtripper to discovery and identity clients for easier debugging #683

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

Merged
merged 1 commit into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/internal/cyberark/dataupload/dataupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewCyberArkClient(trustedCAs *x509.CertPool, baseURL string, authenticateRe
if trustedCAs != nil {
tr.TLSClientConfig.RootCAs = trustedCAs
}
cyberClient.Transport = transport.DebugWrappers(tr)
cyberClient.Transport = transport.NewDebuggingRoundTripper(tr, transport.DebugByContext)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DebugWrappers function is better in a way because it only wraps the transport if the global logger has been configured at level >=6....which avoids the inefficiency of looking up the log level for each request if the log level is at its default level.
But it makes it harder to control the logging verbosity from tests because the wrapper doesn't know about the verbosity setting on the ktesting logger.
I might revert back to DebugWrapper in future, but for now this makes it easier for me to see what's going on in the test logs.


return &CyberArkClient{
baseURL: baseURL,
Expand Down
11 changes: 9 additions & 2 deletions pkg/internal/cyberark/dataupload/dataupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/jetstack/preflight/pkg/internal/cyberark/dataupload"
"github.com/jetstack/preflight/pkg/internal/cyberark/identity"
"github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"

_ "k8s.io/klog/v2/ktesting/init"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sets up the -testing.v and -testing.vmodule command line flags for the test.

)

func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
Expand Down Expand Up @@ -126,6 +128,11 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
// An API token is obtained by authenticating with the ARK_USERNAME and ARK_SECRET from the environment.
// ARK_SUBDOMAIN should be your tenant subdomain.
// ARK_PLATFORM_DOMAIN should be either integration-cyberark.cloud or cyberark.cloud
//
// To enable verbose request logging:
//
// go test ./pkg/internal/cyberark/dataupload/... \
// -v -count 1 -run TestPostDataReadingsWithOptionsWithRealAPI -args -testing.v 6
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pasted some examples in the PR description.

func TestPostDataReadingsWithOptionsWithRealAPI(t *testing.T) {
platformDomain := os.Getenv("ARK_PLATFORM_DOMAIN")
subdomain := os.Getenv("ARK_SUBDOMAIN")
Expand All @@ -137,7 +144,7 @@ func TestPostDataReadingsWithOptionsWithRealAPI(t *testing.T) {
return
}

logger := ktesting.NewLogger(t, ktesting.NewConfig())
logger := ktesting.NewLogger(t, ktesting.DefaultConfig)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DefaultConfig has the flag names which are registered by importing the init package (above)

ctx := klog.NewContext(t.Context(), logger)

const (
Expand Down Expand Up @@ -165,7 +172,7 @@ func TestPostDataReadingsWithOptionsWithRealAPI(t *testing.T) {
cyberArkClient, err := dataupload.NewCyberArkClient(nil, serviceURL, identityClient.AuthenticateRequest)
require.NoError(t, err)

err = cyberArkClient.PostDataReadingsWithOptions(t.Context(), api.DataReadingsPost{}, dataupload.Options{
err = cyberArkClient.PostDataReadingsWithOptions(ctx, api.DataReadingsPost{}, dataupload.Options{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a mistake I made in #681

ClusterName: "bb068932-c80d-460d-88df-34bc7f3f3297",
})
require.NoError(t, err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/internal/cyberark/identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/cenkalti/backoff/v5"
"k8s.io/client-go/transport"
"k8s.io/klog/v2"

"github.com/jetstack/preflight/pkg/internal/cyberark/servicediscovery"
Expand Down Expand Up @@ -212,7 +213,8 @@ func NewWithDiscoveryClient(ctx context.Context, discoveryClient *servicediscove

return &Client{
client: &http.Client{
Timeout: 10 * time.Second,
Timeout: 10 * time.Second,
Transport: transport.NewDebuggingRoundTripper(http.DefaultTransport, transport.DebugByContext),
},

endpoint: endpoint,
Expand Down
5 changes: 4 additions & 1 deletion pkg/internal/cyberark/servicediscovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/url"
"time"

"k8s.io/client-go/transport"

"github.com/jetstack/preflight/pkg/version"
)

Expand Down Expand Up @@ -62,7 +64,8 @@ func WithCustomEndpoint(endpoint string) ClientOpt {
func New(clientOpts ...ClientOpt) *Client {
client := &Client{
client: &http.Client{
Timeout: 10 * time.Second,
Timeout: 10 * time.Second,
Transport: transport.NewDebuggingRoundTripper(http.DefaultTransport, transport.DebugByContext),
},
endpoint: prodDiscoveryEndpoint,
}
Expand Down