Files
teleport/lib/auth/oidc.go
rosstimothy ec2718b810 Convert lib/auth to use slog (#50577)
* Convert auth_with_roles to use slog

* allow benchmark tests to log

* remove last use of logrus in github.go

* remove last use of logrus in rotate.go

* remove auth package logrus logger

* convert auth.go to exclusively use slog

* convert db.go to exclusively use slog

* convert methods.go to exclusively use slog

* convert password.go to exclusively use slog

* convert join_ec2.go to exclusively use slog

* convert join_iam.go to exclusively use slog

* convert kube.go to exclusively use slog

* convert oidc.go to exclusively use slog

* convert saml.go to exclusively use slog

* convert sso_diag_context.go to exclusively use slog

* convert transport_credentials.go to exclusively use slog

* convert trustedcluster.go to exclusively use slog

* convert usertoken.go to exclusively use slog

* convert user.go to exclusively use slog

* remove logrus import

* fix: pass in context

* fix: remove new use of package logrus logger
2025-01-06 21:31:53 +00:00

160 lines
5.6 KiB
Go

/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package auth
import (
"context"
"net/url"
"github.com/gravitational/trace"
"github.com/gravitational/teleport/api/types"
apievents "github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/teleport/lib/auth/authclient"
"github.com/gravitational/teleport/lib/authz"
"github.com/gravitational/teleport/lib/events"
)
type OIDCService interface {
CreateOIDCAuthRequest(ctx context.Context, req types.OIDCAuthRequest) (*types.OIDCAuthRequest, error)
CreateOIDCAuthRequestForMFA(ctx context.Context, req types.OIDCAuthRequest) (*types.OIDCAuthRequest, error)
ValidateOIDCAuthCallback(ctx context.Context, q url.Values) (*authclient.OIDCAuthResponse, error)
}
var errOIDCNotImplemented = &trace.AccessDeniedError{Message: "OIDC is only available in enterprise subscriptions"}
// UpsertOIDCConnector creates or updates an OIDC connector.
func (a *Server) UpsertOIDCConnector(ctx context.Context, connector types.OIDCConnector) (types.OIDCConnector, error) {
upserted, err := a.Services.UpsertOIDCConnector(ctx, connector)
if err != nil {
return nil, trace.Wrap(err)
}
if err := a.emitter.EmitAuditEvent(ctx, &apievents.OIDCConnectorCreate{
Metadata: apievents.Metadata{
Type: events.OIDCConnectorCreatedEvent,
Code: events.OIDCConnectorCreatedCode,
},
UserMetadata: authz.ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: connector.GetName(),
},
}); err != nil {
a.logger.WarnContext(ctx, "Failed to emit OIDC connector create event", "error", err)
}
return upserted, nil
}
// UpdateOIDCConnector updates an existing OIDC connector.
func (a *Server) UpdateOIDCConnector(ctx context.Context, connector types.OIDCConnector) (types.OIDCConnector, error) {
updated, err := a.Services.UpdateOIDCConnector(ctx, connector)
if err != nil {
return nil, trace.Wrap(err)
}
if err := a.emitter.EmitAuditEvent(ctx, &apievents.OIDCConnectorUpdate{
Metadata: apievents.Metadata{
Type: events.OIDCConnectorUpdatedEvent,
Code: events.OIDCConnectorUpdatedCode,
},
UserMetadata: authz.ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: connector.GetName(),
},
}); err != nil {
a.logger.WarnContext(ctx, "Failed to emit OIDC connector update event", "error", err)
}
return updated, nil
}
// CreateOIDCConnector creates a new OIDC connector.
func (a *Server) CreateOIDCConnector(ctx context.Context, connector types.OIDCConnector) (types.OIDCConnector, error) {
created, err := a.Services.CreateOIDCConnector(ctx, connector)
if err != nil {
return nil, trace.Wrap(err)
}
if err := a.emitter.EmitAuditEvent(ctx, &apievents.OIDCConnectorCreate{
Metadata: apievents.Metadata{
Type: events.OIDCConnectorCreatedEvent,
Code: events.OIDCConnectorCreatedCode,
},
UserMetadata: authz.ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: connector.GetName(),
},
}); err != nil {
a.logger.WarnContext(ctx, "Failed to emit OIDC connector create event", "error", err)
}
return created, nil
}
// DeleteOIDCConnector deletes an OIDC connector by name.
func (a *Server) DeleteOIDCConnector(ctx context.Context, connectorName string) error {
if err := a.Services.DeleteOIDCConnector(ctx, connectorName); err != nil {
return trace.Wrap(err)
}
if err := a.emitter.EmitAuditEvent(ctx, &apievents.OIDCConnectorDelete{
Metadata: apievents.Metadata{
Type: events.OIDCConnectorDeletedEvent,
Code: events.OIDCConnectorDeletedCode,
},
UserMetadata: authz.ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: connectorName,
},
}); err != nil {
a.logger.WarnContext(ctx, "Failed to emit OIDC connector delete event", "error", err)
}
return nil
}
// CreateOIDCAuthRequest delegates the method call to the oidcAuthService if present,
// or returns a NotImplemented error if not present.
func (a *Server) CreateOIDCAuthRequest(ctx context.Context, req types.OIDCAuthRequest) (*types.OIDCAuthRequest, error) {
if a.oidcAuthService == nil {
return nil, errOIDCNotImplemented
}
rq, err := a.oidcAuthService.CreateOIDCAuthRequest(ctx, req)
return rq, trace.Wrap(err)
}
// CreateOIDCAuthRequestForMFA delegates the method call to the oidcAuthService if present,
// or returns a NotImplemented error if not present.
func (a *Server) CreateOIDCAuthRequestForMFA(ctx context.Context, req types.OIDCAuthRequest) (*types.OIDCAuthRequest, error) {
if a.oidcAuthService == nil {
return nil, errOIDCNotImplemented
}
rq, err := a.oidcAuthService.CreateOIDCAuthRequestForMFA(ctx, req)
return rq, trace.Wrap(err)
}
// ValidateOIDCAuthCallback delegates the method call to the oidcAuthService if present,
// or returns a NotImplemented error if not present.
func (a *Server) ValidateOIDCAuthCallback(ctx context.Context, q url.Values) (*authclient.OIDCAuthResponse, error) {
if a.oidcAuthService == nil {
return nil, errOIDCNotImplemented
}
resp, err := a.oidcAuthService.ValidateOIDCAuthCallback(ctx, q)
return resp, trace.Wrap(err)
}