-
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.
Merge pull request #118 from datadrivers/feat/add-privilege-for-scrip…
…t-resources feature: privilege script api
- Loading branch information
Showing
23 changed files
with
1,106 additions
and
72 deletions.
There are no files selected for viewing
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
2 changes: 1 addition & 1 deletion
2
nexus3/pkg/security/privilege.go → nexus3/pkg/deprecated/privilege.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package security | ||
package deprecated | ||
|
||
import ( | ||
"encoding/json" | ||
|
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
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,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), | ||
} | ||
} |
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 ( | ||
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 | ||
} |
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,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) | ||
} |
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 ( | ||
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 | ||
} |
Oops, something went wrong.