Skip to content

federation: bootstrap https_spiffe trust from a config path - #7260

Merged
MarcosDY merged 4 commits into
spiffe:mainfrom
samuraixwandering:feat/federation-bootstrap-bundle-path
Sep 19, 2026
Merged

MarcosDY merged 4 commits into
spiffe:mainfrom
samuraixwandering:feat/federation-bootstrap-bundle-path

Conversation

@samuraixwandering

Copy link
Copy Markdown
Contributor

Pull Request check list

  • Commit conforms to CONTRIBUTING.md?
  • Proper tests/regressions included?
  • Documentation updated?

Affected functionality

SPIRE Server federation (federates_with). Optional config; default behavior is unchanged.

Description of change

https_spiffe federation currently fails with local copy of bundle not found until someone runs spire-server bundle set. Following the design on #6322, this adds bootstrap_bundle_path (and optional bootstrap_bundle_format, default pem) so the first fetch can authenticate from a file when the datastore has no bundle.

The fetched bundle is stored with create, not update. Later polls use the stored copy. The path is not watched. Rejected on https_web and when the endpoint SPIFFE ID is outside the federated trust domain.

Which issue this PR fixes

Fixes #6322

Test plan

  • go test ./pkg/server/bundle/client/ ./cmd/spire-server/cli/run/
  • make lint

}

