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

Switch Kerberos authentication provider to a dedicated _kerberos grant. Introduce Tokens for common access/refresh token tasks. #39366

Merged
merged 5 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { DeauthenticationResult } from './deauthentication_result';
import { Session } from './session';
import { LoginAttempt } from './login_attempt';
import { AuthenticationProviderSpecificOptions } from './providers/base';
import { Tokens } from './tokens';

interface ProviderSession {
provider: string;
Expand Down Expand Up @@ -56,11 +57,14 @@ function assertRequest(request: Legacy.Request) {
*/
function getProviderOptions(server: Legacy.Server) {
const config = server.config();
const client = getClient(server);
const log = server.log.bind(server);

return {
client: getClient(server),
log: server.log.bind(server),
client,
log,
basePath: config.get<string>('server.basePath'),
tokens: new Tokens({ client, log }),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: basic is the only provider that doesn't use tokens and it's pretty cheap to instantiate it even if basic provider is used, so I decided to have it as provider option instead of re-creating it for every provider that uses tokens.

};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { stub } from 'sinon';
import { stub, createStubInstance } from 'sinon';
import { Tokens } from '../tokens';
import { AuthenticationProviderOptions } from './base';

export function mockAuthenticationProviderOptions(
providerOptions: Partial<AuthenticationProviderOptions> = {}
providerOptions: Partial<Pick<AuthenticationProviderOptions, 'basePath'>> = {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: we don't need anything except for basePath as an argument here, but I decided to keep "args as an object" pattern here for now, to have less updates and account for the future options we may want to tweak in tests.

) {
const client = { callWithRequest: stub(), callWithInternalUser: stub() };
const log = stub();

return {
client: { callWithRequest: stub(), callWithInternalUser: stub() },
log: stub(),
client,
log,
basePath: '/base-path',
tokens: createStubInstance(Tokens),
...providerOptions,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Legacy } from 'kibana';
import { AuthenticationResult } from '../authentication_result';
import { DeauthenticationResult } from '../deauthentication_result';
import { LoginAttempt } from '../login_attempt';
import { Tokens } from '../tokens';

/**
* Describes a request complemented with `loginAttempt` method.
Expand All @@ -23,6 +24,7 @@ export interface AuthenticationProviderOptions {
basePath: string;
client: Legacy.Plugins.elasticsearch.Cluster;
log: (tags: string[], message: string) => void;
tokens: PublicMethodsOf<Tokens>;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: relying on PublicMethodsOf makes it super simple to mock tokens in TS tests. I hate that tests dictate how we should write code, but here it's more or less logically acceptable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels similar to what you have to do in Java/C# when you extract an interface that only has the public properties for the sake of testing. What you have here seems less intrusive, and makes me wonder if it's a pattern that we should embrace more widely?

I have a bad habit of pretending that TS is more similar to the type systems in Java/C# than it really is, so it's great learning techniques like these from you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you have here seems less intrusive, and makes me wonder if it's a pattern that we should embrace more widely?

In a NP world we won't have too many of these cases (contracts are usually plain objects with public only stuff on them, that's is trivial to mock and work with without additional TS voodoo), hopefully, but in places where we still want something like this I believe we embrace this approach.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the core has the same problems. We pass around ES6 classes and later in tests have type errors passing POJO with the same interface. We haven't decided on the official solution in the core #33396
but in some places already started using a separate interface for the contracts https://github.com/skaapgif/kibana/blob/a52d038cbaf605c29a94a3872d4eb70fb60c3fd2/src/legacy/server/saved_objects/service/saved_objects_client.ts#L132
Probably declaring TokenContract is more self-describing than PublicMethodsOf<Tokens>

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('BasicAuthenticationProvider', () => {
let callWithRequest: sinon.SinonStub;
beforeEach(() => {
const providerOptions = mockAuthenticationProviderOptions();
callWithRequest = providerOptions.client.callWithRequest as sinon.SinonStub;
callWithRequest = providerOptions.client.callWithRequest;
provider = new BasicAuthenticationProvider(providerOptions);
});

Expand Down
Loading