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

SimpleTokenCache (java) for node js azure Identity #29387

Open
rassiju06 opened this issue Apr 22, 2024 · 16 comments
Open

SimpleTokenCache (java) for node js azure Identity #29387

rassiju06 opened this issue Apr 22, 2024 · 16 comments
Assignees
Labels
Azure.Identity Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. issue-addressed The Azure SDK team member assisting with this issue believes it to be addressed and ready to close. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that

Comments

@rassiju06
Copy link

rassiju06 commented Apr 22, 2024

I'm interested in a feature similar to the SimpleTokenCache class from the Azure SDK for Java in Node.js for managing authentication tokens efficiently. Currently, Node.js does not seem to have an equivalent feature within the Azure SDK for JavaScript that provides simple token caching capabilities. Having such a class would greatly simplify the process of token management by automatically handling the caching, refreshing, and retrieval of tokens. This functionality is particularly useful for long-running applications or services that frequently interact with Azure services, requiring frequent token renewals. I believe integrating a SimpleTokenCache class into the Node.js Azure SDK could significantly improve developer productivity and application performance. Could this be considered for inclusion in a future release of the SDK? thank you.

https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/core/azure-core/src/main/java/com/azure/core/credential/SimpleTokenCache.java?ref=azureblue.io

@github-actions github-actions bot added Azure.Identity Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Apr 22, 2024
Copy link

@KarishmaGhiya @maorleger

Copy link

Thank you for your feedback. Tagging and routing to the team member best able to assist.

@xirzec
Copy link
Member

xirzec commented Apr 22, 2024

This sounds very much like what the tokenCycler in core does.

It's not exposed directly, but is used by our bearerTokenAuthenticationPolicy:

@rassiju06
Copy link
Author

rassiju06 commented Apr 22, 2024

Thanks, can that class be used for this? I am trying to use node js with azure user managed Identity with PostgreSQL. and I am trying to handle token refresh scenario's. thanks.

@maorleger
Copy link
Member

Hey @rassiju06 - Appreciate your patience here and apologies for the delayed followup.

I think what @xirzec was alluding to is that you should have no need to manage token lifetimes and refreshing yourself. Instead, our core libraries handle this for you as an implementation detail. Are you seeing behavior that you do not expect with token refreshing or mostly looking to understand whether you need to implement token caching yourself?

If the former, could you share some information about your scenario and what you observe / expected to see?

If the latter, the answer is no as far as I am aware - @KarishmaGhiya thoughts?

@maorleger maorleger added the needs-author-feedback More information is needed from author to address the issue. label May 1, 2024
Copy link

github-actions bot commented May 1, 2024

Hi @rassiju06. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

@github-actions github-actions bot removed the needs-team-attention This issue needs attention from Azure service team or SDK team label May 1, 2024
@rassiju06
Copy link
Author

rassiju06 commented May 2, 2024 via email

@github-actions github-actions bot added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels May 2, 2024
@maorleger
Copy link
Member

Hey @rassiju06 - thank you for that added information. I also expect the token refresh logic to handle token refresh gracefully and so I'm surprised to hear that in your case you're seeing token refresh errors. It's something that we will need to investigate on our end. I do expect the Azure SDK to handle this for you without requiring additional configuration or using a custom TokenCache.

What would help me (if at all possible) is seeing the logs - I know it's likely not possible but if you're able to turn on verbose logging and see if anything in the logs stands out as interesting or suspicious I'd love to see the logs! Specifically around the 24 hour mark when you start seeing errors.

In the meanwhile, I'll try to repro this on my end and check with the MSAL folks to see if they have any guidance. If it turns out that this is expected, we can consider exposing something similar to what Java has.

@maorleger maorleger self-assigned this May 2, 2024
@maorleger
Copy link
Member

hey @rassiju06 I wanted to get more information about your scenario - you say you're using user managed identity to connect to our postgres DB from an app service

Can you expand a bit on this?

Are you using an Azure client library for postgres?
Are you using @azure/identity to get tokens?
Are you using Azure postgres?

A small code sample would help clarify a lot of this if you can provide one - either putting something on github or just sharing the package.json and a small js script that mimics your setup so I can better understand your scenario.

I have a few suspicions but need the above information to confirm. thanks so much!

@maorleger maorleger added the needs-author-feedback More information is needed from author to address the issue. label May 7, 2024
@github-actions github-actions bot removed the needs-team-attention This issue needs attention from Azure service team or SDK team label May 7, 2024
Copy link

github-actions bot commented May 7, 2024

Hi @rassiju06. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

@rassiju06
Copy link
Author

hey @rassiju06 I wanted to get more information about your scenario - you say you're using user managed identity to connect to our postgres DB from an app service

Can you expand a bit on this?

Are you using an Azure client library for postgres? Are you using @azure/identity to get tokens? Are you using Azure postgres?

