Usage Guide Go (golang)ΒΆ

Note

In order to have sensitivity.io added to your Go (golang) application, please install the C version of our SDK, then call C. functions inside your go code:

Please see the integrate-cpp documentation.

Tip

C - You can download specific documentation here: https://static.sensitivity.io/docs/sensitivityio_c_api.chm

Code example:

  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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright (C) 2017 CoSoSys S.R.L.
//       sensitivity.io
//       +40-264-593 110
//       Ovidiu CICAL - ovidiu.cical@sensitivity.io
// api/scanner.go

package api

/*
#cgo CFLAGS: -DSENSITIVITYIO_SHARED -I. -I/opt/cososys/include
#cgo LDFLAGS: -Wl,-rpath,/opt/cososys/lib64 -L/opt/cososys/lib64 -lsensitivityio_base -lsensitivityio_c_base -lsensitivityio_c_license -lsensitivityio_c_sds

#include <sensitivityio/c_sds/scanner.h>
#include <sensitivityio/c_sds/scanner_settings_loader.h>
*/
import "C"

import (
	"bytes"
	"encoding/hex"
	"fmt"
	"io"
	"net/http"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"time"

	"github.com/gorilla/mux"

	log "github.com/alecthomas/log4go"

	"sensitivity.io/api/model"
	"sensitivity.io/api/utils"
)

func InitScanner() {
	BaseRoutes.Scanner.Handle("/data", ApiAccountRequired(scanData)).Methods("POST")              // scan from upload data
	BaseRoutes.Scanner.Handle("/upload_file", ApiAccountRequired(scanUploadFile)).Methods("POST") // scan from upload file

	BaseRoutes.NeedScanner.Handle("/", ApiAccountRequired(getScanner)).Methods("GET") // get scanner by Id
}

var dumpJsonToFile = true

func scanData(c *Context, w http.ResponseWriter, r *http.Request) {

	// Scan start time in milliseconds
	scanStartTimestamp := utils.MillisFromTime(time.Now())

	params := mux.Vars(r)
	// return if provided data for scanner is not valid
	if _, valid := validScannerData(c, params); !valid {
		c.SetInvalidParam("scanData", "params")
		return
	}
	projectId := params["project_id"]
	appId := params["app_id"]

	if r.ContentLength > *utils.Cfg.FileSettings.MaxFileSize {
		c.Err = model.NewAppError("scannerCaller", "api.scanner.scan_data.too_large.error", nil, "")
		c.Err.StatusCode = http.StatusRequestEntityTooLarge
		return
	}

	if err := r.ParseMultipartForm(*utils.Cfg.FileSettings.MaxFileSize); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	m := r.MultipartForm

	fields := m.Value
	if len(fields["data"]) == 0 {
		c.SetInvalidParam("scanData", "data fields")
		return
	}

	c.LogAdminAction(
		"Scanner",  // module
		"",         // objectId
		"",         // identifier
		"ScanData", // action
		"",         // beforeDesc
		"",         // afterDesc
		fmt.Sprintf("ProjectId=%s, AppId=%s", projectId, appId)) // extraInfo

	var data string
	for _, part := range fields["data"] {
		data += fmt.Sprintf("%v ", part)
	}

	var verbose bool
	if len(r.FormValue("verbose")) > 0 {
		verbose, _ = strconv.ParseBool(strings.TrimSpace(r.FormValue("verbose")))
	} else {
		verbose = true
	}
	stopAtFirst, _ := strconv.ParseBool(strings.TrimSpace(r.FormValue("stop_at_first")))
	stopAt, _ := strconv.Atoi(strings.TrimSpace(r.FormValue("stop_at")))
	var enableThreatHandler bool
	if stopAt > 0 {
		enableThreatHandler = true
	}
	var maskThreats bool
	if len(r.FormValue("mask_threats")) > 0 {
		maskThreats, _ = strconv.ParseBool(strings.TrimSpace(r.FormValue("mask_threats")))
	}

	scannerSettings := &model.ScannerSettings{
		ThreatHandler: enableThreatHandler,
		Verbose:       verbose,
		StopAtFirst:   stopAtFirst,
		StopAt:        stopAt,
		MaskThreats:   maskThreats,
		License:       new(string),
		Settings:      new(string),
	}

	// get App Settings
	if license, settings, valid := getAppDataForScanner(c, projectId, appId); !valid {
		return
	} else {
		*scannerSettings.License = license
		*scannerSettings.Settings = settings.ToJson()
	}
	scannerSettings.SetDefaults()

	if pScanner, pSettingsLoader, err := utils.InitSensitivityIoScanner(scannerSettings); err != nil {
		utils.UnregisterAndDestroy(pScanner, pSettingsLoader)
		c.Err = err
		c.Err.StatusCode = http.StatusServiceUnavailable
		return
	} else {
		scannerId := model.NewRandomHex(model.SCANNER_ID_LENGTH)
		resStruct := &model.ThreatResponse{ScannerId: scannerId}

		quitChan := make(chan int)
		defer close(quitChan)

		cchan := utils.ScanData(data, quitChan, scannerSettings, pScanner)
		defer utils.UnregisterAndDestroy(pScanner, pSettingsLoader)

		if cresult := <-cchan; cresult.Err != nil {
			fmt.Println(cresult.Err)
		} else {
			resStruct.ThreatResults = append(resStruct.ThreatResults, cresult)
			res := []byte(resStruct.ToJson())
			if dumpJsonToFile {
				scanLogger := utils.InitScanLogger(c.AccountId, projectId, appId, scannerId)
				logFile, lfErr := scanLogger.OpenLogFile()
				if lfErr != nil {
					return
				}
				defer logFile.Close()
				scanLogger.WriteToFile(logFile, resStruct, scanStartTimestamp)
			}
			w.Write(res)
		}
	}
}

