-
Notifications
You must be signed in to change notification settings - Fork 159
/
ca_store.go
186 lines (159 loc) · 7.76 KB
/
ca_store.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
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 db
import (
"github.com/openziti/storage/ast"
"github.com/openziti/storage/boltz"
)
const (
FieldCaFingerprint = "fingerprint"
FieldCaCertPem = "certPem"
FieldCaIsVerified = "isVerified"
FieldCaVerificationToken = "verificationToken"
FieldCaIsAutoCaEnrollmentEnabled = "isAutoCaEnrollmentEnabled"
FieldCaIsOttCaEnrollmentEnabled = "isOttCaEnrollmentEnabled"
FieldCaIsAuthEnabled = "isAuthEnabled"
FieldCaIdentityNameFormat = "identityNameFormat"
FieldCaEnrollments = "enrollments"
FieldCaExternalIdClaim = "externalIdClaim"
FieldCaExternalIdClaimLocation = "externalIdClaim.location"
FieldCaExternalIdClaimIndex = "externalIdClaim.index"
FieldCaExternalIdClaimMatcher = "externalIdClaim.matcher"
FieldCaExternalIdClaimMatcherCriteria = "externalIdClaim.matcherCriteria"
FieldCaExternalIdClaimParser = "externalIdClaim.parser"
FieldCaExternalIdClaimParserCriteria = "externalIdClaim.parserSeparator"
)
const (
ExternalIdClaimLocCommonName = "COMMON_NAME"
ExternalIdClaimLocSanUri = "SAN_URI"
ExternalIdClaimLocSanEmail = "SAN_EMAIL"
ExternalIdClaimMatcherAll = "ALL"
ExternalIdClaimMatcherSuffix = "SUFFIX"
ExternalIdClaimMatcherPrefix = "PREFIX"
ExternalIdClaimMatcherScheme = "SCHEME"
ExternalIdClaimParserNone = "NONE"
ExternalIdClaimParserSplit = "SPLIT"
)
type Ca struct {
boltz.BaseExtEntity
Name string `json:"name"`
Fingerprint string `json:"fingerprint"`
CertPem string `json:"certPem"`
IsVerified bool `json:"isVerified"`
VerificationToken string `json:"verificationToken"`
IsAutoCaEnrollmentEnabled bool `json:"isAutoCaEnrollmentEnabled"`
IsOttCaEnrollmentEnabled bool `json:"isOttCaEnrollmentEnabled"`
IsAuthEnabled bool `json:"isAuthEnabled"`
IdentityRoles []string `json:"identityRoles"`
IdentityNameFormat string `json:"identityNameFormat"`
ExternalIdClaim *ExternalIdClaim `json:"externalIdClaim"`
}
type ExternalIdClaim struct {
Location string `json:"location"`
Matcher string `json:"matcher"`
MatcherCriteria string `json:"matcherCriteria"`
Parser string `json:"parser"`
ParserCriteria string `json:"parserCriteria"`
Index int64 `json:"index"`
}
func (entity *Ca) GetName() string {
return entity.Name
}
func (entity *Ca) GetEntityType() string {
return EntityTypeCas
}
var _ CaStore = (*caStoreImpl)(nil)
type CaStore interface {
Store[*Ca]
}
func newCaStore(stores *stores) *caStoreImpl {
store := &caStoreImpl{}
store.baseStore = newBaseStore[*Ca](stores, store)
store.InitImpl(store)
return store
}
type caStoreImpl struct {
*baseStore[*Ca]
indexName boltz.ReadIndex
symbolEnrollments boltz.EntitySetSymbol
}
func (store *caStoreImpl) initializeLocal() {
store.AddExtEntitySymbols()
store.indexName = store.addUniqueNameField()
store.AddSymbol(FieldCaFingerprint, ast.NodeTypeString)
store.AddSymbol(FieldCaIsVerified, ast.NodeTypeBool)
store.AddSymbol(FieldCaVerificationToken, ast.NodeTypeString)
store.AddSymbol(FieldCaIsAutoCaEnrollmentEnabled, ast.NodeTypeBool)
store.AddSymbol(FieldCaIsOttCaEnrollmentEnabled, ast.NodeTypeBool)
store.AddSymbol(FieldCaIsAuthEnabled, ast.NodeTypeBool)
store.AddSetSymbol(FieldIdentityRoles, ast.NodeTypeString)
store.symbolEnrollments = store.AddFkSetSymbol(FieldCaEnrollments, store.stores.enrollment)
}
func (store *caStoreImpl) initializeLinked() {}
func (store *caStoreImpl) NewEntity() *Ca {
return &Ca{}
}
func (store *caStoreImpl) FillEntity(entity *Ca, bucket *boltz.TypedBucket) {
entity.LoadBaseValues(bucket)
entity.Name = bucket.GetStringOrError(FieldName)
entity.Fingerprint = bucket.GetStringOrError(FieldCaFingerprint)
entity.CertPem = bucket.GetStringOrError(FieldCaCertPem)
entity.IsVerified = bucket.GetBoolWithDefault(FieldCaIsVerified, false)
entity.VerificationToken = bucket.GetStringOrError(FieldCaVerificationToken)
entity.IsAutoCaEnrollmentEnabled = bucket.GetBoolWithDefault(FieldCaIsAutoCaEnrollmentEnabled, false)
entity.IsOttCaEnrollmentEnabled = bucket.GetBoolWithDefault(FieldCaIsOttCaEnrollmentEnabled, false)
entity.IsAuthEnabled = bucket.GetBoolWithDefault(FieldCaIsAuthEnabled, false)
entity.IdentityRoles = bucket.GetStringList(FieldIdentityRoles)
entity.IdentityNameFormat = bucket.GetStringWithDefault(FieldCaIdentityNameFormat, "")
if externalField := bucket.GetBucket(FieldCaExternalIdClaim); externalField != nil {
entity.ExternalIdClaim = &ExternalIdClaim{}
entity.ExternalIdClaim.Location = externalField.GetStringWithDefault(FieldCaExternalIdClaimLocation, "")
entity.ExternalIdClaim.Matcher = externalField.GetStringWithDefault(FieldCaExternalIdClaimMatcher, "")
entity.ExternalIdClaim.MatcherCriteria = externalField.GetStringWithDefault(FieldCaExternalIdClaimMatcherCriteria, "")
entity.ExternalIdClaim.Parser = externalField.GetStringWithDefault(FieldCaExternalIdClaimParser, "")
entity.ExternalIdClaim.ParserCriteria = externalField.GetStringWithDefault(FieldCaExternalIdClaimParserCriteria, "")
entity.ExternalIdClaim.Index = externalField.GetInt64WithDefault(FieldCaExternalIdClaimIndex, 0)
}
}
func (store *caStoreImpl) PersistEntity(entity *Ca, ctx *boltz.PersistContext) {
entity.SetBaseValues(ctx)
ctx.SetString(FieldName, entity.Name)
ctx.SetString(FieldCaFingerprint, entity.Fingerprint)
ctx.SetString(FieldCaCertPem, entity.CertPem)
ctx.SetBool(FieldCaIsVerified, entity.IsVerified)
ctx.SetString(FieldCaVerificationToken, entity.VerificationToken)
ctx.SetBool(FieldCaIsAutoCaEnrollmentEnabled, entity.IsAutoCaEnrollmentEnabled)
ctx.SetBool(FieldCaIsOttCaEnrollmentEnabled, entity.IsOttCaEnrollmentEnabled)
ctx.SetBool(FieldCaIsAuthEnabled, entity.IsAuthEnabled)
ctx.SetStringList(FieldIdentityRoles, entity.IdentityRoles)
ctx.SetString(FieldCaIdentityNameFormat, entity.IdentityNameFormat)
if entity.ExternalIdClaim != nil {
externalField := ctx.Bucket.GetOrCreateBucket(FieldCaExternalIdClaim)
externalField.SetString(FieldCaExternalIdClaimLocation, entity.ExternalIdClaim.Location, ctx.FieldChecker)
externalField.SetString(FieldCaExternalIdClaimMatcher, entity.ExternalIdClaim.Matcher, ctx.FieldChecker)
externalField.SetString(FieldCaExternalIdClaimMatcherCriteria, entity.ExternalIdClaim.MatcherCriteria, ctx.FieldChecker)
externalField.SetString(FieldCaExternalIdClaimParser, entity.ExternalIdClaim.Parser, ctx.FieldChecker)
externalField.SetString(FieldCaExternalIdClaimParserCriteria, entity.ExternalIdClaim.ParserCriteria, ctx.FieldChecker)
externalField.SetInt64(FieldCaExternalIdClaimIndex, entity.ExternalIdClaim.Index, ctx.FieldChecker)
} else {
_ = ctx.Bucket.DeleteBucket([]byte(FieldCaExternalIdClaim))
}
}
func (store *caStoreImpl) DeleteById(ctx boltz.MutateContext, id string) error {
for _, enrollmentId := range store.GetRelatedEntitiesIdList(ctx.Tx(), id, FieldCaEnrollments) {
if err := store.stores.enrollment.DeleteById(ctx, enrollmentId); err != nil {
return err
}
}
return store.baseStore.DeleteById(ctx, id)
}