A small code sample would help clarify a lot of this if you can provide one - either putting something on github or just sharing the package.json and a small js script that mimics your setup so I can better understand your scenario.

I have a few suspicions but need the above information to confirm. thanks so much!

Hello so to answer you question we are using azure Postgres and we are using the PG npm library. we are using azure identity package as well, and following the examples from this document. I cannot share my full code, but here are the relevant parts the code works fine, and we are able to connect to the database, but once the app service reaches the 24 hour mar the connection is severed because the class cannot update the connection without a restart of the application (we are using TypeORM). perhaps a working example would help more? I have tried generic application examples from the sdk and they all seem to have the same token issue? thank you.

https://learn.microsoft.com/en-us/azure/app-service/tutorial-connect-msi-azure-database?tabs=sqldatabase-sc%2Cuserassigned-sc%2Cdotnet%2Cdotnet-mysql-mi%2Cdotnet-postgres-mi%2Cwindowsclient

`
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { ConfigService } from '@nestjs/config';
import { DefaultAzureCredential } from '@azure/identity';

export const datasourceConfig = async (
configService: ConfigService,
): Promise => {
const credentials = await getCredentials(configService);

  const newConnectionOptions: ConnectionOptions = {
    name: connectionName,
    type: 'postgres',
    host: configService.get('APP_DB_HOST'),
    username: configService.get('APP_DB_USER'),
    password: accessToken,
    database: configService.get('APP_DB_DATABASE'),
    port: configService.get<number>('APP_DB_PORT'),

async function getCredentials(configService: ConfigService) {
if (configService.get('APP_DB_MANAGED_IDENTITY', { infer: true })) {
return await getManagedIdentityCredentials(configService);
} else {
return getBasicAuthCredentials(configService);
}
}

async function getManagedIdentityCredentials(configService: ConfigService) {
const managedClientId = configService.get('APP_DB_MANAGED_IDENTITY', {
infer: true,
});
const credential = new DefaultAzureCredential({
managedIdentityClientId: managedClientId,
});

const accessToken = await credential.getToken(
'https://ossrdbms-aad.database.windows.net/.default',
);

return {
username: configService.get('APP_DB_USER', { infer: true }),
password: accessToken.token,
};
}
`

@github-actions github-actions bot added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels May 8, 2024
@maorleger
Copy link
Member

That helps a lot, thanks so much! I'll try to repro this using the code you shared and a long-running process and see what I can find

@maorleger
Copy link
Member

maorleger commented May 9, 2024

Just wanted to follow up here as I think I understand what may be the issue.

You are getting the token once and then passing that string to the PG connection options. That token, as you know, is valid for 24 hours and so feeding it the hardcoded string (as you already observed) will not work once the token expires.

But, you should be able to call getToken on the credential as many times as you'd like and the DefaultAzureCredential's implementation will intelligently return the same token or a refreshed token as needed.

It looks like node-postgres (the pg library) supports this exact scenario!

Take a look at these docs - there's a section about short-lived authentication tokens. The short of it is that this package supports passing a callback instead of a hardcoded string for the password to support short-lived credentials.

I believe if you do something like (this rough pseudocode):

const credential = new DefaultAzureCredential({managedIdentityClientId: managedClientId})
const getToken = async () => (await credential.getToken('https://ossrdbms-aad.database.windows.net/.default')).token;
const newConnectionOptions: ConnectionOptions = {
    name: connectionName,
    type: 'postgres',
    host: configService.get('APP_DB_HOST'),
    username: configService.get('APP_DB_USER'),
    password: getToken, // <-- pass the callback instead of the accesstoken directly
    database: configService.get('APP_DB_DATABASE'),
    port: configService.get<number>('APP_DB_PORT'),
}
// etc.

It should work as you'd expect with token refreshes occurring as needed. The docs I linked to should provide a more complete example, the above was not tested so excuse any typos 😄

Hope this helps! Let me know if that improves your scenario. If it does, I can work with the right folks on the documentation side to update the examples to showcase using short-lived credentials with a long-lived process

@xirzec xirzec added the needs-author-feedback More information is needed from author to address the issue. label May 10, 2024
@github-actions github-actions bot removed the needs-team-attention This issue needs attention from Azure service team or SDK team label May 10, 2024
Copy link

Hi @rassiju06. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

@rassiju06
Copy link
Author

ok thanks, Ill try this out and see

@github-actions github-actions bot added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels May 14, 2024
@maorleger maorleger added the issue-addressed The Azure SDK team member assisting with this issue believes it to be addressed and ready to close. label May 15, 2024
@github-actions github-actions bot removed the needs-team-attention This issue needs attention from Azure service team or SDK team label May 15, 2024
Copy link

Hi @rassiju06. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure.Identity Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. issue-addressed The Azure SDK team member assisting with this issue believes it to be addressed and ready to close. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Projects
Status: Untriaged
Development

No branches or pull requests

4 participants