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

Add PGP key retrieval endpoint #2977

Merged
merged 6 commits into from
Sep 25, 2023
Merged

Conversation

michel-laterman
Copy link
Contributor

@michel-laterman michel-laterman commented Sep 19, 2023

What is the problem this PR solves?

Fleet-server must be able to have a request path where a valid PGP key will be served to requesting agents.
This endpoint must only be valid when the server runs HTTPS.
An operator must be able to change the key that is served.

How does this PR solve the problem?

Add a new endpoint GET /api/agents/upgrades/{major}.{minor}.{patch}/pgp-public-key that will return a PGP key.
The endpoint needs a valid API key and is rate limited.
Currently the major, minor, and patch values are ignored.

The endpoint will server a PGP key from it's cache if one exists, otherwise it will serve the contents of a PGP key found on disk at FLEETSERVER_BINARY_DIR/elastic-agent-upgrade-keys/default.pgp. Note that the directory can be changed through configuration.
The key value is added to cache if it exists.

If this file does not exist the fleet-server will retrieve the key from https://artifacts.elastic.co/GPG-KEY-elastic-agent and serve it's contents (writing them to cache and disk). The upstream URL can also be changed through configuration.

Design Checklist

  • I have ensured my design is stateless and will work when multiple fleet-server instances are behind a load balancer.
  • I have or intend to scale test my changes, ensuring it will work reliably with 100K+ agents connected.
  • I have included fail safe mechanisms to limit the load on fleet-server: rate limiting, circuit breakers, caching, load shedding, etc.

Checklist

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in ./changelog/fragments using the changelog tool

Related issues

@elasticmachine
Copy link
Contributor

elasticmachine commented Sep 19, 2023

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2023-09-25T14:34:38.622+0000

  • Duration: 39 min 41 sec

Test stats 🧪

Test Results
Failed 0
Passed 827
Skipped 1
Total 828

💚 Flaky test report

Tests succeeded.

🤖 GitHub comments

Expand to view the GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

@michel-laterman michel-laterman marked this pull request as ready for review September 21, 2023 20:42
@michel-laterman michel-laterman requested a review from a team as a code owner September 21, 2023 20:42
@@ -260,6 +260,17 @@ the test BenchmarkXXXX and not unit tests)
- `BENCHMARK_COUNT`: you can define the number of iterations go test will run. Having larger number helps
remove run-to-run variations (Default: 8)

#### E2E Tests
Copy link
Member

Choose a reason for hiding this comment

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

🎉 nice to add some doc on how to run tests

@nchaulet nchaulet requested review from a team and nchaulet September 21, 2023 22:34
@nchaulet
Copy link
Member

I am wondering if we should read the key on start as we do for the the other filer we read from the config like (SecretTokenPath , ...) it will be less confusing for the user to change the key it will have to restart fleet-server and not rely on a cache expiring no?

return nil, err
}
pt.cache.SetPGPKey(key, p)
pt.writeKeyToDir(ctx, zlog, key, p)
Copy link
Member

@nchaulet nchaulet Sep 22, 2023

Choose a reason for hiding this comment

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

If we write the upstream key to disk, this mean if that key is updated user will have to manually update it on their fleet-server? does the cache is not good enough here?

Copy link
Contributor Author

@michel-laterman michel-laterman Sep 22, 2023

Choose a reason for hiding this comment

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

Yes, if the upstream key is updated the user will need to manually update the key on disk (or remove it)

edit: the issue is to have fleet-server serve a local key with the fallback being an upstream source.
I added caching because if this endpoint is ever used it is likely to serve very bursty traffic, caching the key from disk or from the network would reduce the work fleet-server does

@michel-laterman
Copy link
Contributor Author

Currently pgp key config can be specified through the fleet-server integration.
If we changed it to be read on startup, then it should be specified similar to the others and need a command line/env var option in the elastic-agent.
This would change it (from a user's perspective) to a setting that they can specify through config* to one that is only specified on install.

I think we should keep it as is, it may be easier to do an emergency key replacement with the current behaviour (if upstream has changed and an old key is on disk), the user would just need to change pgp.dir and restart the process (to clear the cache) instead of doing any on-host operations if we were to change it.

@michel-laterman michel-laterman changed the title Add openapi spec for PGP endpoint Add PGP key retrieval endpoint Sep 22, 2023
Copy link
Contributor

@juliaElastic juliaElastic left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@nchaulet nchaulet left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the cache changes 🚀

@michel-laterman michel-laterman enabled auto-merge (squash) September 25, 2023 14:35
@elastic-sonarqube
Copy link

@michel-laterman michel-laterman merged commit 7bc0c51 into elastic:main Sep 25, 2023
17 checks passed
@michalpristas
Copy link
Contributor

after merge comment: was testing this with agent. everything seems fine

@michel-laterman michel-laterman deleted the api-pgp branch November 15, 2023 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Team:Fleet Label for the Fleet team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow Fleet Server to serve the PGP key when stored on disk
5 participants