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

feat: Show org token selector #7385

Merged
merged 2 commits into from
Jul 18, 2023
Merged

feat: Show org token selector #7385

merged 2 commits into from
Jul 18, 2023

Conversation

mydea
Copy link
Member

@mydea mydea commented Jul 10, 2023

This adds a new org token generator for the source maps setup. For now, we only add it in one place (the webpack plugin configuration), if it works well we can roll it out to all places that use SENTRY_AUTH_TOKEN.

Screenshot 2023-07-10 at 15 36 54

If not signed in, we use this instead:

Screenshot 2023-07-10 at 15 34 25

Note that this is blocked by the org tokens being rolled out to all orgs.

Closes #7226

@mydea mydea self-assigned this Jul 10, 2023
@vercel
Copy link

vercel bot commented Jul 10, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
sentry-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 18, 2023 8:33am


const KEYWORDS_REGEX = /\b___(?:([A-Z_][A-Z0-9_]*)\.)?([A-Z_][A-Z0-9_]*)___\b/g;

const ORG_AUTH_TOKEN_REGEX = /__ORG_AUTH_TOKEN__/g;
Copy link
Member

Choose a reason for hiding this comment

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

This is inconsistently two underscores instead of three like the rest of these keywords

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually did this on purpose to avoid this being captured by the other regex. If we use triple underscore here, we need to make sure checking order is right (IMHO making this a bit more brittle for future refactorings), and it may also not be too clear that this does not go through the same code 🤔 maybe a completely different identifier would be even better, to make it more clear this is something else? For example +++ORG_AUTH_TOKEN+++?

Copy link
Member

Choose a reason for hiding this comment

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

In that case we should write tests.

If we're going to start having custom behavior for different tokens, we should implement it in a way that it's clear that this component supports handling that.

Right now using a different number of underscores is just going to be confusing, since it's really hard to notice (I already fixed a number of places where we were using 2 instead of 3).

I think the way to go here is to implement something a little bit more robust

return csrfCookie ? csrfCookie.split('=')[1] : null;
}

