Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/state/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package state

import (
"context"
"crypto/x509"
"net/http"

"github.com/canonical/microcluster/v2/rest/types"
)
Expand Down Expand Up @@ -45,4 +47,12 @@ type Hooks struct {

// OnDaemonConfigUpdate is a post-action hook that is run on all cluster members when any cluster member receives a local configuration update.
OnDaemonConfigUpdate func(ctx context.Context, s State, config types.DaemonConfig) error

// OnAuthenticate is run when an incoming HTTP request needs to be authenticated.
// If defined it is used as a replacement for the regular mTLS check.
// trustedCerts contains the certs from the truststore.
// Returning true indicates that the request is trusted.
// Returning false indicates that the request is not trusted.
// Returning an error is optional but will always result in a forbidden request.
OnAuthenticate func(ctx context.Context, s State, r *http.Request, trustedCerts map[string]x509.Certificate) (bool, error)
}
4 changes: 2 additions & 2 deletions microcluster/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (m *MicroCluster) Status(ctx context.Context) (*internalTypes.Server, error
// Ready waits for the daemon to report it has finished initial setup and is ready to be bootstrapped or join an
// existing cluster.
func (m *MicroCluster) Ready(ctx context.Context) error {
finger := make(chan error, 1)
finger := make(chan struct{}, 1)
var errLast error
go func() {
for i := 0; ; i++ {
Expand Down Expand Up @@ -156,7 +156,7 @@ func (m *MicroCluster) Ready(ctx context.Context) error {
continue
}

finger <- nil
finger <- struct{}{}
return
}
}()
Expand Down
4 changes: 4 additions & 0 deletions rest/access/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func Authenticate(state state.State, r *http.Request, hostAddress string, truste

switch r.Host {
case hostAddrPort.WithZone("").String():
if intState.Hooks != nil && intState.Hooks.OnAuthenticate != nil {
return intState.Hooks.OnAuthenticate(r.Context(), intState, r, trustedCerts)
}

if r.TLS != nil {
for _, cert := range r.TLS.PeerCertificates {
trusted, fingerprint := util.CheckMutualTLS(*cert, trustedCerts)
Expand Down
Loading