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

feat: Allow custom headers when introspecting in supergraph compose #1574

Merged
merged 10 commits into from Apr 19, 2023

Conversation

dbanty
Copy link
Contributor

@dbanty dbanty commented Apr 13, 2023

Supports the same syntax as Apollo Router for variable expansion (e.g., ${env.MY_SECRET}) when defining introspection_headers.

Closes #615

@EverlastingBugstopper EverlastingBugstopper added the feature 🎉 new commands, flags, functionality, and improved error messages label Apr 14, 2023
@EverlastingBugstopper EverlastingBugstopper added this to the vNext milestone Apr 14, 2023
Copy link
Contributor

@EverlastingBugstopper EverlastingBugstopper left a comment

Choose a reason for hiding this comment

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

this is looking great, thanks for the work on this - i just left one note

src/command/supergraph/resolve_config.rs Outdated Show resolved Hide resolved
@EverlastingBugstopper
Copy link
Contributor

i really like that this is backwards compatible with older versions of federation - when folks update their version of rover they don't also need to update their composition binary, because rover is what is handling the schema resolution. by the time the config file makes its way to the plugin, the sdl is resolved inline and introspection queries don't need to be performed/different config doesn't need to be serialized/deserialized 👍🏼

@dbanty
Copy link
Contributor Author

dbanty commented Apr 17, 2023

i really like that this is backwards compatible with older versions of federation

That was part of the plan 😅. I'm pretty sure serialization won't get messed up in older versions because by default serde ignores extra keys... right? So if someone tried to use new config with old fed/rover it wouldn't error, it just wouldn't have the new feature?

@EverlastingBugstopper
Copy link
Contributor

The cool thing about this is that it doesn't matter.

Input to rover can be like this:

subgraphs:
  products:
    routing_url: "https://products.example.com"
    schema:
      subgraph_url: "http://localhost:4003"
      introspection_headers:
        "new": "thing"

but the actual YAML that gets fed to the plugin looks like this:

subgraphs:
  products:
    routing_url: "https://products.example.com"
    schema:
      sdl:
        extend schema
          @link(url: "https://specs.apollo.dev/federation/v2.0",
             import: ["@key", "@shareable", "@tag", "@inaccessible"])

      type Query {
        allProducts: [ProductItf]
        product(id: ID!): ProductItf
      }
      ...... truncated

so adding this feature (and potentially other ways of resolving schemas in the future) doesn't matter to pre-compiled pre-distributed composition binaries, because all they care about is name, routing_url, and sdl for each subgraph. how rover gets that is completely decoupled.

@dbanty
Copy link
Contributor Author

dbanty commented Apr 17, 2023

If we're not just matching on either env var or static value, do we handle resolving multiple variables in a single string? What about nesting env vars like Weird: ${env.${env.I_WANT_INDIRECTION}}. Matching combinations and resolving as expected might get a little messy.

My idea for an algorithm right now is:

  1. Look for the last instance of ${env., then find the first }. If one is missing, break.
  2. Interpret the content between the markers from 1 as the name of an env var
  3. Update the whole string
  4. Loop back to 1

@EverlastingBugstopper
Copy link
Contributor

I think we should explicitly disallow nested configuration like that because it's a pain to handle, prone to bugs, and probably just a bad idea for someone to do.

I tried doing this for the router by doing the following from ./examples/supergraph-demo:

first, create ./router.yaml with these contents:

supergraph:
  path: "/"

telemetry:
  experimental_logging:
    when_header:
      - name: apollo-router-${env.${env.MY_ENV_VAR}}
        value: my_client
        headers: true
        body: true

get a composed schema

$ rover supergraph compose --config ./supergraph.yaml --output ./supergraph.graphql

download the router

$ curl -sSL https://router.apollo.dev/nix/latest | sh

run the router with our config file and supergraph file

$ MY_ENV_VAR="CLIENT_NAME" CLIENT_NAME="client-name" ./router -s ./supergraph.graphql -c ./router.yaml --dev
... truncated logs ...
2023-04-17T20:45:44.407486Z  ERROR could not expand variable: ${env.MY_ENV_VAR, environment variable not found
2023-04-17T20:45:44.408319Z  INFO stopped
2023-04-17T20:45:44.410397Z  ERROR no valid configuration was supplied
no valid configuration was supplied

I think if we detect nesting like this we just error and say it's invalid config.

@dbanty
Copy link
Contributor Author

dbanty commented Apr 17, 2023

@EverlastingBugstopper I pushed a change that implements the algorithm I mentioned above—I think detecting nesting would be as hard as resolving nesting (if we're still allowing multiple values). Maybe I am wrong though, if you can think of a simple want to handle it.

@dbanty
Copy link
Contributor Author

dbanty commented Apr 18, 2023

@EverlastingBugstopper I switched this to use the same variable expansion rules as router for more consistency. Now the docs about it can just point to those 😁 . I also opened #1578 and #1579 as potential follow-ups.

Left to do:

  • Release apollo-federation-types and update here
  • Add tests for file-based expansion

Copy link
Contributor

@EverlastingBugstopper EverlastingBugstopper left a comment

Choose a reason for hiding this comment

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

few small notes and then i think we're off to the races!

Cargo.toml Outdated Show resolved Hide resolved
src/utils/expansion.rs Outdated Show resolved Hide resolved
@EverlastingBugstopper
Copy link
Contributor

oh and would you also mind updating the top level PR comment to reflect the current state of the PR? we use "squash and merge, use the PR body for the commit body" settings on this repo

@dbanty
Copy link
Contributor Author

dbanty commented Apr 19, 2023

@EverlastingBugstopper I think this is good to go now, but let me know if I missed something!

Copy link
Contributor

@EverlastingBugstopper EverlastingBugstopper left a comment

Choose a reason for hiding this comment

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

LGTM! just pushed one commit that moves the shellexpand dep to be a workspace dep. i think we're good here 👍🏼

@EverlastingBugstopper EverlastingBugstopper enabled auto-merge (squash) April 19, 2023 18:24
@EverlastingBugstopper EverlastingBugstopper merged commit 659beee into main Apr 19, 2023
7 of 8 checks passed
@EverlastingBugstopper EverlastingBugstopper deleted the supergraph-compose-introspection-headers branch April 19, 2023 19:55
EverlastingBugstopper added a commit that referenced this pull request Apr 19, 2023
# [0.14.0] - 2023-04-19

> Important: 1 potentially breaking changes below, indicated by **❗
BREAKING ❗**

## ❗ BREAKING ❗

- **`rover config whoami` outputs to `stdout` instead of `stderr` and
using `--format json` includes more information than success or failure
- @scombat, #1560 fixes #1380**

When running `rover config whoami`, the output will print to `stdout`
instead of `stderr`. This may break scripts that relied on parsing the
output from `stderr`. The good news is that these scripts should be
easier to write because passing `--format json` to `rover config whoami`
will print structured output that can be parsed with a tool like
[`jq`](https://stedolan.github.io/jq/tutorial/).

## 🚀 Features

- **ALlow custom headers when running introspection with `rover
supergraph compose` - @dbanty, #1574 fixes #615**

A new field is available in `supergraph.yaml` files that allows sending
headers along with introspection. This value also supports environment
variable interpolation for sensitive values like authentication tokens.

- **Print a wanring when attempting to publish a subgraph with an
invalid routing URL - @trevor-scheer, #1543 fixes #1477**

When running `rover subgraph publish`, if the `--routing-url` you
specify or the routing URL stored in GraphOS is unroutable, a warning
will be printed. If you are not in CI, you will need to manually confirm
the publish to continue. You can dismiss the warning by passing
`--allow-invalid-routing-url`.

  **Note:** This warning will become a hard error in the future.

## 🐛 Fixes

- **Spawn a thread to avoid a rare deadlock in `rover dev` -
@EverlastingBugstopper, #1548 fixes #1544**

## 🛠 Maintenance

- **Updates dependencies - @EverlastingBugstopper, #1562**

  `apollo-parser` 0.4 -> 0.5
  `git2` 0.16 -> 0.17
  `opener` 0.5 -> 0.6
  `predicates` 2 -> 3
  `serial_test` 1 -> 2
  `toml` 0.5 -> 0.7
  ~`crossterm`~

- **Use Apple Silicon in CI - @EverlastingBugstopper, #1557 fixes
#1555**

There should be no user facing change here, we just run builds in CI
much faster.

## 📚 Documentation

- **Adds Apollo CLI migration guide to Rover docs - @StephenBarlow,
#1568**

The (deprecated) Apollo CLI documentation and the migration guide for
Rover now live in Rover's docset.

- **Cleans up nomenclature and links in Rover docs - @StephenBarlow,
#1571 and #1573**

Rover's documentation has been updated to refer to the [new GraphOS
documentation](https://www.apollographql.com/docs/graphos) along with
updating some terminology.

- **Mention community-maintained installation methods - @dbanty, #1542**

Rover's documentation now mentions the unofficial installation methods
`nix` and `brew`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 🎉 new commands, flags, functionality, and improved error messages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom introspection headers for supergraph compose
2 participants