func scanUploadFile(c *Context, w http.ResponseWriter, r *http.Request) {

	// Scan start time in milliseconds
	scanStartTimestamp := utils.MillisFromTime(time.Now())

	params := mux.Vars(r)
	// return if provided data for scanner is not valid
	if _, valid := validScannerData(c, params); !valid {
		c.SetInvalidParam("scanUploadFile", "params")
		return
	}
	projectId := params["project_id"]
	appId := params["app_id"]

	if r.ContentLength > *utils.Cfg.FileSettings.MaxFileSize {
		c.Err = model.NewAppError("scanUploadFile", "api.scanner.scan_file.too_large", nil, "")
		c.Err.StatusCode = http.StatusRequestEntityTooLarge
		return
	}

	if err := r.ParseMultipartForm(*utils.Cfg.FileSettings.MaxFileSize); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	m := r.MultipartForm

	files := m.File
	if len(files) == 0 {
		c.SetInvalidParam("scanUploadFile", "file")
		return
	}

	c.LogAdminAction(
		"Scanner",        // module
		"",               // objectId
		"",               // identifier
		"ScanUploadFile", // action
		"",               // beforeDesc
		"",               // afterDesc
		fmt.Sprintf("ProjectId=%s, AppId=%s", projectId, appId)) // extraInfo

	var verbose bool
	if len(r.FormValue("verbose")) > 0 {
		verbose, _ = strconv.ParseBool(strings.TrimSpace(r.FormValue("verbose")))
	} else {
		verbose = true
	}
	stopAtFirst, _ := strconv.ParseBool(strings.TrimSpace(r.FormValue("stop_at_first")))
	stopAt, _ := strconv.Atoi(strings.TrimSpace(r.FormValue("stop_at")))
	var enableThreatHandler bool
	if stopAt > 0 {
		enableThreatHandler = true
	}
	var maskThreats bool
	if len(r.FormValue("mask_threats")) > 0 {
		maskThreats, _ = strconv.ParseBool(strings.TrimSpace(r.FormValue("mask_threats")))
	}

	scannerSettings := &model.ScannerSettings{
		ThreatHandler: enableThreatHandler,
		Verbose:       verbose,
		StopAtFirst:   stopAtFirst,
		StopAt:        stopAt,
		MaskThreats:   maskThreats,
		License:       new(string),
		Settings:      new(string),
	}

	// get App Settings
	if license, settings, valid := getAppDataForScanner(c, projectId, appId); !valid {
		return
	} else {
		*scannerSettings.License = license
		*scannerSettings.Settings = settings.ToJson()
	}
	scannerSettings.SetDefaults()

	if pScanner, pSettingsLoader, err := utils.InitSensitivityIoScanner(scannerSettings); err != nil {
		utils.UnregisterAndDestroy(pScanner, pSettingsLoader)
		c.Err = err
		c.Err.StatusCode = http.StatusServiceUnavailable
		return
	} else {
		scannerId := model.NewRandomHex(model.SCANNER_ID_LENGTH)
		resStruct := &model.ThreatResponse{ScannerId: scannerId}

		defer utils.UnregisterAndDestroy(pScanner, pSettingsLoader)

		for _, fileHeader := range files["files"] {
			file, fileErr := fileHeader.Open()
			if fileErr != nil {
				resStruct.Details = append(resStruct.Details, fileErr.Error())
				continue
			}
			defer file.Close()

			buf := bytes.NewBuffer(nil)
			io.Copy(buf, file)
			data := buf.Bytes()

			// START - Scan file from Disk with saving it locally
			info, err := utils.DoUploadFile(c.AccountId, projectId, appId, fileHeader.Filename, data)
			if err != nil {
				resStruct.Details = append(resStruct.Details, err.Error())
				continue
			}
			info.Name = filepath.Base(fileHeader.Filename)

			if !model.IsFileExAllowed(filepath.Ext(info.Path)) {
				resStruct.Details = append(resStruct.Details, "Filetype not allowed: "+info.Name+" - "+filepath.Ext(info.Path))
				continue
			}

			quitChan := make(chan int)
			defer close(quitChan)
			doneChan := make(chan int)
			defer close(doneChan)

			cchan := utils.ScanFile(info, doneChan, quitChan, scannerSettings, pScanner)
			//END - Scan file from Disk with saving it locally

			// START - Scan file buffered data without saving it to disk
			/*quitChan := make(chan int)
			defer close(quitChan)

			cchan := utils.ScanBuffer(data, "", quitChan, scannerSettings, pScanner)*/
			// END - Scan file buffered data without saving it to disk

			if cresult := <-cchan; cresult.Err != nil {
				c.Err = cresult.Err
			} else {
				resStruct.ThreatResults = append(resStruct.ThreatResults, cresult)
			}

			// discard file after scanning (so disk won't fill)
			if err := os.Remove(info.Path); err != nil {
				log.Debug("Unable to delete file ", info.Path, " from disk. Error: ", err.Error())
			}
		}
		res := []byte(resStruct.ToJson())
		if dumpJsonToFile {
			scanLogger := utils.InitScanLogger(c.AccountId, projectId, appId, scannerId)
			logFile, lfErr := scanLogger.OpenLogFile()
			if lfErr != nil {
				return
			}
			defer logFile.Close()
			scanLogger.WriteToFile(logFile, resStruct, scanStartTimestamp)
		}
		w.Write(res)
	}
}

