Skip to content

Commit

Permalink
feat: add privilege repository content selector
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrue committed Aug 10, 2023
1 parent c84a993 commit 663d566
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
62 changes: 62 additions & 0 deletions nexus3/pkg/security/privilege/repository_content_selector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package privilege

import (
"fmt"
"net/http"

"github.com/datadrivers/go-nexus-client/nexus3/pkg/client"
"github.com/datadrivers/go-nexus-client/nexus3/pkg/tools"
"github.com/datadrivers/go-nexus-client/nexus3/schema/security"
)

const (
securityContentSelectorAPIEndpoint = securityPrivilegesAPIEndpoint + "/repository-content-selector"
)

type SecurityPrivilegeContentSelectorService struct {
client *client.Client

// Script *SecurityPrivilegeContentSelectorService
}

func NewSecurityPrivilegeContentSelectorService(c *client.Client) *SecurityPrivilegeContentSelectorService {
return &SecurityPrivilegeContentSelectorService{
client: c,
}
}

func (s *SecurityPrivilegeContentSelectorService) Create(p security.PrivilegeRepositoryContentSelector) error {
ioReader, err := tools.JsonMarshalInterfaceToIOReader(p)
if err != nil {
return err
}

body, resp, err := s.client.Post(securityContentSelectorAPIEndpoint, ioReader)
if err != nil {
return err
}

if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusCreated {
return fmt.Errorf("could not create privilege \"%s\": HTTP: %d, %s", p.Name, resp.StatusCode, string(body))
}

return nil
}

func (s *SecurityPrivilegeContentSelectorService) Update(name string, p security.PrivilegeRepositoryContentSelector) error {
ioReader, err := tools.JsonMarshalInterfaceToIOReader(p)
if err != nil {
return err
}

body, resp, err := s.client.Put(fmt.Sprintf("%s/%s", securityContentSelectorAPIEndpoint, p.Name), ioReader)
if err != nil {
return err
}

if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("could not update privilege \"%s\": HTTP %d, %s", name, resp.StatusCode, string(body))
}

return nil
}
77 changes: 77 additions & 0 deletions nexus3/pkg/security/privilege/repository_content_selector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package privilege_test

import (
"fmt"
"testing"

"github.com/datadrivers/go-nexus-client/nexus3/pkg/security"
"github.com/datadrivers/go-nexus-client/nexus3/pkg/security/privilege"
"github.com/datadrivers/go-nexus-client/nexus3/pkg/tools"
schemasecurity "github.com/datadrivers/go-nexus-client/nexus3/schema/security"
"github.com/stretchr/testify/assert"
)

func getTestPrivilegeRepositoryContentSelector(name string, description string, actions []string, format string, repository string, contentSelector string) *schemasecurity.PrivilegeRepositoryContentSelector {
return &schemasecurity.PrivilegeRepositoryContentSelector{
Name: name,
Description: description,
Actions: actions,
Format: format,
Repository: repository,
ContentSelector: contentSelector,
}
}

func getTestContentSelector(name string, description string, expression string) *schemasecurity.ContentSelector {
return &schemasecurity.ContentSelector{
Name: name,
Description: description,
Expression: expression,
}
}

func TestContentSelectorPrivilegeSecurity(t *testing.T) {
contentSelectorName := fmt.Sprintf("content-selector-%d", tools.GetSeededRandomInteger(999))
contentSelectorExpression := `format == "npm" or (format == "maven2" and path =~ "^/org/apache/commons/.*")`
privilegeRepositoryContentSelectorName := fmt.Sprintf("content-selector-privilege%d", tools.GetSeededRandomInteger(999))
testService := privilege.NewSecurityPrivilegeContentSelectorService(getTestClient())
contentSelectorService := security.NewSecurityContentSelectorService(getTestClient())
privilegeService := getSecurityPrivilegeService()

// Create Content Selector Object
err := contentSelectorService.Create(*getTestContentSelector(contentSelectorName, "description", contentSelectorExpression))
assert.Nil(t, err)

// Create repository-content-selector-privilege object for already existing Maven repo (was created by Nexus itself)
contentSelectorPrivilege := getTestPrivilegeRepositoryContentSelector(privilegeRepositoryContentSelectorName, "descr", []string{"ADD"}, "maven2", "maven-snapshots", contentSelectorName)
err = testService.Create(*contentSelectorPrivilege)
assert.Nil(t, err)

// Fetch recently created repository-content-selector-privilege object and do some checks
contentSelectorPrivilegeFetched, err := privilegeService.Get(privilegeRepositoryContentSelectorName)
assert.Nil(t, err)
assert.Equal(t, privilegeRepositoryContentSelectorName, contentSelectorPrivilegeFetched.Name)
assert.Equal(t, "descr", contentSelectorPrivilegeFetched.Description)
assert.Equal(t, []string{"ADD"}, contentSelectorPrivilegeFetched.Actions)
assert.Equal(t, "maven2", contentSelectorPrivilegeFetched.Format)
assert.Equal(t, "maven-snapshots", contentSelectorPrivilegeFetched.Repository)
assert.Equal(t, contentSelectorName, contentSelectorPrivilegeFetched.ContentSelector)

// Update repository-content-selector-privilege object
contentSelectorPrivilege = getTestPrivilegeRepositoryContentSelector(privilegeRepositoryContentSelectorName, "demo descrp", []string{"BROWSE", "READ", "EDIT", "ADD", "DELETE"}, "maven2", "maven-snapshots", contentSelectorName)
err = testService.Update(privilegeRepositoryContentSelectorName, *contentSelectorPrivilege)
assert.Nil(t, err)
contentSelectorPrivilegeFetched, err = privilegeService.Get(privilegeRepositoryContentSelectorName)
assert.Nil(t, err)
assert.Equal(t, privilegeRepositoryContentSelectorName, contentSelectorPrivilegeFetched.Name)
assert.Equal(t, []string{"BROWSE", "READ", "EDIT", "ADD", "DELETE"}, contentSelectorPrivilegeFetched.Actions)

// // Delete repository-content-selector-privilege-object
err = privilegeService.Delete(privilegeRepositoryContentSelectorName)
assert.Nil(t, err)

// Check for successful deletion
contentSelectorPrivilegeFetched, err = privilegeService.Get(privilegeRepositoryContentSelectorName)
assert.Error(t, err)
assert.Nil(t, contentSelectorPrivilegeFetched)
}
16 changes: 16 additions & 0 deletions nexus3/schema/security/privilege.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,19 @@ type PrivilegeRepositoryAdmin struct {
Format string `json:"format"`
Repository string `json:"repository"`
}

type PrivilegeRepositoryContentSelector struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Actions []string `json:"actions"`
Format string `json:"format"`
Repository string `json:"repository"`
ContentSelector string `json:"contentSelector"`
}

type PrivilegeApplication struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Actions []string `json:"actions"`
Domain string `json:"domain"`
}

0 comments on commit 663d566

Please sign in to comment.