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

[Uptime] Generate api key for synthetics service #119590

Merged
merged 8 commits into from
Nov 25, 2021

Conversation

shahzad31
Copy link
Contributor

@shahzad31 shahzad31 commented Nov 24, 2021

Summary

fixes elastic/uptime#398

Basic idea is that we will call this utility function from the CRUD requests or kibana task manager, in case of task manager, it will not have access to the request object, so that part we still need to figure out. But i think in most cases i assume when the task manager will run, we will have pushed some saved object, in a sense that key will already be there so it can just reuse it.

you can test the encryption/decryption aspect by placing the following piece of code in existing uptime route , perhaps get_index_status.

const savedObjectsClient = context.core.savedObjects.getClient({
  includedHiddenTypes: [syntheticsServiceApiKey.name],
});

let apKey;

 try{ apKey = await savedObjectsClient.get(syntheticsServiceApiKey.name, syntheticsApiKeyID);}catch(){}
const apiKey = await getAPIKeyForSyntheticsService({
  request,
  savedObjectsClient,
  security: server.security,
  encryptedSavedObject: server.encryptedSavedObjects,
});
const res = await libs.requests.getIndexStatus({ uptimeEsClient });
return { ...res, apiKey, apKey };

@shahzad31 shahzad31 changed the title Generate api key [Uptime] Generate api key for synthetics service Nov 24, 2021
@shahzad31 shahzad31 marked this pull request as ready for review November 24, 2021 16:02
@shahzad31 shahzad31 requested a review from a team as a code owner November 24, 2021 16:02
@shahzad31 shahzad31 added release_note:skip Skip the PR/issue when compiling release notes v8.0.0 labels Nov 24, 2021
import { SyntheticsServiceApiKey } from '../../../common/runtime_types/synthetics_service_api_key';
import { EncryptedSavedObjectsClient } from '../../../../encrypted_saved_objects/server';

export const syntheticsApiKeyID = 'ba997842-b0cf-4429-aa9d-578d9bf0d391';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

using UUID is a requirement to work with encrypted saved objects

@botelastic botelastic bot added the Team:Uptime - DEPRECATED Synthetics & RUM sub-team of Application Observability label Nov 24, 2021
@elasticmachine
Copy link
Contributor

Pinging @elastic/uptime (Team:uptime)

cluster: ['monitor', 'read_ilm', 'read_pipeline'],
index: [
{
names: ['synthetics-*', 'heartbeat-*'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we be indexing to both heartbeat-* and synthetics-*? I was under the impression we should only be indexing to synthetics-*

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm realizing that I'm commenting this on the test, but it applies to the actual implementation too. Same for the below comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

since we are going to use data stream format, it makes sense to remove heartbeat, i will do that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

index: [
{
names: ['synthetics-*', 'heartbeat-*'],
privileges: ['view_index_metadata', 'create_doc'],
Copy link
Contributor

Choose a reason for hiding this comment

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

We will need auto_configure privilege as well to create the data stream

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

export const SyntheticsServiceApiKeyType = t.type({
id: t.string,
name: t.string,
apiKey: t.string,
Copy link
Contributor

Choose a reason for hiding this comment

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

In practice, I've also seen the api service return an encoded key on the object, which is the encoded id and name which saves us the trouble of having to encode it ourselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think we are not using the encoded part, since we only need apiKey and that will get saved in savedObjects in encrypted form. But will add it just in case for typing purpose.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we are not saving encoded part at all in the saved object


encryptedSavedObjects.registerType({
type: syntheticsServiceApiKey.name,
attributesToEncrypt: new Set(['apiKey']),
Copy link
Contributor

Choose a reason for hiding this comment

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

As I mentioned above, when I was testing this the saved object service also returned to me an encoded key, which was the pre-encoded id:apiKey combination. If that's the case, we should also encrypted that key, or only save the encoded key on the saved object and encrypted that.

const isApiKeysEnabled = await security.authc.apiKeys?.areAPIKeysEnabled();

if (!isApiKeysEnabled) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know a lot about the types of helpers we have with our API layer, but when we return null, what is response that is returned to the client? Status 500?

We may need to have a more specific error message here, so that we can let the user know how to remedy this situation. I don't suppose anyone wouldn't be using tls in production, which is required to use API keys, but perhaps if they are just trying to get started and testing out synthetics service, we could populate an error message to let them know to run with tls.

At any rate, this isn't necessary right now for an incremental step, but it could be good to keep in mind.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think in the route if the key is undefined or an error, that should be passed on to the UI. i have updated this line to bit better

Comment on lines 62 to 63
names: ['synthetics-*', 'heartbeat-*'],
privileges: ['view_index_metadata', 'create_doc'],
Copy link
Contributor

Choose a reason for hiding this comment

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

See my comment on the test about these two lines.

client: SavedObjectsClientContract,
apiKey: SyntheticsServiceApiKey
) => {
await client.create(syntheticsServiceApiKey.name, apiKey, {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need an error handling? Unsure of what potential points of failure there are.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i don't think so, it will throw an error, kibana route will auto pick it up and generate a message if it needs be.

Copy link
Contributor

@dominiqueclarke dominiqueclarke left a comment

Choose a reason for hiding this comment

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

LGTM for incremental step!

@shahzad31 shahzad31 added the auto-backport Deprecated - use backport:version if exact versions are needed label Nov 25, 2021
@shahzad31
Copy link
Contributor Author

@elasticmachine merge upstream

@shahzad31
Copy link
Contributor Author

@elasticmachine merge upstream

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@shahzad31 shahzad31 merged commit 5e93d91 into elastic:main Nov 25, 2021
@shahzad31 shahzad31 deleted the generate-api-key branch November 25, 2021 13:19
@kibanamachine
Copy link
Contributor

The following labels were identified as gaps in your version labels and will be added automatically:

  • v8.1.0

If any of these should not be on your pull request, please manually remove them.

@kibanamachine
Copy link
Contributor

💔 Backport failed

Status Branch Result
8.0 Commit could not be cherrypicked due to conflicts

To backport manually run:
node scripts/backport --pr 119590

shahzad31 added a commit to shahzad31/kibana that referenced this pull request Nov 25, 2021
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
shahzad31 added a commit that referenced this pull request Nov 25, 2021
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
TinLe pushed a commit to TinLe/kibana that referenced this pull request Dec 22, 2021
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Deprecated - use backport:version if exact versions are needed release_note:skip Skip the PR/issue when compiling release notes Team:Uptime - DEPRECATED Synthetics & RUM sub-team of Application Observability v8.0.0 v8.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Kibana API key generation for synthetic service
5 participants