Skip to content

Commit 7b47436

Browse files
committed
New Singul amgic
1 parent 492f71f commit 7b47436

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ toolchain go1.24.4
99

1010
require (
1111
github.com/frikky/kin-openapi v0.42.0
12-
github.com/frikky/schemaless v0.0.20
12+
github.com/frikky/schemaless v0.0.21
1313
github.com/satori/go.uuid v1.2.0
14-
github.com/shuffle/shuffle-shared v0.9.4
14+
github.com/shuffle/shuffle-shared v0.9.30
1515
github.com/spf13/cobra v1.9.1
1616
)
1717

@@ -47,7 +47,7 @@ require (
4747
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
4848
github.com/davecgh/go-spew v1.1.1 // indirect
4949
github.com/distribution/reference v0.6.0 // indirect
50-
github.com/docker/docker v28.2.2+incompatible // indirect
50+
github.com/docker/docker v28.3.3+incompatible // indirect
5151
github.com/docker/go-connections v0.5.0 // indirect
5252
github.com/docker/go-units v0.5.0 // indirect
5353
github.com/emicklei/go-restful/v3 v3.11.0 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
116116
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
117117
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
118118
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
119-
github.com/docker/docker v28.2.2+incompatible h1:CjwRSksz8Yo4+RmQ339Dp/D2tGO5JxwYeqtMOEe0LDw=
120-
github.com/docker/docker v28.2.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
119+
github.com/docker/docker v28.3.3+incompatible h1:Dypm25kh4rmk49v1eiVbsAtpAsYURjYkaKubwuBdxEI=
120+
github.com/docker/docker v28.3.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
121121
github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
122122
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
123123
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
@@ -142,8 +142,8 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
142142
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
143143
github.com/frikky/kin-openapi v0.42.0 h1:d5Z6vnuQ6RnCCPIxZaDL+TH2ODLxT8abytOt+Zh+Kd0=
144144
github.com/frikky/kin-openapi v0.42.0/go.mod h1:ev9OZAw7Bv5p0w93j91++6a1ElPzGcCofst+kmrWsj4=
145-
github.com/frikky/schemaless v0.0.20 h1:S/A2pQcRN9qa2RnufvxwCeM06trjG0JLTF3urt1tFQI=
146-
github.com/frikky/schemaless v0.0.20/go.mod h1:m9s+6gALXhA5ZERCrJw+jI2rRtTPNa8mkl4vav9sxnY=
145+
github.com/frikky/schemaless v0.0.21 h1:uL1KJ4p1qUXhl77bgDwUbWWfr2cJlU/w0MQ2K3Rv3y0=
146+
github.com/frikky/schemaless v0.0.21/go.mod h1:m9s+6gALXhA5ZERCrJw+jI2rRtTPNa8mkl4vav9sxnY=
147147
github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E=
148148
github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
149149
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
@@ -331,8 +331,8 @@ github.com/sendgrid/sendgrid-go v3.16.1+incompatible h1:zWhTmB0Y8XCDzeWIm2/BIt1G
331331
github.com/sendgrid/sendgrid-go v3.16.1+incompatible/go.mod h1:QRQt+LX/NmgVEvmdRw0VT/QgUn499+iza2FnDca9fg8=
332332
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
333333
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
334-
github.com/shuffle/shuffle-shared v0.9.4 h1:xg8qjPgc1tCdIoM218LtQywNSs791m+Sn8F30pFb0W4=
335-
github.com/shuffle/shuffle-shared v0.9.4/go.mod h1:klSHfahYORgrQS4AdG5RIfTaL4lakbJ+R9EXfc7QQo0=
334+
github.com/shuffle/shuffle-shared v0.9.30 h1:3CYvNyD7sTxdxoZjTVrtaDqFvSWQRKAFGaga6rPGf8A=
335+
github.com/shuffle/shuffle-shared v0.9.30/go.mod h1:PhDEizuz4SmJaSmy0+yrFWwD1mXVUsy8/knKlrqF1qw=
336336
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
337337
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
338338
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=

pkg/api.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ func handleDirectTranslation(ctx context.Context, user shuffle.User, value shuff
300300
authConfig = ""
301301
}
302302

303+
303304
// Remapping into non-list JSON blob
304305
newMap := map[string]interface{}{}
305306
for _, field := range value.Fields {
@@ -330,6 +331,10 @@ func handleDirectTranslation(ctx context.Context, user shuffle.User, value shuff
330331
value.Label = "ticket"
331332
}
332333

334+
if debug {
335+
log.Printf("[DEBUG] Translating label %s with body:\n%s\n\n", value.Label, string(marshalledFields))
336+
}
337+
333338
// Is there any way to ingest these as well?
334339
schemalessOutput, err := schemaless.Translate(ctx, value.Label, marshalledFields, authConfig)
335340
if err != nil {
@@ -422,15 +427,15 @@ func autoUploadSingulOutput(ctx context.Context, orgId string, curApikey string,
422427
for cnt, item := range foundArray {
423428
// Check if uid, uuid, id or similar is a valid key
424429
if _, ok := item.(map[string]interface{}); !ok {
425-
log.Printf("[WARNING] Item in list output for label %s is not a map: %#v", value.Label, item)
430+
log.Printf("[ERROR] Item in Singul list output for label %s is not a map: %#v", value.Label, item)
426431
continue
427432
}
428433

429434
generatedItem := item.(map[string]interface{})
430435

431436
idKeys := []string{"id", "uid", "uuid", "identifier", "key"}
432-
433437
foundIdentifier := ""
438+
foundIdentifierKey := ""
434439
for _, idKey := range idKeys {
435440
// Check if lower case exists
436441
for key, mapValue := range generatedItem {
@@ -441,18 +446,19 @@ func autoUploadSingulOutput(ctx context.Context, orgId string, curApikey string,
441446
// Found the key, set it as the actual label
442447
if val, ok := mapValue.(string); ok {
443448
foundIdentifier = val
449+
foundIdentifierKey = key
444450
break
445451
} else {
446452
log.Printf("[ERROR] Failed finding ID key in item for label %s: %s", value.Label, mapValue)
447453
}
448454
}
449455

450-
if foundIdentifier != "" {
456+
if foundIdentifier != "" && len(foundIdentifier) > 4 {
451457
break
452458
}
453459
}
454460

455-
// hardcoded override for product name
461+
// hardcoded override for product name. Just a test.
456462
toolKeys := []string{"product", "tool", "service", "application", "app"}
457463
if len(secondAction.AppName) > 0 {
458464
for _, toolKey := range toolKeys {
@@ -471,9 +477,29 @@ func autoUploadSingulOutput(ctx context.Context, orgId string, curApikey string,
471477
}
472478
}
473479

474-
if len(foundIdentifier) == 0 {
475-
log.Printf("[ERROR] Failed finding ID key in item for label %s: %#v", value.Label, item)
476-
break
480+
// Generate an ID if we didn't find one
481+
if len(foundIdentifier) <= 4 {
482+
log.Printf("[ERROR] Failed finding VALID ID key in item for label %s: %#v. ID Key: %s, Value: %s", value.Label, item, foundIdentifierKey, foundIdentifier)
483+
484+
marshalledItem, err := json.Marshal(generatedItem)
485+
if err != nil {
486+
log.Printf("[ERROR] Failed marshalling item in schemaless output for label %s: %s", value.Label, err)
487+
break
488+
} else {
489+
foundIdentifier = fmt.Sprintf("%x", md5.Sum(marshalledItem))
490+
log.Printf("MARSHALLED OCSF (%s):\n%s\n", foundIdentifier, string(marshalledItem))
491+
}
492+
}
493+
494+
// In case it finds some weird thingy
495+
if len(foundIdentifier) > 36 {
496+
// md5 sum it
497+
foundIdentifier = fmt.Sprintf("%x", md5.Sum([]byte(foundIdentifier)))
498+
}
499+
500+
// Overrides the key that is being ingested
501+
if len(foundIdentifierKey) > 0 && len(foundIdentifier) > 0 {
502+
generatedItem[foundIdentifierKey] = foundIdentifier
477503
}
478504

479505
// Check if we have already uploaded it recently based on identifier
@@ -490,7 +516,8 @@ func autoUploadSingulOutput(ctx context.Context, orgId string, curApikey string,
490516

491517
shuffle.SetCache(ctx, cacheKey, []byte("true"), 60*60*24*3) // 3 days
492518

493-
marshalledBody, err := json.Marshal(item)
519+
//marshalledBody, err := json.Marshal(item)
520+
marshalledBody, err := json.Marshal(generatedItem)
494521
if err != nil {
495522
log.Printf("[ERROR] Failed marshalling item in schemaless output for label %s: %s", value.Label, err)
496523
continue
@@ -2672,7 +2699,6 @@ func RunActionWrapper(ctx context.Context, user shuffle.User, value shuffle.Cate
26722699

26732700
// Handles the actual uploading itself
26742701
if len(foundLabelSplit) > 1 && (strings.HasPrefix(value.Label, "list_") || strings.HasPrefix(value.Label, "search_")) && len(curApikey) > 0 && len(curOrg) > 0 {
2675-
26762702
autoUploadSingulOutput(ctx, curOrg, curApikey, curBackend, parsedTranslation, value, secondAction)
26772703
}
26782704
}

0 commit comments

Comments
 (0)