Skip to content

Conversation

@kderusso
Copy link
Member

@kderusso kderusso commented Jun 13, 2023

Adds a PUT Query Ruleset API request. Also changes this from a system property to a feature flag.

Example API request:

PUT /_query_rules/test-ruleset
{
  "ruleset_id": "test-ruleset",
  "rules": [
    {
      "rule_id": "query-rule-id1",
      "type": "pinned",
      "criteria": [
        {
          "type": "exact",
          "metadata": "query_string",
          "value": "puggles"
        }
      ],
      "actions":
        {
          "ids": [
            "abc",
            "def"
          ]
        }
    },
    {
      "rule_id": "query-rule-id1",
      "type": "pinned",
      "criteria": [
        {
          "type": "exact",
          "metadata": "query_string",
          "value": "puggles"
        }
      ],
      "actions":
        {
          "docs": [
            { "_index": "test-index-1", "_id": "ghi"},
            { "_index": "test-index-1", "_id": "jkl"}
          ]
        }
    }
  ]
}

@kderusso kderusso added >non-issue :EnterpriseSearch/Application Enterprise Search Team:Enterprise Search Meta label for Enterprise Search team labels Jun 14, 2023

public static final String BEHAVIORAL_ANALYTICS_API_ENDPOINT = APPLICATION_API_ENDPOINT + "/analytics";

public static final String QUERY_RULES_API_ENDPOINT = "_query_rules";
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: I purposely kept this external to the _application endpoint. (If we like this and think the classes need to move packages, I'd prefer to do that refactor/move in a single PR to limit noise)



---
'Create Query Ruleset':
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: Since only the PUT API call is implemented and not GET or DELETE, this test is pretty lightweight. It will be extended as additional CRUD API calls are added.

public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;

// TODO: validate query ruleset
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not quite sure what validation we'd want here that we don't already do in constructors. Ideas welcome?

Copy link
Member

Choose a reason for hiding this comment

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

I can't think of any! Maybe we can change the TODO to a comment indicating that they are already validated in constructors

Copy link
Contributor

@ioanatia ioanatia Jun 15, 2023

Choose a reason for hiding this comment

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

So I would actually try to move all the validations here if possible.
Because right now, what happens is that we validate the request payload before we validate the license.
We want the license check to be the first one that happens, not the validation of the payload.
It's a problem we have with the rest of the APIs in this module, that we did not get to fix (we have an issue for it).
But since this is a new API, maybe we can make this right from the beginning?

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated with validations here. Is that enough, @ioanatia? I don't like the idea of removing constructor validation too, but we can discuss for a followup PR

MANAGE_SEARCH_APPLICATION_PATTERN
);

public static final NamedClusterPrivilege MANAGE_QUERY_RULES = new ActionClusterPrivilege(
Copy link
Member Author

Choose a reason for hiding this comment

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

I'd especially like feedback from @ioanatia here - Is a "manage query rules" cluster privilege sufficient?

Copy link
Contributor

@ioanatia ioanatia Jun 15, 2023

Choose a reason for hiding this comment

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

If it was me, I would actually just use the manage cluster privilege for now because this feature is behind a feature flag and it's disabled by default. So there's no immediate need to add a cluster privilege.
The manage cluster privilege includes all the cluster privileges except manage_security, so it is perfectly fine.
We can then add this as a separate cluster privilege and have a review from the Elasticsearch security team.
The manage cluster privilege would most likely include this new cluster privilege.

One unknown that I have would be what would happen if at some point we decide to remove or rename this manage_query_rules privilege - if a cluster has an API key that uses it, when it gets upgraded to a new version where this privilege does not exist, what would the behaviour of the API key be? Would the upgrade even fail if API keys or roles refer to a cluster privilege that does not exist? We had a similar conversation when we thought about removing the cluster privilege we have for behavioral analytics and rely on the new workflow restrictions.

So instead of trying to answer questions like these right away, I would just not focus on it at the moment and use the manage cluster privilege for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds reasonable, thank you. I'll update this 👍

@kderusso kderusso marked this pull request as ready for review June 14, 2023 13:44
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/ent-search-eng (Team:Enterprise Search)

@kderusso kderusso changed the title Add Put Query Rule API call Add Put Query Ruleset API call Jun 14, 2023
Copy link
Member

@carlosdelest carlosdelest left a comment

Choose a reason for hiding this comment

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

Some minor comments, overall 💯

]
},
"params": {
"create": {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this from the start?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably not, but it's a paradigm we already use with search applications and it wasn't a big lift to support.

Copy link
Member

Choose a reason for hiding this comment

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

I'd say YAGNI - let's not add anything we're not sure to need, to avoid testing / documenting / maintaining it. It's easy to add later if needed

Copy link
Member Author

Choose a reason for hiding this comment

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

Most of the time I'd agree, but there is a chance of accidentally overwriting an entire ruleset, with no history. This could be a guardrail.

Copy link
Member

Choose a reason for hiding this comment

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

I think we added that option to Search Applications for a very specific UI use case, and I don't feel this is something we need as a rule. I don't see this as a deal breaker for the PR, just a warning that we probably should not add it at least yet. It adds code and tests that we are not needing.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, I don't feel strongly enough that it has to stay in to argue more 😉 - I will plan to remove it, but I'll do so in a follow up PR (I want to get this merged to unblock the next one).

public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;

// TODO: validate query ruleset
Copy link
Member

Choose a reason for hiding this comment

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

I can't think of any! Maybe we can change the TODO to a comment indicating that they are already validated in constructors

kderusso and others added 3 commits June 14, 2023 16:18
…et.put.json

Co-authored-by: Carlos Delgado <6339205+carlosdelest@users.noreply.github.com>
…/application/EnterpriseSearch.java

Co-authored-by: Carlos Delgado <6339205+carlosdelest@users.noreply.github.com>
…/application/EnterpriseSearch.java

Co-authored-by: Carlos Delgado <6339205+carlosdelest@users.noreply.github.com>
@kderusso kderusso merged commit 008a9fc into elastic:main Jun 15, 2023
@kderusso kderusso deleted the kderusso/query-rules-put-api-call branch July 18, 2023 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:EnterpriseSearch/Application Enterprise Search >non-issue Team:Enterprise Search Meta label for Enterprise Search team v8.9.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants