-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkrn.go
More file actions
573 lines (486 loc) · 14 KB
/
Copy pathkrn.go
File metadata and controls
573 lines (486 loc) · 14 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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Copyright (c) Kopexa GRC
// SPDX-License-Identifier: Apache-2.0
// Package krn implements Kopexa Resource Names (KRN) following Google's Resource Name Design.
//
// KRN Format:
//
// //kopexa.com/{collection}/{resource-id}[/{collection}/{resource-id}][@{version}]
// //{service}.kopexa.com/{collection}/{resource-id}[/{collection}/{resource-id}][@{version}]
//
// Examples:
//
// //kopexa.com/frameworks/iso27001
// //kopexa.com/frameworks/iso27001/controls/a-5-1
// //catalog.kopexa.com/frameworks/iso27001
// //isms.kopexa.com/tenants/acme-corp/workspaces/main
// //kopexa.com/frameworks/iso27001/controls/a-5-1@v2
package krn
import (
"errors"
"fmt"
"regexp"
"strings"
)
// Domain is the base domain for all KRNs.
const Domain = "kopexa.com"
// Error types for KRN parsing and validation.
var (
ErrEmptyKRN = errors.New("krn: empty KRN string")
ErrInvalidKRN = errors.New("krn: invalid KRN format")
ErrInvalidDomain = errors.New("krn: invalid domain")
ErrInvalidResourceID = errors.New("krn: invalid resource ID")
ErrInvalidVersion = errors.New("krn: invalid version format")
ErrResourceNotFound = errors.New("krn: resource not found")
)
// Validation patterns.
var (
// resourceIDPattern validates resource IDs: 1-200 chars, alphanumeric plus - _ .
// Cannot start or end with - or .
resourceIDPattern = regexp.MustCompile(`^[a-zA-Z0-9]([a-zA-Z0-9._-]{0,198}[a-zA-Z0-9])?$|^[a-zA-Z0-9]$`)
// versionPattern validates version strings (OSCAL-compatible):
// - Alphanumeric, dots, dashes, underscores allowed
// - Cannot start or end with dash or dot
// - "v" alone is checked separately in IsValidVersion
// Examples: v1, v1.2.3, 2022, 2022-01-15, 1.0.0, latest, draft
versionPattern = regexp.MustCompile(`^[a-zA-Z0-9]([a-zA-Z0-9._-]*[a-zA-Z0-9])?$`)
// servicePattern validates service names: lowercase alphanumeric, 1-63 chars (DNS label)
servicePattern = regexp.MustCompile(`^[a-z][a-z0-9-]{0,61}[a-z0-9]$|^[a-z]$`)
)
// Segment represents a collection/resource-id pair in a KRN path.
type Segment struct {
Collection string
ResourceID string
}
// KRN represents a Kopexa Resource Name.
type KRN struct {
service string // Optional service name (e.g., "catalog", "isms")
segments []Segment
version string
}
// Parse parses a KRN string and returns a KRN struct.
func Parse(s string) (*KRN, error) {
if s == "" {
return nil, ErrEmptyKRN
}
// Must start with //
if !strings.HasPrefix(s, "//") {
return nil, fmt.Errorf("%w: must start with //", ErrInvalidKRN)
}
// Remove // prefix
s = s[2:]
// Extract version if present
var version string
if idx := strings.LastIndex(s, "@"); idx != -1 {
version = s[idx+1:]
s = s[:idx]
if !IsValidVersion(version) {
return nil, fmt.Errorf("%w: %s", ErrInvalidVersion, version)
}
}
// Split by /
parts := strings.Split(s, "/")
if len(parts) < 3 {
return nil, fmt.Errorf("%w: must have at least domain/collection/id", ErrInvalidKRN)
}
// Parse domain - can be "kopexa.com" or "{service}.kopexa.com"
var service string
domain := parts[0]
switch {
case domain == Domain:
// Simple case: //kopexa.com/...
service = ""
case strings.HasSuffix(domain, "."+Domain):
// Service case: //{service}.kopexa.com/...
service = strings.TrimSuffix(domain, "."+Domain)
if !IsValidService(service) {
return nil, fmt.Errorf("%w: invalid service name %s", ErrInvalidDomain, service)
}
default:
return nil, fmt.Errorf("%w: expected %s or {service}.%s, got %s", ErrInvalidDomain, Domain, Domain, domain)
}
// Parse resource path (must be pairs of collection/id)
resourcePath := parts[1:]
if len(resourcePath)%2 != 0 {
return nil, fmt.Errorf("%w: resource path must be pairs of collection/id", ErrInvalidKRN)
}
segments := make([]Segment, 0, len(resourcePath)/2)
for i := 0; i < len(resourcePath); i += 2 {
collection := resourcePath[i]
resourceID := resourcePath[i+1]
if collection == "" {
return nil, fmt.Errorf("%w: empty collection name", ErrInvalidKRN)
}
if !IsValidResourceID(resourceID) {
return nil, fmt.Errorf("%w: %s", ErrInvalidResourceID, resourceID)
}
segments = append(segments, Segment{
Collection: collection,
ResourceID: resourceID,
})
}
return &KRN{
service: service,
segments: segments,
version: version,
}, nil
}
// MustParse parses a KRN string and panics on error.
func MustParse(s string) *KRN {
krn, err := Parse(s)
if err != nil {
panic(err)
}
return krn
}
// IsValid checks if a string is a valid KRN.
func IsValid(s string) bool {
_, err := Parse(s)
return err == nil
}
// IsValidResourceID checks if a string is a valid resource ID.
func IsValidResourceID(id string) bool {
if id == "" || len(id) > 200 {
return false
}
return resourceIDPattern.MatchString(id)
}
// IsValidVersion checks if a string is a valid version.
// Versions must be OSCAL-compatible: alphanumeric with dots, dashes, underscores.
// Cannot start or end with dash or dot. "v" alone is invalid.
func IsValidVersion(v string) bool {
if v == "" || v == "v" {
return false
}
return versionPattern.MatchString(v)
}
// IsValidService checks if a string is a valid service name.
// Service names must be lowercase, start with a letter, and contain only alphanumeric characters and hyphens.
func IsValidService(s string) bool {
if s == "" {
return false
}
return servicePattern.MatchString(s)
}
// SafeResourceID converts a string to a valid resource ID by replacing invalid characters.
func SafeResourceID(s string) string {
if s == "" {
return ""
}
// Replace invalid characters with -
var result strings.Builder
for _, r := range s {
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '-' || r == '_' || r == '.' {
result.WriteRune(r)
} else {
result.WriteRune('-')
}
}
res := result.String()
// Trim leading/trailing - and .
res = strings.Trim(res, "-.")
// Truncate to 200 characters
if len(res) > 200 {
res = res[:200]
// Make sure we don't end with - or .
res = strings.TrimRight(res, "-.")
}
return res
}
// GetResource extracts a resource ID from a KRN string by collection name.
func GetResource(krnString, collection string) (string, error) {
k, err := Parse(krnString)
if err != nil {
return "", err
}
return k.ResourceID(collection)
}
// String returns the string representation of the KRN.
func (k *KRN) String() string {
var sb strings.Builder
sb.WriteString("//")
if k.service != "" {
sb.WriteString(k.service)
sb.WriteString(".")
}
sb.WriteString(Domain)
for _, seg := range k.segments {
sb.WriteString("/")
sb.WriteString(seg.Collection)
sb.WriteString("/")
sb.WriteString(seg.ResourceID)
}
if k.version != "" {
sb.WriteString("@")
sb.WriteString(k.version)
}
return sb.String()
}
// Path returns the resource path without domain (alias: RelativeResourceName).
func (k *KRN) Path() string {
var parts []string
for _, seg := range k.segments {
parts = append(parts, seg.Collection, seg.ResourceID)
}
return strings.Join(parts, "/")
}
// RelativeResourceName returns the path without domain (Mondoo-compatible naming).
func (k *KRN) RelativeResourceName() string {
return k.Path()
}
// Version returns the version string, or empty if no version.
func (k *KRN) Version() string {
return k.version
}
// HasVersion returns true if the KRN has a version.
func (k *KRN) HasVersion() bool {
return k.version != ""
}
// Service returns the service name, or empty if no service.
func (k *KRN) Service() string {
return k.service
}
// HasService returns true if the KRN has a service.
func (k *KRN) HasService() bool {
return k.service != ""
}
// FullDomain returns the full domain including service if present.
// Examples: "kopexa.com" or "catalog.kopexa.com"
func (k *KRN) FullDomain() string {
if k.service != "" {
return k.service + "." + Domain
}
return Domain
}
// WithService returns a new KRN with the specified service.
func (k *KRN) WithService(service string) (*KRN, error) {
if !IsValidService(service) {
return nil, fmt.Errorf("%w: invalid service name %s", ErrInvalidDomain, service)
}
newSegments := make([]Segment, len(k.segments))
copy(newSegments, k.segments)
return &KRN{
service: service,
segments: newSegments,
version: k.version,
}, nil
}
// WithoutService returns a new KRN without the service.
func (k *KRN) WithoutService() *KRN {
newSegments := make([]Segment, len(k.segments))
copy(newSegments, k.segments)
return &KRN{
service: "",
segments: newSegments,
version: k.version,
}
}
// ResourceID returns the resource ID for a given collection.
func (k *KRN) ResourceID(collection string) (string, error) {
for _, seg := range k.segments {
if seg.Collection == collection {
return seg.ResourceID, nil
}
}
return "", fmt.Errorf("%w: %s", ErrResourceNotFound, collection)
}
// MustResourceID returns the resource ID for a given collection, panics if not found.
func (k *KRN) MustResourceID(collection string) string {
id, err := k.ResourceID(collection)
if err != nil {
panic(err)
}
return id
}
// HasResource returns true if the KRN has a resource with the given collection.
func (k *KRN) HasResource(collection string) bool {
for _, seg := range k.segments {
if seg.Collection == collection {
return true
}
}
return false
}
// Basename returns the last resource ID in the path.
func (k *KRN) Basename() string {
if len(k.segments) == 0 {
return ""
}
return k.segments[len(k.segments)-1].ResourceID
}
// BasenameCollection returns the last collection name in the path.
func (k *KRN) BasenameCollection() string {
if len(k.segments) == 0 {
return ""
}
return k.segments[len(k.segments)-1].Collection
}
// Parent returns a new KRN without the last segment, or nil if this is a root resource.
func (k *KRN) Parent() *KRN {
if len(k.segments) <= 1 {
return nil
}
newSegments := make([]Segment, len(k.segments)-1)
copy(newSegments, k.segments[:len(k.segments)-1])
return &KRN{
service: k.service,
segments: newSegments,
version: "", // Parent doesn't inherit version
}
}
// WithVersion returns a new KRN with the specified version.
func (k *KRN) WithVersion(version string) (*KRN, error) {
if !IsValidVersion(version) {
return nil, fmt.Errorf("%w: %s", ErrInvalidVersion, version)
}
newSegments := make([]Segment, len(k.segments))
copy(newSegments, k.segments)
return &KRN{
service: k.service,
segments: newSegments,
version: version,
}, nil
}
// WithoutVersion returns a new KRN without the version.
func (k *KRN) WithoutVersion() *KRN {
newSegments := make([]Segment, len(k.segments))
copy(newSegments, k.segments)
return &KRN{
service: k.service,
segments: newSegments,
version: "",
}
}
// Equals checks if two KRNs are equal.
func (k *KRN) Equals(other *KRN) bool {
if other == nil {
return false
}
return k.String() == other.String()
}
// EqualsString checks if the KRN equals another KRN string.
func (k *KRN) EqualsString(other string) bool {
otherKRN, err := Parse(other)
if err != nil {
return false
}
return k.Equals(otherKRN)
}
// Segments returns a copy of all segments in the KRN.
func (k *KRN) Segments() []Segment {
result := make([]Segment, len(k.segments))
copy(result, k.segments)
return result
}
// Depth returns the number of resource levels in the KRN.
func (k *KRN) Depth() int {
return len(k.segments)
}
// NewChild creates a new KRN as a child of the given parent.
func NewChild(parent *KRN, collection, resourceID string) (*KRN, error) {
if parent == nil {
return nil, fmt.Errorf("%w: parent cannot be nil", ErrInvalidKRN)
}
if collection == "" {
return nil, fmt.Errorf("%w: collection cannot be empty", ErrInvalidKRN)
}
if !IsValidResourceID(resourceID) {
return nil, fmt.Errorf("%w: %s", ErrInvalidResourceID, resourceID)
}
newSegments := make([]Segment, len(parent.segments)+1)
copy(newSegments, parent.segments)
newSegments[len(parent.segments)] = Segment{
Collection: collection,
ResourceID: resourceID,
}
return &KRN{
service: parent.service,
segments: newSegments,
version: "", // Child doesn't inherit version
}, nil
}
// NewChildFromString creates a new KRN as a child of the given parent KRN string.
func NewChildFromString(parentKRN, collection, resourceID string) (*KRN, error) {
parent, err := Parse(parentKRN)
if err != nil {
return nil, err
}
return NewChild(parent, collection, resourceID)
}
// Builder provides a fluent API for building KRNs.
type Builder struct {
service string
segments []Segment
version string
err error
}
// New creates a new KRN builder.
func New() *Builder {
return &Builder{
segments: make([]Segment, 0),
}
}
// Service sets the service for the KRN (optional).
func (b *Builder) Service(service string) *Builder {
if b.err != nil {
return b
}
if !IsValidService(service) {
b.err = fmt.Errorf("%w: invalid service name %s", ErrInvalidDomain, service)
return b
}
b.service = service
return b
}
// Resource adds a resource segment to the builder.
func (b *Builder) Resource(collection, resourceID string) *Builder {
if b.err != nil {
return b
}
if collection == "" {
b.err = fmt.Errorf("%w: collection cannot be empty", ErrInvalidKRN)
return b
}
if !IsValidResourceID(resourceID) {
b.err = fmt.Errorf("%w: %s", ErrInvalidResourceID, resourceID)
return b
}
b.segments = append(b.segments, Segment{
Collection: collection,
ResourceID: resourceID,
})
return b
}
// Version sets the version for the KRN.
func (b *Builder) Version(version string) *Builder {
if b.err != nil {
return b
}
if !IsValidVersion(version) {
b.err = fmt.Errorf("%w: %s", ErrInvalidVersion, version)
return b
}
b.version = version
return b
}
// Build creates the KRN. Returns nil and error if any error occurred during building.
func (b *Builder) Build() (*KRN, error) {
if b.err != nil {
return nil, b.err
}
if len(b.segments) == 0 {
return nil, fmt.Errorf("%w: must have at least one resource", ErrInvalidKRN)
}
return &KRN{
service: b.service,
segments: b.segments,
version: b.version,
}, nil
}
// MustBuild creates the KRN and panics on error.
func (b *Builder) MustBuild() *KRN {
k, err := b.Build()
if err != nil {
panic(err)
}
return k
}