Skip to content

Commit

Permalink
fix: log missing user at warn level (#3735)
Browse files Browse the repository at this point in the history
When using PATs if the user that the PAT is for has been removed, we
currently log the missing user at ERROR level. Since this is not
something our SREs can fix, this PR downgrades the NotFoundError to
WARN, instead of ERROR.
  • Loading branch information
Christopher Kolstad committed May 10, 2023
1 parent 9c39559 commit af3944b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/lib/middleware/pat-middleware.test.ts
@@ -1,6 +1,7 @@
import getLogger from '../../test/fixtures/no-logger';
import patMiddleware from './pat-middleware';
import User from '../types/user';
import NotFoundError from '../error/notfound-error';

let config: any;

Expand Down Expand Up @@ -108,3 +109,37 @@ test('should call next if accountService throws exception', async () => {
expect(cb).toHaveBeenCalled();
getLogger.setMuteError(false);
});

test('Should not log at error level if user not found', async () => {
let fakeLogger = {
debug: () => {},
info: () => {},
warn: jest.fn(),
error: jest.fn(),
fatal: console.error,
};
const conf = {
getLogger: () => {
return fakeLogger;
},
flagResolver: {
isEnabled: jest.fn().mockReturnValue(true),
},
};
const accountService = {
getAccountByPersonalAccessToken: jest.fn().mockImplementation(() => {
throw new NotFoundError('Could not find pat');
}),
};
let mw = patMiddleware(conf, { accountService });
const cb = jest.fn();

const req = {
header: jest.fn().mockReturnValue('user:some-token'),
user: undefined,
};

await mw(req, undefined, cb);
expect(fakeLogger.error).not.toHaveBeenCalled();
expect(fakeLogger.warn).toHaveBeenCalled();
});
10 changes: 9 additions & 1 deletion src/lib/middleware/pat-middleware.ts
@@ -1,5 +1,6 @@
import { IUnleashConfig } from '../types';
import { IAuthRequest } from '../routes/unleash-types';
import NotFoundError from '../error/notfound-error';

/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
const patMiddleware = (
Expand All @@ -21,7 +22,14 @@ const patMiddleware = (
accountService.addPATSeen(apiToken);
}
} catch (error) {
logger.error(error);
if (error instanceof NotFoundError) {
logger.warn(
'Tried to use a PAT token for user that no longer existed',
error,
);
} else {
logger.error(error);
}
}
next();
};
Expand Down

0 comments on commit af3944b

Please sign in to comment.