Skip to content

Commit

Permalink
Merge pull request #325 from subutai-io/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
emli authored Mar 21, 2018
2 parents 8936425 + 5a6aac2 commit 4a42562
Show file tree
Hide file tree
Showing 16 changed files with 949 additions and 183 deletions.
38 changes: 23 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
language: go

os:
- linux
matrix:
include:
- os: linux
dist: xenial
sudo: enabled
go:
- '1.9'
- '1.9.4'
script:
- go get
- make
- sudo apt-get install systemd
- sudo cp -f /home/travis/gopath/src/github.com/subutai-io/cdn/libgorjun/gorjun.service /etc/systemd/system/gorjun.service
- sudo systemctl daemon-reload
- sudo systemctl start gorjun.service
- sudo systemctl status gorjun.service
- sudo apt install -y rng-tools
- sudo rngd -r /dev/urandom
- gpg --gen-key --batch /home/travis/gopath/src/github.com/subutai-io/cdn/libgorjun/gpg.txt
- sudo chmod +x /home/travis/gopath/src/github.com/subutai-io/gorjun/libgorjun/register.sh
- cd /home/travis/gopath/src/github.com/subutai-io/cdn/libgorjun/; ./register.sh
- cd /home/travis/gopath/src/github.com/subutai-io/cdn/libgorjun; go get github.com/stretchr/testify/assert;
- go test -v
# #- go get
# #- make
# - sudo apt-get install systemd
# - sudo cp -f /home/travis/gopath/src/github.com/subutai-io/cdn/libgorjun/gorjun.service /etc/systemd/system/gorjun.service
# - sudo systemctl daemon-reload
# - sudo systemctl start gorjun.service
# - sudo systemctl status gorjun.service
# - sudo apt install -y rng-tools
# - sudo rngd -r /dev/urandom
# - gpg --gen-key --batch /home/travis/gopath/src/github.com/subutai-io/cdn/libgorjun/gpg.txt
# - gpg --gen-key --batch /home/travis/gopath/src/github.com/subutai-io/cdn/libgorjun/gpg_user.txt
# - gpg --gen-key --batch /home/travis/gopath/src/github.com/subutai-io/cdn/libgorjun/gpg_verified_user.txt
# - sudo chmod +x /home/travis/gopath/src/github.com/subutai-io/gorjun/libgorjun/register.sh
# # - cd /home/travis/gopath/src/github.com/subutai-io/cdn/libgorjun/; ./register.sh
# - cd /home/travis/gopath/src/github.com/subutai-io/cdn/libgorjun; go get github.com/stretchr/testify/assert;
# #- go test -v
16 changes: 8 additions & 8 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,27 @@ try {
if (env.BRANCH_NAME == 'sysnet') {
sh """
set +x
scp -P 8022 gorjun root@${cdnHost}:/tmp
ssh root@${cdnHost} -p8022 <<- EOF
scp -P 8022 gorjun ubuntu@${cdnHost}:/tmp
ssh ubuntu@${cdnHost} -p8022 <<- EOF
set -e
/bin/mv /tmp/gorjun /var/snap/subutai-sysnet/common/lxc/gorjun/opt/gorjun/bin/
subutai attach gorjun "systemctl restart gorjun.service"
sudo mv /tmp/gorjun /var/snap/subutai-sysnet/common/lxc/gorjun/opt/gorjun/bin/
sudo subutai attach gorjun "systemctl restart gorjun.service"
EOF"""
// check remote gorjun version

} else {
sh """
set +x
scp -P 8022 gorjun root@${cdnHost}:/tmp
ssh root@${cdnHost} -p8022 <<- EOF
scp -P 8022 gorjun ubuntu@${cdnHost}:/tmp
ssh ubuntu@${cdnHost} -p8022 <<- EOF
set -e
/bin/mv /tmp/gorjun /var/snap/subutai-dev/common/lxc/gorjun/opt/gorjun/bin/
sudo mv /tmp/gorjun /var/snap/subutai-dev/common/lxc/gorjun/opt/gorjun/bin/
sudo subutai attach gorjun systemctl restart gorjun
EOF"""

// check remote gorjun version
sh """
[ "${version}" == "\$(ssh root@${cdnHost} -p8022 sudo subutai attach gorjun curl -s -q http://127.0.0.1:8080/kurjun/rest/about)" ]
[ "${version}" == "\$(ssh ubuntu@${cdnHost} -p8022 sudo subutai attach gorjun curl -s -q http://127.0.0.1:8080/kurjun/rest/about)" ]
"""
}

Expand Down
1 change: 1 addition & 0 deletions apt/apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,5 @@ func Generate(w http.ResponseWriter, r *http.Request) {
return
}
GenerateReleaseFile()
w.Write([]byte("New Packages file generated and Release file signed"))
}
13 changes: 13 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,16 @@ func Sign(w http.ResponseWriter, r *http.Request) {
log.Info("File " + hash + " has been signed by " + owner)
return
}

