// TestEncryptMethod func TestEncryptMethod(t *testing.T) { cases := []struct { data string expected utils.EncryptMethod }{ {"rsa", utils.EncryptRSA}, {"", utils.EncryptNone}, {"anyother", utils.EncryptNotSupported}, } for _, c := range cases { assert.Equal(t, utils.NewEncryptMethod(c.data), c.expected, "Fail to get encrypt method") } }
} return nil }, } var pushCommand = cli.Command{ Name: "push", Usage: "push a file to a repository", Action: func(context *cli.Context) error { //TODO: we can have a default server var encrypt utils.EncryptMethod argsLen := len(context.Args()) if argsLen == 4 { method := context.Args().Get(3) encrypt = utils.NewEncryptMethod(method) if encrypt == utils.EncryptNotSupported { err := fmt.Errorf("Encrypt method %s is not supported, should neither be 'none' or 'gpg'", method) fmt.Println(err) return err } } else { encrypt = utils.EncryptNone } if argsLen < 3 || argsLen > 4 { err := errors.New("wrong syntax: 'repoURL' 'fileURL' 'prefix' 'encryptMethod(default to none)'. prefix in appv1 means 'os/arch'") fmt.Println(err) return err }
// 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) }