-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathsysdig.go
More file actions
116 lines (95 loc) · 2.38 KB
/
sysdig.go
File metadata and controls
116 lines (95 loc) · 2.38 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
package v2
import (
"context"
"fmt"
"io"
"net/http"
"sync"
)
type SysdigRequest struct {
config *config
httpClient *http.Client
teamIDLock *sync.Mutex
teamID *int
}
type SysdigCommon interface {
Common
CustomRoleInterface
CustomRolePermissionInterface
GroupMappingConfigInterface
GroupMappingInterface
IPFilteringSettingsInterface
IPFiltersInterface
TeamServiceAccountInterface
}
type SysdigMonitor interface {
SysdigCommon
MonitorCommon
CloudAccountMonitorInterface
}
type SysdigSecure interface {
SysdigCommon
SecureCommon
CloudAccountSecureInterface
CloudauthAccountComponentSecureInterface
CloudauthAccountFeatureSecureInterface
CloudauthAccountSecureInterface
CompositePolicyInterface
DeprecatedScanningPolicyAssignmentInterface
DeprecatedScanningPolicyInterface
DeprecatedVulnerabilityExceptionInterface
DeprecatedVulnerabilityExceptionListInterface
ListInterface
MacroInterface
OnboardingSecureInterface
OrganizationSecureInterface
PolicyInterface
RuleInterface
VulnerabilityPolicyClient
}
func (sr *SysdigRequest) Request(ctx context.Context, method string, url string, payload io.Reader) (*http.Response, error) {
r, err := http.NewRequest(method, url, payload)
if err != nil {
return nil, err
}
r = r.WithContext(ctx)
r.Header.Set(AuthorizationHeader, fmt.Sprintf("Bearer %s", sr.config.token))
r.Header.Set(ContentTypeHeader, ContentTypeJSON)
r.Header.Set(SysdigProviderHeader, SysdigProviderHeaderValue)
return request(sr.httpClient, sr.config, r)
}
func NewSysdigMonitor(opts ...ClientOption) SysdigMonitor {
return newSysdigClient(opts...)
}
func NewSysdigSecure(opts ...ClientOption) SysdigSecure {
return newSysdigClient(opts...)
}
func newSysdigClient(opts ...ClientOption) *Client {
cfg := configure(opts...)
return &Client{
config: cfg,
requester: &SysdigRequest{
teamIDLock: &sync.Mutex{},
config: cfg,
httpClient: newHTTPClient(cfg),
},
}
}
func (sr *SysdigRequest) CurrentTeamID(ctx context.Context) (int, error) {
sr.teamIDLock.Lock()
defer sr.teamIDLock.Unlock()
if sr.teamID != nil {
return *sr.teamID, nil
}
user, err := getMe(ctx, sr.config, sr.httpClient, map[string]string{
AuthorizationHeader: fmt.Sprintf("Bearer %s", sr.config.token),
})
if err != nil {
return -1, err
}
if user.CurrentTeam == nil {
return -1, errMissingCurrentTeam
}
sr.teamID = user.CurrentTeam
return *sr.teamID, nil
}