func Owner(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("token")
owner := strings.ToLower(db.CheckToken(token))
if len(token) == 0 || len(owner) == 0 {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Not authorized"))
log.Warn(r.RemoteAddr + " - rejecting unauthorized owner request")
return
}
w.Write([]byte(owner))
return
}
92 changes: 92 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,80 @@ func Write(owner, key, value string, options ...map[string]string) {
log.Check(log.WarnLevel, "Writing data to db", err)
}

// Edit record about file in DB
func Edit(owner, key, value string, options ...map[string]string) {
if len(owner) == 0 {
owner = "subutai"
}
err := db.Update(func(tx *bolt.Tx) error {
// Associating files with user
b, _ := tx.Bucket(users).CreateBucketIfNotExists([]byte(owner))
if b, err := b.CreateBucketIfNotExists([]byte("files")); err == nil {
if v := b.Get([]byte(key)); v == nil {
// log.Warn("Associating: " + owner + " with " + value + " (" + key + ")")
b.Put([]byte(key), []byte(value))
}
}

// Editing record about file
if len(value) > 0 {
b.Put([]byte("name"), []byte(value))
}
// Editing owners, shares and tags to files
if b := tx.Bucket(bucket).Bucket([]byte(key)); b != nil {
if c := b.Bucket([]byte("owner")); len(owner) > 0 {
c.Put([]byte(owner), []byte("w"))

}
for i := range options {
for k, v := range options[i] {
switch k {
case "type":
c := b.Bucket([]byte("type"))
if c := c.Bucket([]byte(v)); len(owner) > 0 {
c.Put([]byte(owner), []byte("w"))
}
case "md5", "sha256":
if c := b.Bucket([]byte("hash")); len(k) > 0 {
c.Put([]byte(k), []byte(v))
// Getting file size
if f, err := os.Open(config.Storage.Path + v); err == nil {
fi, _ := f.Stat()
f.Close()
b.Put([]byte("size"), []byte(fmt.Sprint(fi.Size())))
}
}
case "tags":
if c := b.Bucket([]byte("tags")); len(v) > 0 {
for _, v := range strings.Split(v, ",") {
tag := []byte(strings.ToLower(strings.TrimSpace(v)))
t, _ := tx.Bucket(tags).CreateBucketIfNotExists(tag)
c.Put(tag, []byte("w"))
t.Put([]byte(key), []byte("w"))
}
}
case "signature":
if c := b.Bucket([]byte("owner")); len(v) > 0 {
c.Put([]byte(owner), []byte(v))
}
default:
{
b.Put([]byte(k), []byte(v))
}
}
}
}

if b = b.Bucket([]byte("scope")); b != nil {
if b = b.Bucket([]byte(owner)); b != nil {
}
}
}
return nil
})
log.Check(log.WarnLevel, "Editing data in db", err)
}

