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

Fix cookie expiry issues from IDP/JWT auth methods, disables keepalive for JWT/IDP #1773

Merged
merged 31 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
183b502
Fix SAML timeout issues when keepalive is true
derek-ho Feb 8, 2024
a1848b0
lint
derek-ho Feb 8, 2024
9afd31e
Fix oidc flow
derek-ho Feb 9, 2024
b621d10
Merge branch 'main' of github.com:opensearch-project/security-dashboa…
derek-ho Feb 13, 2024
1da9739
Add keep alive test
derek-ho Feb 13, 2024
cf0b857
Lint
derek-ho Feb 13, 2024
bc96a3f
Add openid valid cookie test
derek-ho Feb 13, 2024
5883c80
Push up stale work
derek-ho Feb 13, 2024
278c40d
remove const assignment
derek-ho Feb 13, 2024
364827f
Introduce abstract method to block jwt implementations from keep alive
derek-ho Feb 13, 2024
8e3f434
Lint
derek-ho Feb 13, 2024
cf2b4ae
Fix compile issues
derek-ho Feb 13, 2024
52fd304
Fix warnings
derek-ho Feb 15, 2024
b9528e8
Lint
derek-ho Feb 15, 2024
1202368
Add test for JWT and fix warnings
derek-ho Feb 16, 2024
934ca76
Remove unused testing code
derek-ho Feb 16, 2024
804cce7
Remove test after PR changed
derek-ho Feb 16, 2024
3b1d5d3
Merge branch 'main' of github.com:opensearch-project/security-dashboa…
derek-ho Feb 19, 2024
529ded1
Refactor keep alive for JWT and no-op for SAML, OIDC
derek-ho Feb 19, 2024
0549a52
Lint
derek-ho Feb 19, 2024
ace02e2
Refactor test for readability
derek-ho Feb 19, 2024
6b76c4c
Add tests for each auth type
derek-ho Feb 19, 2024
275d569
Fix test to make it clear that it is taking value from date.now + ttl
derek-ho Feb 19, 2024
40878d8
Remove console log
derek-ho Feb 21, 2024
30ff169
PR feedback
derek-ho Feb 21, 2024
4c1e448
Lint
derek-ho Feb 21, 2024
1c542fc
Test fixes
derek-ho Feb 22, 2024
8721eef
Merge branch 'main' into idp-timeout
derek-ho Feb 22, 2024
d426aae
Fix variable names
derek-ho Feb 22, 2024
da0f7b9
Merge branch 'main' of github.com:opensearch-project/security-dashboa…
derek-ho Feb 22, 2024
5e7e45b
Merge branch 'idp-timeout' of github.com:derek-ho/security-dashboards…
derek-ho Feb 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions server/auth/types/authentication_type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
import { SecurityPluginConfigType } from '../..';
import { AuthenticationType } from './authentication_type';
import { httpServerMock } from '../../../../../src/core/server/mocks';
import { OpenSearchDashboardsRequest } from '../../../../../src/core/server';

class DummyAuthType extends AuthenticationType {
authNotRequired(request: OpenSearchDashboardsRequest): boolean {
return false;
}
buildAuthHeaderFromCookie() {}
getAdditionalAuthHeader() {}
handleUnauthedRequest() {}
Expand Down
14 changes: 13 additions & 1 deletion server/auth/types/authentication_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export abstract class AuthenticationType implements IAuthenticationType {

// extend session expiration time
if (this.config.session.keepalive) {
cookie!.expiryTime = Date.now() + this.config.session.ttl;
cookie!.expiryTime = this.getKeepAliveExpiry(cookie!, request);
this.sessionStorageFactory.asScoped(request).set(cookie!);
}
// cookie is valid
Expand Down Expand Up @@ -266,6 +266,13 @@ export abstract class AuthenticationType implements IAuthenticationType {
});
}

public getKeepAliveExpiry(
cookie: SecuritySessionCookie,
request: OpenSearchDashboardsRequest
): number {
return Date.now() + this.config.session.ttl;
}

