-
Notifications
You must be signed in to change notification settings - Fork 12
fix: send coder User-Agent on all deployment requests #180
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| using System.Reflection; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace Coder.Desktop.CoderSdk; | ||
|
|
||
| /// <summary> | ||
| /// Identifies which Coder Desktop process is making a request. | ||
| /// </summary> | ||
| public enum CoderComponent | ||
| { | ||
| /// <summary>The tray application.</summary> | ||
| Desktop, | ||
|
|
||
| /// <summary>The privileged background service that manages the VPN tunnel.</summary> | ||
| Core, | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Builds the User-Agent header value sent by every Coder Desktop HTTP client. | ||
| /// </summary> | ||
| public static class UserAgent | ||
| { | ||
| private const string DesktopToken = "coder-desktop"; | ||
| private const string CoreToken = "coder-desktop-core"; | ||
|
|
||
| private const string UnknownVersion = "0.0.0"; | ||
| private const string UnknownPlatform = "unknown"; | ||
|
|
||
| /// <summary> | ||
| /// Builds a User-Agent for <paramref name="component" />, taking the version from the entry assembly. | ||
| /// </summary> | ||
| public static string Build(CoderComponent component) | ||
| { | ||
| return Build(component, Assembly.GetEntryAssembly()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Builds a User-Agent for component, taking the version from versionSource. Prefer the single-argument overload outside of tests. | ||
| /// </summary> | ||
| public static string Build(CoderComponent component, Assembly? versionSource) | ||
| { | ||
| return $"{TokenOf(component)}/{VersionOf(versionSource)} ({Goos()}/{Goarch()})"; | ||
| } | ||
|
|
||
| private static string TokenOf(CoderComponent component) | ||
| { | ||
| return component switch | ||
| { | ||
| CoderComponent.Desktop => DesktopToken, | ||
| CoderComponent.Core => CoreToken, | ||
| _ => throw new ArgumentOutOfRangeException(nameof(component), component, null), | ||
| }; | ||
| } | ||
|
|
||
| private static string VersionOf(Assembly? assembly) | ||
| { | ||
| // Assembly versions are four-part (0.8.4.0); the User-Agent reports the three-part release. | ||
| var version = assembly?.GetName().Version; | ||
| return version is null ? UnknownVersion : $"{version.Major}.{version.Minor}.{version.Build}"; | ||
| } | ||
|
|
||
| // Platform names deliberately match Go's GOOS/GOARCH Desktop clients, the CLI and the vpn-daemon | ||
| private static string Goos() | ||
| { | ||
| if (OperatingSystem.IsWindows()) return "windows"; | ||
| if (OperatingSystem.IsMacOS()) return "darwin"; | ||
| if (OperatingSystem.IsLinux()) return "linux"; | ||
| return UnknownPlatform; | ||
| } | ||
|
|
||
| private static string Goarch() | ||
| { | ||
| return RuntimeInformation.ProcessArchitecture switch | ||
| { | ||
| Architecture.X64 => "amd64", | ||
| Architecture.Arm => "arm", | ||
| Architecture.Arm64 => "arm64", | ||
| _ => UnknownPlatform, | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| using System.Reflection; | ||
| using System.Text.RegularExpressions; | ||
| using Coder.Desktop.CoderSdk; | ||
|
|
||
| namespace Coder.Desktop.Tests.CoderSdk; | ||
|
|
||
| [TestFixture] | ||
| public class UserAgentTest | ||
| { | ||
| // Every Coder client is expected to match this, so operators can allowlist the token set. Keep it in | ||
| // sync with the Coder CLI (cli/root.go) and the macOS and Linux Desktop clients. | ||
| private static readonly Regex Grammar = new( | ||
| @"^coder-(cli|desktop|desktop-core|vpn-daemon)/\d+\.\d+\.\d+ \((windows|darwin|linux|unknown)/(386|amd64|arm|arm64|unknown)(; .+)?\)$"); | ||
|
|
||
| [Test(Description = "Matches the shared User-Agent grammar")] | ||
| [TestCase(CoderComponent.Desktop)] | ||
| [TestCase(CoderComponent.Core)] | ||
| public void MatchesGrammar(CoderComponent component) | ||
| { | ||
| Assert.That(UserAgent.Build(component), Does.Match(Grammar)); | ||
| } | ||
|
|
||
| [Test(Description = "Each component reports its own token")] | ||
| public void ComponentTokens() | ||
| { | ||
| Assert.That(UserAgent.Build(CoderComponent.Desktop), Does.StartWith("coder-desktop/")); | ||
| Assert.That(UserAgent.Build(CoderComponent.Core), Does.StartWith("coder-desktop-core/")); | ||
| } | ||
|
|
||
| [Test(Description = "Four-part assembly versions are trimmed to the three-part release version")] | ||
| public void TrimsAssemblyVersion() | ||
| { | ||
| var assembly = Assembly.GetExecutingAssembly(); | ||
| var version = assembly.GetName().Version; | ||
| Assert.That(version, Is.Not.Null, "test assembly has no version"); | ||
|
|
||
| var ua = UserAgent.Build(CoderComponent.Desktop, assembly); | ||
| Assert.That(ua, Does.Match(Grammar)); | ||
| Assert.That(ua, Does.StartWith($"coder-desktop/{version!.Major}.{version.Minor}.{version.Build} (")); | ||
| } | ||
|
|
||
| [Test(Description = "An unversioned assembly still produces a well-formed User-Agent")] | ||
| public void UnknownVersion() | ||
| { | ||
| var ua = UserAgent.Build(CoderComponent.Desktop, null); | ||
| Assert.That(ua, Does.Match(Grammar)); | ||
| Assert.That(ua, Does.StartWith("coder-desktop/0.0.0 (")); | ||
| } | ||
|
|
||
| [Test(Description = "Unrecognized components are rejected rather than silently mislabeled")] | ||
| public void UnknownComponent() | ||
| { | ||
| Assert.Throws<ArgumentOutOfRangeException>(() => UserAgent.Build((CoderComponent)(-1))); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.