-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathartifacts.go
87 lines (76 loc) · 1.87 KB
/
artifacts.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
package go4th
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"path"
"github.com/gabriel-vasile/mimetype"
)
// Artifact defines an Alert/Case artifact
type Artifact struct {
DataType string `json:"dataType,omitempty"`
Data string `json:"data,omitempty"`
Message string `json:"message,omitempty"`
TLP TLP `json:"tlp,omitempty"`
Tags []string `json:"tags,omitempty"`
}
// NewArtifact returns a new artifact
func NewArtifact(dataType, data string) (*Artifact, error) {
var art *Artifact
art = new(Artifact)
art.DataType = dataType
art.TLP = Amber
art.Tags = []string{}
if data == "" {
return nil, fmt.Errorf("missing artifact data")
}
if dataType != "" && dataType == "file" {
if _, err := os.Stat(data); !os.IsNotExist(err) {
dat, err := ioutil.ReadFile(data)
if err != nil {
return nil, fmt.Errorf("unable to open/read artifact %s", data)
}
mime := mimetype.Detect(dat)
filename := path.Base(data)
encodedData := base64.StdEncoding.EncodeToString(dat)
art.DataType = dataType
art.Data = fmt.Sprintf("%s;%s;%s", filename, mime, encodedData)
} else {
return nil, fmt.Errorf("unable to read artifact %s", data)
}
} else {
art.Data = data
}
return art, nil
}
/*
Artifact methods
*/
// SetTLP sets the TLP for the asset
func (a *Artifact) SetTLP(tlp TLP) error {
if tlp != White && tlp != Green && tlp != Amber && tlp != Red {
return fmt.Errorf("tlp provided is not a valid value")
}
a.TLP = tlp
return nil
}
// SetMessage sets the message for the artifact
func (a *Artifact) SetMessage(msg string) error {
if msg == "" {
return fmt.Errorf("msg could not be empty")
}
a.Message = msg
return nil
}
// SetTags sets the tags for the artifact
func (a *Artifact) SetTags(tags []string) error {
if len(tags) == 0 {
return fmt.Errorf("tags could not be empty")
}
a.Tags = tags
return nil
}
/*
API Calls
*/