export async function createOrgAuthToken({
Copy link
Member

Choose a reason for hiding this comment

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

Is there a better place we can put this export? Doesn't feel right to have this inside codeContext.

I know this code is a bit of a mess right now, so it's probably unclear where this should go

Copy link
Member Author

Choose a reason for hiding this comment

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

There isn't really a utils kind of place as far as I've seen so far? Just the components, and this doesn't really have a dedicated component backing it 😬 it's only used in the codeBlock as of now. Happy to move it to any place you think makes sense!

Copy link
Member

Choose a reason for hiding this comment

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

Leave it here for now, we can move it around later once this repo is a little better organized.

}

const currentSelectionIdx = sharedSelection.PROJECT ?? 0;
const currentSelection = choices[currentSelectionIdx];
Copy link
Member

Choose a reason for hiding this comment

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

Does it prompt you for which organization to create the token for?

I've been thinking about how we can generally make this experience better, so maybe the context would be a lot more obvious

Copy link
Member Author

Choose a reason for hiding this comment

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

this just takes the org that is selected - in the UI we moved the __ORG___ above this selector to accommodate this - it would take example-org in this case:

Screenshot 2023-07-10 at 15 36 54

Comment on lines 179 to 201
function getCsrfToken(cookies = document.cookie): string | null {
// is sentry-sc in production, but may also be sc in other envs
// So we just try both variants
const cookieNames = ['sentry-sc', 'sc'];

return cookieNames
.map(cookieName => getCookie(cookies, cookieName))
.find(token => token !== null);
}
Copy link
Member

Choose a reason for hiding this comment

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

Did this not already live somewhere else? Why does this have to be in here now?

Copy link
Member Author

Choose a reason for hiding this comment

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

AFAIK this only exists in sentry frontend (where I kind of stole this from), not in docs yet! We have not been doing any POST stuff yet, only GET which does not need this.

@evanpurkhiser
Copy link
Member

Am I missing something, where does it actually show your tokens that you've already generated?

We should add them to this endpoint https://github.com/getsentry/getsentry/blob/933c060c7efe4b90ab12cca434322f2443a4eadc/getsentry/web/docsupport.py

export function SignedInCheck({
children,
isUserAuthenticated,
}: {
children: React.ReactNode;
isUserAuthenticated: boolean;
}) {
const {codeKeywords, status} = useContext(CodeContext);
const {codeKeywords, isLoading} = useContext(CodeContext);
Copy link
Member Author

Choose a reason for hiding this comment

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

@evanpurkhiser refactored this based on your feedback from the other PR!

@mydea
Copy link
Member Author

mydea commented Jul 11, 2023

Am I missing something, where does it actually show your tokens that you've already generated?

We should add them to this endpoint https://github.com/getsentry/getsentry/blob/933c060c7efe4b90ab12cca434322f2443a4eadc/getsentry/web/docsupport.py

Tokens can only be viewed once after they are created, so we always have to create a new token (which is also why we have a button that has to be manually pressed in order to create/show the token). Every time you press the button it will generate a new token!

@evanpurkhiser
Copy link
Member

Every time you press the button it will generate a new token!

Should we make this clear to the user? Seems a bit annoying that just clicking a button is going to pollute my token list. I'm not able to name it or give it a description here right?

src/components/codeBlock.tsx Outdated Show resolved Hide resolved
src/components/codeBlock.tsx Outdated Show resolved Hide resolved
Comment on lines +238 to +240
} catch {
return null;
}
Copy link
Member

Choose a reason for hiding this comment

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

will Sentry catch a bug here, in case it happens?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, we just ignore it in this case - not sure if it is worth it to capture this? May lead to some noise, not sure.

Comment on lines 30 to 32
<ExternalLink href="https://sentry.io/settings/auth-tokens/" target="_blank">
manually create an Auth Token
</ExternalLink>{' '}
Copy link
Member

Choose a reason for hiding this comment

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

doesn't the ExternalLink component already open the link in a new tab? asking because I am not familiar with the docs UI/components

Copy link
Member Author

Choose a reason for hiding this comment

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

as far as I can tell, in docs it does not open in a new tab by default..?

@priscilawebdev
Copy link
Member

Is this PR still a draft?

@mydea
Copy link
Member Author

mydea commented Jul 13, 2023

OK, so I added some tests for this for different scenarios! Thanks @priscilawebdev for helping me figure out the react test stuff 😅 But should be good now!

@mydea
Copy link
Member Author

mydea commented Jul 14, 2023

Should we make this clear to the user? Seems a bit annoying that just clicking a button is going to pollute my token list. I'm not able to name it or give it a description here right?

We generate a (hopefully explanatory) name for the generated token, so it shouldn't be too confusing, hopefully. Also, since we track which token has been used when & where, it should be easy to clean up unusued tokens from the sentry UI. We can always add a further explanation here later when needed, IMHO. We could also eventually solve this in-product (by providing some auto-cleanup functionality or whatnot).

@mdtro
Copy link
Member

mdtro commented Jul 14, 2023

Should we make this clear to the user? Seems a bit annoying that just clicking a button is going to pollute my token list. I'm not able to name it or give it a description here right?

We generate a (hopefully explanatory) name for the generated token, so it shouldn't be too confusing, hopefully. Also, since we track which token has been used when & where, it should be easy to clean up unusued tokens from the sentry UI. We can always add a further explanation here later when needed, IMHO. We could also eventually solve this in-product (by providing some auto-cleanup functionality or whatnot).

+1 to making this clear in the doc here. The comment line above it now makes it sound like I can go retrieve the token later at the link provided.

@mydea
Copy link
Member Author

mydea commented Jul 18, 2023

OK, so I updated it to show this when signed in:

Screenshot 2023-07-18 at 10 22 59

vs. this when not signed in:

Screenshot 2023-07-18 at 10 23 11

@mydea mydea merged commit 59158be into master Jul 18, 2023
6 checks passed
@mydea mydea deleted the fn/org-tokens branch July 18, 2023 08:45
shanamatthews pushed a commit that referenced this pull request Jul 31, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Aug 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auth Token Creation in Docs
4 participants