Skip to content

Commit

Permalink
fix headers in Fleet
Browse files Browse the repository at this point in the history
  • Loading branch information
mattapperson committed Nov 17, 2019
1 parent 0831ef0 commit 9447054
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class ElasticsearchAdapter implements ElasticsearchAdapterType {
}

if (user.kind === 'authenticated') {
return await callWithRequest({ headers: user[internalAuthData] }, endpoint, params);
return await callWithRequest(user[internalAuthData], endpoint, params);
}

throw new Error('Elastic search call is not implemented for unauthenticated user');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class HapiFrameworkAdapter {
return isAuthenticated
? {
kind: 'authenticated',
[internalAuthData]: request.headers,
[internalAuthData]: request,
}
: {
kind: 'unauthenticated',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ export class SODatabaseAdapter implements SODatabaseAdapterType {

private getClient(user: FrameworkUser) {
if (user.kind === 'authenticated') {
return this.savedObject.getScopedSavedObjectsClient({
headers: user[internalAuthData],
});
return this.savedObject.getScopedSavedObjectsClient(user[internalAuthData]);
}

if (user.kind === 'internal') {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/fleet/server/libs/__mocks__/api_keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class ApiKeyLib implements Interface<ApiKeyLibType> {
user: FrameworkUser
): Promise<EnrollmentApiKeyVerificationResponse> {
if (user.kind === 'authenticated') {
const apiKeyHeader = user[internalAuthData].authorization.split(' ')[1];
const apiKeyHeader = user[internalAuthData].headers.authorization.split(' ')[1];
const apiKey = Buffer.from(apiKeyHeader, 'base64')
.toString('utf8')
.split(':')[1];
Expand All @@ -76,7 +76,7 @@ export class ApiKeyLib implements Interface<ApiKeyLibType> {
user: FrameworkUser<any>
): Promise<AccessApiKeyVerificationResponse> {
if (user.kind === 'authenticated') {
const apiKeyHeader = user[internalAuthData].authorization.split(' ')[1];
const apiKeyHeader = user[internalAuthData].headers.authorization.split(' ')[1];
const [apiKeyId, apiKey] = Buffer.from(apiKeyHeader, 'base64')
.toString('utf8')
.split(':');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ function getUser(apiKey?: string, apiKeyId?: string) {
return ({
kind: 'authenticated',
[internalAuthData]: {
authorization: `ApiKey ${Buffer.from(`${apiKeyId || 'key_id'}:${apiKey}`).toString(
'base64'
)}`,
headers: {
authorization: `ApiKey ${Buffer.from(`${apiKeyId || 'key_id'}:${apiKey}`).toString(
'base64'
)}`,
},
},
} as unknown) as FrameworkUser;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ function getUser(): FrameworkUser {
return ({
kind: 'authenticated',
[internalAuthData]: {
authorization: `Basic ${Buffer.from(`elastic:changeme`).toString('base64')}`,
headers: {
authorization: `Basic ${Buffer.from(`elastic:changeme`).toString('base64')}`,
},
},
} as unknown) as FrameworkUser;
}
Expand All @@ -33,7 +35,9 @@ function getUserForApiKey(apiKey: { id: string; api_key: string }) {
return {
kind: 'authenticated',
[internalAuthData]: {
authorization: `ApiKey ${apiKeyToString(apiKey)}`,
headers: {
authorization: `ApiKey ${apiKeyToString(apiKey)}`,
},
},
} as FrameworkUser;
}
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/fleet/server/libs/api_keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class ApiKeyLib {
throw new Error('Error must provide an authenticated user');
}

const authorizationHeader = user[internalAuthData].authorization;
const authorizationHeader = user[internalAuthData].headers.authorization;

if (!authorizationHeader) {
throw new Error('Authorization header must be set');
Expand Down
3 changes: 3 additions & 0 deletions x-pack/legacy/plugins/fleet/server/mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export const mappings = {
name: {
type: 'keyword',
},
type: {
type: 'keyword',
},
api_key: {
type: 'binary',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ describe('AgentsEventsRepository', () => {
return ({
kind: 'authenticated',
[internalAuthData]: {
authorization: `Basic ${Buffer.from(`elastic:changeme`).toString('base64')}`,
headers: {
authorization: `Basic ${Buffer.from(`elastic:changeme`).toString('base64')}`,
},
},
} as unknown) as FrameworkUser;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ describe('AgentsRepository', () => {
return ({
kind: 'authenticated',
[internalAuthData]: {
authorization: `Basic ${Buffer.from(`elastic:changeme`).toString('base64')}`,
headers: {
authorization: `Basic ${Buffer.from(`elastic:changeme`).toString('base64')}`,
},
},
} as unknown) as FrameworkUser;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ describe('Enrollment api key Repository', () => {
return ({
kind: 'authenticated',
[internalAuthData]: {
authorization: `Basic ${Buffer.from(`elastic:changeme`).toString('base64')}`,
headers: {
authorization: `Basic ${Buffer.from(`elastic:changeme`).toString('base64')}`,
},
},
} as unknown) as FrameworkUser;
}
Expand Down

0 comments on commit 9447054

Please sign in to comment.