Exemple #1
0
// AppDeleteFileV1Handler remove a file from a repo
func AppDeleteFileV1Handler(ctx *macaron.Context) (int, []byte) {
	// setup the repository.
	namespace := ctx.Params(":namespace")
	repository := ctx.Params(":repository")
	r, err := models.NewAppV1(namespace, repository)
	if err != nil {
		log.Errorf("[%s] setup repository error: %s", ctx.Req.RequestURI, err.Error())

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

	a := models.ArtifactV1{
		OS:   ctx.Params(":os"),
		Arch: ctx.Params(":arch"),
		App:  ctx.Params(":app"),
		Tag:  ctx.Params(":tag"),
	}

	// Remove from update service
	appV1, err := us.NewUpdateService("appV1", setting.Storage, setting.KeyManager)
	if err != nil {
		log.Errorf("[%s] create update service: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Create Update Service Error"})
		return http.StatusBadRequest, result
	}

	err = appV1.Delete(namespace+"/"+repository, a.GetName())
	if err != nil {
		log.Errorf("[%s] delete from update service error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Delete Update Service Error"})
		return http.StatusBadRequest, result
	}

	err = r.Delete(a)
	if err != nil {
		log.Errorf("[%s] delete artifact error: %s", ctx.Req.RequestURI, err.Error())

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

	return httpRet("AppV1 Delete data", nil, err)
}
Exemple #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)
}
Exemple #3
0
// AppGetFileV1Handler gets the data of a certain app
func AppGetFileV1Handler(ctx *macaron.Context) (int, []byte) {
	namespace := ctx.Params(":namespace")
	repository := ctx.Params(":repository")
	a := models.ArtifactV1{
		OS:   ctx.Params(":os"),
		Arch: ctx.Params(":arch"),
		App:  ctx.Params(":app"),
		Tag:  ctx.Params(":tag"),
	}

	appV1, _ := us.NewUpdateService("appV1", setting.Storage, setting.KeyManager)
	data, err := appV1.Get(namespace+"/"+repository, a.GetName())
	if err == nil {
		return http.StatusOK, data
	} else {
		return httpRet("AppV1 Get File", nil, err)
	}
}
Exemple #4
0
// AppPutFileV1Handler creates or updates a certain app
func AppPutFileV1Handler(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
	}

	// Query or Create the repository.
	namespace := ctx.Params(":namespace")
	repository := ctx.Params(":repository")
	r, err := models.NewAppV1(namespace, repository)
	if err != nil {
		log.Errorf("[%s] query/create repository error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Query/Create Repository Error"})
		return http.StatusBadRequest, result
	}

	reqMethod := ctx.Req.Header.Get("Dockyard-Encrypt-Method")
	encryptMethod := utils.NewEncryptMethod(reqMethod)
	if encryptMethod == utils.EncryptNotSupported {
		log.Errorf("[%s] encrypt method %s is invalid", ctx.Req.RequestURI, reqMethod)

		result, _ := json.Marshal(map[string]string{"Error": "Invalid Encrypt Method"})
		return http.StatusBadRequest, result
	}

	a := models.ArtifactV1{
		OS:            ctx.Params(":os"),
		Arch:          ctx.Params(":arch"),
		App:           ctx.Params(":app"),
		Tag:           ctx.Params(":tag"),
		EncryptMethod: string(encryptMethod),
		Size:          int64(len(data)),
	}

	// Add to update service
	appV1, err := us.NewUpdateService("appV1", setting.Storage, setting.KeyManager)
	if err != nil {
		log.Errorf("[%s] create update service: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Create Update Service Error"})
		return http.StatusBadRequest, result
	}

	tmpPath, err := appV1.Put(namespace+"/"+repository, a.GetName(), data, encryptMethod)
	if err != nil {
		log.Errorf("[%s] put to update service error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Put Update Service Error"})
		return http.StatusBadRequest, result
	}

	// Although we record the local storage path (or object storage key), we do load it by UpdateService.
	a.Path = tmpPath
	err = r.Put(a)
	if err != nil {
		log.Errorf("[%s] put artifact error: %s", ctx.Req.RequestURI, err.Error())

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

	return httpRet("AppV1 Put data", nil, err)
}