Skip to content

Commit

Permalink
Merge pull request #118 from datadrivers/feat/add-privilege-for-scrip…
Browse files Browse the repository at this point in the history
…t-resources

feature: privilege script api
  • Loading branch information
fabrue authored Aug 11, 2023
2 parents 8c39ffb + abf64cd commit 2835fa3
Show file tree
Hide file tree
Showing 23 changed files with 1,106 additions and 72 deletions.
3 changes: 3 additions & 0 deletions nexus3/nexus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nexus3
import (
"github.com/datadrivers/go-nexus-client/nexus3/pkg/blobstore"
"github.com/datadrivers/go-nexus-client/nexus3/pkg/client"
"github.com/datadrivers/go-nexus-client/nexus3/pkg/deprecated"
"github.com/datadrivers/go-nexus-client/nexus3/pkg/repository"
"github.com/datadrivers/go-nexus-client/nexus3/pkg/security"
)
Expand All @@ -25,6 +26,7 @@ type NexusClient struct {
Security *security.SecurityService
Script *ScriptService
MailConfig *MailConfigService
Deprecated *deprecated.DeprecatedService
}

// NewClient returns an instance of client that implements the Client interface
Expand All @@ -38,5 +40,6 @@ func NewClient(config client.Config) *NexusClient {
Security: security.NewSecurityService(client),
Script: NewScriptService(client),
MailConfig: NewMailConfigService(client),
Deprecated: deprecated.NewDeprecatedService(client),
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package security
package deprecated

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
package security
package deprecated

import (
"fmt"
"testing"
"time"

"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"
"github.com/stretchr/testify/assert"
)

var (
testClient *client.Client = nil
)

func getDefaultConfig() client.Config {
return client.Config{
Insecure: tools.GetEnv("NEXUS_INSECURE_SKIP_VERIFY", true).(bool),
Password: tools.GetEnv("NEXUS_PASSWORD", "admin123").(string),
URL: tools.GetEnv("NEXUS_URL", "http://127.0.0.1:8081").(string),
Username: tools.GetEnv("NEXUS_USRNAME", "admin").(string),
}
}

func getTestClient() *client.Client {
if testClient != nil {
return testClient
}
return client.NewClient(getDefaultConfig())
}

func getTestService() *SecurityPrivilegeService {
return NewSecurityPrivilegeService(getTestClient())
}

func TestPrivileges(t *testing.T) {
service := getTestService()

privs, err := service.Privilege.List()
privs, err := service.List()
assert.Nil(t, err)
assert.NotNil(t, privs)
assert.Greater(t, len(privs), 0)
}

func TestPrivilegeTypeWildcardRead(t *testing.T) {
service := getTestService()
privName := "nx-all"

priv, err := service.Privilege.Get(privName)
priv, err := service.Get(privName)
assert.Nil(t, err)
assert.NotNil(t, priv)
if priv != nil {
Expand All @@ -39,7 +64,7 @@ func TestPrivilegeTypeAnalyticsRead(t *testing.T) {
service := getTestService()
privName := "nx-analytics-all"

priv, err := service.Privilege.Get(privName)
priv, err := service.Get(privName)
assert.Nil(t, err)
assert.NotNil(t, priv)
if priv != nil {
Expand All @@ -59,7 +84,7 @@ func TestPrivilegeTypeApplicationRead(t *testing.T) {
service := getTestService()
privName := "nx-apikey-all"

priv, err := service.Privilege.Get(privName)
priv, err := service.Get(privName)
assert.Nil(t, err)
assert.NotNil(t, priv)
if priv != nil {
Expand All @@ -79,7 +104,7 @@ func TestPrivilegeTypeRepositoryAdminRead(t *testing.T) {
service := getTestService()
privName := "nx-repository-admin-*-*-*"

priv, err := service.Privilege.Get(privName)
priv, err := service.Get(privName)
assert.Nil(t, err)
assert.NotNil(t, priv)
if priv != nil {
Expand All @@ -98,7 +123,7 @@ func TestPrivilegeTypeRepositoryViewRead(t *testing.T) {
service := getTestService()
privName := "nx-repository-view-*-*-*"

priv, err := service.Privilege.Get(privName)
priv, err := service.Get(privName)
assert.Nil(t, err)
assert.NotNil(t, priv)
if priv != nil {
Expand All @@ -117,10 +142,10 @@ func TestPrivilegeCreateReadUpdateDelete(t *testing.T) {
service := getTestService()
privilege := testPrivilege("test-privilege")

err := service.Privilege.Create(privilege)
err := service.Create(privilege)
assert.Nil(t, err)

createdPrivilege, err := service.Privilege.Get(privilege.Name)
createdPrivilege, err := service.Get(privilege.Name)
assert.Nil(t, err)
assert.NotNil(t, createdPrivilege)

Expand All @@ -133,19 +158,19 @@ func TestPrivilegeCreateReadUpdateDelete(t *testing.T) {
createdPrivilege.Description = "updated"
createdPrivilege.Domain = "datastores"

err = service.Privilege.Update(privilege.Name, *createdPrivilege)
err = service.Update(privilege.Name, *createdPrivilege)
assert.Nil(t, err)

updatedPrivilege, err := service.Privilege.Get(privilege.Name)
updatedPrivilege, err := service.Get(privilege.Name)
assert.Nil(t, err)
assert.NotNil(t, updatedPrivilege)
assert.Equal(t, createdPrivilege.Description, updatedPrivilege.Description)
assert.Equal(t, createdPrivilege.Domain, updatedPrivilege.Domain)

err = service.Privilege.Delete(privilege.Name)
err = service.Delete(privilege.Name)
assert.Nil(t, err)

deletedPrivilege, err := service.Privilege.Get(privilege.Name)
deletedPrivilege, err := service.Get(privilege.Name)
assert.Nil(t, err)
assert.Nil(t, deletedPrivilege)
}
Expand All @@ -159,5 +184,3 @@ func testPrivilege(name string) security.Privilege {
Type: "application",
}
}

// Testfunction TestPrivilegeTypeScriptCreateReadAndDelete located in main client folder, because this test function use multiple services
24 changes: 24 additions & 0 deletions nexus3/pkg/deprecated/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package deprecated

import (
"github.com/datadrivers/go-nexus-client/nexus3/pkg/client"
)

const (
securityAPIEndpoint = client.BasePath + "v1/security"
)

type DeprecatedService struct {
client *client.Client

// API Services
Privilege *SecurityPrivilegeService
}

func NewDeprecatedService(c *client.Client) *DeprecatedService {
return &DeprecatedService{
client: c,

Privilege: NewSecurityPrivilegeService(c),
}
}
62 changes: 62 additions & 0 deletions nexus3/pkg/security/privilege/application.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 (
securityPrivilegesApplicationAPIEndpoint = securityPrivilegesAPIEndpoint + "/application"
)

type SecurityPrivilegeApplicationService struct {
client *client.Client

// Script *SecurityPrivilegeApplicationService
}

func NewSecurityPrivilegeApplicationService(c *client.Client) *SecurityPrivilegeApplicationService {
return &SecurityPrivilegeApplicationService{
client: c,
}
}

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

body, resp, err := s.client.Post(securityPrivilegesApplicationAPIEndpoint, 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 *SecurityPrivilegeApplicationService) Update(name string, p security.PrivilegeApplication) error {
ioReader, err := tools.JsonMarshalInterfaceToIOReader(p)
if err != nil {
return err
}

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

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

return nil
}
57 changes: 57 additions & 0 deletions nexus3/pkg/security/privilege/application_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 getTestPrivilegeApplication(name string, description string, actions []string, domain string) *schemasecurity.PrivilegeApplication {
return &schemasecurity.PrivilegeApplication{
Name: name,
Description: description,
Actions: actions,
Domain: domain,
}
}

func TestApplicationPrivilegeSecurity(t *testing.T) {
PrivilegeApplicationName := fmt.Sprintf("application-%d", tools.GetSeededRandomInteger(999))
testService := privilege.NewSecurityPrivilegeApplicationService(getTestClient())
privilegeService := privilege.NewSecurityPrivilegeService(getTestClient())

// Create application-privilege object
applicationPrivilege := getTestPrivilegeApplication(PrivilegeApplicationName, "demo descrp", []string{"READ"}, "domain")
err := testService.Create(*applicationPrivilege)
assert.Nil(t, err)

// Fetch recently created application-privilege object and do some checks
applicationPrivilegeFetched, err := privilegeService.Get(PrivilegeApplicationName)
assert.Nil(t, err)
assert.Equal(t, PrivilegeApplicationName, applicationPrivilegeFetched.Name)
assert.Equal(t, "demo descrp", applicationPrivilegeFetched.Description)
assert.Equal(t, []string{"READ"}, applicationPrivilegeFetched.Actions)

// Update application-privilege object
applicationPrivilege = getTestPrivilegeApplication(PrivilegeApplicationName, "demo descrp updated", []string{"ADD", "READ", "DELETE", "ASSOCIATE"}, "domain")
err = testService.Update(PrivilegeApplicationName, *applicationPrivilege)
assert.Nil(t, err)
applicationPrivilegeFetched, err = privilegeService.Get(PrivilegeApplicationName)
assert.Nil(t, err)
assert.Equal(t, "demo descrp updated", applicationPrivilegeFetched.Description)
assert.Equal(t, []string{"ADD", "READ", "DELETE", "ASSOCIATE"}, applicationPrivilegeFetched.Actions)
assert.Equal(t, "domain", applicationPrivilege.Domain)

// Delete application-privilege-object
err = privilegeService.Delete(PrivilegeApplicationName)
assert.Nil(t, err)

// Check for successful deletion
applicationPrivilegeFetched, err = privilegeService.Get(PrivilegeApplicationName)
assert.Error(t, err)
assert.Nil(t, applicationPrivilegeFetched)
}
62 changes: 62 additions & 0 deletions nexus3/pkg/security/privilege/repository_admin.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 (
securityRepositoryAdminAPIEndpoint = securityPrivilegesAPIEndpoint + "/repository-admin"
)

type SecurityPrivilegeRepositoryAdminService struct {
client *client.Client

// Script *SecurityPrivilegeRepositoryAdminService
}

func NewSecurityPrivilegeRepositoryAdminService(c *client.Client) *SecurityPrivilegeRepositoryAdminService {
return &SecurityPrivilegeRepositoryAdminService{
client: c,
}
}

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

body, resp, err := s.client.Post(securityRepositoryAdminAPIEndpoint, 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 *SecurityPrivilegeRepositoryAdminService) Update(name string, p security.PrivilegeRepositoryAdmin) error {
ioReader, err := tools.JsonMarshalInterfaceToIOReader(p)
if err != nil {
return err
}

body, resp, err := s.client.Put(fmt.Sprintf("%s/%s", securityRepositoryAdminAPIEndpoint, 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
}
Loading

0 comments on commit 2835fa3

Please sign in to comment.