func getScanner(c *Context, w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	// return if provided data for scanner is not valid
	if scanId, valid := validScannerData(c, params); valid {
		result := make(map[string]interface{})
		result["scanner_id"] = scanId
		w.Write([]byte(model.StringInterfaceToJson(result)))
	} else {
		c.SetInvalidParam("getScanner", "params")
	}

}

func validScannerData(c *Context, params map[string]string) (string, bool) {
	projectId := params["project_id"]
	appId := params["app_id"]
	scannerId := params["scanner_id"]

	if len(projectId) != model.PROJECT_ID_LENGTH {
		return scannerId, false
	}

	if len(appId) != model.APP_ID_LENGTH {
		return scannerId, false
	}

	// Check that app exists and belongs to this Account and Project
	if !IsValidApp(appId, projectId, c.AccountId) {
		return scannerId, false
	}

	if len(scannerId) != 0 && len(scannerId) != model.SCANNER_ID_LENGTH {
		return scannerId, false
	}

	return scannerId, true
}

func getAppDataForScanner(c *Context, projectId string, appId string) (license string, settingsWrapper *model.SettingsWrapper, valid bool) {
	cchan := Srv.Store.App().GetByProjectId(c.AccountId, projectId, appId)

	if cresult := <-cchan; cresult.Err != nil {
		c.Err = cresult.Err
		return license, settingsWrapper, false
	} else {
		app := cresult.Data.(*model.App)

		if protectionProfileResult := <-Srv.Store.ProtectionProfile().GetForApp(app); protectionProfileResult.Err != nil {
			c.Err = protectionProfileResult.Err
			return license, settingsWrapper, false
		} else {

			protectionProfile := &model.ProtectionProfile{}
			protectionProfile = protectionProfileResult.Data.(*model.ProtectionProfile)

			protectionProfileSettings := protectionProfile.ProfileSettings
			if len(protectionProfileSettings) == 0 {
				profileSettingsResult := <-Srv.Store.ProtectionProfile().BuildSettings(protectionProfile)
				if profileSettingsResult.Err != nil {
					c.Err = profileSettingsResult.Err
					return license, settingsWrapper, false
				}

				protectionProfileSettings = profileSettingsResult.Data.(string)
			}

			settingsWrapper = model.SettingsWrapperFromJson(strings.NewReader(protectionProfileSettings))

			if licenseResult := <-Srv.Store.License().GetByAccount(c.AccountId); licenseResult.Err != nil {
				c.Err = licenseResult.Err
				return license, settingsWrapper, false
			} else {

				var currentPlan model.Plan
				licenseRecord := licenseResult.Data.(*model.LicenseRecord)

				for _, plan := range *utils.Plans {
					if plan.Name == licenseRecord.PlanType {
						currentPlan = plan
					}
				}

				expirationDate := licenseRecord.ComputeExpirationDate()

				if currentPlan.Name == "" {
					c.Err = model.NewAppError("getApp", "api.app.get_app.invalid_license", map[string]interface{}{"AppId": appId, "AccountId": c.AccountId}, "")
					return license, settingsWrapper, false
				}

				for key := range currentPlan.ModulesInfo {
					currentPlan.ModulesInfo[key].ExpirationDate = expirationDate
				}

				licenseDetails := model.LicenseInfo{c.Account.CompanyName, c.Account.ApiId, strconv.FormatInt(licenseRecord.CreateAt, 10)}

				licenseObj := model.License{app.Id, licenseDetails, currentPlan.ModulesInfo}
				licenseWrapper := &model.LicenseWrapper{licenseObj}
				signature, sErr := model.SignLicense(licenseWrapper)
				if sErr != nil {
					c.Err = model.NewAppError("getApp", "api.app.get_app.invalid_license_signature", map[string]interface{}{"AppId": appId, "AccountId": c.AccountId}, "")
					return license, settingsWrapper, false
				}
				signatureHex := hex.EncodeToString(signature)

				/*
					license response is of form:
					//signature: 0123aabbcc
					{"license":{"app_id": "abcd", "modules_info": ...}}
				*/
				license = "// signature: " + signatureHex + "\n" + licenseWrapper.ToJson()

				if settingsWrapper == nil {
					c.Err = model.NewAppError("getApp", "api.app.get_app.invalid_settings_object", map[string]interface{}{"AppId": appId, "AccountId": c.AccountId}, "")
					return license, settingsWrapper, false
				}
				settings := settingsWrapper.SettingsObject
				settings = settings.ValidateSettings(&licenseObj)
				//settingsWrapper = &model.SettingsWrapper{settings}

				lsError := setLastSeen(c.AccountId, projectId, appId)
				if lsError != nil {
					c.Err = lsError
				}
			}
		}
	}

	return license, settingsWrapper, true
}