예제 #1
0
// AppRegistScanHooksV1Handler adds a scan plugin to a user repo
// TODO: to make it easier as a start, we assume each repo could only have one scan plugin
func AppRegistScanHooksV1Handler(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
	}

	type scanPlugin struct {
		//Name should be simple name or group name
		Name string
	}
	var n scanPlugin
	err = json.Unmarshal(data, &n)
	if err != nil {
		log.Errorf("[%s] Invalid body data: %s", ctx.Req.RequestURI, err.Error())

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

	var reg models.ScanHookRegist
	namespace := ctx.Params(":namespace")
	repository := ctx.Params(":repository")
	err = reg.Regist("appv1", namespace, repository, n.Name)
	if err != nil {
		log.Errorf("[%s] scan hook regist error: %s", ctx.Req.RequestURI, err.Error())

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

	return httpRet("AppV1 Scan Hook Regist", nil, err)
}
예제 #2
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)
}