Token from DefaultAzureCredential expires/limited based on app config
See original GitHub issue- Package Name: @azure/identity
- Package Version: 2.0.4
- Operating system: Windows 10
- nodejs
- version: 16.13.0
- browser
- name/version:
- typescript
- version:
- Is the bug related to documentation in
- README.md
- source code documentation
- SDK API docs on https://docs.microsoft.com
Describe the bug A clear and concise description of what the bug is.
When using the DefaultAzureCredential
, if I create a new instance upon server startup, then occasionally requests error out because the token has expired. Subsequent requests work, I assume because the token gets refreshed afterwards, but even one failed request is unacceptable for my scenario.
To try to work around this issue, I moved the DefaultAzureCredential
instance creation to the routes, which works locally (short-running server), but then throws this error when server has been running for long time (>1 day for sure, but could start sooner):
{
"code": "ELOGIN",
"originalError": { "code": "ELOGIN" },
"name": "ConnectionError"
}
To Reproduce Steps to reproduce the behavior:
- Start Nodejs app with
DefaultAzureCredential
created at startup - Ping route that tests connection and receive 200
- Wait >24hrs
- Ping same route and receive 500
Expected behavior A clear and concise description of what you expected to happen.
When creating new DefaultAzureCredential
instance on server startup, expected token to automatically refresh during authentication process with Azure resource. When creating new DefaultAzureCredential
instance on each request, expected no limit on token creation (suspected issue).
Screenshots If applicable, add screenshots to help explain your problem.
Additional context Add any other context about the problem here.
Here’s an example of an MVP of my Nodejs app:
const express = require('express');
const { DefaultAzureCredential } = require('@azure/identity');
const sql = require('mssql');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const configureSQL = async credential => {
const { token } = await credential.getToken(
'https://database.windows.net/.default'
);
const config = {
authentication: {
type: 'azure-active-directory-access-token',
options: {
token: token
}
},
server: process.env.DB_SERVER,
database: process.env.DB_NAME,
options: {
encrypt: true,
enableArithAbort: true
},
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000
}
};
return config;
};
app.get('/', (req, res) => {
const credential = new DefaultAzureCredential();
const config = await configureSQL(credential);
const pool = await sql.connect(config);
const result = await pool.query('SELECT * FROM TestTable');
res.send(result.recordset);
})
app.listen(7000, () => {
console.log('Server started on port 7000');
});
Issue Analytics
- State:
- Created a year ago
- Comments:14 (7 by maintainers)
That’s great!! Thanks @nihonjinboy85
Hi @KarishmaGhiya ,
Great news! I’ve turned on the verbose logging as discussed and waited 24 hours for the server startup token to expire. In reviewing the logs, it looks like the new token is successfully being created. What I think is actually happening is that the
mssql
NPM package is not establishing a new connection to my SQL DB using the new token, but rather responding with the existing connection pool in order to prevent my app from creating multiple connection pools and eventually overloading the SQL server. I’ve confirmed this in logs, so I’ll go ahead and close this issue and instead take it up with themssql
team.Thank you so much for all of you help!