Skip to content

Conversation

@jen-huang
Copy link
Contributor

@jen-huang jen-huang commented Mar 11, 2025

Description

Part of elastic/kibana#205102. This PR aims to modify POST /_fleet/secret to accept a string or an array of strings (it currently only supports a string) so that Fleet can support integrations which have multi-secret fields. Similarly, GET /_fleet/secret/<secret id> should return string or array of strings.

Testing

# Create secret with array of strings
curl -u elastic:password localhost:9200/_fleet/secret/ -X POST -H 'Content-Type: application/json' -d '{"value": ["secret 1", "secret 2"]}'

# Create secret with a string
curl -u elastic:password localhost:9200/_fleet/secret/ -X POST -H 'Content-Type: application/json' -d '{"value": "single secret"}'

# Check that secrets are stored in index correctly
curl -u elastic:password localhost:9200/.fleet-secrets/_search

# Check that the secrets are returned correctly
curl -u elastic:password localhost:9200/_fleet/secret/<array secret id>
curl -u elastic:password localhost:9200/_fleet/secret/<string secret id>

Add tests

Fix tests

[CI] Auto commit changes from spotless
@jen-huang jen-huang force-pushed the jh/fleet-multi-secrets branch from b6ac015 to 4fe3ef9 Compare March 12, 2025 00:41
@jen-huang jen-huang requested a review from Copilot March 12, 2025 21:31
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates the Fleet secret APIs to allow storing and retrieving secrets as an array of strings in addition to a single string. Key changes include updating the secret request/response classes to support multiple value types, adding appropriate parser logic, and extending YAML tests to cover the multi-value scenarios.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
x-pack/plugin/fleet/src/main/java/org/elasticsearch/xpack/fleet/action/TransportGetSecretAction.java Adjusts GET secret handling to support a list of strings
x-pack/plugin/fleet/src/main/java/org/elasticsearch/xpack/fleet/action/PostSecretRequest.java Updates parsing, serialization, and validation to handle multi-value secrets
x-pack/plugin/fleet/src/main/java/org/elasticsearch/xpack/fleet/action/GetSecretResponse.java Modifies response serialization to support returning a string array
x-pack/plugin/fleet/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/fleet/40_secrets_get.yml Adds integration test for retrieving multi-value secrets
x-pack/plugin/fleet/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/fleet/30_secrets_post.yml Adds integration test for creating multi-value secrets
x-pack/plugin/fleet/src/test/java/org/elasticsearch/xpack/fleet/action/PostSecretRequestTests.java Introduces unit tests for validating behavior with multiple and invalid secret values
Comments suppressed due to low confidence (1)

x-pack/plugin/fleet/src/main/java/org/elasticsearch/xpack/fleet/action/PostSecretRequest.java:60

  • The StreamInput deserialization only reads a String, which does not support the new array type. Update the deserialization logic to distinguish between a single string and an array of strings.
this.value = in.readString();

id = in.readString();
value = in.readString();
this.id = in.readString();
this.value = in.readString();
Copy link

Copilot AI Mar 12, 2025

Choose a reason for hiding this comment

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

The StreamInput deserialization for 'value' only handles a single String. When a multi-value secret is returned, the stream reading should be updated to properly deserialize a String array.

Suggested change
this.value = in.readString();
if (in.readBoolean()) {
this.value = in.readString();
} else {
this.value = in.readStringArray();
}

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

@jen-huang jen-huang Mar 13, 2025

Choose a reason for hiding this comment

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

I tried this but it makes tests fail with error about unexpected byte

I also tried this.value = in.readGenericValue(); and tests also fail with errors like:
unexpected error expanding serialized delayed writeable
tried to read: 119 bytes but only 9 remaining

(tests can be run with ./gradlew ":x-pack:plugin:fleet:test" --tests "org.elasticsearch.xpack.fleet.action.GetSecretResponseTests.*")

I tried similar for PostSecretResponse as well with the same errors.

but it seems that this constructor is not used by REST endpoints, so it may be safe to leave readString() only here?

@jen-huang jen-huang changed the title [UII] Allow Fleet Fleet post secret and get secret to accept array of strings [UII] Allow Fleet post secret and get secret to accept array of strings Mar 13, 2025
@jen-huang jen-huang marked this pull request as ready for review March 13, 2025 19:08
@jen-huang jen-huang requested a review from a team as a code owner March 13, 2025 19:08
@jen-huang jen-huang added external-contributor Pull request authored by a developer outside the Elasticsearch team Team:Fleet labels Mar 13, 2025
@jen-huang jen-huang self-assigned this Mar 13, 2025
@jen-huang jen-huang added the :Core/Infra/Plugins Plugin API and infrastructure label Mar 13, 2025
@elasticsearchmachine elasticsearchmachine added the Team:Core/Infra Meta label for core/infra team label Mar 13, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

@jen-huang jen-huang added >enhancement and removed Team:Core/Infra Meta label for core/infra team labels Mar 13, 2025
@elasticsearchmachine elasticsearchmachine added the Team:Core/Infra Meta label for core/infra team label Mar 13, 2025
@elasticsearchmachine
Copy link
Collaborator

Hi @jen-huang, I've created a changelog YAML for you.

@jen-huang
Copy link
Contributor Author

Closing, going to handle this on Kibana side instead (elastic/kibana#205102 (comment)).

@jen-huang jen-huang closed this Mar 14, 2025
@jen-huang jen-huang deleted the jh/fleet-multi-secrets branch August 20, 2025 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Core/Infra/Plugins Plugin API and infrastructure >enhancement external-contributor Pull request authored by a developer outside the Elasticsearch team Team:Core/Infra Meta label for core/infra team Team:Fleet v9.1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants