Skip to content

feat: support custom headers#670

Open
alexanderzobnin wants to merge 3 commits intoopenfga:mainfrom
grafana:alexz/custom-headers
Open

feat: support custom headers#670
alexanderzobnin wants to merge 3 commits intoopenfga:mainfrom
grafana:alexz/custom-headers

Conversation

@alexanderzobnin
Copy link
Copy Markdown

@alexanderzobnin alexanderzobnin commented Apr 8, 2026

Description

What problem is being solved?

Solves #669

How is it being solved?

Provide new --custom-headers flag which allows to pass custom user-defined headers which are then included to every request to API.

What changes are made to solve it?

Implemented new --custom-headers option by utilizing existing DefaultHeaders client config field.

References

Review Checklist

  • I have clicked on "allow edits by maintainers".
  • I have added documentation for new/changed functionality in this PR or in a PR to openfga.dev [Provide a link to any relevant PRs in the references section above]
  • The correct base branch is being used, if not main
  • I have added tests to validate that the change in functionality is working as expected

Summary by CodeRabbit

  • New Features
    • Added support for custom HTTP headers via the --custom-headers CLI flag. This repeatable flag accepts header values in name:value format, enabling users to include custom headers in requests.

@alexanderzobnin alexanderzobnin requested a review from a team as a code owner April 8, 2026 10:22
Copilot AI review requested due to automatic review settings April 8, 2026 10:22
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 8, 2026

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8e68cdcf-790e-41d5-9e76-0dc41f3e75ae

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

A new CLI flag --custom-headers is added to collect custom HTTP headers as a repeatable string array argument. This value is read during configuration initialization and passed to the OpenFGA SDK client via a new CustomHeaders field, with header parsing logic to extract name-value pairs from the provided strings.

Changes

Cohort / File(s) Summary
CLI Input Collection
cmd/root.go, internal/cmdutils/get-client-config.go
Added persistent --custom-headers flag definition and integrated flag value reading into client configuration initialization.
SDK Client Configuration
internal/fga/fga.go
Added CustomHeaders field to ClientConfig struct and implemented header parsing logic (getCustomHeaders()) to convert string entries into a map format for SDK DefaultHeaders configuration.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: support custom headers' directly and clearly describes the main change in the pull request - adding support for custom HTTP headers via a new CLI flag.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
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 adds a CLI-level mechanism for supplying user-defined HTTP headers that are automatically included on all OpenFGA API requests, addressing #669 for deployments that require non-standard auth headers.

Changes:

  • Introduces a new --custom-headers persistent flag and wires it into ClientConfig.
  • Parses provided header strings into a map[string]string and passes them via the SDK DefaultHeaders configuration.
  • Extends the serialized client config model to include custom_headers.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
internal/fga/fga.go Adds CustomHeaders to config and applies parsed headers through the SDK client configuration.
internal/cmdutils/get-client-config.go Reads the new custom-headers flag into fga.ClientConfig.
cmd/root.go Registers the new persistent --custom-headers flag.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

cmd/root.go Outdated
rootCmd.PersistentFlags().String("client-id", "", "Client ID. Sent to the Token Issuer during the Client Credentials flow") //nolint:lll
rootCmd.PersistentFlags().String("client-secret", "", "Client Secret. Sent to the Token Issuer during the Client Credentials flow") //nolint:lll
rootCmd.PersistentFlags().StringArray("api-scopes", []string{}, "API Scopes (repeat option for multiple values). Used in the Client Credentials flow") //nolint:lll
rootCmd.PersistentFlags().StringArray("custom-headers", []string{}, "Custom HTTP Headers (repeat option for multiple values)") //nolint:lll
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

The flag help text doesn’t describe the expected header format (e.g. Header: value). As implemented, values are parsed by splitting on the first :; without documenting this, users can easily pass a value that is silently ignored. Consider updating the description to explicitly state the required format and note that the flag can be repeated.

Suggested change
rootCmd.PersistentFlags().StringArray("custom-headers", []string{}, "Custom HTTP Headers (repeat option for multiple values)") //nolint:lll
rootCmd.PersistentFlags().StringArray("custom-headers", []string{}, "Custom HTTP headers in 'Header: value' format (repeat option for multiple values)") //nolint:lll

Copilot uses AI. Check for mistakes.
Comment on lines +106 to +112
for _, header := range c.CustomHeaders {
parts := strings.SplitN(header, ":", 2)
if len(parts) == 2 {
head := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
headers[head] = value
}
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

Custom header parsing silently skips entries that don’t contain a :. This makes misconfiguration hard to detect (the CLI will run but the header won’t be sent). Consider validating CustomHeaders and returning an error (e.g. from GetFgaClient) when any entry is malformed.

Copilot uses AI. Check for mistakes.
if len(parts) == 2 {
head := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
headers[head] = value
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

getCustomHeaders will accept an empty header name (e.g. input ": value"), resulting in a map entry with key "". It’s better to reject empty/whitespace-only header names during parsing/validation to avoid sending invalid HTTP headers.

Suggested change
headers[head] = value
if head != "" {
headers[head] = value
}

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

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
cmd/root.go (1)

71-71: Clarify --custom-headers value format in help text.

Consider documenting the expected format (for example, Header-Name:Header Value) directly in the flag description to reduce invalid input.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cmd/root.go` at line 71, Update the flag description for
rootCmd.PersistentFlags().StringArray("custom-headers", ...) to clarify the
expected input format and reduce invalid values by documenting an example like
"Header-Name: Header Value" and specifying that the colon separates name and
value and the flag can be repeated for multiple headers; modify only the
description string passed to StringArray for the "custom-headers" flag to
include this format guidance and an example.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@internal/fga/fga.go`:
- Around line 104-113: The getCustomHeaders method currently adds headers even
when the parsed header name is empty (e.g., ":value"); update
ClientConfig.getCustomHeaders to ignore entries with empty header names by
trimming parts[0] and checking head != "" before assigning headers[head] =
value, keeping the rest of the parsing logic the same so only valid non-empty
header keys are included.

---

Nitpick comments:
In `@cmd/root.go`:
- Line 71: Update the flag description for
rootCmd.PersistentFlags().StringArray("custom-headers", ...) to clarify the
expected input format and reduce invalid values by documenting an example like
"Header-Name: Header Value" and specifying that the colon separates name and
value and the flag can be repeated for multiple headers; modify only the
description string passed to StringArray for the "custom-headers" flag to
include this format guidance and an example.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 605667f4-98bb-4e92-9330-aac2d46d26b7

📥 Commits

Reviewing files that changed from the base of the PR and between add64f4 and f5174c7.

📒 Files selected for processing (3)
  • cmd/root.go
  • internal/cmdutils/get-client-config.go
  • internal/fga/fga.go

@alexanderzobnin alexanderzobnin requested review from a team as code owners April 8, 2026 10:30
Copy link
Copy Markdown
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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +104 to +115
func (c ClientConfig) getCustomHeaders() map[string]string {
headers := map[string]string{}
for _, header := range c.CustomHeaders {
parts := strings.SplitN(header, ":", 2)
if len(parts) == 2 {
head := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
if head != "" {
headers[head] = value
}
}
}
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

getCustomHeaders silently ignores malformed --custom-headers entries (missing ':' or empty header name). This can lead to confusing behavior where the user thinks a header is being sent but it is dropped without any feedback. Consider validating the flag values and returning an error (or at least emitting a warning to stderr) when an entry is invalid.

Copilot uses AI. Check for mistakes.
| Token Audience | `--api-audience` | `FGA_API_AUDIENCE` | `api-audience` |
| Store ID | `--store-id` | `FGA_STORE_ID` | `store-id` |
| Authorization Model ID | `--model-id` | `FGA_MODEL_ID` | `model-id` |
| Custom Headers | `--custom-headers` | `FGA_CUSTOM_HEADERS` | `custom-headers` |
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

The new Custom Headers row documents the flag/env/config key but doesn't describe the required value format or how to specify multiple headers in ~/.fga.yaml / FGA_CUSTOM_HEADERS. Adding a brief example (and aligning it with the actual parsing rules) would prevent user confusion.

Copilot uses AI. Check for mistakes.
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.

2 participants