// Delete removes record about file from DB
func Delete(owner, repo, key string) (total int) {
db.Update(func(tx *bolt.Tx) error {
Expand Down Expand Up @@ -431,6 +505,24 @@ func UserFile(owner, file string) (list []string) {
return list
}

// All artifacts of user by repo
func All(owner string, repo string) (list []string) {
db.View(func(tx *bolt.Tx) error {
if b := tx.Bucket(users).Bucket([]byte(owner)); b != nil {
if files := b.Bucket([]byte("files")); files != nil {
files.ForEach(func(k, v []byte) error {
if CheckRepo(owner, repo, string(k)) > 0 {
list = append(list, string(k))
}
return nil
})
}
}
return nil
})
return list
}

// GetScope shows users with whom shared a certain owner of the file
func GetScope(hash, owner string) (scope []string) {
scope = []string{}
Expand Down
88 changes: 52 additions & 36 deletions download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@ import (

// ListItem describes Gorjun entity. It can be APT package, Subutai template or Raw file.
type ListItem struct {
ID string `json:"id"`
Hash hashsums `json:"hash"`
Size int `json:"size"`
Date time.Time `json:"upload-date-formatted"`
Timestamp string `json:"upload-date-timestamp,omitempty"`
Name string `json:"name,omitempty"`
Tags []string `json:"tags,omitempty"`
Owner []string `json:"owner,omitempty"`
Parent string `json:"parent,omitempty"`
Version string `json:"version,omitempty"`
Filename string `json:"filename,omitempty"`
Prefsize string `json:"prefsize,omitempty"`
Signature map[string]string `json:"signature,omitempty"`
Description string `json:"description,omitempty"`
Architecture string `json:"architecture,omitempty"`
ID string `json:"id"`
Hash hashsums `json:"hash"`
Size int `json:"size"`
Name string `json:"name,omitempty"`
Tags []string `json:"tags,omitempty"`
Owner []string `json:"owner,omitempty"`
Parent string `json:"parent,omitempty"`
ParentVersion string `json:"parent-version,omitempty"`
ParentOwner string `json:"parent-owner,omitempty"`
Version string `json:"version,omitempty"`
Filename string `json:"filename,omitempty"`
Prefsize string `json:"prefsize,omitempty"`
Signature map[string]string `json:"signature,omitempty"`
Description string `json:"description,omitempty"`
Architecture string `json:"architecture,omitempty"`
Date time.Time `json:"upload-date-formatted"`
Timestamp string `json:"upload-date-timestamp,omitempty"`
}

type hashsums struct {
Expand Down Expand Up @@ -137,6 +139,7 @@ func Info(repo string, r *http.Request) []byte {
subname := r.URL.Query().Get("subname")
version := r.URL.Query().Get("version")
verified := r.URL.Query().Get("verified")
version = processVersion(version)
if len(subname) != 0 {
name = subname
}
Expand All @@ -155,13 +158,15 @@ func Info(repo string, r *http.Request) []byte {
if len(id) > 0 {
list = append(list[:0], id)
} else if verified == "true" {
items = append(items, getVerified(list, name, repo))
items[0].Signature = db.FileSignatures(items[0].ID)
itemLatestVersion = GetVerified(list, name, repo, version)
if itemLatestVersion.ID != "" {
items = append(items, GetVerified(list, name, repo, version))
items[0].Signature = db.FileSignatures(items[0].ID)
}
output, err := json.Marshal(items)
if err == nil && len(items) > 0 && items[0].ID != "" {
return output
}
//return nil
}

pstr := strings.Split(page, ",")
Expand Down Expand Up @@ -212,6 +217,12 @@ func Info(repo string, r *http.Request) []byte {
}
return output
}
func processVersion(version string) string {
if version == "latest" {
return ""
}
return version
}

func in(str string, list []string) bool {
for _, s := range list {
Expand All @@ -222,18 +233,21 @@ func in(str string, list []string) bool {
return false
}

func getVerified(list []string, name, repo string) ListItem {
func GetVerified(list []string, name, repo string, versionTemplate string) ListItem {
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"])
if in(owner, []string{"subutai", "jenkins", "docker"}) &&
itemVersion.GTE(latestVersion) {
latestVersion = itemVersion
itemLatestVersion = FormatItem(db.Info(k), repo, name)
if in(owner, []string{"subutai", "jenkins", "docker"}) {
if itemVersion.GTE(latestVersion) && len(versionTemplate) == 0 {
latestVersion = itemVersion
itemLatestVersion = FormatItem(db.Info(k), repo, name)
} else if versionTemplate == itemVersion.String() {
itemLatestVersion = FormatItem(db.Info(k), repo, name)
}
}
}
}
Expand All @@ -250,19 +264,21 @@ func FormatItem(info map[string]string, repo, name string) ListItem {
date, _ := time.Parse(time.RFC3339Nano, info["date"])
timestamp := strconv.FormatInt(date.Unix(), 10)
item := ListItem{
ID: info["id"],
Date: date,
Hash: hashsums{Md5: info["md5"], Sha256: info["sha256"]},
Name: strings.Split(info["name"], "-subutai-template")[0],
Tags: db.FileField(info["id"], "tags"),
Owner: db.FileField(info["id"], "owner"),
Version: info["version"],
Filename: info["name"],
Parent: info["parent"],
Prefsize: info["prefsize"],
Architecture: strings.ToUpper(info["arch"]),
Description: info["Description"],
Timestamp: timestamp,
ID: info["id"],
Date: date,
Hash: hashsums{Md5: info["md5"], Sha256: info["sha256"]},
Name: strings.Split(info["name"], "-subutai-template")[0],
Tags: db.FileField(info["id"], "tags"),
Owner: db.FileField(info["id"], "owner"),
Version: info["version"],
Filename: info["name"],
Parent: info["parent"],
ParentVersion: info["parent-version"],
ParentOwner: info["parent-owner"],
Prefsize: info["prefsize"],
Architecture: strings.ToUpper(info["arch"]),
Description: info["Description"],
Timestamp: timestamp,
}
item.Size, _ = strconv.Atoi(info["size"])

Expand Down
9 changes: 8 additions & 1 deletion libgorjun/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet"
"mime/multipart"
"os/exec"
)

func (g *GorjunServer) RegisterUser(username string, publicKey string) (string, error) {
Expand Down Expand Up @@ -55,6 +56,11 @@ func (g *GorjunServer) RegisterUser(username string, publicKey string) (string,
return string(response), nil
}

func (g *GorjunServer) Register(username string) {
output, _ := exec.Command("bash", "-c", "gpg --armor --export " + username).Output()
g.RegisterUser(g.Username, string(output))
}

// AuthenticateUser will try to authenticate user by downloading his token code, signing it with GPG
// and sending it back to server to get user token
// If passphrase is not empty, PGP will try to decrypt the private key before signing the code
Expand All @@ -78,6 +84,7 @@ func (g *GorjunServer) AuthenticateUser() error {
// GetAuthTokenCode is a first step of authentication - it requests a special code from the server.
// This code needs to be PGP-signed later
func (g *GorjunServer) GetAuthTokenCode() error {
fmt.Println("Getting auth id for user " + g.Username)
resp, err := http.Get(fmt.Sprintf("http://%s/kurjun/rest/auth/token?user=%s", g.Hostname, g.Username))
if err != nil {
return fmt.Errorf("Failed to retrieve unsigned token: %v", err)
Expand Down Expand Up @@ -208,4 +215,4 @@ func (g *GorjunServer) decodePrivateKey() (*packet.PrivateKey, error) {
return nil, fmt.Errorf("Error parsing private key")
}
return key, nil
}
}
Loading

0 comments on commit 4a42562

Please sign in to comment.