Skip to content
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

Improve logging and validation for license keys. #2982

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jaffinito
Copy link
Member

Description

Adds an additional warning when the agent receives a HTTP 401 Unauthorized response from the collector. This occurs when the key is not valid (for many reasons).

Expands the validation logic from the license key in DefaultConfiguration to:

  • Still perform the checks once and save the value if one is found. Continue to check if nothing is found (null).
  • Check for empty/missing keys
  • Check if the value is our default value - "REPLACE_WITH_LICENSE_KEY"
  • Check if it is the correct length - 40 characters
  • Check if the characters are valid for a key - ASCII characters from 0x21 to 0x7E
  • Check each of the different key locations and work through them in priority order to find a key without issues.
    • When a location has a value, but that value is invalid log a Finest message letting you know that a bad key was found and where.
    • If a valid key is found, report a Finest message including where. Happens once since value is cached.
    • If no valid key is found log a Finest message to let us know. Happens each time the key is request from config.

Unit tests have been expanded to cover the new logic.

Note: valid in this context means that it is not null or whitespace, exactly 40 ASCII characters from 0x21 to 0x7E, or "REPLACE_WITH_LICENSE_KEY".

Fixes #2969
Fixes #2783

Author Checklist

  • Unit tests, Integration tests, and Unbounded tests completed
  • Performance testing completed with satisfactory results (if required)

Reviewer Checklist

  • Perform code review
  • Pull request was adequately tested (new/existing tests, performance tests)

@jaffinito jaffinito requested a review from a team as a code owner February 3, 2025 18:14
}

// return string.Empty instead of null to allow caching and prevent checking repeatedly
Log.Finest("No valid license key found.");
Copy link
Member

Choose a reason for hiding this comment

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

Seems like we might want to log this at a higher level -- info or warning?


// If the key is the default value from newrelic.config, we return the default value
// AgentManager.AssertAgentEnabled() relies on this behavior and will throw an exception if the key is the default value
if (candidateKey.Value.Equals("REPLACE_WITH_LICENSE_KEY"))
Copy link
Member

Choose a reason for hiding this comment

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

A small suggestion: a lot of our sample code uses variations of YOUR_LICENSE_KEY for the environment variable. Maybe if we search for the string "license", it's a good bet that they haven't filled it in? That should cover us for all three key sources, and may be more helpful than the fall-through invalid key log messages.

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.19%. Comparing base (7ee9825) to head (1faa00d).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2982      +/-   ##
==========================================
+ Coverage   82.16%   82.19%   +0.03%     
==========================================
  Files         473      473              
  Lines       30364    30355       -9     
  Branches     3388     3384       -4     
==========================================
+ Hits        24948    24950       +2     
+ Misses       4617     4605      -12     
- Partials      799      800       +1     
Flag Coverage Δ
Profiler 73.13% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...c/Agent/Core/Configuration/DefaultConfiguration.cs 89.72% <ø> (-0.04%) ⬇️
...elic/Agent/Core/DataTransport/ConnectionManager.cs 77.55% <ø> (ø)

... and 3 files with indirect coverage changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve logging around invalid license key Do some sanity checking on license keys
4 participants