Files
ziti/controller/model/authenticator_mod_ext_jwt.go
T
Andrew Martinez c5f156dd9e adds x509 claims support for external ids to CAs
- adds REST API CA external id field
- adds rest api for external fields
- refactors authentication interfaces for federation
- alters authentication moduels to return AuthResult interfaces
- allows AuthResults to provide external x509 certs as session certs
2022-05-23 13:08:16 -04:00

405 lines
11 KiB
Go

/*
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package model
import (
"crypto/x509"
"github.com/golang-jwt/jwt"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/edge/controller/apierror"
"github.com/openziti/edge/controller/persistence"
nfPem "github.com/openziti/foundation/util/pem"
"github.com/openziti/storage/boltz"
cmap "github.com/orcaman/concurrent-map"
"go.etcd.io/bbolt"
"strings"
)
var _ AuthProcessor = &AuthModuleExtJwt{}
const (
AuthMethodExtJwt = "ext-jwt"
ExtJwtInternalClaim = "-internal-ext-jwt"
)
type AuthModuleExtJwt struct {
env Env
method string
signers cmap.ConcurrentMap //map[kid string]*signer
}
func NewAuthModuleExtJwt(env Env) *AuthModuleExtJwt {
ret := &AuthModuleExtJwt{
env: env,
method: AuthMethodExtJwt,
signers: cmap.New(),
}
env.GetStores().ExternalJwtSigner.AddListener(boltz.EventCreate, ret.onExternalSignerCreate)
env.GetStores().ExternalJwtSigner.AddListener(boltz.EventUpdate, ret.onExternalSignerUpdate)
env.GetStores().ExternalJwtSigner.AddListener(boltz.EventDelete, ret.onExternalSignerDelete)
ret.loadExistingSigners()
return ret
}
type signerRecord struct {
externalJwtSigner *persistence.ExternalJwtSigner
cert *x509.Certificate
}
func (a *AuthModuleExtJwt) CanHandle(method string) bool {
return method == a.method
}
func (a *AuthModuleExtJwt) pubKeyLookup(token *jwt.Token) (interface{}, error) {
kidToSignerRectInterface := a.getKnownSignerRecords()
kidVal, ok := token.Header["kid"]
if !ok {
pfxlog.Logger().Error("missing kid")
return nil, apierror.NewInvalidAuth()
}
kid, ok := kidVal.(string)
if !ok {
pfxlog.Logger().Error("kid is not a string")
return nil, apierror.NewInvalidAuth()
}
signerRecordInterface, ok := kidToSignerRectInterface[kid]
if !ok {
pfxlog.Logger().Error("unknown kid")
return nil, apierror.NewInvalidAuth()
}
signerRecord := signerRecordInterface.(*signerRecord)
if !signerRecord.externalJwtSigner.Enabled {
pfxlog.Logger().WithField("externalJwtId", signerRecord.externalJwtSigner.Id).Error("external jwt is disabled")
return nil, apierror.NewInvalidAuth()
}
mapClaims := token.Claims.(jwt.MapClaims)
mapClaims[ExtJwtInternalClaim] = signerRecord.externalJwtSigner
return signerRecord.cert.PublicKey, nil
}
func (a *AuthModuleExtJwt) Process(context AuthContext) (AuthResult, error) {
return a.process(context, true)
}
func (a *AuthModuleExtJwt) ProcessSecondary(context AuthContext) (AuthResult, error) {
return a.process(context, false)
}
type AuthResultJwt struct {
AuthResultBase
externalJwtSignerId string
externalJwtSigner *persistence.ExternalJwtSigner
}
func (a *AuthResultJwt) IsSuccessful() bool {
return a.externalJwtSignerId != "" && a.AuthResultBase.identityId != ""
}
func (a *AuthModuleExtJwt) process(context AuthContext, isPrimary bool) (AuthResult, error) {
logger := pfxlog.Logger().WithField("authMethod", AuthMethodExtJwt)
headers := map[string]interface{}{}
for key, value := range context.GetHeaders() {
headers[strings.ToLower(key)] = value
}
var authHeaders []string
if authHeaderVal, ok := headers["authorization"]; ok {
authHeaders = authHeaderVal.([]string)
}
if len(authHeaders) != 1 {
logger.Error("no authorization header found")
return nil, apierror.NewInvalidAuth()
}
authHeader := authHeaders[0]
if !strings.HasPrefix(authHeader, "Bearer ") {
logger.Error("authorization header missing Bearer prefix")
return nil, apierror.NewInvalidAuth()
}
jwtStr := strings.Replace(authHeader, "Bearer ", "", 1)
//pubKeyLookup also handles extJwtSigner.enabled checking
jwtToken, err := jwt.Parse(jwtStr, a.pubKeyLookup)
if err == nil && jwtToken.Valid {
mapClaims := jwtToken.Claims.(jwt.MapClaims)
extJwt := mapClaims[ExtJwtInternalClaim].(*persistence.ExternalJwtSigner)
if extJwt == nil {
logger.Error("no external jwt signer found for internal claims")
return nil, apierror.NewInvalidAuth()
}
logger = logger.WithField("externalJwtSignerId", extJwt.Id)
issuer := ""
if issuerVal, ok := mapClaims["iss"]; ok {
issuer, ok = issuerVal.(string)
if !ok {
logger.Error("issuer in claims was not a string")
return nil, apierror.NewInvalidAuth()
}
}
logger = logger.WithField("claimsIssuer", issuer)
if extJwt.Issuer != nil && *extJwt.Issuer != issuer {
logger.WithField("expectedIssuer", *extJwt.Issuer).Error("invalid issuer")
return nil, apierror.NewInvalidAuth()
}
if extJwt.Audience != nil {
audValues := mapClaims["aud"]
if audValues == nil {
logger.WithField("audience", audValues).Error("audience is missing")
return nil, apierror.NewInvalidAuth()
}
audSlice, ok := audValues.([]string)
if !ok {
audString, ok := audValues.(string)
if !ok {
logger.WithField("audience", audValues).Error("audience is not a string or array of strings")
return nil, apierror.NewInvalidAuth()
}
audSlice = []string{audString}
}
found := false
for _, validAud := range audSlice {
if validAud == *extJwt.Audience {
found = true
break
}
}
if !found {
logger.WithField("expectedAudience", *extJwt.Audience).WithField("claimsAudiences", audSlice).Error("invalid audience")
return nil, apierror.NewInvalidAuth()
}
}
idClaimProperty := "sub"
if extJwt.ClaimsProperty != nil {
idClaimProperty = *extJwt.ClaimsProperty
}
logger = logger.WithField("idClaimProperty", idClaimProperty)
identityIdInterface, ok := mapClaims[idClaimProperty]
if !ok {
logger.Error("claims property on external jwt signer not found in claims")
return nil, apierror.NewInvalidAuth()
}
identityId, ok := identityIdInterface.(string)
if !ok || identityId == "" {
logger.Error("expected claims id was not a string or was empty")
return nil, apierror.NewInvalidAuth()
}
var authPolicy *AuthPolicy
var identity *Identity
if extJwt.UseExternalId {
authPolicy, identity, err = getAuthPolicyByExternalId(a.env, AuthMethodExtJwt, "", identityId)
} else {
authPolicy, identity, err = getAuthPolicyByIdentityId(a.env, AuthMethodExtJwt, "", identityId)
}
if err != nil {
logger.WithError(err).Error("encountered unhandled error during authentication")
return nil, apierror.NewInvalidAuth()
}
if authPolicy == nil {
logger.WithError(err).Error("encountered unhandled nil auth policy during authentication")
return nil, apierror.NewInvalidAuth()
}
externalJwtSignerId := ""
if identity.Disabled {
logger.
WithField("disabledAt", identity.DisabledAt).
WithField("disabledUntil", identity.DisabledUntil).
Error("authentication failed, identity is disabled")
return nil, apierror.NewInvalidAuth()
}
if !authPolicy.Primary.ExtJwt.Allowed {
logger.Error("external jwt authentication on auth policy is disabled")
return nil, apierror.NewInvalidAuth()
}
if isPrimary {
if len(authPolicy.Primary.ExtJwt.AllowedExtJwtSigners) > 0 {
found := false
for _, allowedId := range authPolicy.Primary.ExtJwt.AllowedExtJwtSigners {
if allowedId == extJwt.Id {
externalJwtSignerId = allowedId
found = true
break
}
}
if !found {
logger.
WithField("allowedSigners", authPolicy.Primary.ExtJwt.AllowedExtJwtSigners).
Error("auth policy does not allow specified signer")
return nil, apierror.NewInvalidAuth()
}
} else {
return nil, apierror.NewInvalidAuth()
}
} else if authPolicy.Secondary.RequiredExtJwtSigner != nil {
if extJwt.Id != *authPolicy.Secondary.RequiredExtJwtSigner {
return nil, apierror.NewInvalidAuth()
}
externalJwtSignerId = extJwt.Id
}
result := &AuthResultJwt{
AuthResultBase: AuthResultBase{
authPolicyId: authPolicy.Id,
authPolicy: authPolicy,
env: a.env,
},
externalJwtSignerId: externalJwtSignerId,
externalJwtSigner: extJwt,
}
if extJwt.UseExternalId {
result.externalId = identityId
} else {
result.identityId = identityId
}
return result, nil
}
logger.Error("authorization failed, jwt did not verify")
return nil, apierror.NewInvalidAuth()
}
func (a *AuthModuleExtJwt) getKnownSignerRecords() map[string]interface{} {
return a.signers.Items()
}
func (a *AuthModuleExtJwt) onExternalSignerCreate(args ...interface{}) {
signer, ok := args[0].(*persistence.ExternalJwtSigner)
if !ok {
pfxlog.Logger().Errorf("error on external signature create for authentication module %T: expected %T got %T", a, signer, args[0])
return
}
a.addSigner(signer)
}
func (a *AuthModuleExtJwt) onExternalSignerUpdate(args ...interface{}) {
signer, ok := args[0].(*persistence.ExternalJwtSigner)
if !ok {
pfxlog.Logger().Errorf("error on external signature update for authentication module %T: expected %T got %T", a, signer, args[0])
return
}
//read on update because patches can pass partial data
err := a.env.GetDbProvider().GetDb().View(func(tx *bbolt.Tx) error {
var err error
signer, err = a.env.GetStores().ExternalJwtSigner.LoadOneById(tx, signer.Id)
return err
})
if err != nil {
pfxlog.Logger().Errorf("error on external signature update for authentication module %T: could not read entity: %v", a, err)
}
a.addSigner(signer)
}
func (a *AuthModuleExtJwt) addSigner(signer *persistence.ExternalJwtSigner) {
certs := nfPem.PemStringToCertificates(signer.CertPem)
if len(certs) > 0 {
a.signers.Set(signer.Kid, &signerRecord{
externalJwtSigner: signer,
cert: certs[0],
})
} else {
pfxlog.Logger().Error("could not add signer, PEM did not parse to any certificates")
}
}
func (a *AuthModuleExtJwt) onExternalSignerDelete(args ...interface{}) {
signer, ok := args[0].(*persistence.ExternalJwtSigner)
if !ok {
pfxlog.Logger().Errorf("error on external signature update for authentication module %T: expected %T got %T", a, signer, args[0])
return
}
a.signers.Remove(signer.Kid)
}
func (a *AuthModuleExtJwt) loadExistingSigners() {
err := a.env.GetDbProvider().GetDb().View(func(tx *bbolt.Tx) error {
ids, _, err := a.env.GetStores().ExternalJwtSigner.QueryIds(tx, "")
if err != nil {
return err
}
for _, id := range ids {
signer, err := a.env.GetStores().ExternalJwtSigner.LoadOneById(tx, id)
if err != nil {
return err
}
a.addSigner(signer)
}
return nil
})
if err != nil {
pfxlog.Logger().Errorf("error loading external jwt signers: %v", err)
}
}