Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
feat(permissions v2): add update and get functions (#53)
Browse files Browse the repository at this point in the history
* feat(permissions v2): add update and get functions

* fmt
  • Loading branch information
JordanSussman authored Apr 8, 2020
1 parent a0ff7fb commit 17dbfca
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 0 deletions.
64 changes: 64 additions & 0 deletions artifactory/artifactory-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions artifactory/fixtures/permissions/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ func FakeHandler() http.Handler {

e.GET("/api/security/permissions", getPermissions)
e.GET("/api/security/permissions/:target", getPermission)
e.GET("/api/v2/security/permissions/:target", getV2Permission)
e.PUT("/api/security/permissions/:target", createPermission)
e.PUT("/api/v2/security/permissions/:target", updatePermission)
e.DELETE("/api/security/permissions/:target", deletePermission)
e.HEAD("/api/v2/security/permissions/:target", getExistence)

Expand Down Expand Up @@ -75,6 +77,32 @@ func getExistence(c *gin.Context) {
}
}

func updatePermission(c *gin.Context) {
target := c.Param("target")

switch target {
case "valid":
c.Status(201)
return
case "invalid":
c.JSON(400, loadFixture("fixtures/permissions/invalid.json"))
return
}
}

func getV2Permission(c *gin.Context) {
target := c.Param("target")

switch target {
case "java-developers":
c.JSON(200, loadFixture("fixtures/permissions/permissionv2.json"))
return
default:
c.JSON(404, loadFixture("fixtures/permissions/non-existent.json"))
return
}
}

func loadFixture(file string) string {
data, _ := ioutil.ReadFile(file)

Expand Down
6 changes: 6 additions & 0 deletions artifactory/fixtures/permissions/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"status": 400,
"message": "Permission target contains a reference to a non-existing repository 'foobarstar'."
}
]
6 changes: 6 additions & 0 deletions artifactory/fixtures/permissions/non-existent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"errors" : [ {
"status" : 404,
"message" : "Not Found"
} ]
}
48 changes: 48 additions & 0 deletions artifactory/fixtures/permissions/permissionv2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "java-developers",
"repo": {
"include-patterns": ["**"],
"exclude-patterns": [""],
"repositories": ["local-rep1", "local-rep2", "remote-rep1", "virtual-rep2"],
"actions": {
"users": {
"bob": ["read", "write", "manage"],
"alice": ["write", "annotate", "read"]
},
"groups": {
"dev-leads": ["manage", "read", "annotate"],
"readers": ["read"]
}
}
},
"build": {
"include-patterns": [""],
"exclude-patterns": [""],
"repositories": ["artifactory-build-info"],
"actions": {
"users": {
"bob": ["read", "write", "manage"],
"alice": ["write", "annotate", "read"]
},
"groups": {
"dev-leads": ["manage", "read", "annotate"],
"readers": ["read"]
}
}
},
"releaseBundle": {
"include-patterns": ["**"],
"exclude-patterns": [""],
"repositories": ["release-bundles"],
"actions": {
"users": {
"bob": ["read", "write", "manage"],
"alice": ["write", "annotate", "read"]
},
"groups": {
"dev-leads": ["manage", "read", "annotate"],
"readers": ["read"]
}
}
}
}
50 changes: 50 additions & 0 deletions artifactory/permissionsv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,53 @@ func (s *PermissionsServiceV2) Exists(target string) (bool, error) {

return true, nil
}

// PermissionDetails represents the information about the repo, build, or releasebundle within the permission target.
type PermissionDetails struct {
IncludePatterns *[]string `json:"include-patterns,omitempty"`
ExcludePatterns *[]string `json:"exclude-patterns,omitempty"`
Repositories *[]string `json:"repositories,omitempty"`
Actions *Actions `json:"actions,omitempty"`
}

// Actions represents user and group permissions.
type Actions struct {
Users *map[string][]string `json:"users,omitempty"`
Groups *map[string][]string `json:"groups,omitempty"`
}

// PermissionTargetV2 represents a v2 permission target.
//
// Docs: https://www.jfrog.com/confluence/display/JFROG/Security+Configuration+JSON#SecurityConfigurationJSON-application/vnd.org.jfrog.artifactory.security.PermissionTargetV2+json
type PermissionTargetV2 struct {
Name *string `json:"name,omitempty"`
Repo *PermissionDetails `json:"repo,omitempty"`
Build *PermissionDetails `json:"build,omitempty"`
ReleaseBundle *PermissionDetails `json:"releaseBundle,omitempty"`
}

