Skip to content

fix(profile): preserve "." in profile names so credentials are stored as a literal key - #1854

Open
pranav-new-relic wants to merge 1 commit into
mainfrom
fix/profile-name-dot-truncation
Open

fix(profile): preserve "." in profile names so credentials are stored as a literal key#1854
pranav-new-relic wants to merge 1 commit into
mainfrom
fix/profile-name-dot-truncation

Conversation

@pranav-new-relic

Copy link
Copy Markdown
Member

What broke

Creating a CLI profile whose name contains a . — for example newrelic profile add --profile ava.morgan ... — appeared to succeed, but every subsequent command reported FATAL profile ava.morgan does not exist. The root cause is that the credentials store is JSON-backed via gjson/sjson, and those libraries treat . as a path separator for nested objects. So writing the value at the path ava.morgan.apiKey produced {"ava":{"morgan":{"apiKey":"..."}}} instead of {"ava.morgan":{"apiKey":"..."}}. Because the top-level key was now ava and not ava.morgan, GetScopes() lost the original name, and any read or delete keyed on ava.morgan failed. Underscores worked because they are not special characters to gjson/sjson. The customer-reported thread referenced this exact symptom with a profile name like firstname.lastname.

What changed

The fix lives in internal/config/json_store.go: a small helper escapePathSegment runs over the user-controlled segments (the profile name passed as scope, and the optional global scope) before they're joined with the literal . separator used to build a gjson/sjson path. It escapes \ first and then ., which is the documented escape syntax both libraries accept, so a name like ava.morgan becomes the path ava\.morgan.apiKey and is stored as a single literal top-level key. The same helper is also applied in RemoveScope so deleting a dotted profile no longer also nukes its siblings. Three new unit tests in json_store_test.go cover set+get, list-scopes, and remove for dotted names (ava.morgan, riley.chen, prod.us-east-1, team.platform, team.observability) and confirm that two profiles sharing a prefix like team.* are stored independently. The fix is a no-op for any profile name that does not contain . or \, so existing on-disk credentials and the common profile-naming case stay completely untouched.

Profile names with a "." (e.g. "ava.morgan") were being interpreted as
nested gjson/sjson paths in the JSON-backed credentials store. As a
result, "newrelic profile add --profile ava.morgan ..." silently wrote
{"ava":{"morgan":{...}}}, GetScopes returned only "ava", and follow-up
commands reported the profile did not exist.

Escape "." (and "\") in user-controlled path segments — profile name and
the optional global scope — before joining them with the literal "."
separator that gjson/sjson uses for nesting. The well-known field keys
defined inside the package never contain dots, so behavior for existing
non-dotted profiles is unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

1 participant