Skip to content

Commit 54c570c

Browse files
committed
Adding types to tags
1 parent e1c6dc8 commit 54c570c

File tree

6 files changed

+55
-34
lines changed

6 files changed

+55
-34
lines changed

cli/environment.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ func addEnvironment(c *cli.Context) error {
6161
return err
6262
}
6363
// Create a tag for this new environment
64-
if err := tagsmgr.NewTag(newEnv.Name, "Tag for environment "+newEnv.Name, tags.RandomColor(), newEnv.Icon, appName, newEnv.ID, false); err != nil {
64+
if err := tagsmgr.NewTag(
65+
newEnv.Name,
66+
"Tag for environment "+newEnv.Name,
67+
tags.RandomColor(),
68+
newEnv.Icon,
69+
appName,
70+
newEnv.ID,
71+
false,
72+
tags.TagTypeEnv); err != nil {
6573
return err
6674
}
6775
// Generate flags

cli/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ func init() {
11451145
Usage: "Tag an existing node",
11461146
Flags: []cli.Flag{
11471147
&cli.StringFlag{
1148-
Name: "uuid, u",
1148+
Name: "uuid",
11491149
Aliases: []string{"u"},
11501150
Usage: "Node UUID to be tagged",
11511151
},
@@ -1159,6 +1159,12 @@ func init() {
11591159
Aliases: []string{"T"},
11601160
Usage: "Tag value to be used. It will be created if does not exist",
11611161
},
1162+
&cli.StringFlag{
1163+
Name: "tag-type",
1164+
Aliases: []string{"type"},
1165+
Value: "custom",
1166+
Usage: "Tag type to be used. It can be 'env', 'uuid', 'localname' and 'custom'",
1167+
},
11621168
},
11631169
Action: cliWrapper(tagNode),
11641170
},

cli/node.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/jmpsec/osctrl/nodes"
1010
"github.com/jmpsec/osctrl/settings"
11+
"github.com/jmpsec/osctrl/tags"
1112
"github.com/olekukonko/tablewriter"
1213
"github.com/urfave/cli/v2"
1314
)
@@ -152,6 +153,16 @@ func tagNode(c *cli.Context) error {
152153
fmt.Println("❌ tag is required")
153154
os.Exit(1)
154155
}
156+
tagType := c.String("tag-type")
157+
tagTypeInt := tags.TagTypeCustom
158+
switch tagType {
159+
case "env":
160+
tagTypeInt = tags.TagTypeEnv
161+
case "uuid":
162+
tagTypeInt = tags.TagTypeUUID
163+
case "localname":
164+
tagTypeInt = tags.TagTypeLocalname
165+
}
155166
if dbFlag {
156167
e, err := envs.Get(env)
157168
if err != nil {
@@ -162,7 +173,7 @@ func tagNode(c *cli.Context) error {
162173
return fmt.Errorf("error get uuid - %s", err)
163174
}
164175
if tagsmgr.Exists(tag) {
165-
if err := tagsmgr.TagNode(tag, n, appName, false); err != nil {
176+
if err := tagsmgr.TagNode(tag, n, appName, false, tagTypeInt); err != nil {
166177
return fmt.Errorf("error tagging - %s", err)
167178
}
168179
}
@@ -172,7 +183,7 @@ func tagNode(c *cli.Context) error {
172183
}
173184
}
174185
if !silentFlag {
175-
fmt.Println("✅ node was deleted successfully")
186+
fmt.Println("✅ node was tagged successfully")
176187
}
177188
return nil
178189
}

settings/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
require (
1919
github.com/mattn/go-colorable v0.1.14 // indirect
2020
github.com/mattn/go-isatty v0.0.20 // indirect
21-
golang.org/x/sys v0.29.0 // indirect
21+
golang.org/x/sys v0.30.0 // indirect
2222
)
2323

2424
require (

settings/go.sum

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
88
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
99
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
1010
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
11-
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
1211
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
1312
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
1413
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
1514
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
16-
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
1715
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
1816
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
1917
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
18+
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
19+
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
2020
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
2121
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2222
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -29,31 +29,14 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
2929
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3030
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3131
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
32-
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
3332
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
34-
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
35-
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
36-
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
37-
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
38-
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
39-
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
40-
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
41-
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
42-
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
43-
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
44-
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
45-
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
46-
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
47-
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
48-
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
49-
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
33+
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
34+
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
5035
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
5136
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
5237
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
5338
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
54-
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
55-
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
56-
gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg=
57-
gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
39+
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
40+
gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
5841
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
5942
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=

tags/tags.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ const (
1616
DefaultAutoTagUser uint = 0
1717
// DefaultAutocreated as default username and description for tags
1818
DefaultAutocreated = "Autocreated"
19+
// TagTypeEnv as tag type for environment name
20+
TagTypeEnv uint = 0
21+
// TagTypeUUID as tag type for node UUID
22+
TagTypeUUID uint = 1
23+
// TagTypePlatform as tag type for node platform
24+
TagTypePlatform uint = 2
25+
// TagTypeLocalname as tag type for node localname
26+
TagTypeLocalname uint = 3
27+
// TagTypeCustom as tag type for custom tags
28+
TagTypeCustom uint = 4
1929
)
2030

2131
// AdminTag to hold all tags
@@ -28,6 +38,7 @@ type AdminTag struct {
2838
CreatedBy string
2939
AutoTag bool
3040
EnvironmentID uint
41+
TagType uint
3142
}
3243

3344
// AdminTagForNode to check if this tag is used for an specific node
@@ -91,7 +102,7 @@ func (m *TagManager) Create(tag *AdminTag) error {
91102
}
92103

93104
// New empty tag
94-
func (m *TagManager) New(name, description, color, icon, user string, envID uint, auto bool) (AdminTag, error) {
105+
func (m *TagManager) New(name, description, color, icon, user string, envID uint, auto bool, tagType uint) (AdminTag, error) {
95106
tagColor := color
96107
tagIcon := icon
97108
if tagColor == "" {
@@ -109,14 +120,15 @@ func (m *TagManager) New(name, description, color, icon, user string, envID uint
109120
CreatedBy: user,
110121
EnvironmentID: envID,
111122
AutoTag: auto,
123+
TagType: tagType,
112124
}, nil
113125
}
114126
return AdminTag{}, fmt.Errorf("%s already exists", name)
115127
}
116128

117129
// NewTag to create a tag and creates it without returning it
118-
func (m *TagManager) NewTag(name, description, color, icon, user string, envID uint, auto bool) error {
119-
tag, err := m.New(name, description, color, icon, user, envID, auto)
130+
func (m *TagManager) NewTag(name, description, color, icon, user string, envID uint, auto bool, tagType uint) error {
131+
tag, err := m.New(name, description, color, icon, user, envID, auto, tagType)
120132
if err != nil {
121133
return err
122134
}
@@ -220,8 +232,8 @@ func (m *TagManager) AutoTagNode(env string, node nodes.OsqueryNode, user string
220232
// TagNodeMulti to tag a node with multiple tags
221233
// TODO use the correct user_id
222234
func (m *TagManager) TagNodeMulti(tags []string, node nodes.OsqueryNode, user string, auto bool) error {
223-
for _, t := range tags {
224-
if err := m.TagNode(t, node, user, auto); err != nil {
235+
for i, t := range tags {
236+
if err := m.TagNode(t, node, user, auto, uint(i)); err != nil {
225237
return err
226238
}
227239
}
@@ -230,7 +242,7 @@ func (m *TagManager) TagNodeMulti(tags []string, node nodes.OsqueryNode, user st
230242

231243
// TagNode to tag a node
232244
// TODO use the correct user_id
233-
func (m *TagManager) TagNode(name string, node nodes.OsqueryNode, user string, auto bool) error {
245+
func (m *TagManager) TagNode(name string, node nodes.OsqueryNode, user string, auto bool, tagType uint) error {
234246
if len(name) == 0 {
235247
return fmt.Errorf("empty tag")
236248
}
@@ -244,6 +256,7 @@ func (m *TagManager) TagNode(name string, node nodes.OsqueryNode, user string, a
244256
CreatedBy: user,
245257
AutoTag: auto,
246258
EnvironmentID: node.EnvironmentID,
259+
TagType: tagType,
247260
}
248261
if err := m.Create(&newTag); err != nil {
249262
return fmt.Errorf("error creating tag %v", err)

0 commit comments

Comments
 (0)