core-app-api: warn if IdentityApi returns an invalid userEntityRef

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-01-11 10:13:19 +01:00
parent d879072b0c
commit c4a1dc4ec8
2 changed files with 92 additions and 1 deletions
@@ -0,0 +1,82 @@
/*
* Copyright 2020 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { withLogCollector } from '@backstage//test-utils';
import { AppIdentityProxy } from './AppIdentityProxy';
describe('AppIdentityProxy', () => {
const mockIdentityApi = {
getBackstageIdentity: jest.fn(),
getProfileInfo: jest.fn(),
getCredentials: jest.fn(),
signOut: jest.fn(),
};
beforeEach(() => {
jest.resetAllMocks();
});
it('should forward user identities', async () => {
const proxy = new AppIdentityProxy();
proxy.setTarget(mockIdentityApi);
const logs = await withLogCollector(async () => {
mockIdentityApi.getBackstageIdentity.mockResolvedValueOnce({
type: 'user',
userEntityRef: 'user:default/foo',
ownershipEntityRefs: [],
});
await expect(proxy.getBackstageIdentity()).resolves.toEqual({
type: 'user',
userEntityRef: 'user:default/foo',
ownershipEntityRefs: [],
});
});
expect(logs).toEqual({
log: [],
warn: [],
error: [],
});
});
it('should warn about invalid user entity refs', async () => {
const proxy = new AppIdentityProxy();
proxy.setTarget(mockIdentityApi);
const logs = await withLogCollector(async () => {
mockIdentityApi.getBackstageIdentity.mockResolvedValueOnce({
type: 'user',
userEntityRef: 'bar',
ownershipEntityRefs: [],
});
await expect(proxy.getBackstageIdentity()).resolves.toEqual({
type: 'user',
userEntityRef: 'bar',
ownershipEntityRefs: [],
});
});
expect(logs).toEqual({
log: [],
warn: [
`WARNING: The App IdentityApi provided an invalid userEntityRef, 'bar'. ` +
`It must be a full Entity Reference of the form '<kind>:<namespace>/<name>'.`,
],
error: [],
});
});
});
@@ -86,7 +86,16 @@ export class AppIdentityProxy implements IdentityApi {
if (!this.target) {
throw mkError('getBackstageIdentity');
}
return this.target.getBackstageIdentity();
const identity = await this.target.getBackstageIdentity();
if (!identity.userEntityRef.match(/^.*:.*\/.*$/)) {
// eslint-disable-next-line no-console
console.warn(
`WARNING: The App IdentityApi provided an invalid userEntityRef, '${identity.userEntityRef}'. ` +
`It must be a full Entity Reference of the form '<kind>:<namespace>/<name>'.`,
);
}
return identity;
}
async getCredentials(): Promise<{ token?: string | undefined }> {