Files
ziti/controller/persistence/authenticator_store.go
T
Andrew Martinez c8e2729f05 GH-18 removes ziti-edge/edge folder and moves all contents up 1 row
- Updates .gitignore files
- Fixes imports
2019-12-18 10:02:25 -05:00

201 lines
5.9 KiB
Go

/*
Copyright 2019 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 persistence
import (
"github.com/michaelquigley/pfxlog"
"github.com/netfoundry/ziti-foundation/storage/ast"
"github.com/netfoundry/ziti-foundation/storage/boltz"
"go.etcd.io/bbolt"
)
const (
FieldAuthenticatorMethod = "method"
FieldAuthenticatorIdentity = "identity"
FieldAuthenticatorCertFingerprint = "certFingerprint"
FieldAuthenticatorUpdbUsername = "updbUsername"
FieldAuthenticatorUpdbPassword = "updbPassword"
FieldAuthenticatorUpdbSalt = "updbSalt"
MethodAuthenticatorUpdb = "updb"
MethodAuthenticatorCert = "cert"
)
type AuthenticatorSubType interface {
Fingerprints() []string
}
type AuthenticatorCert struct {
Authenticator
Fingerprint string
}
func (entity *AuthenticatorCert) Fingerprints() []string {
return []string{entity.Fingerprint}
}
type AuthenticatorUpdb struct {
Authenticator
Username string
Password string
Salt string
}
func (entity *AuthenticatorUpdb) Fingerprints() []string {
return nil
}
type Authenticator struct {
BaseEdgeEntityImpl
Type string
IdentityId string
SubType AuthenticatorSubType
}
var authenticatorFieldMappings = map[string]string{FieldAuthenticatorIdentity: "identityId"}
func (entity *Authenticator) LoadValues(_ boltz.CrudStore, bucket *boltz.TypedBucket) {
entity.Type = bucket.GetStringOrError(FieldAuthenticatorMethod)
entity.IdentityId = bucket.GetStringOrError(FieldAuthenticatorIdentity)
if entity.Type == MethodAuthenticatorCert {
authCert := &AuthenticatorCert{}
authCert.Fingerprint = bucket.GetStringWithDefault(FieldAuthenticatorCertFingerprint, "")
entity.SubType = authCert
} else if entity.Type == MethodAuthenticatorUpdb {
authUpdb := &AuthenticatorUpdb{}
authUpdb.Username = bucket.GetStringWithDefault(FieldAuthenticatorUpdbUsername, "")
authUpdb.Password = bucket.GetStringWithDefault(FieldAuthenticatorUpdbPassword, "")
authUpdb.Salt = bucket.GetStringWithDefault(FieldAuthenticatorUpdbSalt, "")
entity.SubType = authUpdb
}
}
func (entity *Authenticator) SetValues(ctx *boltz.PersistContext) {
ctx.WithFieldOverrides(authenticatorFieldMappings)
entity.SetBaseValues(ctx)
ctx.SetString(FieldAuthenticatorMethod, entity.Type)
ctx.SetString(FieldAuthenticatorIdentity, entity.IdentityId)
if entity.Type == MethodAuthenticatorCert {
if authCert, ok := entity.SubType.(*AuthenticatorCert); ok {
ctx.SetString(FieldAuthenticatorCertFingerprint, authCert.Fingerprint)
} else {
pfxlog.Logger().Panic("type conversion error setting values for AuthenticatorCert")
}
} else if entity.Type == MethodAuthenticatorUpdb {
if authUpdb, ok := entity.SubType.(*AuthenticatorUpdb); ok {
ctx.SetString(FieldAuthenticatorUpdbPassword, authUpdb.Password)
ctx.SetString(FieldAuthenticatorUpdbUsername, authUpdb.Username)
ctx.SetString(FieldAuthenticatorUpdbSalt, authUpdb.Salt)
} else {
pfxlog.Logger().Panic("type conversion error setting values for AuthenticatorUpdb")
}
}
}
func (entity *Authenticator) GetEntityType() string {
return EntityTypeAuthenticators
}
func (entity *Authenticator) ToUpdb() *AuthenticatorUpdb {
if updb, ok := entity.SubType.(*AuthenticatorUpdb); ok {
updb.Authenticator = *entity
return updb
}
return nil
}
func (entity *Authenticator) ToCert() *AuthenticatorCert {
if cert, ok := entity.SubType.(*AuthenticatorCert); ok {
cert.Authenticator = *entity
return cert
}
return nil
}
func (entity *Authenticator) ToSubType() AuthenticatorSubType {
if entity.Type == MethodAuthenticatorCert {
return entity.ToCert()
}
if entity.Type == MethodAuthenticatorUpdb {
return entity.ToUpdb()
}
pfxlog.Logger().Panicf("unknown authenticator subtype %s", entity.Type)
return nil
}
type AuthenticatorStore interface {
Store
LoadOneById(tx *bbolt.Tx, id string) (*Authenticator, error)
LoadOneByQuery(tx *bbolt.Tx, query string) (*Authenticator, error)
}
func newAuthenticatorStore(stores *stores) *authenticatorStoreImpl {
store := &authenticatorStoreImpl{
baseStore: newBaseStore(stores, EntityTypeAuthenticators),
}
store.InitImpl(store)
return store
}
type authenticatorStoreImpl struct {
*baseStore
symbolIdentityId boltz.EntitySymbol
}
func (store *authenticatorStoreImpl) NewStoreEntity() boltz.BaseEntity {
return &Authenticator{}
}
func (store *authenticatorStoreImpl) initializeLocal() {
store.addBaseFields()
store.AddSymbol(FieldAuthenticatorMethod, ast.NodeTypeString)
store.AddSymbol(FieldAuthenticatorCertFingerprint, ast.NodeTypeString)
store.AddSymbol(FieldAuthenticatorUpdbUsername, ast.NodeTypeString)
store.AddSymbol(FieldAuthenticatorUpdbPassword, ast.NodeTypeString)
store.AddSymbol(FieldAuthenticatorUpdbSalt, ast.NodeTypeString)
store.symbolIdentityId = store.AddFkSymbol(FieldAuthenticatorIdentity, store.stores.identity)
}
func (store *authenticatorStoreImpl) initializeLinked() {
store.AddFkIndex(store.symbolIdentityId, store.stores.identity.symbolAuthenticators)
}
func (store *authenticatorStoreImpl) LoadOneById(tx *bbolt.Tx, id string) (*Authenticator, error) {
entity := &Authenticator{}
if err := store.baseLoadOneById(tx, id, entity); err != nil {
return nil, err
}
return entity, nil
}
func (store *authenticatorStoreImpl) LoadOneByQuery(tx *bbolt.Tx, query string) (*Authenticator, error) {
entity := &Authenticator{}
if found, err := store.BaseLoadOneByQuery(tx, query, entity); !found || err != nil {
return nil, err
}
return entity, nil
}