spiffeProfile, ok := cfg.EndpointProfile.(bundleClient.HTTPSSPIFFEProfile)
if !ok {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fails open for profiles it doesn't recognize.
It rejects HTTPSWebProfile explicitly, then does spiffeProfile, ok := cfg.EndpointProfile.(bundleClient.HTTPSSPIFFEProfile) and returns nil (accept) when the assertion fails.
So the trust-domain check on the next line is skipped entirely for anything that isn't one of those two types.
Today that's unreachable: EndpointProfileInfo has exactly two implementations (manager.go:63 and :72), and both are handled, so this isn't a live hole.
But the default direction is backwards for a check whose whole job is constraining when a file on disk becomes a TLS trust anchor, adding a third profile silently reopens what this function was written to close.

Inverting it to an allow-list (if !ok { return errors.New(...) }) makes the HTTPSWebProfile case above redundant and makes a future third profile fail closed until someone decides otherwise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Inverted to an allow-list. Anything that is not https_spiffe gets the same error as https_web. Added a test for an unknown profile type.

Comment thread pkg/server/bundle/client/updater.go Outdated

if localEndpointBundle == nil {
return nil, errors.New("can't perform SPIFFE Authentication: local copy of bundle not found")
rootCAs, err := rootCAsForSPIFFEAuth(localEndpointBundle, trustDomainConfig, trustDomain)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The invariant that makes bootstrap auth safe (that the endpoint's trust domain equals the federated one) is enforced only in cmd/spire-server/cli/run/run.go, and this package does not validate that.

newClient resolves trustDomain from spiffeAuth.EndpointSPIFFEID and passes it to rootCAsForSPIFFEAuth, which will fall back to the bootstrap file whenever that trust domain has no local bundle, regardless of whether it matches u.td.

Asserting spiffeAuth.EndpointSPIFFEID.TrustDomain() == u.td before using the bootstrap path would make the property local to the code that depends on it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The updater now refuses the bootstrap file unless the endpoint SPIFFE ID is in the federated trust domain. Non-self-serving with no stored bundle fails closed.

}
}

func TestBundleUpdaterBootstrapCreateOnly(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

TestBundleUpdaterBootstrapCreateOnly is not validating what he name say.
countingDataStore wraps a fakedatastore that starts empty, so CreateBundle always succeeds and the new AlreadyExists handling at updater.go:97 is never excersized,
Exercising it needs a datastore that reports no local bundle from FetchBundle and returns AlreadyExists from CreateBundle.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a datastore that Fetch-misses and Create-returns AlreadyExists. That path no longer returns the fetched bundle as if it were stored. Also covered the case where a bundle appears after bootstrap auth: still Create, no Set.

https_spiffe federation currently fails until someone runs
bundle set. Add bootstrap_bundle_path on federates_with so the
first fetch can authenticate when the datastore has no bundle.
The fetched bundle is created, not updated. Later polls use the
stored copy.

Fixes spiffe#6322

Signed-off-by: samuraiXwandering <[email protected]>
The path is https_spiffe only. The updater will not use the file
unless the endpoint SPIFFE ID is in the federated domain. A racing
CreateBundle does not count as a stored refresh.

Signed-off-by: samuraiXwandering <[email protected]>
@samuraixwandering
samuraixwandering force-pushed the feat/federation-bootstrap-bundle-path branch from 4c35570 to 2b18f04 Compare September 7, 2026 05:48

@MarcosDY MarcosDY left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks great, I just added a couple comments sorry about delay

Comment thread pkg/server/bundle/client/updater.go Outdated
if err != nil {
return localFederatedBundleOrNil, nil, fmt.Errorf("failed to store fetched federated bundle: %w", err)
// Create if the store was empty at auth time so bootstrap roots cannot overwrite a bundle that appears before persist.
if localFederatedBundleOrNil == nil || usedBootstrap {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the rework here, the usedBootstrap flag and the AlreadyExists fix from the last round both look good.

One thing I think we missed: localFederatedBundleOrNil == nil isn't gated on bootstrap, so this also changes the store path for federations that never enabled it.

Before, UpdateBundle always used SetBundle, which is an upsert. Now the first store uses CreateBundle, including https_web and https_spiffe without bootstrap_bundle_path. I checked on the head commit with a quick test, https_web, no bootstrap, empty datastore: creates=1 sets=0.

Where I think it bites is the branch below. sqlstore.go:1091 maps any constraint violation to AlreadyExists, and sqlcommon/constraint.go:14 treats the whole postgres class 23 as one, so NOT NULL, FK and CHECK errors land there too and return nil. runUpdateOnce only logs on error, so a real store failure would retry silently every minute on a path that worked before.

I think gating on usedBootstrap alone keeps the property you're after, bootstrap roots still can't overwrite a stored bundle, and leaves existing configs on SetBundle:

 if usedBootstrap {
     _, err = u.ds.CreateBundle(ctx, bundle)
     ...
 } else {
     _, err = u.ds.SetBundle(ctx, bundle)
     ...
 }

One more idea while you're in here, no strong feelings: on AlreadyExists you could re-fetch to confirm someone else stored it and return the error if not, so it only covers the race it's meant for.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Create is only used when bootstrap roots were used. https_web first store is SetBundle again.

// Create if the store was empty at auth time so bootstrap roots cannot overwrite a bundle that appears before persist.
if localFederatedBundleOrNil == nil || usedBootstrap {
_, err = u.ds.CreateBundle(ctx, bundle)
if err != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for adding the test for this path, it made the behavior easy to see.

Now that it's visible I think the handling needs another look. Returning nil here means the fetch worked but nothing was stored, and nobody finds out.

spire-server bundle refresh returns 200 OK in this case, so an operator asks for a refresh and is told it worked when the bundle was dropped.

The other thing is the error class is wider than the race we're targeting. The datastore maps any constraint violation to AlreadyExists, not just duplicates, so things like NOT NULL or FK errors land here too and would retry every minute with nothing in the logs.

What I'd suggest: on AlreadyExists, re-fetch and check if someone else actually stored it. If it's there, that's the real race and a no-op. If not, return the error so it surfaces.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

On AlreadyExists we re-fetch. If a bundle is there, no-op. If not, the error is returned.

Comment thread pkg/server/bundle/client/updater.go Outdated
if local != nil {
return local.X509Authorities(), false, nil
}
if cfg.BootstrapBundlePath == "" || endpointTD != federatedTD {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

NIT: this folds two different cases into the same message.

If someone sets bootstrap_bundle_path but the endpoint SPIFFE ID is in another trust domain, they get "local copy of bundle not found", which sends them to the datastore and bundle set. The file they configured is just ignored and never mentioned.

Config validation catches this at startup for the HCL path, so it's mostly the datastore-backed relationships that would hit it at runtime.

Could you split the two conditions so the mismatch says what's actually wrong, something like that the bootstrap file can't be used because the endpoint is in a different trust domain?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Split it. Mismatch now says the bootstrap file cannot be used because the endpoint SPIFFE ID is in a different trust domain.

AlreadyExists re-fetches so a constraint error is not a silent
refresh. Endpoint trust-domain mismatch has its own error.

Signed-off-by: samuraiXwandering <[email protected]>
@MarcosDY MarcosDY added this to the 1.16.0 milestone Sep 19, 2026
@MarcosDY
MarcosDY added this pull request to the merge queue Sep 19, 2026
Merged via the queue into spiffe:main with commit a3a1ac3 Sep 19, 2026
59 of 60 checks passed
Manuthor pushed a commit to Manuthor/spire that referenced this pull request Sep 25, 2026
)

* feat: bootstrap federated bundles from a path in server config

https_spiffe federation currently fails until someone runs
bundle set. Add bootstrap_bundle_path on federates_with so the
first fetch can authenticate when the datastore has no bundle.
The fetched bundle is created, not updated. Later polls use the
stored copy.

Fixes spiffe#6322

Signed-off-by: samuraiXwandering <[email protected]>

* chore: retrigger CI after flaky k8s job

Signed-off-by: samuraiXwandering <[email protected]>

* federation: fail closed bootstrap auth on profile and store

The path is https_spiffe only. The updater will not use the file
unless the endpoint SPIFFE ID is in the federated domain. A racing
CreateBundle does not count as a stored refresh.

Signed-off-by: samuraiXwandering <[email protected]>

* federation: keep SetBundle unless bootstrap roots were used

AlreadyExists re-fetches so a constraint error is not a silent
refresh. Endpoint trust-domain mismatch has its own error.

Signed-off-by: samuraiXwandering <[email protected]>

---------

Signed-off-by: samuraiXwandering <[email protected]>
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.

Allow preconfiguration of a bundle path in SPIRE server federation config

2 participants