-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add privilege repository content selector
- Loading branch information
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
nexus3/pkg/security/privilege/repository_content_selector.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
77
nexus3/pkg/security/privilege/repository_content_selector_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters