Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

WIP: 64 client #68

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions client/api/authorize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package api

import (
"encoding/json"
"strings"

internalhttp "github.com/Tecsisa/foulkon/http"
)

func (c *ClientAPI) GetAuthorizedResources(action, resources string) (string, error) {
numResources := strings.Count("resources", ",") + 1
resourcesJson := make([]string, numResources)
if err := json.Unmarshal([]byte(resources), &resourcesJson); err != nil {
return "", err
}
body := map[string]interface{}{
"action": action,
"resources": resourcesJson,
}

req, err := c.prepareRequest("POST", internalhttp.RESOURCE_URL, body, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}
130 changes: 130 additions & 0 deletions client/api/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package api

import internalhttp "github.com/Tecsisa/foulkon/http"

func (c *ClientAPI) GetGroup(organizationId, groupName string) (string, error) {
req, err := c.prepareRequest("GET", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/groups/"+groupName, nil, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) GetAllGroups(pathPrefix, offset, limit, orderBy string) (string, error) {
urlParams := map[string]string{
"PathPrefix": pathPrefix,
"Offset": offset,
"Limit": limit,
"OrderBy": orderBy,
}
req, err := c.prepareRequest("GET", internalhttp.API_VERSION_1+"/groups", nil, urlParams)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) GetGroupsByOrg(organizationId, pathPrefix, offset, limit, orderBy string) (string, error) {
urlParams := map[string]string{
"PathPrefix": pathPrefix,
"Offset": offset,
"Limit": limit,
"OrderBy": orderBy,
}
req, err := c.prepareRequest("GET", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/groups", nil, urlParams)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) CreateGroup(organizationId, groupName, path string) (string, error) {
body := map[string]interface{}{
"name": groupName,
"path": path,
}
req, err := c.prepareRequest("POST", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/groups", body, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) UpdateGroup(organizationId, groupName, newName, newPath string) (string, error) {
body := map[string]interface{}{
"name": newName,
"path": newPath,
}
req, err := c.prepareRequest("PUT", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/groups/"+groupName, body, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) DeleteGroup(organizationId, groupName string) (string, error) {
req, err := c.prepareRequest("DELETE", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/groups/"+groupName, nil, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) GetGroupPolicies(organizationId, groupName, offset, limit, orderBy string) (string, error) {
urlParams := map[string]string{
"Offset": offset,
"Limit": limit,
"OrderBy": orderBy,
}
req, err := c.prepareRequest("GET", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/groups/"+groupName+"/policies", nil, urlParams)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) AttachPolicyToGroup(organizationId, groupName, policyName string) (string, error) {
req, err := c.prepareRequest("POST", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/groups/"+groupName+"/policies/"+policyName, nil, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) DetachPolicyFromGroup(organizationId, groupName, policyName string) (string, error) {
req, err := c.prepareRequest("DELETE", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/groups/"+groupName+"/policies/"+policyName, nil, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) GetGroupMembers(organizationId, groupName, pathPrefix, offset, limit, orderBy string) (string, error) {
urlParams := map[string]string{
"PathPrefix": pathPrefix,
"Offset": offset,
"Limit": limit,
"OrderBy": orderBy,
}
req, err := c.prepareRequest("GET", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/groups/"+groupName+"/users", nil, urlParams)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) AddMemberToGroup(organizationId, groupName, userName string) (string, error) {
req, err := c.prepareRequest("POST", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/groups/"+groupName+"/users/"+userName, nil, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) RemoveMemberFromGroup(organizationId, groupName, userName string) (string, error) {
req, err := c.prepareRequest("DELETE", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/groups/"+groupName+"/users/"+userName, nil, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}
47 changes: 47 additions & 0 deletions client/api/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package api

import "net/http"

type UserAPI interface {
GetUser(externalId string) (string, error)
GetAllUsers(pathPrefix, offset, limit, orderBy string) (string, error)
GetUserGroups(externalId, offset, limit, orderBy string) (string, error)
CreateUser(externalId, path string) (string, error)
UpdateUser(externalId, path string) (string, error)
DeleteUser(externalId string) (string, error)
}

type PolicyAPI interface {
GetPolicy(organizationId, policyName string) (string, error)
GetAllPolicies(pathPrefix, offset, limit, orderBy string) (string, error)
CreatePolicy(organizationId, policyName, path, statement string) (string, error)
UpdatePolicy(organizationId, policyName, path, statement string) (string, error)
DeletePolicy(organizationId, policyName string) (string, error)
GetGroupsAttached(organizationId, policyName, offset, limit, orderBy string) (string, error)
GetPoliciesOrganization(organizationId, pathPrefix, offset, limit, orderBy string) (string, error)
}

type GroupAPI interface {
GetGroup(organizationId, groupName string) (string, error)
GetAllGroups(pathPrefix, offset, limit, orderBy string) (string, error)
GetGroupsByOrg(organizationId, pathPrefix, offset, limit, orderBy string) (string, error)
CreateGroup(organizationId, groupName, path string) (string, error)
UpdateGroup(organizationId, groupName, newName, newPath string) (string, error)
DeleteGroup(organizationId, groupName string) (string, error)
GetGroupPolicies(organizationId, groupName, offset, limit, orderBy string) (string, error)
AttachPolicyToGroup(organizationId, groupName, policyName string) (string, error)
DetachPolicyFromGroup(organizationId, groupName, policyName string) (string, error)
GetGroupMembers(organizationId, groupName, pathPrefix, offset, limit, orderBy string) (string, error)
AddMemberToGroup(organizationId, groupName, userName string) (string, error)
RemoveMemberFromGroup(organizationId, groupName, userName string) (string, error)
}

type AuthorizeAPI interface {
GetAuthorizedResources(action, resources string) (string, error)
}

type ClientAPI struct {
Address string
requestInfo map[string]string
http.Client
}
102 changes: 102 additions & 0 deletions client/api/policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package api

import (
"encoding/json"

"github.com/Tecsisa/foulkon/api"
internalhttp "github.com/Tecsisa/foulkon/http"
)

func (c *ClientAPI) GetPolicy(organizationId, policyName string) (string, error) {
req, err := c.prepareRequest("GET", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/policies/"+policyName, nil, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) GetAllPolicies(pathPrefix, offset, limit, orderBy string) (string, error) {
urlParams := map[string]string{
"PathPrefix": pathPrefix,
"Offset": offset,
"Limit": limit,
"OrderBy": orderBy,
}
req, err := c.prepareRequest("GET", internalhttp.API_VERSION_1+"/policies", nil, urlParams)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) CreatePolicy(organizationId, policyName, path, statement string) (string, error) {

statementApi := []api.Statement{}
if err := json.Unmarshal([]byte(statement), &statementApi); err != nil {
return "", err
}
body := map[string]interface{}{
"name": policyName,
"path": path,
"Statements": statementApi,
}

req, err := c.prepareRequest("POST", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/policies", body, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) UpdatePolicy(organizationId, policyName, path, statement string) (string, error) {
statementApi := []api.Statement{}
if err := json.Unmarshal([]byte(statement), &statementApi); err != nil {
return "", err
}
body := map[string]interface{}{
"name": policyName,
"path": path,
"Statements": statementApi,
}

req, err := c.prepareRequest("PUT", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/policies/"+policyName, body, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) DeletePolicy(organizationId, policyName string) (string, error) {
req, err := c.prepareRequest("DELETE", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/policies/"+policyName, nil, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) GetGroupsAttached(organizationId, policyName, offset, limit, orderBy string) (string, error) {
urlParams := map[string]string{
"Offset": offset,
"Limit": limit,
"OrderBy": orderBy,
}
req, err := c.prepareRequest("GET", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/policies/"+policyName+"/groups", nil, urlParams)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) GetPoliciesOrganization(organizationId, pathPrefix, offset, limit, orderBy string) (string, error) {
urlParams := map[string]string{
"PathPrefix": pathPrefix,
"Offset": offset,
"Limit": limit,
"OrderBy": orderBy,
}
req, err := c.prepareRequest("GET", internalhttp.API_VERSION_1+"/organizations/"+organizationId+"/policies", nil, urlParams)
if err != nil {
return "", err
}
return c.makeRequest(req)
}
71 changes: 71 additions & 0 deletions client/api/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package api

import internalhttp "github.com/Tecsisa/foulkon/http"

func (c *ClientAPI) GetUser(externalId string) (string, error) {
req, err := c.prepareRequest("GET", internalhttp.USER_ROOT_URL+"/"+externalId, nil, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) GetAllUsers(pathPrefix, offset, limit, orderBy string) (string, error) {
urlParams := map[string]string{
"PathPrefix": pathPrefix,
"Offset": offset,
"Limit": limit,
"OrderBy": orderBy,
}
req, err := c.prepareRequest("GET", internalhttp.USER_ROOT_URL, nil, urlParams)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) GetUserGroups(externalId, offset, limit, orderBy string) (string, error) {
urlParams := map[string]string{
"Offset": offset,
"Limit": limit,
"OrderBy": orderBy,
}
req, err := c.prepareRequest("GET", internalhttp.USER_ROOT_URL+"/"+externalId+"/groups", nil, urlParams)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) CreateUser(externalId, path string) (string, error) {
body := map[string]interface{}{
"externalId": externalId,
"path": path,
}

req, err := c.prepareRequest("POST", internalhttp.USER_ROOT_URL, body, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) UpdateUser(externalId, path string) (string, error) {
body := map[string]interface{}{
"path": path,
}

req, err := c.prepareRequest("PUT", internalhttp.USER_ROOT_URL+"/"+externalId, body, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}

func (c *ClientAPI) DeleteUser(externalId string) (string, error) {
req, err := c.prepareRequest("DELETE", internalhttp.USER_ROOT_URL+"/"+externalId, nil, nil)
if err != nil {
return "", err
}
return c.makeRequest(req)
}
Loading