-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdk.go
149 lines (112 loc) · 3.89 KB
/
sdk.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
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
package client
import (
"context"
"fmt"
"google.golang.org/grpc"
)
const (
_defaultCommPort = 443 // Port used by AMaaS Client to communicate with server.
_defaultTimeoutSecs = 300 // 5 minutes
)
const (
_envvarAuthKey = "TM_AM_AUTH_KEY" // Can be API key or token; SDK will auto-detect
_envvarScanTimeoutSecs = "TM_AM_SCAN_TIMEOUT_SECS" // Number of seconds before scan request timeout; default is 180 seconds.
_envvarLogLevel = "TM_AM_LOG_LEVEL" // Can be used to override program's current log level
)
const (
LogLevelOff LogLevel = iota
LogLevelFatal LogLevel = 1
LogLevelError LogLevel = 2
LogLevelWarning LogLevel = 3
LogLevelInfo LogLevel = 4
LogLevelDebug LogLevel = 5
)
type AmaasClientReader interface {
// Return the identifier of the data source. For example, name of the file being read.
Identifier() string
// Return the total size of the data source.
DataSize() (int64, error)
// Return requested number of bytes from the data source starting at the specified offset.
ReadBytes(offset int64, length int32) ([]byte, error)
}
func NewClient(key string, region string) (c *AmaasClient, e error) {
ac := &AmaasClient{digest: true}
ac.appName = appNameV1FS
var err error
if ac.authKey, err = checkAuthKey(key); err != nil {
return nil, err
}
if ac.addr, err = identifyServerAddr(region); err != nil {
return nil, err
}
ac.useTLS, ac.verifyCert = retrieveTLSSettings()
if ac.timeoutSecs, err = getDefaultScanTimeout(); err != nil {
return nil, err
}
if err = ac.archHandler.initHandler(ac); err != nil {
return nil, err
}
if err = ac.setupComm(); err != nil {
return nil, err
}
// TBD: We might want to do a hello/ping here against the target server so we can assess
// service connectivity and access before user actually calls ScanFile().
return ac, nil
}
//
// The Destroy() function should only be called after all ScanFile() invocations have completed.
//
func (ac *AmaasClient) Destroy() {
if ac.conn != nil {
ac.conn.Close()
ac.conn = nil
}
}
//
// The ScanFile(), ScanBuffer() and ScanReader() functions are thread-safe and can be invoked by multiple threads,
// but all file scans must complete before Destroy() can be invoked.
// If your application utilizes ScanReader() for scanning, it's advisable to deactivate digest feature by SetDigestDisable().
// This is because the Digest feature requires the entire file to be read/feteched.
//
// For instance, scanning a cloud-based file requires downloading the entire file locally for digest calculation.
// It increases network usage and processing time.
//
func (ac *AmaasClient) ScanFile(filePath string, tags []string) (resp string, e error) {
currentLogLevel = getLogLevel()
return ac.fileScanRun(filePath, tags)
}
func (ac *AmaasClient) ScanBuffer(buffer []byte, identifier string, tags []string) (resp string, e error) {
currentLogLevel = getLogLevel()
return ac.bufferScanRun(buffer, identifier, tags)
}
func (ac *AmaasClient) ScanReader(reader AmaasClientReader, tags []string) (resp string, e error) {
currentLogLevel = getLogLevel()
return ac.readerScanRun(reader, tags)
}
func (ac *AmaasClient) DumpConfig() (output string) {
return fmt.Sprintf("%+v", ac)
}
func SetLoggingLevel(level LogLevel) {
currentLogLevel = level
}
func ConfigLoggingCallback(f func(level LogLevel, levelStr string, format string, a ...interface{})) {
userLogger = f
}
func (ac *AmaasClient) GetTimeoutSetting() int {
return ac.timeoutSecs
}
func (ac *AmaasClient) GetConnection() *grpc.ClientConn {
return ac.conn
}
func (ac *AmaasClient) ConfigAuth(ctx context.Context) context.Context {
return ac.buildAuthContext(ctx)
}
func (ac *AmaasClient) SetAppName(appName string) {
ac.appName = appName
}
func (ac *AmaasClient) GetAppName() string {
return ac.appName
}
func (ac *AmaasClient) ConfigAppName(ctx context.Context) context.Context {
return ac.buildAppNameContext(ctx)
}