func (p PermissionTargetV2) String() string {
return Stringify(p)
}

// Update creates a new permission target or replaces an existing permission target.
//
// Docs: https://www.jfrog.com/confluence/display/JFROG/Artifactory+REST+API+V2#ArtifactoryRESTAPIV2-UpdatePermissionTarget
func (s *PermissionsServiceV2) Update(target *PermissionTargetV2) (*string, *Response, error) {
u := fmt.Sprintf("/api/v2/security/permissions/%s", *target.Name)
v := new(string)

resp, err := s.client.Call("PUT", u, target, v)
return v, resp, err
}

// Get returns the provided permission target.
//
// Docs: https://www.jfrog.com/confluence/display/JFROG/Artifactory+REST+API+V2#ArtifactoryRESTAPIV2-GetPermissionTargetDetails
func (s *PermissionsServiceV2) Get(target string) (*PermissionTargetV2, *Response, error) {
u := fmt.Sprintf("/api/v2/security/permissions/%s", target)
v := new(PermissionTargetV2)

resp, err := s.client.Call("GET", u, nil, v)
return v, resp, err
}
84 changes: 84 additions & 0 deletions artifactory/permissionsv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package artifactory

import (
"encoding/json"
"io/ioutil"
"net/http/httptest"
"testing"

Expand All @@ -36,6 +38,49 @@ func Test_PermissionsV2(t *testing.T) {

g := goblin.Goblin(t)
g.Describe("PermissionsV2 Service", func() {
target := &PermissionTargetV2{}
users := make(map[string][]string)
groups := make(map[string][]string)

g.BeforeEach(func() {
users["bob"] = []string{"read", "write", "manage"}
users["alice"] = []string{"write", "annotate", "read"}

groups["dev-leads"] = []string{"manage", "read", "annotate"}
groups["readers"] = []string{"read"}

target = &PermissionTargetV2{
Name: String("java-developers"),
Repo: &PermissionDetails{
IncludePatterns: &[]string{"**"},
ExcludePatterns: &[]string{""},
Repositories: &[]string{"local-rep1", "local-rep2", "remote-rep1", "virtual-rep2"},
Actions: &Actions{
Users: &users,
Groups: &groups,
},
},
Build: &PermissionDetails{
IncludePatterns: &[]string{""},
ExcludePatterns: &[]string{""},
Repositories: &[]string{"artifactory-build-info"},
Actions: &Actions{
Users: &users,
Groups: &groups,
},
},
ReleaseBundle: &PermissionDetails{
IncludePatterns: &[]string{"**"},
ExcludePatterns: &[]string{""},
Repositories: &[]string{"release-bundles"},
Actions: &Actions{
Users: &users,
Groups: &groups,
},
},
}
})

// Close http test server after we're done using it
g.After(func() {
s.Close()
Expand All @@ -54,6 +99,45 @@ func Test_PermissionsV2(t *testing.T) {
g.Assert(actual).Equal(false)
g.Assert(err).Equal(nil)
})

g.It("- should return no error with update", func() {
actual, resp, err := c.PermissionsV2.Update(target)
g.Assert(resp.StatusCode).Equal(200)
g.Assert(actual != nil).IsTrue()
g.Assert(err).Equal(nil)
})

g.It("- should return error with update", func() {
target.Name = String("invalid")
actual, resp, err := c.PermissionsV2.Update(target)
g.Assert(resp.StatusCode).Equal(400)
g.Assert(actual != nil).IsTrue()
g.Assert(err != nil).IsTrue()
})

g.It("- should return valid string for PermissionTargetV2 with String()", func() {
data, _ := ioutil.ReadFile("fixtures/permissions/permissionv2.json")

var expected PermissionTargetV2
_ = json.Unmarshal(data, &expected)

g.Assert(target.String() == expected.String()).IsTrue()
})

g.It("- should return no error with get", func() {
actual, resp, err := c.PermissionsV2.Get("java-developers")
g.Assert(resp.StatusCode).Equal(200)
g.Assert(actual != nil).Equal(true)
g.Assert(err).Equal(nil)
})

g.It("- should return error with get due to non existent permission", func() {
actual, resp, err := c.PermissionsV2.Get("foobar")
g.Assert(resp.StatusCode).Equal(404)
g.Assert(actual != nil).IsTrue()
g.Assert(err != nil).IsTrue()
})

})
})
}

0 comments on commit 17dbfca

Please sign in to comment.