-
-
Notifications
You must be signed in to change notification settings - Fork 390
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 metadata retrieved from the context
to the user agent when a new HTTP client is created
#2789
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2789 +/- ##
==========================================
+ Coverage 67.69% 67.71% +0.01%
==========================================
Files 238 238
Lines 22388 22392 +4
==========================================
+ Hits 15156 15163 +7
+ Misses 6036 6034 -2
+ Partials 1196 1195 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some comments/
It would be cool if we're able to test it.
Maybe we could use a proxy HTTP, and inspect the call that arrives are with the expected User agent. Something similar to: https://github.com/arduino/arduino-cli/blob/master/internal/cli/configuration/network_test.go#L29-L51
1dc53f7
to
b78fdb5
Compare
60ad95e
to
fe84afb
Compare
This allows the extraction of the user-agent in a single place. Also it forces the context passing on all operations that requires access to network.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to block this over some minor refactoring in the integration test part.
If possible I would extract the hardcoded assertion logic in the HTTPServeFile.
The implementation is LGTM. I've also tested locally with the usage of network.proxy and without and it correctly sends the UserAgent
return CreateEnvForDaemonWithUserAgent(t, "cli-test/0.0.0") | ||
} | ||
|
||
// CreateEnvForDaemonWithUserAgent performs the minimum required operations to start the arduino-cli daemon. | ||
// It returns a testsuite.Environment and an ArduinoCLI client to perform the integration tests. | ||
// The Environment must be disposed by calling the CleanUp method via defer. | ||
func CreateEnvForDaemonWithUserAgent(t *testing.T, userAgent string) (*Environment, *ArduinoCLI) { | ||
env := NewEnvironment(t) | ||
|
||
cli := NewArduinoCliWithinEnvironment(env, &ArduinoCLIConfig{ | ||
ArduinoCLIPath: FindRepositoryRootPath(t).Join("arduino-cli"), | ||
UseSharedStagingFolder: true, | ||
}) | ||
|
||
_ = cli.StartDaemon(false) | ||
_ = cli.StartDaemon(false, userAgent) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return CreateEnvForDaemonWithUserAgent(t, "cli-test/0.0.0") | |
} | |
// CreateEnvForDaemonWithUserAgent performs the minimum required operations to start the arduino-cli daemon. | |
// It returns a testsuite.Environment and an ArduinoCLI client to perform the integration tests. | |
// The Environment must be disposed by calling the CleanUp method via defer. | |
func CreateEnvForDaemonWithUserAgent(t *testing.T, userAgent string) (*Environment, *ArduinoCLI) { | |
env := NewEnvironment(t) | |
cli := NewArduinoCliWithinEnvironment(env, &ArduinoCLIConfig{ | |
ArduinoCLIPath: FindRepositoryRootPath(t).Join("arduino-cli"), | |
UseSharedStagingFolder: true, | |
}) | |
_ = cli.StartDaemon(false) | |
_ = cli.StartDaemon(false, userAgent) | |
env := NewEnvironment(t) | |
cli := NewArduinoCliWithinEnvironment(env, &ArduinoCLIConfig{ | |
ArduinoCLIPath: FindRepositoryRootPath(t).Join("arduino-cli"), | |
UseSharedStagingFolder: true, | |
}) | |
_ = cli.StartDaemon(false) |
CreateEnvForDaemonWithUserAgent
this function is used only by the CreateEnvForDaemon
I would simplify and put everything in the same function as it was before.
The useragent will be hardcoded inside the StartDaemon
@@ -410,7 +417,7 @@ func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader | |||
} | |||
|
|||
// StartDaemon starts the Arduino CLI daemon. It returns the address of the daemon. | |||
func (cli *ArduinoCLI) StartDaemon(verbose bool) string { | |||
func (cli *ArduinoCLI) StartDaemon(verbose bool, userAgent string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func (cli *ArduinoCLI) StartDaemon(verbose bool, userAgent string) string { | |
func (cli *ArduinoCLI) StartDaemon(verbose bool) string { |
I cannot think of uses cases that we need to pass different userAgent. The StartDaemon
currently is only called in the CreateEnvForDaemon
. It's responsibility is to create the grpclient and initializing grpc options.
My suggestion is to just hardcode the userAgent
in the grpc option. See later suggestion
conn, err := grpc.NewClient( | ||
cli.daemonAddr, | ||
grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
grpc.WithUserAgent(userAgent), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
grpc.WithUserAgent(userAgent), | |
grpc.WithUserAgent("cli-test/0.0.0"), |
I'd simply hardcode the user-agent here.
func (env *Environment) HTTPServeFile(port uint16, path *paths.Path, isDaemon bool) *url.URL { | ||
t := env.T() | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/"+path.Base(), func(w http.ResponseWriter, r *http.Request) { | ||
http.ServeFile(w, r, path.String()) | ||
if isDaemon { | ||
// Test that the user-agent contains metadata from the context when the CLI is in daemon mode | ||
userAgent := r.Header.Get("User-Agent") | ||
require.Contains(t, userAgent, "arduino-cli/git-snapshot") | ||
require.Contains(t, userAgent, "cli-test/0.0.0") | ||
require.Contains(t, userAgent, "grpc-go") | ||
} | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The HTTPServeFile
should just serve files, and not perform any assertion based on a boolean flag.
I understand why this was done, as it is convenient to do so, but if in he future we have to make assertions based on some other http header, this function will quickly be polluted with stuff that goes out of scope of this function.
I suggest to rollback these changes, and creating a dedicated HTTP server that asserts that. I would put such server inside the dedicated test of TestDaemonUserAgent
.
See suggestion over the TestDaemonUserAgent
.
Another approach could be to simply pass some assertions function as a callback, or create another abstraction.
func TestDaemonUserAgent(t *testing.T) { | ||
env, cli := integrationtest.CreateEnvForDaemon(t) | ||
defer env.CleanUp() | ||
|
||
// Set up an http server to serve our custom index file | ||
// The user-agent is tested inside the HTTPServeFile function | ||
test_index := paths.New("..", "testdata", "test_index.json") | ||
url := env.HTTPServeFile(8000, test_index, true) | ||
|
||
grpcInst := cli.Create() | ||
require.NoError(t, grpcInst.Init("", "", func(ir *commands.InitResponse) { | ||
fmt.Printf("INIT> %v\n", ir.GetMessage()) | ||
})) | ||
|
||
// Set extra indexes | ||
err := cli.SetValue("board_manager.additional_urls", `["http://127.0.0.1:8000/test_index.json"]`) | ||
require.NoError(t, err) | ||
|
||
{ | ||
cl, err := grpcInst.UpdateIndex(context.Background(), false) | ||
require.NoError(t, err) | ||
res, err := analyzeUpdateIndexClient(t, cl) | ||
require.NoError(t, err) | ||
require.Len(t, res, 2) | ||
require.True(t, res[url.String()].GetSuccess()) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, if possible would be nice to extract the assertion code that is done by passing the true
flag in the HTTPServerFile
. Or make an abstraction that passes some assertion functions.
Another possible idea is by "proxing" the request to the file server:
diff --git a/internal/integrationtest/daemon/daemon_test.go b/internal/integrationtest/daemon/daemon_test.go
index 7d800c84..982ba8bb 100644
--- a/internal/integrationtest/daemon/daemon_test.go
+++ b/internal/integrationtest/daemon/daemon_test.go
@@ -20,6 +20,10 @@ import (
"errors"
"fmt"
"io"
+ "maps"
+ "net/http"
+ "net/http/httptest"
+ "strings"
"testing"
"time"
@@ -562,15 +566,41 @@ func TestDaemonUserAgent(t *testing.T) {
// Set up an http server to serve our custom index file
// The user-agent is tested inside the HTTPServeFile function
test_index := paths.New("..", "testdata", "test_index.json")
- url := env.HTTPServeFile(8000, test_index, true)
+ url := env.HTTPServeFile(8000, test_index)
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ // Test that the user-agent contains metadata from the context when the CLI is in daemon mode
+ userAgent := r.Header.Get("User-Agent")
+
+ require.Contains(t, userAgent, "cli-test/0.0.0")
+ require.Contains(t, userAgent, "grpc-go")
+ // Depends on how we built the client we may have git-snapshot or 0.0.0-git in dev releases
+ require.Condition(t, func() (success bool) {
+ return strings.Contains(userAgent, "arduino-cli/git-snapshot") ||
+ strings.Contains(userAgent, "arduino-cli/0.0.0-git")
+ })
+
+ proxiedReq, err := http.NewRequest(r.Method, url.String(), r.Body)
+ require.NoError(t, err)
+ maps.Copy(proxiedReq.Header, r.Header)
+
+ proxiedResp, err := http.DefaultTransport.RoundTrip(proxiedReq)
+ require.NoError(t, err)
+ defer proxiedResp.Body.Close()
+
+ // Copy the headers from the proxy response to the original response
+ maps.Copy(r.Header, proxiedReq.Header)
+ w.WriteHeader(proxiedResp.StatusCode)
+ io.Copy(w, proxiedResp.Body)
+ }))
+ defer ts.Close()
grpcInst := cli.Create()
require.NoError(t, grpcInst.Init("", "", func(ir *commands.InitResponse) {
fmt.Printf("INIT> %v\n", ir.GetMessage())
}))
- // Set extra indexes
- err := cli.SetValue("board_manager.additional_urls", `["http://127.0.0.1:8000/test_index.json"]`)
+ additionalURL := ts.URL + "/test_index.json"
+ err := cli.SetValue("board_manager.additional_urls", fmt.Sprintf(`["%s"]`, additionalURL))
require.NoError(t, err)
{
@@ -579,7 +609,7 @@ func TestDaemonUserAgent(t *testing.T) {
res, err := analyzeUpdateIndexClient(t, cl)
require.NoError(t, err)
require.Len(t, res, 2)
- require.True(t, res[url.String()].GetSuccess())
+ require.True(t, res[additionalURL].GetSuccess())
}
}
daemon
to the user agent when the CLI is started in daemon modecontext
to the user agent when a new HTTP client is created
Please check if the PR fulfills these requirements
See how to contribute
before creating one)
our contributing guidelines
UPGRADING.md
has been updated with a migration guide (for breaking changes)configuration.schema.json
updated if new parameters are added.What kind of change does this PR introduce?
Bug fix
What is the current behavior?
The user agent does not specify if the CLI runs in daemon mode. It is always something like
arduino-cli/git-snapshot (amd64; windows; go1.23.2) Commit:84fc413a
.What is the new behavior?
The user agent obtained from the context metadata is propagated to the
network.user_agent_ext
if it's empty and the CLI runs in daemon mode. The complete user agent isarduino-cli/git-snapshot arduino-ide/2.3.4 grpc-node-js/1.9.5 daemon (amd64; windows; go1.23.4) Commit:f3dc127e
.Does this PR introduce a breaking change, and is titled accordingly?
Other information