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

release-22.1: server: new HTTP API to execute SQL statements #84374

Merged
merged 2 commits into from
Aug 5, 2022

Conversation

dhartunian
Copy link
Collaborator

@dhartunian dhartunian commented Jul 13, 2022

Backport 2/2 commits from #79663 and #84386.

/cc @cockroachdb/release


Requested by @andreimatei .

This commit introduces a new API endpoint /api/v2/sql/,
which can execute arbitrary SQL statements over HTTP.

The endpoint is authenticated, i.e. the client must provide
an appropriate X-Cockroach-API-Session: header on each request.

There is a Swagger spec to enable auto-documentation of the API.

The request payload should be submitted using JSON.

Summary of request fields:

  • database: the current database, defaults to defaultdb
  • application_name
  • timeout: max execution time, defaults to 5s
  • max_results_size: max size of results, defaults to ~10KB.
  • statements: which statements to run. An array of {"sql",
    "arguments"} objects. The number of items in "arguments"
    must match the number of placehoders in "sql".
  • execute: must be set to true to actually execute the SQL.
    Otherwise, the request is merely parsed to verify the syntax is correct.

Example use:

  1. Generate some test data and create a user with access to it:

    $ cockroach gen example-data intro | cockroach sql --certs-dir=...
    $ cockroach sql --certs-dir=... \
        -e 'create user myuser with password abc'
        -e 'grant select on table intro.mytable to myuser'
    
  2. Generate an HTTP API key, for example using the CLI (an HTTP
    request to /api/v2/login is also possible):

    $ cockroach session-auth login myuser  --certs-dir=certs
    

    (this prints out the session key, assign it manually to the $APIKEY env var.)

  3. Run some HTTP API queries from the shell using curl. For example:

    $ curl -k -H "X-Cockroach-API-Session: $APIKEY" \
        -H "Content-Type: application/json" \
        --data-binary '{"database":"intro",
        "statements":[{"sql":"SELECT v FROM mytable WHERE l%2 = 0"}],
        "execute": true}' \
        https://127.0.0.1:8080/api/v2/sql/
    

Example output:

{"num_statements": 1,
 "execution": {
   "retries":0,
   "txn_results": [
{
  "statement": 1,
  "tag": "SELECT",
  "start":"2022-04-08T15:55:30.659257101Z",
  "columns": [{"name":"v", "type":"STRING", "oid":25}],
  "rows": [
   {"v":"..."},
   {"v":"..."},
   {"v":"..."}
  ],
  "end":"2022-04-08T15:55:30.665283699Z"
}
  ]
}}

This currently executes using the "internal executor" and thus
does not support SQL sessions and transaction control. Some consequences:

  • SET statements are ineffective.
  • BEGIN/COMMIT/ABORT/SAVEPOINT etc are ineffective.

Security note: We place this logic under /api/v2 and not
/_admin/v1 because the latter uses session cookies and is thus
susceptible to CSRF.
The /api/v2 tree requires an explicit header and thus blocks CSRF.

Release note (api change): A new /api/v2/sql/ endpoints enable
execution of simple SQL queries over HTTP.


Fixes #84385.

Release note (bug fix): The SQL execution HTTP endpoint did not
properly support queries with multiple result values. This is now
fixed. This bug was introduced when the feature was first implemented.


Release justification: this is a high-priority need for SQL Observability and makes it easier to port over functionality in DB Console. Furthermore, this endpoint is purely additive and can only be opted into by new frontend code.

@dhartunian dhartunian requested a review from a team as a code owner July 13, 2022 19:16
@blathers-crl
Copy link

blathers-crl bot commented Jul 13, 2022

Thanks for opening a backport.

Please check the backport criteria before merging:

  • Patches should only be created for serious issues or test-only changes.
  • Patches should not break backwards-compatibility.
  • Patches should change as little code as possible.
  • Patches should not change on-disk formats or node communication protocols.
  • Patches should not add new functionality.
  • Patches must not add, edit, or otherwise modify cluster versions; or add version gates.
If some of the basic criteria cannot be satisfied, ensure that the exceptional criteria are satisfied within.
  • There is a high priority need for the functionality that cannot wait until the next release and is difficult to address in another way.
  • The new functionality is additive-only and only runs for clusters which have specifically “opted in” to it (e.g. by a cluster setting).
  • New code is protected by a conditional check that is trivial to verify and ensures that it only runs for opt-in clusters.
  • The PM and TL on the team that owns the changed code have signed off that the change obeys the above rules.

