Skip to content

Conversation

ryysud
Copy link
Contributor

@ryysud ryysud commented Sep 1, 2022

Signed-off-by: Ryuma Yoshida [email protected]

Pull Request check list

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

Affected functionality

Federation.

Description of change

Detect misconfiguration of bundle_endpoint_profile.
https://github.com/spiffe/spire/blob/v1.4.0/doc/spire_server.md#configuration-options-for-federationfederates_withtrust-domainbundle_endpoint

Which issue this PR fixes

@ryysud ryysud force-pushed the detect-misconfiguration branch from 5086c66 to 4de58b1 Compare September 1, 2022 08:26
@ryysud ryysud marked this pull request as ready for review September 1, 2022 08:27
Copy link
Member

@azdagron azdagron left a comment

Choose a reason for hiding this comment

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

I think this is a good diagnostic! Thanks for this, @ryysud!

Comment on lines 76 to 83
var hostnameError *x509.HostnameError
if errors.As(err, &hostnameError) && c.c.SPIFFEAuth == nil {
id, e := spiffeid.FromString(hostnameError.Certificate.URIs[0].String())
if e != nil {
return nil, errs.New("failed to fetch bundle: %v", err)
}
return nil, errs.New("failed to fetch bundle, the server certificate contains SPIFFE ID %q, should specify https_spiffe instead of https_web: %v", id, err)
}
Copy link
Member

Choose a reason for hiding this comment

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

Line 78 will panic if the certificate does not contain a URI SAN. Also, the branch when e != nil is not necessary since we can fall through to the return statement on line 84:

Suggested change
var hostnameError *x509.HostnameError
if errors.As(err, &hostnameError) && c.c.SPIFFEAuth == nil {
id, e := spiffeid.FromString(hostnameError.Certificate.URIs[0].String())
if e != nil {
return nil, errs.New("failed to fetch bundle: %v", err)
}
return nil, errs.New("failed to fetch bundle, the server certificate contains SPIFFE ID %q, should specify https_spiffe instead of https_web: %v", id, err)
}
var hostnameError *x509.HostnameError
if errors.As(err, &hostnameError) && c.c.SPIFFEAuth == nil && len(hostnameError.Certificate.URIs) > 0 {
if id, idErr := spiffeid.FromString(hostnameError.Certificate.URIs[0].String()); idErr == nil {
return nil, errs.New("failed to authenticate bundle endpoint using web authentication but the server certificate contains SPIFFE ID %q: maybe use https_spiffe instead of https_web: %v", id, err)
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your review, @azdagron! I fixed with 50a169c.

azdagron
azdagron previously approved these changes Sep 2, 2022
@@ -72,6 +73,12 @@ func NewClient(config ClientConfig) (Client, error) {
func (c *client) FetchBundle(ctx context.Context) (*bundleutil.Bundle, error) {
resp, err := c.client.Get(c.c.EndpointURL)
if err != nil {
var hostnameError *x509.HostnameError
Copy link
Member

Choose a reason for hiding this comment

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

I was doing some quick testing and noticed that the x509 stack returns this error as a struct value (i.e. x509.HostnameError{}), not a pointer to struct value (i.e. &x509.HostnameError). This means that errors.As will return false unless passed a pointer to the struct, not a pointer to a pointer.

Suggested change
var hostnameError *x509.HostnameError
var hostnameError x509.HostnameError

I can push a commit with my tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fix: 9562435

@evan2645
Copy link
Member

evan2645 commented Sep 6, 2022

Thanks for your help shepherding this @azdagron - @amartinezfayo has volunteered to review when you're done

Signed-off-by: Andrew Harding <[email protected]>
@azdagron
Copy link
Member

azdagron commented Sep 6, 2022

I've added a unit test. @amartinezfayo if you could please take a look.

@azdagron azdagron modified the milestones: 1.4.2, 1.4.3 Sep 6, 2022
@azdagron
Copy link
Member

azdagron commented Sep 6, 2022

Oops. I forgot one crucial change to the test framework to support this :) Change incoming.

Signed-off-by: Andrew Harding <[email protected]>
Signed-off-by: Andrew Harding <[email protected]>
Copy link
Member

@amartinezfayo amartinezfayo left a comment

Choose a reason for hiding this comment

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

Thank you @ryysud and @azdagron, LGTM!

@azdagron azdagron merged commit 39139a8 into spiffe:main Sep 7, 2022
@ryysud ryysud deleted the detect-misconfiguration branch September 8, 2022 01:40
stevend-uber pushed a commit to stevend-uber/spire that referenced this pull request Oct 16, 2023
* Detect misconfiguration of bundle_endpoint_profile

Signed-off-by: Ryuma Yoshida <[email protected]>
Co-authored-by: Andrew Harding <[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.

4 participants