Skip to content

Commit

Permalink
Merge pull request #473 from baytulakova/403
Browse files Browse the repository at this point in the history
  • Loading branch information
crioto authored Jun 14, 2018
2 parents 6439491 + 0dff026 commit 1c89fe1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
42 changes: 41 additions & 1 deletion apt/apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -80,22 +81,32 @@ func getSize(file string) (size int) {
}

func Upload(w http.ResponseWriter, r *http.Request) {
log.Info("Start uploading the file")
if r.Method == "POST" {
_, header, _ := r.FormFile("file")
if e := db.IsFileExists(header.Filename); e == true {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Failed to upload apt with the same name"))
return
}
md5, sha256, owner := upload.Handler(w, r)
if len(md5) == 0 || len(sha256) == 0 {
log.Info("Md5 or sha256 is empty. Failed to calculate the hash")
return
}
log.Info(fmt.Sprintf("Starting to read deb package %v", header.Filename))
control, err := readDeb(header.Filename)
if err != nil {
log.Warn(err.Error())
log.Warn("Reading deb package finished with error: ", err.Error())
w.WriteHeader(http.StatusUnsupportedMediaType)
w.Write([]byte(err.Error()))
if db.Delete(owner, "apt", md5) == 0 {
log.Info(fmt.Sprintf("Removed file %v", config.Storage.Path+md5))
os.Remove(config.Storage.Path + md5)
}
return
}
log.Info("Starting to read control file of deb package")
meta := getControl(control)
meta["Filename"] = header.Filename
meta["Size"] = strconv.Itoa(getSize(config.Storage.Path + header.Filename))
Expand All @@ -108,14 +119,17 @@ func Upload(w http.ResponseWriter, r *http.Request) {
meta["tag"] = tags
my_uuid, err := uuid.NewV4()
if err != nil {
log.Warn("Failed to generate UUID")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
ID := my_uuid.String()
db.AddTag(strings.Split(tags, ","), ID, "apt")
log.Info(fmt.Sprintf("Writing deb package %v into database", header.Filename))
err = db.Write(owner, ID, header.Filename, meta)
if err != nil {
log.Warn("Failed writing record into database")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
Expand All @@ -134,24 +148,28 @@ func Upload(w http.ResponseWriter, r *http.Request) {

func Download(w http.ResponseWriter, r *http.Request) {
file := r.URL.Query().Get("hash")
log.Info(fmt.Sprintf("Starting download the deb package %v", file))
if len(file) == 0 {
file = strings.TrimPrefix(r.RequestURI, "/kurjun/rest/apt/")
}
size := getSize(config.Storage.Path + "Packages")
if file == "Packages" && size == 0 {
GenerateReleaseFile()
}
log.Info(fmt.Sprintf("Opening file %v", config.Storage.Path+file))
if f, err := os.Open(config.Storage.Path + file); err == nil && file != "" {
defer f.Close()
io.Copy(w, f)
} else {
log.Info(fmt.Sprintf("File %v not found", config.Storage.Path+file))
w.WriteHeader(http.StatusNotFound)
}
}

func Delete(w http.ResponseWriter, r *http.Request) {
if r.Method == "DELETE" {
if hash := upload.Delete(w, r); len(hash) != 0 {
log.Info(fmt.Sprintf("Removed file with hash %v", hash))
w.Write([]byte("Removed"))
return
}
Expand Down Expand Up @@ -184,6 +202,7 @@ func List(w http.ResponseWriter, r *http.Request) {
}

func renameOldDebFiles() {
log.Info("Starting rename old deb files")
list := db.SearchName("")
for _, k := range list {
if db.CheckRepo("", []string{"apt"}, k) == 0 {
Expand All @@ -192,48 +211,69 @@ func renameOldDebFiles() {
item := download.FormatItem(db.Info(k), "apt")
os.Rename(config.Storage.Path+item.Hash.Md5, config.Storage.Path+item.Name)
}
log.Info("Finished rename old deb packages")
}

func GenerateReleaseFile() {
log.Warn("Starting GenerateReleaseFiles")
cmd := exec.Command("bash", "-c", "dpkg-scanpackages . /dev/null | tee Packages | gzip > Packages.gz")
cmd.Dir = config.Storage.Path
log.Warn("config.Storage.Path: ", config.Storage.Path)
log.Warn("Cmd.Dir: ", cmd.Dir)
log.Warn("Running command to run dpkg-scanpackages and waiting for it to finish")
err := cmd.Run()
if err != nil {
log.Fatal(err)
log.Info("Can't run dpkg-scanpackages")
}
log.Warn(fmt.Sprintf("Command to run dpkg-scanpackages finished with error %v", err))

cmd = exec.Command("bash", "-c", "apt-ftparchive release . > Release")
cmd.Dir = config.Storage.Path
log.Warn("config.Storage.Path: ", config.Storage.Path)
log.Warn("Cmd.Dir: ", cmd.Dir)
log.Warn("Running command to run apt-ftparchive and waiting for it to finish")
err = cmd.Run()
if err != nil {
log.Fatal(err)
log.Info("Can't run apt-ftparchive")
}
log.Warn(fmt.Sprintf("Command to run apt-ftparchive finished with error %v", err))

cmd = exec.Command("bash", "-c", "gpg --batch --yes --armor -u [email protected] -abs -o Release.gpg Release")
cmd.Dir = config.Storage.Path
log.Warn("config.Storage.Path: ", config.Storage.Path)
log.Warn("Cmd.Dir: ", cmd.Dir)
log.Warn("Running command to sign Realease file and waiting for it to finish")
err = cmd.Run()
if err != nil {
log.Fatal(err)
log.Info("Can't sign Realease file")
}
log.Warn(fmt.Sprintf("Command to sign Realease file finished with error %v", err))
log.Warn("Finished GenerateReleaseFiles")
}

func Generate(w http.ResponseWriter, r *http.Request) {
log.Info("Starting Generate")
token := strings.ToLower(r.Header.Get("token"))
owner := strings.ToLower(db.TokenOwner(token))
if len(token) == 0 || len(owner) == 0 {
log.Warn("Not authorized user wanted to generate release file")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Not authorized"))
log.Warn(r.RemoteAddr + " - rejecting generate request")
return
}
if owner != "subutai" && owner != "jenkins" {
log.Warn("Not allowed user wanted to generate release file")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Only allowed users can generate release file"))
log.Warn(r.RemoteAddr + " - rejecting generate request")
return
}
log.Info("Generating release file")
GenerateReleaseFile()
w.Write([]byte("New Packages file generated and Release file signed"))
log.Info("New Packages file generated and Release file signed")
}
13 changes: 13 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -663,6 +664,18 @@ func OwnerFilesByRepo(owner string, repo string) (list []string) {
return
}

func IsFileExists(filename string) bool {
exist := false
files, _ := ioutil.ReadDir(config.Storage.Path)
for _, file := range files {
if file.Name() == filename {
exist = true
return exist
}
}
return exist
}

func PrintBucketName(buckets []string) (path string) {
path = "tx"
for i := range buckets {
Expand Down
4 changes: 2 additions & 2 deletions upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func Handler(w http.ResponseWriter, r *http.Request) (md5sum, sha256sum, owner s
return
}
if repo[3] != "apt" {
log.Debug(fmt.Sprintf("repo[3] is not apt. Renaming %+v to %+v", config.Storage.Path + header.Filename, config.Storage.Path + md5sum))
os.Rename(config.Storage.Path + header.Filename, config.Storage.Path + md5sum)
log.Debug(fmt.Sprintf("repo[3] is not apt. Renaming %+v to %+v", config.Storage.Path+header.Filename, config.Storage.Path+md5sum))
os.Rename(config.Storage.Path+header.Filename, config.Storage.Path+md5sum)
}
log.Info("File received: " + header.Filename + "(" + md5sum + ")")
return md5sum, sha256sum, owner
Expand Down

0 comments on commit 1c89fe1

Please sign in to comment.