Skip to content

Commit

Permalink
Merge pull request #408 from subutai-io/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
crioto authored May 23, 2018
2 parents 41d3004 + 4a4bdbe commit 160bda4
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 97 deletions.
57 changes: 45 additions & 12 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,34 @@ func GetFileScope(hash, owner string) (scope []string) {
return
}

func GetUserToken(user string) (token string) {
db.View(func(tx *bolt.Tx) error {
if b := tx.Bucket(Tokens); b != nil {
b.ForEach(func(k, v []byte) error {
log.Debug(fmt.Sprintf("(GetUserToken): Current token %s", string(k)))
if c := b.Bucket(k); c != nil {
date := new(time.Time)
date.UnmarshalText(c.Get([]byte("date")))
if date.Add(time.Hour * 24).Before(time.Now()) {
return nil
}
if value := c.Get([]byte("name")); value != nil && string(value) == user {
token = string(k)
}
}
return nil
})
}
return nil
})
if token != "" {
log.Debug(fmt.Sprintf("(GetUserToken): Found %s's valid token %s", user, token))
} else {
log.Debug(fmt.Sprintf("(GetUserToken): Couldn't find valid token of %s", user))
}
return
}

// Hash returns MD5 and SHA256 hashes by ID
func Hash(key string) (md5, sha256 string) {
db.View(func(tx *bolt.Tx) error {
Expand Down Expand Up @@ -862,43 +890,48 @@ func Tag(query string) (list []string, err error) {

// TokenOwner returns the owner of the given token
func TokenOwner(token string) (name string) {
token = fmt.Sprintf("%x", sha256.Sum256([]byte(token)))
// log.Debug(fmt.Sprintf("Checking token %+v...", token))
tokenFormatted := fmt.Sprintf("%x", sha256.Sum256([]byte(token)))
db.View(func(tx *bolt.Tx) error {
if b := tx.Bucket(Tokens).Bucket([]byte(token)); b != nil {
if b := tx.Bucket(Tokens).Bucket([]byte(tokenFormatted)); b != nil {
date := new(time.Time)
date.UnmarshalText(b.Get([]byte("date")))
if date.Add(time.Hour * 24).Before(time.Now()) {
return nil
}
if value := b.Get([]byte("name")); value != nil {
name = string(value)
}
} else if b := tx.Bucket(Tokens).Bucket([]byte(token)); b != nil {
date := new(time.Time)
date.UnmarshalText(b.Get([]byte("date")))
if date.Add(time.Hour * 24).Before(time.Now()) {
// log.Debug(fmt.Sprintf("Token %+v is too old. Deleting this token", token))
tx.Bucket(Tokens).Delete([]byte(token))
return nil
}
if value := b.Get([]byte("name")); value != nil {
name = string(value)
}
} else {
// log.Debug(fmt.Sprintf("Token %+v is not found", token))
log.Debug(fmt.Sprintf("Token %s (formatted: %s) not found", token, tokenFormatted))
}
return nil
})
log.Debug(fmt.Sprintf("Checking token %+v finished. Token corresponds to name %+v", token, name))
log.Debug(fmt.Sprintf("Checking token %s (formatted: %s) finished. Token corresponds to name %s", token, tokenFormatted, name))
return
}

// TokenFilesByRepo returns all public/private/shared files of token owner from specified repo
func TokenFilesByRepo(token string, repo string) (list []string) {
owner := TokenOwner(token)
if owner == "" {
return list
log.Debug(fmt.Sprintf("(TokenFilesByRepo): Couldn't find owner of token %s", token))
return
}
log.Debug(fmt.Sprintf("(TokenFilesByRepo): Gathering all %+v's files from repo %+v...", token, repo))
log.Debug(fmt.Sprintf("(TokenFilesByRepo): Gathering all %+v's files from repo %+v...", owner, repo))
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 {
log.Debug(fmt.Sprintf("(TokenFilesByRepo): File of %+v -- (key: %v, value: %v)", owner, string(k), string(v)))
if /* CheckShare(string(k), owner) */ filesNumber := CheckRepo(owner, []string{repo}, string(k)); filesNumber > 0 {
log.Debug(fmt.Sprintf("(OwnerFilesByRepo): Found %+v files of %+v with key %v in repo %v", filesNumber, owner, string(k), repo))
if filesNumber := CheckRepo(owner, []string{repo}, string(k)); filesNumber > 0 {
list = append(list, string(k))
}
return nil
Expand Down
148 changes: 98 additions & 50 deletions download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,64 +174,90 @@ func Info(repo string, r *http.Request) []byte {
if id != "" && name != "" && db.NameByHash(id) != name {
return nil
}
if owner != "" && token != "" && db.TokenOwner(token) != owner {
return nil
}
if token != "" && db.TokenOwner(token) == "" {
token = ""
verified = "true"
log.Debug(fmt.Sprintf("Info: provided token is invalid"))
}
list := make([]string, 0)
if id != "" {
log.Debug(fmt.Sprintf("id was provided"))
log.Info(fmt.Sprintf("id was provided"))
name = db.NameByHash(id)
list = append(list, id)
if verified != "true" {
if owner == "" && token == "" {
log.Info("Case 1")
list = []string{id}
} else if owner == "" && token != "" {
log.Info("Case 2")
list = intersect([]string{id}, intersect(db.SearchName(name), db.OwnerFilesByRepo(db.TokenOwner(token), repo)))
if len(list) == 0 {
list = intersect([]string{id}, intersect(db.SearchName(name), db.TokenFilesByRepo(token, repo)))
if len(list) == 0 {
list = intersect([]string{id}, db.SearchName(name))
}
}
} else if owner != "" && token == "" {
log.Info("Case 3")
list = intersect([]string{id}, db.OwnerFilesByRepo(owner, repo))
} else {
log.Info("Case 4")
list = intersect([]string{id}, union(db.OwnerFilesByRepo(owner, repo), intersect(db.TokenFilesByRepo(db.GetUserToken(owner), repo), db.TokenFilesByRepo(token, repo))))
}
} else {
list = []string{id}
}
} else {
if name == "" {
log.Warn(fmt.Sprintf("Both id and name were not provided"))
return nil
}
list = db.SearchName(name)
}
if owner != "" {
log.Debug(fmt.Sprintf("If #1"))
log.Debug(fmt.Sprintf("#1 List\n\n%+v\n\nintersected with\n\n%+v\n\nResulting in\n\n%+v\n\n", list, db.OwnerFilesByRepo(owner, repo), intersect(list, db.OwnerFilesByRepo(owner, repo))))
list = intersect(list, db.OwnerFilesByRepo(owner, repo))
}
if token != "" {
log.Debug(fmt.Sprintf("If #2"))
if owner == "" {
log.Debug(fmt.Sprintf("#2.1 List\n\n%+v\n\nintersected with\n\n%+v\n\nResulting in\n\n%+v\n\n", list, db.TokenFilesByRepo(token, repo), intersect(list, db.TokenFilesByRepo(token, repo))))
list = intersect(list, db.TokenFilesByRepo(token, repo))
if len(list) == 0 {
log.Debug(fmt.Sprintf("If #2.1.1"))
if owner != "" && token != "" && db.TokenOwner(token) != owner {
return nil
}
log.Info(fmt.Sprintf("name was provided"))
if verified != "true" {
if owner == "" && token == "" {
log.Info("Case 1")
list = db.SearchName(name)
verified = "true"
}
} else if owner != "" && len(list) == 0 {
log.Debug(fmt.Sprintf("#2.2 List\n\n%+v\n\nintersected with\n\n%+v\n\nResulting in\n\n%+v\n\n", list, db.TokenFilesByRepo(token, repo), intersect(list, db.TokenFilesByRepo(token, repo))))
if id != "" {
list = intersect([]string{id}, db.TokenFilesByRepo(token, repo))
} else {
} else if owner == "" && token != "" {
log.Info("Case 2")
list = intersect(db.SearchName(name), db.TokenFilesByRepo(token, repo))
onlyTokenOwner := make([]string, 0)
for _, k := range list {
if db.FileField(k, "owner")[0] == db.TokenOwner(token) {
onlyTokenOwner = append(onlyTokenOwner, k)
}
}
list = onlyTokenOwner
if len(list) == 0 {
list = intersect(db.SearchName(name), db.TokenFilesByRepo(token, repo))
if len(list) == 0 {
list = db.SearchName(name)
verified = "true"
}
}
} else if owner != "" && token == "" {
log.Info("Case 3")
list = db.OwnerFilesByRepo(owner, repo)
} else {
log.Info("Case 4")
list = intersect(db.SearchName(name), union(db.OwnerFilesByRepo(owner, repo), intersect(db.TokenFilesByRepo(db.GetUserToken(owner), repo), db.TokenFilesByRepo(token, repo))))
}
} else {
list = db.SearchName(name)
}
}
list = unique(list)
if tag != "" {
log.Debug(fmt.Sprintf("Filtering with tag %+v", tag))
listByTag, err := db.Tag(tag)
log.Check(log.DebugLevel, "Looking for artifacts with tag " + tag, err)
listByTag, _ := db.Tag(tag)
list = intersect(list, listByTag)
}
if verified == "true" {
log.Debug(fmt.Sprintf("Filtering files to verified users. List: %+v", list))
itemLatestVersion = GetVerified(list, name, repo, version)
if itemLatestVersion.ID != "" {
items = append(items, itemLatestVersion)
items[0].Signature = db.FileSignatures(items[0].ID)
}
log.Debug(fmt.Sprintf("info collected. items (1): %+v", items))
output, err := json.Marshal(items)
if err == nil && len(items) > 0 && items[0].ID != "" {
return output
Expand All @@ -244,19 +270,22 @@ func Info(repo string, r *http.Request) []byte {
p[1], _ = strconv.Atoi(pstr[1])
}
latestVersion, _ := semver.Make("")
log.Debug(fmt.Sprintf("list to be checked: %+v", list))
log.Info(fmt.Sprintf("info: list to be checked: "))
for i, k := range list {
log.Info(fmt.Sprintf("info: item %d: %s (filename: %s)", i, k, db.NameByHash(k)))
}
for _, k := range list {
if (!db.IsPublic(k) && !db.CheckShare(k, db.TokenOwner(token))) ||
(db.IsPublic(k) && len(owner) > 0 && db.CheckRepo(owner, []string{repo}, k) == 0) ||
db.CheckRepo("", []string{repo}, k) == 0 {
log.Debug(fmt.Sprintf("File %+v (name: %+v, owner: %+v, token: %+v) is ignored: %+v || %+v || %+v", k, db.NameByHash(k), owner, db.TokenOwner(token), !db.IsPublic(k) && !db.CheckShare(k, db.TokenOwner(token)), db.IsPublic(k) && len(owner) > 0 && db.CheckRepo(owner, []string{repo}, k) == 0, db.CheckRepo("", []string{repo}, k) == 0))
continue
}
if p[0]--; p[0] > 0 {
continue
}
item := FormatItem(db.Info(k), repo)
if (name == item.Name || (id != "" && repo == "template" && strings.HasPrefix(name, item.Name + "-subutai-template"))) &&
if (id == "" || id == item.ID) &&
(name == item.Name || strings.HasPrefix(name, item.Name + "-subutai-template")) &&
(version == "" || (version != "" && item.Version == version)) {
items = []ListItem{item}
itemVersion, _ := semver.Make(item.Version)
Expand All @@ -271,14 +300,13 @@ func Info(repo string, r *http.Request) []byte {
}
if len(items) == 1 {
if version == "" && repo == "template" && itemLatestVersion.ID != "" {
log.Debug(fmt.Sprintf("version == \"\" && repo == \"template\" && itemLatestVersion.ID != \"\" returns true.\nitemLatestVersion.ID = %+v", itemLatestVersion))
items[0] = itemLatestVersion
}
items[0].Signature = db.FileSignatures(items[0].ID)
}
log.Debug(fmt.Sprintf("info collected (repo: %+v, r: %+v). items (2):\n\n\n", repo, r))
for i := 0; i < len(items); i++ {
log.Debug(fmt.Sprintf("\nItem #%+v: %+v\n", i, items[i]))
log.Info(fmt.Sprintf("info: final list: "))
for i, k := range items {
log.Info(fmt.Sprintf("info: item %d: %s (filename: %s)", i, k.ID, db.NameByHash(k.ID)))
}
output, err := json.Marshal(items)
if err != nil || string(output) == "null" {
Expand All @@ -297,15 +325,24 @@ func List(repo string, r *http.Request) []byte {
owner := strings.ToLower(r.URL.Query().Get("owner"))
token := strings.ToLower(r.URL.Query().Get("token"))
version := r.URL.Query().Get("version")
list := make([]string, 0)
list = db.SearchName(name)
if owner != "" {
log.Debug(fmt.Sprintf("Owner not empty: %+v. Gathering his public files"), owner)
list = db.OwnerFilesByRepo(owner, repo)
}
if token != "" && db.TokenOwner(token) != "" {
log.Debug(fmt.Sprintf("%+v token's owner not empty: %+v. Adding his private/shared files", token, db.TokenOwner(token)))
list = append(list, db.TokenFilesByRepo(token, repo)[:]...)
if token != "" && db.TokenOwner(token) == "" {
token = ""
log.Debug(fmt.Sprintf("List: provided token is invalid"))
}
list := db.SearchName(name)
if owner == "" && token == "" {
log.Info("Case 1")
} else if owner == "" && token != "" {
log.Info("Case 2")
} else if owner != "" && token == "" {
log.Info("Case 3")
list = intersect(list, db.OwnerFilesByRepo(owner, repo))
} else {
log.Info("Case 4")
list = union(db.OwnerFilesByRepo(owner, repo),
intersect(
db.TokenFilesByRepo(db.GetUserToken(owner), repo),
db.TokenFilesByRepo(token, repo)))
}
list = unique(list)
if tag != "" {
Expand All @@ -318,7 +355,10 @@ func List(repo string, r *http.Request) []byte {
if len(pstr) == 2 {
p[1], _ = strconv.Atoi(pstr[1])
}
log.Debug(fmt.Sprintf("list to be checked: %+v", list))
log.Info(fmt.Sprintf("list: list to be checked: "))
for i, k := range list {
log.Info(fmt.Sprintf("list: item %d: %s (filename: %s)", i, k, db.NameByHash(k)))
}
for i, k := range list {
log.Debug(fmt.Sprintf("checking file #%+v: %+v", i, k))
if (!db.IsPublic(k) && !db.CheckShare(k, db.TokenOwner(token))) ||
Expand All @@ -332,15 +372,18 @@ func List(repo string, r *http.Request) []byte {
}
item := FormatItem(db.Info(k), repo)
log.Debug(fmt.Sprintf("File #%+v (hash: %+v) in formatted way: %+v", i, k, item))
if (name == "" || (name != "" && (name == item.Name || (repo == "template" && strings.HasPrefix(name, item.Name + "-subutai-template"))))) &&
if (name == "" || (name != "" && (name == item.Name || strings.HasPrefix(name, item.Name + "-subutai-template")))) &&
(version == "" || (version != "" && item.Version == version)) {
items = append(items, item)
}
if len(items) >= p[1] {
break
}
}
log.Debug(fmt.Sprintf("*** Final list of items: %+v", items))
log.Info(fmt.Sprintf("list: final list: "))
for i, k := range items {
log.Info(fmt.Sprintf("list: item %d: %s (filename: %s)", i, k.ID, db.NameByHash(k.ID)))
}
output, err := json.Marshal(items)
if err != nil {
return nil
Expand Down Expand Up @@ -461,3 +504,8 @@ func unique(list []string) []string {
}
return result
}

func union(listA []string, listB []string) []string {
listA = append(listA, listB[:]...)
return unique(listA)
}
2 changes: 1 addition & 1 deletion libgorjun/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (g *GorjunUser) Share(lists []GorjunFile, shareWith, artifactType string) (
for _, list := range lists {
locJson, err := json.Marshal(share{Token: g.Token, Id: list.ID, Add: []string{shareWith}, Remove: []string{}, Repo: artifactType})
req, err := http.NewRequest("POST", "http://127.0.0.1:8080/kurjun/rest/share", bytes.NewBuffer(locJson))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
fmt.Printf("Sending request: %+v\njson:%+v\n", req, string(locJson))
resp, err := client.Do(req)
Expand Down
Loading

0 comments on commit 160bda4

Please sign in to comment.