示例#1
0
// AppActiveScanHooksTaskV1Handler actives a scan task
func AppActiveScanHooksTaskV1Handler(ctx *macaron.Context) (int, []byte) {
	namespace := ctx.Params(":namespace")
	repository := ctx.Params(":repository")

	var r models.ScanHookRegist
	rID, err := r.FindID("appv1", namespace, repository)
	if err != nil {
		log.Errorf("[%s] scan hook callback error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Donnot have registed scan plugin"})
		return http.StatusBadRequest, result
	}

	a := models.ArtifactV1{
		OS:   ctx.Params(":os"),
		Arch: ctx.Params(":arch"),
		App:  ctx.Params(":app"),
		Tag:  ctx.Params(":tag"),
	}
	a, err = a.Get()
	if err != nil {
		log.Errorf("[%s] scan hook callback error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Cannot find artifactv1"})
		return http.StatusBadRequest, result
	}

	// create a task
	var t models.ScanHookTask
	tID, err := t.Put(rID, a.Path)
	if err != nil {
		log.Errorf("[%s] scan hook callback error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Fail to create a scan task"})
		return http.StatusBadRequest, result
	}

	idBytes, err := utils.TokenMarshal(tID, setting.ScanKey)

	val := struct {
		TaskID string
	}{TaskID: string(idBytes)}

	return httpRet("AppV1 Active Scan Hook Task", val, nil)
}
示例#2
0
// AppCallbackScanHooksV1Handler gets callback from container and save the scan result.
func AppCallbackScanHooksV1Handler(ctx *macaron.Context) (int, []byte) {
	data, err := ctx.Req.Body().Bytes()
	if err != nil {
		log.Errorf("[%s] Req.Body.Bytes error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Req.Body.Bytes Error"})
		return http.StatusBadRequest, result
	}

	var t models.ScanHookTask
	callbackID := ctx.Params(":callbackID")
	err = t.UpdateResult(callbackID, data)
	if err != nil {
		log.Errorf("[%s] scan hook callback error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Scan Hook Callback Error"})
		return http.StatusBadRequest, result
	}

	return httpRet("AppV1 Scan Hook Callback", nil, err)
}