Skip to content

Commit

Permalink
Merge pull request #272 from subutai-io/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Emil authored Mar 3, 2018
2 parents a307a2e + 68be477 commit 82009ef
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: go

sudo: enabled
go:
- '1.10'
- '1.9'
script:
- go get
- make
Expand Down
14 changes: 7 additions & 7 deletions apt/apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ func getControl(control bytes.Buffer) map[string]string {
return d
}

func getSize(file string) (size string) {
func getSize(file string) (size int) {
f, err := os.Open(file)
if !log.Check(log.WarnLevel, "Opening file "+file, err) {
stat, _ := f.Stat()
f.Close()
size = strconv.Itoa(int(stat.Size()))
size = int(stat.Size())
}
return size
}
Expand All @@ -96,7 +96,7 @@ func Upload(w http.ResponseWriter, r *http.Request) {
}
meta := getControl(control)
meta["Filename"] = header.Filename
meta["Size"] = getSize(config.Storage.Path + md5)
meta["Size"] = strconv.Itoa(getSize(config.Storage.Path + md5))
meta["SHA512"] = upload.Hash(config.Storage.Path+md5, "sha512")
meta["SHA256"] = upload.Hash(config.Storage.Path+md5, "sha256")
meta["SHA1"] = upload.Hash(config.Storage.Path+md5, "sha1")
Expand All @@ -116,7 +116,7 @@ func Download(w http.ResponseWriter, r *http.Request) {
file = strings.TrimPrefix(r.RequestURI, "/kurjun/rest/apt/")
}
size := getSize(config.Storage.Path + "Packages")
if file == "Packages" && (size == "" || size == "0"){
if file == "Packages" && size == 0 {
GenerateReleaseFile()
}
if f, err := os.Open(config.Storage.Path + file); err == nil && file != "" {
Expand Down Expand Up @@ -147,7 +147,7 @@ func Info(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Not found"))
}

func renameOldDebFiles() {
func renameOldDebFiles() {
list := db.Search("")
for _, k := range list {
if db.CheckRepo("", "apt", k) == 0 {
Expand All @@ -158,7 +158,7 @@ func renameOldDebFiles() {
}
}

func GenerateReleaseFile() {
func GenerateReleaseFile() {
cmd := exec.Command("bash", "-c", "dpkg-scanpackages . /dev/null | tee Packages | gzip > Packages.gz")
cmd.Dir = config.Storage.Path
err := cmd.Run()
Expand Down Expand Up @@ -199,4 +199,4 @@ func Generate(w http.ResponseWriter, r *http.Request) {
return
}
GenerateReleaseFile()
}
}
1 change: 1 addition & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func Register(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("PGP key: " + key + "\n"))

db.RegisterUser([]byte(name), []byte(key))
log.Info("User " + name + " registired with this key " + key)
return
} else if len(r.MultipartForm.Value["key"]) > 0 {
key := pgp.Verify("Hub", r.MultipartForm.Value["key"][0])
Expand Down
18 changes: 9 additions & 9 deletions download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"strings"
"time"

"github.com/blang/semver"
"github.com/subutai-io/agent/log"
"github.com/subutai-io/gorjun/config"
"github.com/subutai-io/gorjun/db"
"github.com/blang/semver"
"net/url"
)

Expand Down Expand Up @@ -147,7 +147,7 @@ func Info(repo string, r *http.Request) []byte {
log.Check(log.DebugLevel, "Looking for artifacts with tag "+tag, err)
list = intersect(list, listByTag)
}
if onlyOneParameterProvided("name",r) {
if onlyOneParameterProvided("name", r) {
verified = "true"
}
if len(id) > 0 {
Expand All @@ -166,7 +166,7 @@ func Info(repo string, r *http.Request) []byte {
if len(pstr) == 2 {
p[1], _ = strconv.Atoi(pstr[1])
}
latestVersion, _ := semver.Make("0.0.0")
latestVersion, _ := semver.Make("")
for _, k := range list {
if (!db.Public(k) && !db.CheckShare(k, db.CheckToken(token))) ||
(len(owner) > 0 && db.CheckRepo(owner, repo, k) == 0) ||
Expand All @@ -183,8 +183,8 @@ func Info(repo string, r *http.Request) []byte {
if strings.HasSuffix(item.Version, version) || len(version) == 0 {
items = []ListItem{item}
fullname = true
itemVersion, _:= semver.Make(item.Version)
if itemVersion.Compare(latestVersion) == 1 {
itemVersion, _ := semver.Make(item.Version)
if itemVersion.GTE(latestVersion) {
latestVersion = itemVersion
itemLatestVersion = item
}
Expand Down Expand Up @@ -220,15 +220,15 @@ func in(str string, list []string) bool {
}

func getVerified(list []string, name, repo string) ListItem {
latestVersion, _ := semver.Make("0.0.0")
latestVersion, _ := semver.Make("")
var itemLatestVersion ListItem
for _, k := range list {
if info := db.Info(k); db.CheckRepo("", repo, k) > 0 {
if info["name"] == name || (strings.HasPrefix(info["name"], name+"-subutai-template") && repo == "template") {
for _, owner := range db.FileField(info["id"], "owner") {
itemVersion, _:= semver.Make(info["version"])
itemVersion, _ := semver.Make(info["version"])
if in(owner, []string{"subutai", "jenkins", "docker"}) &&
itemVersion.Compare(latestVersion) == 1 {
itemVersion.GTE(latestVersion) {
latestVersion = itemVersion
itemLatestVersion = FormatItem(db.Info(k), repo, name)
}
Expand Down Expand Up @@ -287,7 +287,7 @@ func intersect(listA, listB []string) (list []string) {
return list
}

func onlyOneParameterProvided(parameter string,r *http.Request) bool {
func onlyOneParameterProvided(parameter string, r *http.Request) bool {
u, _ := url.Parse(r.RequestURI)
parameters, _ := url.ParseQuery(u.RawQuery)
for key, _ := range parameters {
Expand Down
26 changes: 13 additions & 13 deletions libgorjun/auth_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package gorjun

import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"io/ioutil"
"math/rand"
"net/http"
"io/ioutil"
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

var r *rand.Rand // Rand for this package.
Expand All @@ -27,17 +27,17 @@ func RandomString(strlen int) string {
}

func TestGorjunServer_AuthenticateUser(t *testing.T) {
g := NewGorjunServer();
g := NewGorjunServer()
_, err := g.RegisterUser("tester", "publickey")
if err != nil {
t.Errorf("Failed to register user: %v", err)
}
}

func TestGorjunServer_RegisterUserWithMultipleKeys(t *testing.T) {
g := NewGorjunServer();
g := NewGorjunServer()
randomUserName := RandomString(10)
for i:= 1; i <= 100; i++ {
for i := 1; i <= 100; i++ {
randomKey := RandomString(100)
_, err := g.RegisterUser(randomUserName, randomKey)
if err != nil {
Expand All @@ -62,18 +62,18 @@ func TestGorjunServer_RegisterUserWithMultipleKeys(t *testing.T) {
}

func TestGorjunServer_GetKeysByOwner(t *testing.T) {
g := NewGorjunServer();
g := NewGorjunServer()
artifactTypes := [3]string{"template", "raw", "apt"}
for i:= 0; i < len(artifactTypes); i++ {
resp, err := http.Get(fmt.Sprintf("http://%s/kurjun/rest/" + artifactTypes[i] + "/list", g.Hostname))
for i := 0; i < len(artifactTypes); i++ {
resp, err := http.Get(fmt.Sprintf("http://%s/kurjun/rest/"+artifactTypes[i]+"/list", g.Hostname))
if err != nil {
fmt.Errorf("Failed to retrieve list of %s s: %v", artifactTypes[i], err)
}
data, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
var artifacts []GorjunFile
err = json.Unmarshal(data, &artifacts)
for j:= 0; j < len(artifacts); j++ {
for j := 0; j < len(artifacts); j++ {
if len(artifacts[j].Owner) > 0 {
resp, _ := http.Get(fmt.Sprintf("http://%s/kurjun/rest/auth/keys?user=%s", g.Hostname, artifacts[j].Owner[0]))
data, _ := ioutil.ReadAll(resp.Body)
Expand All @@ -87,7 +87,7 @@ func TestGorjunServer_GetKeysByOwner(t *testing.T) {

}
func TestGetAuthTokenCode(t *testing.T) {
g := NewGorjunServer();
g := NewGorjunServer()
err := g.GetAuthTokenCode()
if err != nil {
t.Errorf("Failed to retrieve token: %v", err)
Expand Down
40 changes: 20 additions & 20 deletions libgorjun/gorjun.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"time"
"log"
)

// GorjunServer is a representation for Gorjun bucket
type GorjunServer struct {
Username string // Username of gorjun user
Email string // Email used to identify user in GPG
Hostname string // Hostname of the Gorjun server
GPGDirectory string // GPGDirectory points to a gnupg directory in the file system
Token string // Active token
TokenCode string // Clean token code
Passphrase string // Passphrase used to decrypt private key
Username string // Username of gorjun user
Email string // Email used to identify user in GPG
Hostname string // Hostname of the Gorjun server
GPGDirectory string // GPGDirectory points to a gnupg directory in the file system
Token string // Active token
TokenCode string // Clean token code
Passphrase string // Passphrase used to decrypt private key
}

func NewGorjunServer() GorjunServer {
return GorjunServer{"tester", "[email protected]","127.0.0.1:8080",os.Getenv("HOME") + "/.gnupg",
"", "","pantano"}
return GorjunServer{"tester", "[email protected]", "127.0.0.1:8080", os.Getenv("HOME") + "/.gnupg",
"", "", "pantano"}
}

// GorjunFile is a file located on Gorjun bucket server
Expand Down Expand Up @@ -83,8 +83,8 @@ func (g *GorjunServer) ListUserFiles() ([]GorjunFile, error) {
}

// GetFileByName will return information about a file with specified name
func (g *GorjunServer) GetFileByName(filename string,artifactType string) ([]GorjunFile, error) {
resp, err := http.Get(fmt.Sprintf("http://%s/kurjun/rest/%s/info?name=%s&owner=%s", g.Hostname,artifactType,filename,g.Username))
func (g *GorjunServer) GetFileByName(filename string, artifactType string) ([]GorjunFile, error) {
resp, err := http.Get(fmt.Sprintf("http://%s/kurjun/rest/%s/info?name=%s&owner=%s", g.Hostname, artifactType, filename, g.Username))
if err != nil {
return nil, fmt.Errorf("Failed to retrieve file information from %s: %v", g.Hostname, err)
}
Expand Down Expand Up @@ -129,12 +129,12 @@ func (g *GorjunServer) Upload(filename string, artifactType string) (string, err

w.Close()

req, err := http.NewRequest("POST", fmt.Sprintf("http://%s/kurjun/rest/" + artifactType + "/upload", g.Hostname), &b)
req, err := http.NewRequest("POST", fmt.Sprintf("http://%s/kurjun/rest/"+artifactType+"/upload", g.Hostname), &b)
if err != nil {
return "", fmt.Errorf("Failed to create HTTP request: %v", err)
}
req.Header.Set("Content-Type", w.FormDataContentType())
req.Header.Set("token",g.Token)
req.Header.Set("token", g.Token)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
Expand All @@ -152,13 +152,13 @@ func (g *GorjunServer) Upload(filename string, artifactType string) (string, err

// RemoveFile will delete file on gorjun with specified name. If multiple files with the same
// name exists belong to the same user only the last one (most recent) will be removed
func (g *GorjunServer) RemoveFile(filename string,artifactType string) error {
file, err := g.GetFileByName(filename,artifactType)
func (g *GorjunServer) RemoveFile(filename string, artifactType string) error {
file, err := g.GetFileByName(filename, artifactType)
if err != nil {
return fmt.Errorf("Failed to get file: %v", err)
}
fmt.Printf("\nId of artifact with type %s is %s going to deleted",artifactType, file[0].ID)
return g.RemoveFileByID(file[0].ID,"raw")
fmt.Printf("\nId of artifact with type %s is %s going to deleted", artifactType, file[0].ID)
return g.RemoveFileByID(file[0].ID, "raw")
}

// RemoveFileByID will remove file with specified ID
Expand All @@ -177,8 +177,8 @@ func (g *GorjunServer) RemoveFileByID(ID string, artifactType string) error {
if res.StatusCode != http.StatusOK {
return fmt.Errorf("Can't remove file - HTTP request returned %s code", res.Status)
}
fmt.Printf("\nId of artifact with type %s is %s deleted\n",artifactType, ID)
fmt.Printf("\n%s\n",req.URL)
fmt.Printf("\nId of artifact with type %s is %s deleted\n", artifactType, ID)
fmt.Printf("\n%s\n", req.URL)
return nil
}

Expand Down
Loading

0 comments on commit 82009ef

Please sign in to comment.