Skip to content

Commit f0d0a9d

Browse files
authored
Merge branch 'main' into filter-aoss
2 parents d477423 + 293490d commit f0d0a9d

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

server/auth/types/authentication_type.test.ts

+54
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,57 @@ describe('test tenant header', () => {
111111
expect(result.requestHeaders.securitytenant).toEqual('dummy_tenant');
112112
});
113113
});
114+
115+
describe('test capabilities request authinfo', () => {
116+
const config = {
117+
auth: {
118+
unauthenticated_routes: [] as string[],
119+
},
120+
session: {
121+
keepalive: false,
122+
},
123+
} as SecurityPluginConfigType;
124+
const sessionStorageFactory = {
125+
asScoped: jest.fn(() => {
126+
return {
127+
clear: jest.fn(),
128+
get: jest.fn().mockResolvedValue({}),
129+
};
130+
}),
131+
};
132+
const router = jest.fn();
133+
const esClient = {
134+
asScoped: jest.fn().mockImplementation(() => {
135+
return {
136+
callAsCurrentUser: jest.fn().mockImplementation(() => {
137+
return { username: 'capabilities-username' };
138+
}),
139+
};
140+
}),
141+
};
142+
const coreSetup = jest.fn();
143+
const logger = {
144+
error: jest.fn(),
145+
};
146+
147+
const dummyAuthType = new DummyAuthType(
148+
config,
149+
sessionStorageFactory,
150+
router,
151+
esClient,
152+
coreSetup,
153+
logger
154+
);
155+
156+
it(`Capabilities API includes authinfo`, async () => {
157+
const request = httpServerMock.createOpenSearchDashboardsRequest({
158+
path: '/api/core/capabilities',
159+
});
160+
const response = jest.fn();
161+
const toolkit = {
162+
authenticated: jest.fn((value) => value),
163+
};
164+
const result = await dummyAuthType.authHandler(request, response, toolkit);
165+
expect(result.state.authInfo.username).toEqual('capabilities-username');
166+
});
167+
});

server/auth/types/authentication_type.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,8 @@ export interface OpenSearchDashboardsAuthState {
7373
}
7474

7575
export abstract class AuthenticationType implements IAuthenticationType {
76-
protected static readonly ROUTES_TO_IGNORE: string[] = [
77-
'/api/core/capabilities', // FIXME: need to figure out how to bypass this API call
78-
'/app/login',
79-
];
76+
protected static readonly ROUTES_TO_IGNORE: string[] = ['/app/login'];
77+
protected static readonly ROUTES_AUTH_OPTIONAL: string[] = ['/api/core/capabilities'];
8078

8179
protected static readonly REST_API_CALL_HEADER = 'osd-xsrf';
8280

@@ -153,6 +151,11 @@ export abstract class AuthenticationType implements IAuthenticationType {
153151
return toolkit.notHandled();
154152
}
155153

154+
// allow optional authentication
155+
if (this.authOptional(request)) {
156+
return toolkit.authenticated();
157+
}
158+
156159
// send to auth workflow
157160
return this.handleUnauthedRequest(request, response, toolkit);
158161
}
@@ -236,6 +239,14 @@ export abstract class AuthenticationType implements IAuthenticationType {
236239
return false;
237240
}
238241

242+
authOptional(request: OpenSearchDashboardsRequest): boolean {
243+
const pathname = request.url.pathname;
244+
if (!pathname) {
245+
return false;
246+
}
247+
return AuthenticationType.ROUTES_AUTH_OPTIONAL.includes(pathname!);
248+
}
249+
239250
async resolveTenant(
240251
request: OpenSearchDashboardsRequest,
241252
cookie: SecuritySessionCookie,

0 commit comments

Comments
 (0)