Skip to content

Commit

Permalink
implement rbac on test route
Browse files Browse the repository at this point in the history
  • Loading branch information
vrag99 committed Dec 23, 2023
1 parent 4ef1eea commit c80f792
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
45 changes: 44 additions & 1 deletion api/rbac.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package api

import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"strconv"
"strings"
Expand All @@ -12,6 +15,26 @@ import (
"github.com/sdslabs/nymeria/log"
)

func getResponse(method string, endpoint string, query *bytes.Buffer) (string, error) {
req, _ := http.NewRequest(method, endpoint, query)
req.Header.Set("Content-Type", "application/json")

client := http.Client{}
res, err := client.Do(req)

if err != nil {
return "", err
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}

return string(body), nil
}

func HandleRbac(c *gin.Context) {
log.Logger.Debug("RBAC")
cookie, err := c.Cookie("sdslabs_session")
Expand Down Expand Up @@ -41,9 +64,29 @@ func HandleRbac(c *gin.Context) {
traits := identity.GetTraits()
role := traits.(map[string]interface{})["role"]

queryRelationEndpoint := config.KetoReadURL + "/relation-tuples"
query, _ := json.Marshal(map[string]interface{}{
"namespace": "accounts",
"relation": "view",
"subject_id": role,
})

jsonQuery := bytes.NewBuffer(query)

res, err := getResponse("GET", queryRelationEndpoint, jsonQuery)

if err != nil {
log.ErrorLogger("Failed to query keto", err)
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
"message": "Initialize Rbac failed.",
})
return
}

c.JSON(http.StatusOK, gin.H{
"message": "RBAC passed",
"traits": traits,
"role": role,
"res": res,
})
}
4 changes: 3 additions & 1 deletion config/keto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
client "github.com/ory/client-go"
)

func getKetoClientConfig() (*client.Configuration, *client.Configuration){
func getKetoClientConfig() (*client.Configuration, *client.Configuration) {
readConfiguration := client.NewConfiguration()
readConfiguration.Servers = []client.ServerConfiguration{
{
Expand All @@ -24,4 +24,6 @@ func getKetoClientConfig() (*client.Configuration, *client.Configuration){

var (
KetoReadConfig, KetoWriteConfig = getKetoClientConfig()
KetoReadURL = NymeriaConfig.URL.KetoReadURL
KetoWriteURL = NymeriaConfig.URL.KetoWriteURL
)

0 comments on commit c80f792

Please sign in to comment.