Add a brief release justification to the body of your PR to justify this backport.

Some other things to consider:

  • What did we do to ensure that a user that doesn’t know & care about this backport, has no idea that it happened?
  • Will this work in a cluster of mixed patch versions? Did we test that?
  • If a user upgrades a patch version, uses this feature, and then downgrades, what happens?

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@knz
Copy link
Contributor

knz commented Jul 13, 2022

👍 but don't you need to wrap up the convo around authn before this merges?

@knz
Copy link
Contributor

knz commented Jul 13, 2022

you'll need to backport #84386 too.

Copy link
Contributor

@maryliag maryliag left a comment

Choose a reason for hiding this comment

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

As long as tests are passing and the necessary other PRs are also backported: :lgtm:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @abarganier and @xinhaoz)

@dhartunian
Copy link
Collaborator Author

Auth code backport will be merged right after this one: #85553

@dhartunian
Copy link
Collaborator Author

I added @knz's bugfix PR (#84386) into this backport as well.

knz added 2 commits August 4, 2022 14:07
This commit introduces a new API endpoint `/api/v2/sql/`,
which can execute arbitrary SQL statements over HTTP.

The endpoint is authenticated, i.e. the client must provide
an appropriate `X-Cockroach-API-Session:` header on each request.

There is a Swagger spec to enable auto-documentation of the API.

The request payload should be submitted using JSON.
Summary of request fields:
- `database`: the current database, defaults to `defaultdb`
- `application_name`
- `timeout`: max execution time, defaults to 5s
- `max_results_size`: max size of results, defaults to ~10KB.
- `statements`: which statements to run. An array of {"sql",
  "arguments"} objects. The number of items in "arguments"
  must match the number of placehoders in "sql".
- `execute`: must be set to true to actually execute the SQL.
  Otherwise, the request is merely parsed to verify the syntax is correct.

Example use:

1. Generate some test data and create a user with access to it:

   ```
   $ cockroach gen example-data intro | cockroach sql --certs-dir=...
   $ cockroach sql --certs-dir=... \
       -e 'create user myuser with password abc'
       -e 'grant select on table intro.mytable to myuser'
   ```

2. Generate an HTTP API key, for example using the CLI (an HTTP
   request to /api/v2/login is also possible):

   ```
   $ cockroach session-auth login myuser  --certs-dir=certs
   ```

   (this prints out the session key, assign it manually to the $APIKEY env var.)

3. Run some HTTP API queries from the shell using `curl`. For example:

   ```
   $ curl -k -H "X-Cockroach-API-Session: $APIKEY" \
       -H "Content-Type: application/json" \
       --data-binary '{"database":"intro",
       "statements":[{"sql":"SELECT v FROM mytable WHERE l%2 = 0"}],
       "execute": true}' \
       https://127.0.0.1:8080/api/v2/sql/
   ```

Example output:
```json
{"num_statements": 1,
 "execution": {
   "retries":0,
   "txn_results": [
{
  "statement": 1,
  "tag": "SELECT",
  "start":"2022-04-08T15:55:30.659257101Z",
  "columns": [{"name":"v", "type":"STRING", "oid":25}],
  "rows": [
   {"v":"..."},
   {"v":"..."},
   {"v":"..."}
  ],
  "end":"2022-04-08T15:55:30.665283699Z"
}
  ]
}}
```

This currently executes using the "internal executor" and thus
does not support SQL sessions and transaction control. Some consequences:

- SET statements are ineffective.
- BEGIN/COMMIT/ABORT/SAVEPOINT etc are ineffective.

Security note: We place this logic under `/api/v2` and not
`/_admin/v1` because the latter uses session cookies and is thus
susceptible to [CSRF](https://en.wikipedia.org/wiki/Cross-site_request_forgery).
The `/api/v2` tree requires an explicit header and thus blocks CSRF.

Release note (api change): A new `/api/v2/sql/` endpoints enable
execution of simple SQL queries over HTTP.
Release note (bug fix): The SQL execution HTTP endpoint did not
properly support queries with multiple result values. This is now
fixed. This bug was introduced when the feature was first implemented.
@dhartunian dhartunian merged commit 033c911 into cockroachdb:release-22.1 Aug 5, 2022
@dhartunian dhartunian deleted the backport22.1-79663 branch August 5, 2022 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants