Skip to content

Add getSecret method to ProjectMetadata #125830

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

Merged
merged 4 commits into from
Apr 2, 2025

Conversation

jfreden
Copy link
Contributor

@jfreden jfreden commented Mar 28, 2025

This simplifies getting secrets from ProjectMetadata.

@jfreden jfreden added >non-issue :Core/Infra/Core Core issues without another label labels Mar 28, 2025
@jfreden jfreden requested review from tvernum and a team March 28, 2025 08:22
@elasticsearchmachine elasticsearchmachine added Team:Core/Infra Meta label for core/infra team v9.1.0 labels Mar 28, 2025
@elasticsearchmachine
Copy link
Collaborator

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

@elasticsearchmachine elasticsearchmachine added the serverless-linked Added by automation, don't add manually label Mar 28, 2025
Comment on lines 58 to 61
public Optional<SecureString> getSecret(String key) {
return Optional.ofNullable(metadata().<ProjectSecrets>custom(ProjectSecrets.TYPE))
.map(secrets -> secrets.getSettings().getString(key));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I see https://github.com/elastic/elasticsearch-serverless/pull/3676#discussion_r2018002701 is where this idea originated. Intuitively, since this method is a pure function of ProjectMetadata, I'd be inclined to have this method in ProjectMetadata instead of here. That would change Tim's example snippet to

Optional<SecureString> hash = projectState.metadata().getSecret(
	MULTI_PROJECT_PASSWORD_HASH.apply(principal).getConcreteSettingForNamespace(realmRef().getName())
);
return hash.orElse(null);

That would make more sense to me. What do you think?

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'm not sure. The point of adding it is to make it a better interface that's easier for the caller to use, does the caller need to know that project secrets are stored in a custom in metadata or does the state abstraction make sense? I'm leaning towards keeping it, but I'm not 100% sure, I see your point of it being a pure function of ProjectMetadata too.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the main "pain" current callers see is the null-check - not obtaining the ProjectMetadata. We also don't have any precedent for adding utility methods like these on ProjectState. If we add this method here, we could add all sorts of methods like retrieving indices etc. - which I don't think we want to do. I'd also argue that in most places you'll have to get a reference to a ProjectMetadata first, because you'll want to retrieve things from there multiple times.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the additional context.

I think the main "pain" current callers see is the null-check - not obtaining the ProjectMetadata

Yes, I agree, but obtaining ProjectMetadata is also more painful than not doing it.

I'd also argue that in most places you'll have to get a reference to a ProjectMetadata first, because you'll want to retrieve things from there multiple times.

In the new realm, only the state is required and if you have the state, you can also get the ProjectMetadata from there.

we could add all sorts of methods like retrieving indices etc. - which I don't think we want to do.

I agree with this, but there is an ongoing discussion if the secrets and settings should really be part of metadata, since they're not persisted across restarts. If we don't put it in metadata, we have the option to change it later. Maybe that's not a strong enough argument though, we can always move it later anyway but it would be less code to change. I'll move it for now.

Thanks for the discussion!

@jfreden jfreden changed the title Add getSecret method to ProjectState Add getSecret method to ProjectMetadata Mar 28, 2025
Copy link
Contributor

@nielsbauman nielsbauman left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the discussion!

@jfreden jfreden merged commit 95cf145 into elastic:main Apr 2, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Core Core issues without another label >non-issue serverless-linked Added by automation, don't add manually Team:Core/Infra Meta label for core/infra team v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants