This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
session_model.go
159 lines (136 loc) · 4.95 KB
/
session_model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
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 (
"github.com/michaelquigley/pfxlog"
"github.com/netfoundry/ziti-edge/controller/apierror"
"github.com/netfoundry/ziti-edge/controller/persistence"
"github.com/netfoundry/ziti-fabric/controller/models"
"github.com/netfoundry/ziti-foundation/storage/boltz"
"github.com/netfoundry/ziti-foundation/util/stringz"
"github.com/netfoundry/ziti-foundation/validation"
"github.com/pkg/errors"
"go.etcd.io/bbolt"
"reflect"
"time"
)
type Session struct {
models.BaseEntity
Token string
ApiSessionId string
ServiceId string
Type string
SessionCerts []*SessionCert
}
type SessionCert struct {
Cert string
Fingerprint string
ValidFrom time.Time
ValidTo time.Time
}
func (entity *Session) toBoltEntityForCreate(tx *bbolt.Tx, handler Handler) (boltz.Entity, error) {
apiSession, err := handler.GetEnv().GetStores().ApiSession.LoadOneById(tx, entity.ApiSessionId)
if err != nil {
return nil, err
}
if apiSession == nil {
return nil, validation.NewFieldError("api session not found", "ApiSessionId", entity.ApiSessionId)
}
service, err := handler.GetEnv().GetHandlers().EdgeService.ReadForIdentity(entity.ServiceId, apiSession.IdentityId, nil)
if err != nil {
return nil, err
}
if persistence.SessionTypeDial == entity.Type && !stringz.Contains(service.Permissions, persistence.PolicyTypeDialName) {
return nil, validation.NewFieldError("service not found", "ServiceId", entity.ServiceId)
}
if persistence.SessionTypeBind == entity.Type && !stringz.Contains(service.Permissions, persistence.PolicyTypeBindName) {
return nil, validation.NewFieldError("service not found", "ServiceId", entity.ServiceId)
}
maxRows := 1
result, err := handler.GetEnv().GetHandlers().EdgeRouter.ListForIdentityAndServiceWithTx(tx, apiSession.IdentityId, entity.ServiceId, &maxRows)
if err != nil {
return nil, err
}
if result.Count < 1 {
return nil, apierror.NewNoEdgeRoutersAvailable()
}
boltEntity := &persistence.Session{
BaseExtEntity: *boltz.NewExtEntity(entity.Id, entity.Tags),
Token: entity.Token,
ApiSessionId: entity.ApiSessionId,
ServiceId: entity.ServiceId,
Type: entity.Type,
}
identity, err := handler.GetEnv().GetStores().Identity.LoadOneById(tx, apiSession.IdentityId)
if err != nil {
return nil, err
}
fingerprints := map[string]bool{}
for _, authenticatorId := range identity.Authenticators {
authPrints, err := handler.GetEnv().GetHandlers().Authenticator.ReadFingerprints(authenticatorId)
if err != nil {
pfxlog.Logger().Errorf("encountered error retrieving fingerprints for authenticator [%s]", authenticatorId)
continue
}
for _, fingerprint := range authPrints {
fingerprints[fingerprint] = true
}
}
for fingerprint := range fingerprints {
validFrom := time.Now()
validTo := time.Now().AddDate(1, 0, 0)
cert := "unknown"
boltEntity.Certs = append(boltEntity.Certs, &persistence.SessionCert{
Cert: cert,
Fingerprint: fingerprint,
ValidFrom: validFrom,
ValidTo: validTo,
})
}
return boltEntity, nil
}
func (entity *Session) toBoltEntityForUpdate(*bbolt.Tx, Handler) (boltz.Entity, error) {
return &persistence.Session{
BaseExtEntity: *boltz.NewExtEntity(entity.Id, entity.Tags),
Token: entity.Token,
ApiSessionId: entity.ApiSessionId,
ServiceId: entity.ServiceId,
Type: entity.Type,
}, nil
}
func (entity *Session) toBoltEntityForPatch(tx *bbolt.Tx, handler Handler) (boltz.Entity, error) {
return entity.toBoltEntityForUpdate(tx, handler)
}
func (entity *Session) fillFrom(_ Handler, _ *bbolt.Tx, boltEntity boltz.Entity) error {
boltSession, ok := boltEntity.(*persistence.Session)
if !ok {
return errors.Errorf("unexpected type %v when filling model Session", reflect.TypeOf(boltEntity))
}
entity.FillCommon(boltSession)
entity.Token = boltSession.Token
entity.ApiSessionId = boltSession.ApiSessionId
entity.ServiceId = boltSession.ServiceId
entity.Type = boltSession.Type
return nil
}
func (entity *SessionCert) FillFrom(_ Handler, _ *bbolt.Tx, boltEntity boltz.Entity) error {
boltSessionCert, ok := boltEntity.(*persistence.SessionCert)
if !ok {
return errors.Errorf("unexpected type %v when filling model SessionCert", reflect.TypeOf(boltEntity))
}
entity.Fingerprint = boltSessionCert.Fingerprint
entity.Cert = boltSessionCert.Cert
entity.ValidFrom = boltSessionCert.ValidFrom
entity.ValidTo = boltSessionCert.ValidTo
return nil
}