Files
ziti/controller/model/api_session_certificate_model.go
T
Andrew Martinez 5dc8a66fe9 addresses #1226 api session certificate issues w/ 3rd party cas
- events during a multi-step transaction were immediately emitting events
  causing out of order and incomplete events to be received,
  specifically API Sessions w/o fingerprints
- API Session Certificates created implicitly during 3rd Party CA auth
  did not properly set all properties
- adds tests
2022-11-04 14:40:07 -04:00

107 lines
3.4 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/openziti/edge/controller/persistence"
"github.com/openziti/fabric/controller/models"
"github.com/openziti/foundation/v2/errorz"
nfpem "github.com/openziti/foundation/v2/pem"
"github.com/openziti/storage/boltz"
"github.com/pkg/errors"
"go.etcd.io/bbolt"
"reflect"
"time"
)
type ApiSessionCertificate struct {
models.BaseEntity
ApiSession *ApiSession
ApiSessionId string
Subject string
Fingerprint string
ValidAfter *time.Time
ValidBefore *time.Time
PEM string
}
func NewApiSessionCertificate(cert *x509.Certificate) *ApiSessionCertificate {
ret := &ApiSessionCertificate{
Subject: cert.Subject.String(),
ValidAfter: &cert.NotBefore,
ValidBefore: &cert.NotAfter,
}
ret.Fingerprint = nfpem.FingerprintFromCertificate(cert)
ret.PEM = nfpem.EncodeToString(cert)
return ret
}
func (entity *ApiSessionCertificate) toBoltEntity(tx *bbolt.Tx, manager EntityManager) (boltz.Entity, error) {
if !manager.GetEnv().GetStores().ApiSession.IsEntityPresent(tx, entity.ApiSessionId) {
return nil, errorz.NewFieldError("api session not found", "ApiSessionId", entity.ApiSessionId)
}
boltEntity := &persistence.ApiSessionCertificate{
BaseExtEntity: *boltz.NewExtEntity(entity.Id, entity.Tags),
ApiSessionId: entity.ApiSessionId,
Subject: entity.Subject,
Fingerprint: entity.Fingerprint,
ValidAfter: entity.ValidAfter,
ValidBefore: entity.ValidBefore,
PEM: entity.PEM,
}
return boltEntity, nil
}
func (entity *ApiSessionCertificate) toBoltEntityForCreate(tx *bbolt.Tx, manager EntityManager) (boltz.Entity, error) {
return entity.toBoltEntity(tx, manager)
}
func (entity *ApiSessionCertificate) toBoltEntityForUpdate(tx *bbolt.Tx, manager EntityManager, checker boltz.FieldChecker) (boltz.Entity, error) {
return entity.toBoltEntity(tx, manager)
}
func (entity *ApiSessionCertificate) fillFrom(manager EntityManager, tx *bbolt.Tx, boltEntity boltz.Entity) error {
boltApiSessionCertificate, ok := boltEntity.(*persistence.ApiSessionCertificate)
if !ok {
return errors.Errorf("unexpected type %v when filling model ApiSessionCertificate", reflect.TypeOf(boltEntity))
}
entity.FillCommon(boltApiSessionCertificate)
entity.Subject = boltApiSessionCertificate.Subject
entity.Fingerprint = boltApiSessionCertificate.Fingerprint
entity.ValidAfter = boltApiSessionCertificate.ValidAfter
entity.ValidBefore = boltApiSessionCertificate.ValidBefore
entity.PEM = boltApiSessionCertificate.PEM
entity.ApiSessionId = boltApiSessionCertificate.ApiSessionId
boltApiSession, err := manager.GetEnv().GetStores().ApiSession.LoadOneById(tx, boltApiSessionCertificate.ApiSessionId)
if err != nil {
return err
}
modelApiSession := &ApiSession{}
if err := modelApiSession.fillFrom(manager, tx, boltApiSession); err != nil {
return err
}
entity.ApiSession = modelApiSession
return nil
}