-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_system_management_test.go
More file actions
168 lines (147 loc) · 4.22 KB
/
example_system_management_test.go
File metadata and controls
168 lines (147 loc) · 4.22 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
// package main_test contains examples of GoCTI usage.
//
// This example shows how GoCTI can be used for system management.
package main_test
import (
"context"
"fmt"
"log/slog"
"os"
"strings"
"github.com/kelseyhightower/envconfig"
"github.com/weisshorn-cyd/gocti"
"github.com/weisshorn-cyd/gocti/entity"
"github.com/weisshorn-cyd/gocti/graphql"
"github.com/weisshorn-cyd/gocti/list"
"github.com/weisshorn-cyd/gocti/system"
)
//nolint:funlen // ok for an example
func Example_systemManagement() {
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelError}))
// Get config from env
cfg := struct {
URL string `envconfig:"URL" required:"true"`
Token string `envconfig:"TOKEN" required:"true"`
}{}
if err := envconfig.Process("OPENCTI", &cfg); err != nil {
logger.Error("loading config", "error", err)
os.Exit(1)
}
// Create client
client, err := gocti.NewOpenCTIAPIClient(
cfg.URL,
cfg.Token,
gocti.WithHealthCheck(),
gocti.WithLogger(logger),
)
if err != nil {
logger.Error("creating client", "error", err)
os.Exit(1)
}
ctx := context.Background()
// Get ID of the Connector role
roles, err := client.ListRoles(ctx, "id, name", false, nil, list.WithSearch("connector"))
if err != nil {
logger.Error("getting connector role", "error", err)
}
if len(roles) != 1 && strings.ToLower(roles[0].Name) != "connector" {
logger.Error("did not find the expected role", "roles", roles)
os.Exit(1)
}
connectorRole := roles[0]
// Create group
group, err := client.CreateGroup(ctx, "id", system.GroupAddInput{
Name: "MyExampleGroup",
Description: "A group used in the system management example.",
DefaultAssignation: false,
AutoNewMarking: true,
GroupConfidenceLevel: graphql.ConfidenceLevelInput{MaxConfidence: 50},
})
if err != nil {
logger.Error("creating group", "error", err)
}
defer func() {
if _, err := client.DeleteGroup(ctx, group.ID); err != nil {
logger.Error("deleting", "group", group, "error", err)
}
}()
// Assign the Connector role to the group
if _, err := group.AssignRole(ctx, client, connectorRole.ID); err != nil {
logger.Error("assigning role to group", "error", err)
}
// Create new user
user, err := client.CreateUser(ctx, "id", system.UserAddInput{
Name: "exampleuser",
UserEmail: "example@opencti.io",
Firstname: "Example",
Lastname: "McExampleson",
Password: "1234",
})
if err != nil {
logger.Error("creating user", "error", err)
}
defer func() {
if _, err := client.DeleteUser(ctx, user.ID); err != nil {
logger.Error("deleting", "user", user, "error", err)
}
}()
// Assign user to group
if _, err := user.AssignGroup(ctx, client, group.ID); err != nil {
logger.Error("assigning user to group", "error", err)
}
// Create task
task, err := client.CreateTask(ctx, "id", entity.TaskAddInput{
Name: "Example Task",
Description: "This is a task demonstrates the GoCTI system management capabilities.",
ObjectAssignee: []string{user.ID},
})
if err != nil {
logger.Error("creating task", "error", err)
}
defer func() {
if _, err := client.DeleteTask(ctx, task.ID); err != nil {
logger.Error("deleting", "task", task, "error", err)
}
}()
// Get Task assigned to a given user
tasks, err := client.ListTasks(
ctx,
"id, description, objectAssignee{id, name}",
false,
nil,
list.WithFilters(
list.FilterGroup{
Mode: "and",
Filters: []list.Filter{
{
Key: []string{"objectAssignee"},
Values: []any{user.ID},
Operator: list.FilterOperatorEq,
Mode: list.FilterModeOr,
},
},
FilterGroups: []list.FilterGroup{},
},
),
)
if err != nil {
logger.Error("getting tasks", "error", err)
}
if len(tasks) > 0 {
fmt.Printf("Found %d task(s)\n", len(tasks))
fmt.Printf(
"The task has the description: '%s'\n",
tasks[0].Description,
)
fmt.Printf(
"The task is assigned to user: '%s'\n",
tasks[0].ObjectAssignee[0].Name,
)
} else {
fmt.Printf("Found no task with the given filter\n")
}
// Output:
// Found 1 task(s)
// The task has the description: 'This is a task demonstrates the GoCTI system management capabilities.'
// The task is assigned to user: 'exampleuser'
}