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

auth: define auth handlers as private hive cell #24074

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions pkg/auth/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package auth

import (
"fmt"

"github.com/cilium/cilium/pkg/auth/monitor"
"github.com/cilium/cilium/pkg/endpointmanager"
"github.com/cilium/cilium/pkg/hive/cell"
Expand All @@ -19,19 +21,42 @@ var Cell = cell.Module(
"auth-manager",
"Authenticates requests as demanded by policy",

// The manager is the main entry point which gets registered to the agent monitor and receives auth requests.
cell.Provide(newManager),
cell.ProvidePrivate(
// Null auth handler provides support for auth type "null" - which always succeeds.
newNullAuthHandler,
),
)

type authManagerParams struct {
cell.In

EndpointManager endpointmanager.EndpointManager
AuthHandlers []authHandler `group:"authHandlers"`
}

type Manager interface {
consumer.MonitorConsumer
}

func newManager(params authManagerParams) Manager {
return monitor.AddAuthManager(NewAuthManager(params.EndpointManager))
func newManager(params authManagerParams) (Manager, error) {
manager, err := newAuthManager(params.EndpointManager, params.AuthHandlers)
if err != nil {
return nil, fmt.Errorf("failed to create auth manager: %w", err)
}

return monitor.New(manager), nil
}

type authHandlerResult struct {
cell.Out

AuthHandler authHandler `group:"authHandlers"`
}

func newNullAuthHandler() authHandlerResult {
return authHandlerResult{
AuthHandler: &nullAuthHandler{},
}
}
44 changes: 31 additions & 13 deletions pkg/auth/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package auth

import (
"fmt"
"strconv"

"github.com/cilium/cilium/pkg/endpointmanager"
Expand All @@ -14,17 +15,32 @@ import (
"github.com/cilium/cilium/pkg/u8proto"
)

type AuthManager struct {
type authManager struct {
endpointManager endpointmanager.EndpointsLookup
authHandlers map[policy.AuthType]authHandler
}

func NewAuthManager(epMgr endpointmanager.EndpointsLookup) *AuthManager {
return &AuthManager{
endpointManager: epMgr,
type authHandler interface {
authenticate() error
authType() policy.AuthType
}

func newAuthManager(epMgr endpointmanager.EndpointsLookup, authHandlers []authHandler) (*authManager, error) {
ahs := map[policy.AuthType]authHandler{}
for _, ah := range authHandlers {
if _, ok := ahs[ah.authType()]; ok {
return nil, fmt.Errorf("multiple handlers for auth type: %s", ah.authType())
}
ahs[ah.authType()] = ah
}

return &authManager{
endpointManager: epMgr,
authHandlers: ahs,
}, nil
}

func (a *AuthManager) AuthRequired(dn *monitor.DropNotify, ci *monitor.ConnectionInfo) {
func (a *authManager) AuthRequired(dn *monitor.DropNotify, ci *monitor.ConnectionInfo) {
// Requested authentication type is in DropNotify.ExtError field
authType := policy.AuthType(dn.ExtError)

Expand All @@ -34,7 +50,7 @@ func (a *AuthManager) AuthRequired(dn *monitor.DropNotify, ci *monitor.Connectio
srcAddr := ci.SrcIP.String() + ":" + strconv.FormatUint(uint64(ci.SrcPort), 10)
dstAddr := ci.DstIP.String() + ":" + strconv.FormatUint(uint64(ci.DstPort), 10)

log.Debugf("policy: Authentication type %s required for identity %d->%d, %s %s->%s, ingress: %t",
log.Debugf("auth: Policy is requiring authentication type %s for identity %d->%d, %s %s->%s, ingress: %t",
authType.String(), dn.SrcLabel, dn.DstLabel, ci.Proto, srcAddr, dstAddr, ingress)

proto, err := u8proto.ParseProtocol(ci.Proto)
Expand All @@ -51,16 +67,19 @@ func (a *AuthManager) AuthRequired(dn *monitor.DropNotify, ci *monitor.Connectio
}

// Authenticate according to the requested auth type
switch authType {
case policy.AuthTypeNull:
// Authentication trivially done
default:
h, ok := a.authHandlers[authType]
if !ok {
log.WithField(logfields.AuthType, authType.String()).Warning("auth: Unknown requested auth type")
return
}

if err := h.authenticate(); err != nil {
log.WithError(err).WithField(logfields.AuthType, authType.String()).Warning("auth: Failed to authenticate")
return
}

/* Update CT flags as authorized. */
err = ctmap.Update(ep.ConntrackName(), srcAddr, dstAddr, proto, ingress,
if err := ctmap.Update(ep.ConntrackName(), srcAddr, dstAddr, proto, ingress,
func(entry *ctmap.CtEntry) error {
before := entry.Flags
if entry.Flags&ctmap.AuthRequired != 0 {
Expand All @@ -69,8 +88,7 @@ func (a *AuthManager) AuthRequired(dn *monitor.DropNotify, ci *monitor.Connectio
before&ctmap.AuthRequired, entry.Flags&ctmap.AuthRequired)
}
return nil
})
if err != nil {
}); err != nil {
log.WithError(err).Warning("auth: Conntrack map update failed")
}
}
2 changes: 1 addition & 1 deletion pkg/auth/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type dropMonitor struct {
lostEvents uint64
}

func AddAuthManager(auth AuthManager) *dropMonitor {
func New(auth AuthManager) *dropMonitor {
return &dropMonitor{authManager: auth}
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/auth/null_authhandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package auth

import "github.com/cilium/cilium/pkg/policy"

type nullAuthHandler struct {
}

func (r *nullAuthHandler) authenticate() error {
// Authentication trivially done
return nil
}

func (r *nullAuthHandler) authType() policy.AuthType {
return policy.AuthTypeNull
}