isPageRequest(request: OpenSearchDashboardsRequest) {
const path = request.url.pathname || '/';
return path.startsWith('/app/') || path === '/' || path.startsWith('/goto/');
Expand All @@ -286,5 +293,10 @@ export abstract class AuthenticationType implements IAuthenticationType {
response: LifecycleResponseFactory,
toolkit: AuthToolkit
): IOpenSearchDashboardsResponse | AuthResult;
public abstract requestIncludesAuthInfo(request: OpenSearchDashboardsRequest): boolean;
derek-ho marked this conversation as resolved.
Show resolved Hide resolved
public abstract buildAuthHeaderFromCookie(
cookie: SecuritySessionCookie,
request: OpenSearchDashboardsRequest
): any;
public abstract init(): Promise<void>;
}
69 changes: 69 additions & 0 deletions server/auth/types/basic/basic_auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright OpenSearch Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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 { httpServerMock } from '../../../../../../src/core/server/http/http_server.mocks';

import { SecurityPluginConfigType } from '../../../index';
import { SecuritySessionCookie } from '../../../session/security_cookie';
import {
IRouter,
CoreSetup,
ILegacyClusterClient,
Logger,
SessionStorageFactory,
} from '../../../../../../src/core/server';
import { BasicAuthentication } from './basic_auth';

describe('Basic auth tests', () => {
let router: IRouter;
let core: CoreSetup;
let esClient: ILegacyClusterClient;
let sessionStorageFactory: SessionStorageFactory<SecuritySessionCookie>;
let logger: Logger;

const config = {
session: {
ttl: 1000,
},
} as SecurityPluginConfigType;

test('getKeepAliveExpiry', () => {
const realDateNow = Date.now.bind(global.Date);
const dateNowStub = jest.fn(() => 0);
global.Date.now = dateNowStub;
const basicAuthentication = new BasicAuthentication(
config,
sessionStorageFactory,
router,
esClient,
core,
logger
);

const cookie: SecuritySessionCookie = {
credentials: {
authHeaderValueExtra: true,
},
expiryTime: 0,
};

const request = httpServerMock.createOpenSearchDashboardsRequest({
path: '/internal/v1',
});

expect(basicAuthentication.getKeepAliveExpiry(cookie, request)).toBe(1000);
global.Date.now = realDateNow;
});
});
5 changes: 4 additions & 1 deletion server/auth/types/basic/basic_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ export class BasicAuthentication extends AuthenticationType {
}
}

buildAuthHeaderFromCookie(cookie: SecuritySessionCookie): any {
buildAuthHeaderFromCookie(
cookie: SecuritySessionCookie,
request: OpenSearchDashboardsRequest
): any {
if (this.config.auth.anonymous_auth_enabled && cookie.isAnonymousAuth) {
return {};
}
Expand Down
14 changes: 13 additions & 1 deletion server/auth/types/jwt/jwt_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
getExtraAuthStorageValue,
setExtraAuthStorage,
} from '../../../session/cookie_splitter';
import { getExpirationDate } from './jwt_helper';

export const JWT_DEFAULT_EXTRA_STORAGE_OPTIONS: ExtraAuthStorageOptions = {
cookiePrefix: 'security_authentication_jwt',
Expand Down Expand Up @@ -154,13 +155,17 @@ export class JwtAuthentication extends AuthenticationType {
this.getBearerToken(request) || '',
this.getExtraAuthStorageOptions()
);

return {
username: authInfo.user_name,
credentials: {
authHeaderValueExtra: true,
},
authType: this.type,
expiryTime: Date.now() + this.config.session.ttl,
expiryTime: getExpirationDate(
this.getBearerToken(request),
Date.now() + this.config.session.ttl
),
};
}

Expand All @@ -175,6 +180,13 @@ export class JwtAuthentication extends AuthenticationType {
);
}

getKeepAliveExpiry(cookie: SecuritySessionCookie, request: OpenSearchDashboardsRequest): number {
return getExpirationDate(
this.buildAuthHeaderFromCookie(cookie, request)[this.authHeaderName],
Date.now() + this.config.session.ttl
);
}

handleUnauthedRequest(
request: OpenSearchDashboardsRequest,
response: LifecycleResponseFactory,
Expand Down
Loading
Loading