-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathentity.go
More file actions
451 lines (433 loc) · 12.5 KB
/
Copy pathentity.go
File metadata and controls
451 lines (433 loc) · 12.5 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package schema
import (
"entgo.io/contrib/entgql"
"entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
"github.com/gertd/go-pluralize"
"github.com/theopenlane/entx"
"github.com/theopenlane/entx/accessmap"
"github.com/theopenlane/iam/entfga"
"github.com/theopenlane/core/common/enums"
"github.com/theopenlane/core/common/models"
"github.com/theopenlane/core/internal/ent/generated"
"github.com/theopenlane/core/internal/ent/hooks"
"github.com/theopenlane/core/internal/ent/mixin"
"github.com/theopenlane/core/internal/ent/privacy/policy"
"github.com/theopenlane/core/internal/ent/privacy/rule"
"github.com/theopenlane/core/internal/ent/validator"
)
// Entity holds the schema definition for the Entity entity
type Entity struct {
SchemaFuncs
ent.Schema
}
// SchemaEntity is the name of the Entity schema.
const SchemaEntity = "entity"
// Name returns the name of the Entity schema.
func (Entity) Name() string {
return SchemaEntity
}
// GetType returns the type of the Entity schema.
func (Entity) GetType() any {
return Entity.Type
}
// PluralName returns the plural name of the Entity schema.
func (Entity) PluralName() string {
return pluralize.NewClient().Plural(SchemaEntity)
}
// Fields of the Entity
func (Entity) Fields() []ent.Field {
return []ent.Field{
field.String("name").
Comment("the name of the entity").
Optional().
SchemaType(map[string]string{
dialect.Postgres: "citext",
}).
MinLen(minNameLength).
Validate(validator.SpecialCharValidator).
Annotations(
entx.FieldSearchable(),
entgql.OrderField("name"),
entx.IntegrationMappingField().UpsertKey(),
),
field.String("display_name").
Comment("The entity's displayed 'friendly' name").
MaxLen(nameMaxLen).
Optional().
NotEmpty().
Annotations(
entx.FieldSearchable(),
entgql.OrderField("display_name"),
),
field.String("description").
Comment("An optional description of the entity").
Optional().
Annotations(
entx.FieldSearchable(),
entx.IntegrationMappingField(),
),
field.Strings("domains").
Comment("domains associated with the entity").
Validate(validator.ValidateDomains()).
Optional(),
field.String("entity_type_id").
Comment("The type of the entity").
Optional(),
field.Enum("status").
Comment("status of the entity").
GoType(enums.EntityStatus("")).
Default(enums.EntityStatusActive.String()).
Annotations(
entgql.OrderField("status"),
).
Optional(),
field.Bool("approved_for_use").
Comment("whether the entity is approved for use").
Default(false).
Optional().
Annotations(
entgql.OrderField("approved_for_use"),
),
field.Strings("linked_asset_ids").
Comment("asset identifiers linked to the entity").
Default([]string{}).
Optional(),
field.Bool("has_soc2").
Comment("whether the entity has an active SOC 2 report").
Default(false).
Optional().
Annotations(
entgql.OrderField("has_soc2"),
),
field.Time("soc2_period_end").
Comment("SOC 2 reporting period end date").
GoType(models.DateTime{}).
Optional().
Nillable().
Annotations(
entgql.OrderField("soc2_period_end"),
),
field.Time("contract_start_date").
Comment("start date for the entity contract").
GoType(models.DateTime{}).
Optional().
Nillable().
Annotations(
entgql.OrderField("contract_start_date"),
),
field.Time("contract_end_date").
Comment("end date for the entity contract").
GoType(models.DateTime{}).
Optional().
Nillable().
Annotations(
entgql.OrderField("contract_end_date"),
),
field.Bool("auto_renews").
Comment("whether the contract auto-renews").
Default(false).
Optional().
Annotations(
entgql.OrderField("auto_renews"),
),
field.Int("termination_notice_days").
Comment("number of days required for termination notice").
Optional().
Annotations(
entgql.OrderField("termination_notice_days"),
),
field.Float("annual_spend").
Comment("annual spend associated with the entity").
Optional().
Annotations(
entgql.OrderField("annual_spend"),
),
field.String("spend_currency").
Comment("the currency of the annual spend").
Default("USD").
Optional().
Annotations(
entgql.OrderField("spend_currency"),
),
field.String("billing_model").
Comment("billing model for the entity relationship").
Optional().
Annotations(
entgql.OrderField("billing_model"),
),
field.String("renewal_risk").
Comment("renewal risk rating for the entity").
Optional().
Annotations(
entgql.OrderField("renewal_risk"),
),
field.Bool("sso_enforced").
Comment("whether SSO is enforced for the entity").
Default(false).
Optional().
Annotations(
entgql.OrderField("sso_enforced"),
),
field.Bool("mfa_supported").
Comment("whether MFA is supported by the entity").
Default(false).
Optional().
Annotations(
entgql.OrderField("mfa_supported"),
),
field.Bool("mfa_enforced").
Comment("whether MFA is enforced by the entity").
Default(false).
Optional().
Annotations(
entgql.OrderField("mfa_enforced"),
),
field.String("status_page_url").
Comment("status page URL for the entity").
Validate(func(u string) error {
if u == "" {
return nil
}
return validator.ValidateURL()(u)
}).
Optional().
Annotations(
entgql.OrderField("status_page_url"),
),
field.Strings("provided_services").
Comment("services provided by the entity").
Optional().
Default([]string{}),
field.Strings("links").
Comment("external links associated with the entity").
Validate(validator.ValidateURLs()).
Optional().
Default([]string{}),
field.String("risk_rating").
Comment("the risk rating label for the entity").
Optional().
Annotations(
entgql.OrderField("risk_rating"),
),
field.Int("risk_score").
Comment("the risk score for the entity").
Optional().
Annotations(
entgql.OrderField("risk_score"),
),
field.Int("risk_score_coverage").
Comment("number of scoring questions answered for the current risk score; used to contextualize partial assessments").
Optional().
Annotations(
entgql.OrderField("risk_score_coverage"),
entgql.Skip(entgql.SkipMutationCreateInput, entgql.SkipMutationUpdateInput),
),
field.Enum("tier").
Comment("the vendor risk tier classification, used to determine the depth of TPRM assessment required").
GoType(enums.VendorTier("")).
Default(enums.VendorTierStandard.String()).
Optional().
Default(enums.VendorRiskImpactLow.String()).
Annotations(
entgql.OrderField("tier"),
),
field.Enum("review_frequency").
Comment("the cadence for reviewing the entity").
GoType(enums.Frequency("")).
Default(enums.FrequencyYearly.String()).
Optional().
Annotations(
entgql.OrderField("REVIEW_FREQUENCY"),
),
field.Time("next_review_at").
Comment("when the entity is due for review").
GoType(models.DateTime{}).
Optional().
Nillable().
Annotations(
entgql.OrderField("next_review_at"),
),
field.Time("contract_renewal_at").
Comment("when the entity contract is up for renewal").
GoType(models.DateTime{}).
Optional().
Nillable().
Annotations(
entgql.OrderField("contract_renewal_at"),
),
field.JSON("vendor_metadata", map[string]any{}).
Comment("vendor metadata such as additional enrichment info, company size, public, etc.").
Optional(),
field.String("logo_remote_url").
Comment("URL of the logo for the entity").
MaxLen(urlMaxLen).
Validate(validator.ValidateURL()).
Optional().
Nillable(),
field.String("logo_file_id").
Comment("The logo file id for the entity").
Optional().
Annotations(
entgql.Skip(entgql.SkipMutationCreateInput, entgql.SkipMutationUpdateInput),
).
Nillable(),
field.String("external_id").
Comment("stable identifier assigned by the source system, used for integration ingest deduplication").
Optional().
Annotations(
entgql.OrderField("external_id"),
entx.IntegrationMappingField().UpsertKey().LookupKey(),
),
field.Time("observed_at").
Comment("time when this entity was last observed by the source integration").
GoType(models.DateTime{}).
Optional().
Nillable().
Annotations(
entgql.OrderField("observed_at"),
),
}
}
// Mixin of the Entity
func (e Entity) Mixin() []ent.Mixin {
return mixinConfig{
additionalMixins: []ent.Mixin{
newObjectOwnedMixin[generated.Entity](e,
withParents(TrustCenterEntity{}, Platform{}, SystemDetail{}),
withOrganizationOwner(),
withSkipForSystemAdmin(),
),
newGroupPermissionsMixin(),
newResponsibilityMixin(e, withInternalOwner(), withReviewedBy(), withLastReviewedAt(), withReviewedByOrderField()),
mixin.NewSystemOwnedMixin(),
newCustomEnumMixin(e, withEnumFieldName("relationship_state")),
newCustomEnumMixin(e, withEnumFieldName("security_questionnaire_status")),
newCustomEnumMixin(e, withEnumFieldName("source_type")),
newCustomEnumMixin(e, withEnumFieldName("environment"), withGlobalEnum()),
newCustomEnumMixin(e, withEnumFieldName("scope"), withGlobalEnum()),
},
}.getMixins(e)
}
// Edges of the Entity
func (e Entity) Edges() []ent.Edge {
return []ent.Edge{
edgeToWithPagination(&edgeDefinition{
fromSchema: e,
edgeSchema: Contact{},
annotations: []schema.Annotation{
accessmap.EdgeViewCheck(Organization{}.Name()),
},
}),
defaultEdgeToWithPagination(e, DocumentData{}),
defaultEdgeToWithPagination(e, Note{}),
defaultEdgeToWithPagination(e, File{}),
defaultEdgeToWithPagination(e, Asset{}),
defaultEdgeToWithPagination(e, Scan{}),
defaultEdgeToWithPagination(e, Campaign{}),
defaultEdgeToWithPagination(e, AssessmentResponse{}),
defaultEdgeToWithPagination(e, VendorRiskScore{}),
edgeToWithPagination(&edgeDefinition{
fromSchema: e,
edgeSchema: Integration{},
annotations: []schema.Annotation{
accessmap.EdgeViewCheck(Organization{}.Name()),
},
}),
defaultEdgeToWithPagination(e, Subprocessor{}),
edgeToWithPagination(&edgeDefinition{
fromSchema: e,
name: "auth_methods",
t: CustomTypeEnum.Type,
}),
edgeFromWithPagination(&edgeDefinition{
fromSchema: e,
name: "employer_identity_holders",
t: IdentityHolder.Type,
ref: "employer",
}),
defaultEdgeFromWithPagination(e, IdentityHolder{}),
defaultEdgeFromWithPagination(e, Control{}),
defaultEdgeFromWithPagination(e, Subcontrol{}),
defaultEdgeFromWithPagination(e, Platform{}),
edgeFromWithPagination(&edgeDefinition{
fromSchema: e,
name: "out_of_scope_platforms",
t: Platform.Type,
ref: "out_of_scope_vendors",
}),
edgeFromWithPagination(&edgeDefinition{
fromSchema: e,
name: "source_platforms",
t: Platform.Type,
ref: "source_entities",
annotations: []schema.Annotation{
accessmap.EdgeViewCheck(Platform{}.Name()),
},
}),
uniqueEdgeTo(&edgeDefinition{
fromSchema: e,
edgeSchema: EntityType{},
field: "entity_type_id",
annotations: []schema.Annotation{
accessmap.EdgeViewCheck(Organization{}.Name()),
},
}),
uniqueEdgeTo(&edgeDefinition{
fromSchema: e,
name: "logo_file",
t: File.Type,
field: "logo_file_id",
}),
defaultEdgeFromWithPagination(e, InternalPolicy{}),
}
}
// Indexes of the Entity
func (Entity) Indexes() []ent.Index {
return []ent.Index{
// names should be unique, but ignore deleted names
index.Fields("name", ownerFieldName).
Unique().Annotations(entsql.IndexWhere("deleted_at is NULL")),
index.Fields("reviewed_by_user_id"),
}
}
// Hooks of the Entity
func (Entity) Hooks() []ent.Hook {
return []ent.Hook{
hooks.HookEntityCreate(),
hooks.HookEntityFiles(),
hooks.HookEntityApprovedForUse(),
hooks.HookEntityLogoFile(),
}
}
// Policy of the Entity
func (e Entity) Policy() ent.Policy {
return policy.NewPolicy(
policy.WithMutationRules(
policy.CheckCreateAccess(),
rule.CheckIfCommentOnly(),
entfga.CheckEditAccess[*generated.EntityMutation](),
),
)
}
// Annotations of the Entity
func (e Entity) Annotations() []schema.Annotation {
return []schema.Annotation{
entx.FileCategory(SchemaEntity),
entfga.SelfAccessChecks(),
entx.NewExportable(),
entx.IntegrationMappingSchema().
StockPersist().
Exclude("entity_type_id", "linked_asset_ids"),
}
}
func (Entity) Modules() []models.OrgModule {
return []models.OrgModule{
models.CatalogEntityManagementModule,
models.CatalogComplianceModule,
}
}