Skip to content

Commit

Permalink
feat: add privilege wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrue committed Aug 10, 2023
1 parent 868bb1e commit 9bd08d5
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
62 changes: 62 additions & 0 deletions nexus3/pkg/security/privilege/wildcard.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 (
securityWildcardAPIEndpoint = securityPrivilegesAPIEndpoint + "/wildcard"
)

type SecurityPrivilegeWildcardService struct {
client *client.Client

// Script *SecurityPrivilegeWildcardService
}

func NewSecurityPrivilegeWildcardService(c *client.Client) *SecurityPrivilegeWildcardService {
return &SecurityPrivilegeWildcardService{
client: c,
}
}

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

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

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

return nil
}

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

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

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

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

import (
"fmt"
"testing"

"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 getTestPrivilegeWildcard(name string, description string, pattern string) *schemasecurity.PrivilegeWildcard {
return &schemasecurity.PrivilegeWildcard{
Name: name,
Description: description,
Pattern: pattern,
}
}

func TestWildcardPrivilegeSecurity(t *testing.T) {
privilegeWildcardName := fmt.Sprintf("wildcard-%d", tools.GetSeededRandomInteger(999))
testService := privilege.NewSecurityPrivilegeWildcardService(getTestClient())
privilegeService := privilege.NewSecurityPrivilegeService(getTestClient())

// Create wildcard-privilege object
wildcardPrivilege := getTestPrivilegeWildcard(privilegeWildcardName, "demo descrp", "nexus:*")
err := testService.Create(*wildcardPrivilege)
assert.Nil(t, err)

// Fetch recently created wildcard-privilege object and do some checks
wildcardPrivilegeFetched, err := privilegeService.Get(privilegeWildcardName)
assert.Nil(t, err)
assert.Equal(t, privilegeWildcardName, wildcardPrivilegeFetched.Name)
assert.Equal(t, "demo descrp", wildcardPrivilegeFetched.Description)
assert.Equal(t, "nexus:*", wildcardPrivilegeFetched.Pattern)

// Update wildcard-privilege object
wildcardPrivilege = getTestPrivilegeWildcard(privilegeWildcardName, "demo descrp updated", "nexus:nexus")
err = testService.Update(privilegeWildcardName, *wildcardPrivilege)
assert.Nil(t, err)
wildcardPrivilegeFetched, err = privilegeService.Get(privilegeWildcardName)
assert.Nil(t, err)
assert.Equal(t, "demo descrp updated", wildcardPrivilegeFetched.Description)
assert.Equal(t, "nexus:nexus", wildcardPrivilegeFetched.Pattern)

// Delete wildcard-privilege-object
err = privilegeService.Delete(privilegeWildcardName)
assert.Nil(t, err)

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

type PrivilegeWildcard struct {
Name string `json:"name"`
Pattern string `json:"pattern"`
Description string `json:"description,omitempty"`
}

0 comments on commit 9bd08d5

Please sign in to comment.