Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Oct 30, 2023
1 parent d9257fb commit d9eb33f
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 35 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/constants/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const outputType = {
Elasticsearch: 'elasticsearch',
Logstash: 'logstash',
Kafka: 'kafka',
RemoteElasticsearch: 'remote-elasticsearch',
RemoteElasticsearch: 'remote_elasticsearch',
} as const;

export const DEFAULT_OUTPUT_ID = 'fleet-default-output';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,29 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { useEffect, useState } from 'react';
import { useQuery } from '@tanstack/react-query';

import { sendGetPermissionsCheck } from '../../../hooks';

export const useCheckPermissions = () => {
const [isPermissionsLoading, setIsPermissionsLoading] = useState<boolean>(false);
const [permissionsError, setPermissionsError] = useState<string>();

useEffect(() => {
async function checkPermissions() {
setIsPermissionsLoading(false);
setPermissionsError(undefined);
async function checkPermissions() {
let permissionsError;

try {
setIsPermissionsLoading(true);
const permissionsResponse = await sendGetPermissionsCheck(true);
try {
const permissionsResponse = await sendGetPermissionsCheck(true);

setIsPermissionsLoading(false);
if (!permissionsResponse.data?.success) {
setPermissionsError(permissionsResponse.data?.error || 'REQUEST_ERROR');
}
} catch (err) {
setPermissionsError('REQUEST_ERROR');
}
if (!permissionsResponse.data?.success) {
permissionsError = permissionsResponse.data?.error || 'REQUEST_ERROR';
}
checkPermissions();
}, []);
} catch (err) {
permissionsError = 'REQUEST_ERROR';
}
return permissionsError;
}

return { isPermissionsLoading, permissionsError };
export const useCheckPermissions = () => {
const { data: permissionsError, status } = useQuery([
'fetch-check-permissions',
checkPermissions,
]);
return { isPermissionsLoading: status === 'loading', permissionsError };
};
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const mockApiCallsWithRemoteESOutputs = (http: MockedFleetStartServices['http'])
{
id: 'remote1',
name: 'Remote1',
type: 'remote-elasticsearch',
type: 'remote_elasticsearch',
is_default: false,
is_default_monitoring: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe('EditOutputFlyout', () => {
it('should render the flyout if the output provided is a remote ES output', async () => {
jest.spyOn(ExperimentalFeaturesService, 'get').mockReturnValue({ remoteESOutput: true });
const { utils } = renderFlyout({
type: 'remote-elasticsearch',
type: 'remote_elasticsearch',
name: 'remote es output',
id: 'outputR',
is_default: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ export const OutputFormRemoteEsSection: React.FunctionComponent<Props> = (props)
data-test-subj="serviceTokenCallout"
>
<EuiCodeBlock isCopyable={true}>
{`POST kbn:/api/fleet/service_tokens?remote=true`}
{`POST kbn:/api/fleet/service_tokens
{
"remote": true
}`}
</EuiCodeBlock>
</EuiCallOut>
<EuiSpacer size="m" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function displayOutputType(type: string) {
return i18n.translate('xpack.fleet.settings.outputsTable.elasticsearchTypeLabel', {
defaultMessage: 'Elasticsearch',
});
case 'remote-elasticsearch':
case 'remote_elasticsearch':
return i18n.translate('xpack.fleet.settings.outputsTable.remoteElasticsearchTypeLabel', {
defaultMessage: 'Remote Elasticsearch',
});
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/fleet/server/routes/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ export const getCheckPermissionsHandler: FleetRequestHandler<

export const generateServiceTokenHandler: RequestHandler<
null,
TypeOf<typeof GenerateServiceTokenRequestSchema.query>,
null
null,
TypeOf<typeof GenerateServiceTokenRequestSchema.body>
> = async (context, request, response) => {
// Generate the fleet server service token as the current user as the internal user do not have the correct permissions
const esClient = (await context.core).elasticsearch.client.asCurrentUser;
const serviceAccount = request.query.remote ? 'fleet-server-remote' : 'fleet-server';
const serviceAccount = request.body.remote ? 'fleet-server-remote' : 'fleet-server';
appContextService
.getLogger()
.debug(`Creating service token for account elastic/${serviceAccount}`);
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/server/services/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ describe('Output Service', () => {
is_default: true,
is_default_monitoring: false,
name: 'Test',
type: 'remote-elasticsearch',
type: 'remote_elasticsearch',
},
{ id: 'output-1' }
)
Expand Down Expand Up @@ -1534,7 +1534,7 @@ describe('Output Service', () => {
is_default: true,
is_default_monitoring: false,
name: 'Test',
type: 'remote-elasticsearch',
type: 'remote_elasticsearch',
})
).rejects.toThrow(
`Remote elasticsearch output cannot be set as default output for integration data. Please set "is_default" to false.`
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/server/types/rest_spec/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { schema } from '@kbn/config-schema';

export const GenerateServiceTokenRequestSchema = {
query: schema.object({
body: schema.object({
remote: schema.boolean({ defaultValue: false }),
}),
};
3 changes: 2 additions & 1 deletion x-pack/test/fleet_api_integration/apis/service_tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ export default function (providerContext: FtrProviderContext) {

it('should create a valid remote service account token', async () => {
const { body: apiResponse } = await supertest
.post(`/api/fleet/service_tokens?remote=true`)
.post(`/api/fleet/service_tokens`)
.set('kbn-xsrf', 'xxxx')
.send(`{"remote":true}`)
.expect(200);

expect(apiResponse).have.property('name');
Expand Down

0 comments on commit d9eb33f

Please sign in to comment.