-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
credential_store.test.ts
187 lines (156 loc) · 5.07 KB
/
credential_store.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { KibanaRequest } from 'src/core/server';
import { loggingSystemMock, httpServerMock } from 'src/core/server/mocks';
import { securityMock } from '../../../../security/server/mocks';
import { ReindexStep, ReindexStatus, ReindexSavedObject } from '../../../common/types';
import { credentialStoreFactory } from './credential_store';
const basicAuthHeader = 'Basic abc';
const logMock = loggingSystemMock.create().get();
const requestMock = KibanaRequest.from(
httpServerMock.createRawRequest({
headers: {
authorization: basicAuthHeader,
},
})
);
const securityStartMock = securityMock.createStart();
const reindexOpMock = {
id: 'asdf',
type: 'type',
references: [],
attributes: {
indexName: 'test',
newIndexName: 'new-index',
status: ReindexStatus.inProgress,
lastCompletedStep: ReindexStep.created,
locked: null,
reindexTaskId: null,
reindexTaskPercComplete: null,
errorMessage: null,
runningReindexCount: null,
},
} as ReindexSavedObject;
describe('credentialStore', () => {
it('retrieves the same credentials for the same state', async () => {
const credStore = credentialStoreFactory(logMock);
await credStore.set({
request: requestMock,
reindexOp: reindexOpMock,
security: securityStartMock,
});
expect(credStore.get(reindexOpMock)).toEqual({
authorization: basicAuthHeader,
});
});
it('does not retrieve credentials if the state changed', async () => {
const credStore = credentialStoreFactory(logMock);
await credStore.set({
request: requestMock,
reindexOp: reindexOpMock,
security: securityStartMock,
});
reindexOpMock.attributes.lastCompletedStep = ReindexStep.readonly;
expect(credStore.get(reindexOpMock)).toBeUndefined();
});
it('retrieves credentials after update', async () => {
const credStore = credentialStoreFactory(logMock);
await credStore.set({
request: requestMock,
reindexOp: reindexOpMock,
security: securityStartMock,
});
const updatedReindexOp = {
...reindexOpMock,
attributes: {
...reindexOpMock.attributes,
status: 0,
},
};
await credStore.update({
credential: {
authorization: basicAuthHeader,
},
reindexOp: updatedReindexOp,
security: securityStartMock,
});
expect(credStore.get(updatedReindexOp)).toEqual({
authorization: basicAuthHeader,
});
});
describe('API keys enabled', () => {
const apiKeyResultMock = {
id: 'api_key_id',
name: 'api_key_name',
api_key: '123',
};
const invalidateApiKeyResultMock = {
invalidated_api_keys: [apiKeyResultMock.api_key],
previously_invalidated_api_keys: [],
error_count: 0,
};
const base64ApiKey = Buffer.from(`${apiKeyResultMock.id}:${apiKeyResultMock.api_key}`).toString(
'base64'
);
beforeEach(() => {
securityStartMock.authc.apiKeys.areAPIKeysEnabled.mockReturnValue(Promise.resolve(true));
securityStartMock.authc.apiKeys.grantAsInternalUser.mockReturnValue(
Promise.resolve(apiKeyResultMock)
);
securityStartMock.authc.apiKeys.invalidateAsInternalUser.mockReturnValue(
Promise.resolve(invalidateApiKeyResultMock)
);
});
it('sets API key in authorization header', async () => {
const credStore = credentialStoreFactory(logMock);
await credStore.set({
request: requestMock,
reindexOp: reindexOpMock,
security: securityStartMock,
});
expect(credStore.get(reindexOpMock)).toEqual({
authorization: `ApiKey ${base64ApiKey}`,
});
});
it('invalidates API keys when a reindex operation is complete', async () => {
const credStore = credentialStoreFactory(logMock);
await credStore.set({
request: requestMock,
reindexOp: reindexOpMock,
security: securityStartMock,
});
await credStore.update({
credential: {
authorization: `ApiKey ${base64ApiKey}`,
},
reindexOp: {
...reindexOpMock,
attributes: {
...reindexOpMock.attributes,
status: 1,
},
},
security: securityStartMock,
});
expect(securityStartMock.authc.apiKeys.invalidateAsInternalUser).toHaveBeenCalled();
});
it('falls back to user credentials when error granting API key', async () => {
const credStore = credentialStoreFactory(logMock);
securityStartMock.authc.apiKeys.grantAsInternalUser.mockRejectedValue(
new Error('Error granting API key')
);
await credStore.set({
request: requestMock,
reindexOp: reindexOpMock,
security: securityStartMock,
});
expect(credStore.get(reindexOpMock)).toEqual({
authorization: basicAuthHeader,
});
});
});
});