@@ -11,17 +11,64 @@ import (
11
11
"github.com/smallstep/nosql"
12
12
)
13
13
14
+ type dbX509Policy struct {
15
+ Allow * dbX509Names `json:"allow,omitempty"`
16
+ Deny * dbX509Names `json:"deny,omitempty"`
17
+ AllowWildcardNames bool `json:"allow_wildcard_names,omitempty"`
18
+ }
19
+
20
+ type dbX509Names struct {
21
+ CommonNames []string `json:"cn,omitempty"`
22
+ DNSDomains []string `json:"dns,omitempty"`
23
+ IPRanges []string `json:"ip,omitempty"`
24
+ EmailAddresses []string `json:"email,omitempty"`
25
+ URIDomains []string `json:"uri,omitempty"`
26
+ }
27
+
28
+ type dbSSHPolicy struct {
29
+ // User contains SSH user certificate options.
30
+ User * dbSSHUserPolicy `json:"user,omitempty"`
31
+ // Host contains SSH host certificate options.
32
+ Host * dbSSHHostPolicy `json:"host,omitempty"`
33
+ }
34
+
35
+ type dbSSHHostPolicy struct {
36
+ Allow * dbSSHHostNames `json:"allow,omitempty"`
37
+ Deny * dbSSHHostNames `json:"deny,omitempty"`
38
+ }
39
+
40
+ type dbSSHHostNames struct {
41
+ DNSDomains []string `json:"dns,omitempty"`
42
+ IPRanges []string `json:"ip,omitempty"`
43
+ Principals []string `json:"principal,omitempty"`
44
+ }
45
+
46
+ type dbSSHUserPolicy struct {
47
+ Allow * dbSSHUserNames `json:"allow,omitempty"`
48
+ Deny * dbSSHUserNames `json:"deny,omitempty"`
49
+ }
50
+
51
+ type dbSSHUserNames struct {
52
+ EmailAddresses []string `json:"email,omitempty"`
53
+ Principals []string `json:"principal,omitempty"`
54
+ }
55
+
56
+ type dbPolicy struct {
57
+ X509 * dbX509Policy `json:"x509,omitempty"`
58
+ SSH * dbSSHPolicy `json:"ssh,omitempty"`
59
+ }
60
+
14
61
type dbAuthorityPolicy struct {
15
- ID string `json:"id"`
16
- AuthorityID string `json:"authorityID"`
17
- Policy * linkedca. Policy `json:"policy"`
62
+ ID string `json:"id"`
63
+ AuthorityID string `json:"authorityID"`
64
+ Policy * dbPolicy `json:"policy,omitempty "`
18
65
}
19
66
20
67
func (dbap * dbAuthorityPolicy ) convert () * linkedca.Policy {
21
68
if dbap == nil {
22
69
return nil
23
70
}
24
- return dbap .Policy
71
+ return dbToLinked ( dbap .Policy )
25
72
}
26
73
27
74
func (db * DB ) getDBAuthorityPolicyBytes (ctx context.Context , authorityID string ) ([]byte , error ) {
@@ -69,7 +116,7 @@ func (db *DB) CreateAuthorityPolicy(ctx context.Context, policy *linkedca.Policy
69
116
dbap := & dbAuthorityPolicy {
70
117
ID : db .authorityID ,
71
118
AuthorityID : db .authorityID ,
72
- Policy : policy ,
119
+ Policy : linkedToDB ( policy ) ,
73
120
}
74
121
75
122
if err := db .save (ctx , dbap .ID , dbap , nil , "authority_policy" , authorityPoliciesTable ); err != nil {
@@ -97,7 +144,7 @@ func (db *DB) UpdateAuthorityPolicy(ctx context.Context, policy *linkedca.Policy
97
144
dbap := & dbAuthorityPolicy {
98
145
ID : db .authorityID ,
99
146
AuthorityID : db .authorityID ,
100
- Policy : policy ,
147
+ Policy : linkedToDB ( policy ) ,
101
148
}
102
149
103
150
if err := db .save (ctx , dbap .ID , dbap , old , "authority_policy" , authorityPoliciesTable ); err != nil {
@@ -119,3 +166,174 @@ func (db *DB) DeleteAuthorityPolicy(ctx context.Context) error {
119
166
120
167
return nil
121
168
}
169
+
170
+ func dbToLinked (p * dbPolicy ) * linkedca.Policy {
171
+ if p == nil {
172
+ return nil
173
+ }
174
+ r := & linkedca.Policy {}
175
+ if x509 := p .X509 ; x509 != nil {
176
+ r .X509 = & linkedca.X509Policy {}
177
+ if allow := x509 .Allow ; allow != nil {
178
+ r .X509 .Allow = & linkedca.X509Names {}
179
+ r .X509 .Allow .Dns = allow .DNSDomains
180
+ r .X509 .Allow .Emails = allow .EmailAddresses
181
+ r .X509 .Allow .Ips = allow .IPRanges
182
+ r .X509 .Allow .Uris = allow .URIDomains
183
+ r .X509 .Allow .CommonNames = allow .CommonNames
184
+ }
185
+ if deny := x509 .Deny ; deny != nil {
186
+ r .X509 .Deny = & linkedca.X509Names {}
187
+ r .X509 .Deny .Dns = deny .DNSDomains
188
+ r .X509 .Deny .Emails = deny .EmailAddresses
189
+ r .X509 .Deny .Ips = deny .IPRanges
190
+ r .X509 .Deny .Uris = deny .URIDomains
191
+ r .X509 .Deny .CommonNames = deny .CommonNames
192
+ }
193
+ r .X509 .AllowWildcardNames = x509 .AllowWildcardNames
194
+ }
195
+ if ssh := p .SSH ; ssh != nil {
196
+ r .Ssh = & linkedca.SSHPolicy {}
197
+ if host := ssh .Host ; host != nil {
198
+ r .Ssh .Host = & linkedca.SSHHostPolicy {}
199
+ if allow := host .Allow ; allow != nil {
200
+ r .Ssh .Host .Allow = & linkedca.SSHHostNames {}
201
+ r .Ssh .Host .Allow .Dns = allow .DNSDomains
202
+ r .Ssh .Host .Allow .Ips = allow .IPRanges
203
+ r .Ssh .Host .Allow .Principals = allow .Principals
204
+ }
205
+ if deny := host .Deny ; deny != nil {
206
+ r .Ssh .Host .Deny = & linkedca.SSHHostNames {}
207
+ r .Ssh .Host .Deny .Dns = deny .DNSDomains
208
+ r .Ssh .Host .Deny .Ips = deny .IPRanges
209
+ r .Ssh .Host .Deny .Principals = deny .Principals
210
+ }
211
+ }
212
+ if user := ssh .User ; user != nil {
213
+ r .Ssh .User = & linkedca.SSHUserPolicy {}
214
+ if allow := user .Allow ; allow != nil {
215
+ r .Ssh .User .Allow = & linkedca.SSHUserNames {}
216
+ r .Ssh .User .Allow .Emails = allow .EmailAddresses
217
+ r .Ssh .User .Allow .Principals = allow .Principals
218
+ }
219
+ if deny := user .Deny ; deny != nil {
220
+ r .Ssh .User .Deny = & linkedca.SSHUserNames {}
221
+ r .Ssh .User .Deny .Emails = deny .EmailAddresses
222
+ r .Ssh .User .Deny .Principals = deny .Principals
223
+ }
224
+ }
225
+ }
226
+
227
+ return r
228
+ }
229
+
230
+ func linkedToDB (p * linkedca.Policy ) * dbPolicy {
231
+
232
+ if p == nil {
233
+ return nil
234
+ }
235
+
236
+ // return early if x509 nor SSH is set
237
+ if p .GetX509 () == nil && p .GetSsh () == nil {
238
+ return nil
239
+ }
240
+
241
+ r := & dbPolicy {}
242
+ // fill x509 policy configuration
243
+ if x509 := p .GetX509 (); x509 != nil {
244
+ r .X509 = & dbX509Policy {}
245
+ if allow := x509 .GetAllow (); allow != nil {
246
+ r .X509 .Allow = & dbX509Names {}
247
+ if allow .Dns != nil {
248
+ r .X509 .Allow .DNSDomains = allow .Dns
249
+ }
250
+ if allow .Ips != nil {
251
+ r .X509 .Allow .IPRanges = allow .Ips
252
+ }
253
+ if allow .Emails != nil {
254
+ r .X509 .Allow .EmailAddresses = allow .Emails
255
+ }
256
+ if allow .Uris != nil {
257
+ r .X509 .Allow .URIDomains = allow .Uris
258
+ }
259
+ if allow .CommonNames != nil {
260
+ r .X509 .Allow .CommonNames = allow .CommonNames
261
+ }
262
+ }
263
+ if deny := x509 .GetDeny (); deny != nil {
264
+ r .X509 .Deny = & dbX509Names {}
265
+ if deny .Dns != nil {
266
+ r .X509 .Deny .DNSDomains = deny .Dns
267
+ }
268
+ if deny .Ips != nil {
269
+ r .X509 .Deny .IPRanges = deny .Ips
270
+ }
271
+ if deny .Emails != nil {
272
+ r .X509 .Deny .EmailAddresses = deny .Emails
273
+ }
274
+ if deny .Uris != nil {
275
+ r .X509 .Deny .URIDomains = deny .Uris
276
+ }
277
+ if deny .CommonNames != nil {
278
+ r .X509 .Deny .CommonNames = deny .CommonNames
279
+ }
280
+ }
281
+
282
+ r .X509 .AllowWildcardNames = x509 .GetAllowWildcardNames ()
283
+ }
284
+
285
+ // fill ssh policy configuration
286
+ if ssh := p .GetSsh (); ssh != nil {
287
+ r .SSH = & dbSSHPolicy {}
288
+ if host := ssh .GetHost (); host != nil {
289
+ r .SSH .Host = & dbSSHHostPolicy {}
290
+ if allow := host .GetAllow (); allow != nil {
291
+ r .SSH .Host .Allow = & dbSSHHostNames {}
292
+ if allow .Dns != nil {
293
+ r .SSH .Host .Allow .DNSDomains = allow .Dns
294
+ }
295
+ if allow .Ips != nil {
296
+ r .SSH .Host .Allow .IPRanges = allow .Ips
297
+ }
298
+ if allow .Principals != nil {
299
+ r .SSH .Host .Allow .Principals = allow .Principals
300
+ }
301
+ }
302
+ if deny := host .GetDeny (); deny != nil {
303
+ r .SSH .Host .Deny = & dbSSHHostNames {}
304
+ if deny .Dns != nil {
305
+ r .SSH .Host .Deny .DNSDomains = deny .Dns
306
+ }
307
+ if deny .Ips != nil {
308
+ r .SSH .Host .Deny .IPRanges = deny .Ips
309
+ }
310
+ if deny .Principals != nil {
311
+ r .SSH .Host .Deny .Principals = deny .Principals
312
+ }
313
+ }
314
+ }
315
+ if user := ssh .GetUser (); user != nil {
316
+ r .SSH .User = & dbSSHUserPolicy {}
317
+ if allow := user .GetAllow (); allow != nil {
318
+ r .SSH .User .Allow = & dbSSHUserNames {}
319
+ if allow .Emails != nil {
320
+ r .SSH .User .Allow .EmailAddresses = allow .Emails
321
+ }
322
+ if allow .Principals != nil {
323
+ r .SSH .User .Allow .Principals = allow .Principals
324
+ }
325
+ }
326
+ if deny := user .GetDeny (); deny != nil {
327
+ r .SSH .User .Deny = & dbSSHUserNames {}
328
+ if deny .Emails != nil {
329
+ r .SSH .User .Deny .EmailAddresses = deny .Emails
330
+ }
331
+ if deny .Principals != nil {
332
+ r .SSH .User .Deny .Principals = deny .Principals
333
+ }
334
+ }
335
+ }
336
+ }
337
+
338
+ return r
339
+ }
0 commit comments