Skip to content

Commit

Permalink
PR(ACP): Add HTTP Client For Adding Policy Command
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Feb 28, 2024
1 parent f3cdc1b commit 7b586d7
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
10 changes: 9 additions & 1 deletion http/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ package http

import (
"encoding/json"
"fmt"

"github.com/sourcenetwork/defradb/errors"
)

const (
errFailedToLoadKeys string = "failed to load given keys"
errFailedToLoadKeys string = "failed to load given keys"
errFailedToGetContext string = "failed to get context '%s'"
)

// Errors returnable from this package.
Expand Down Expand Up @@ -52,6 +54,12 @@ func (e *errorResponse) UnmarshalJSON(data []byte) error {
return nil
}

func NewErrFailedToGetContext(contextType string) error {
return errors.New(
fmt.Sprintf(errFailedToGetContext, contextType),
)
}

func NewErrFailedToLoadKeys(inner error, publicKeyPath, privateKeyPath string) error {
return errors.Wrap(
errFailedToLoadKeys,
Expand Down
2 changes: 2 additions & 0 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var playgroundHandler http.Handler = http.HandlerFunc(http.NotFound)
func NewApiRouter() (*Router, error) {
tx_handler := &txHandler{}
store_handler := &storeHandler{}
acp_handler := &acpHandler{}
collection_handler := &collectionHandler{}
p2p_handler := &p2pHandler{}
lens_handler := &lensHandler{}
Expand All @@ -43,6 +44,7 @@ func NewApiRouter() (*Router, error) {

tx_handler.bindRoutes(router)
store_handler.bindRoutes(router)
acp_handler.bindRoutes(router)
p2p_handler.bindRoutes(router)
ccip_handler.bindRoutes(router)

Expand Down
75 changes: 75 additions & 0 deletions http/handler_acp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2024 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package http

import (
"net/http"

"github.com/getkin/kin-openapi/openapi3"

Check failure on line 16 in http/handler_acp.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

File is not `goimports`-ed with -local github.com/sourcenetwork/defradb (goimports)
"github.com/sourcenetwork/defradb/client"
)

type acpHandler struct{}

func (s *acpHandler) AddPolicy(rw http.ResponseWriter, req *http.Request) {
db, ok := req.Context().Value(dbContextKey).(client.DB)
if !ok {
responseJSON(rw, http.StatusBadRequest, errorResponse{NewErrFailedToGetContext("db")})
return
}

var addPolicyRequest AddPolicyRequest
if err := requestJSON(req, &addPolicyRequest); err != nil {
responseJSON(rw, http.StatusBadRequest, errorResponse{err})
return
}

addPolicyResult, err := db.AddPolicy(
req.Context(),
addPolicyRequest.Identity,
addPolicyRequest.Policy,
)
if err != nil {
responseJSON(rw, http.StatusBadRequest, errorResponse{err})
return
}

responseJSON(rw, http.StatusOK, addPolicyResult)
}

func (h *acpHandler) bindRoutes(router *Router) {
successResponse := &openapi3.ResponseRef{
Ref: "#/components/responses/success",
}
errorResponse := &openapi3.ResponseRef{
Ref: "#/components/responses/error",
}
acpAddPolicySchema := &openapi3.SchemaRef{
Ref: "#/components/schemas/add_policy_request",
}

acpAddPolicyRequest := openapi3.NewRequestBody().
WithRequired(true).
WithJSONSchemaRef(acpAddPolicySchema)

acpAddPolicy := openapi3.NewOperation()
acpAddPolicy.OperationID = "add policy"
acpAddPolicy.Description = "Add a policy using acp module"
acpAddPolicy.Tags = []string{"acp_policy"}
acpAddPolicy.Responses = openapi3.NewResponses()
acpAddPolicy.Responses.Set("200", successResponse)
acpAddPolicy.Responses.Set("400", errorResponse)
acpAddPolicy.RequestBody = &openapi3.RequestBodyRef{
Value: acpAddPolicyRequest,
}

router.AddRoute("/acp/policy", http.MethodPost, acpAddPolicy, h.AddPolicy)
}
2 changes: 1 addition & 1 deletion http/handler_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ func (h *storeHandler) bindRoutes(router *Router) {
router.AddRoute("/backup/export", http.MethodPost, backupExport, h.BasicExport)
router.AddRoute("/backup/import", http.MethodPost, backupImport, h.BasicImport)
router.AddRoute("/collections", http.MethodGet, collectionDescribe, h.GetCollection)
router.AddRoute("/view", http.MethodPost, views, h.AddView)
router.AddRoute("/view", http.MethodPost, views, h.AddView) //TODO-FIX duplicate route
router.AddRoute("/view", http.MethodPost, views, h.AddView)
router.AddRoute("/graphql", http.MethodGet, graphQLGet, h.ExecRequest)
router.AddRoute("/graphql", http.MethodPost, graphQLPost, h.ExecRequest)
Expand Down
6 changes: 6 additions & 0 deletions http/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ var openApiSchemas = map[string]any{
"index": &client.IndexDescription{},
"delete_result": &client.DeleteResult{},
"update_result": &client.UpdateResult{},
// "add_policy_result": &client.AddPolicyResult{}, TODO-ACP: Remove
"lens_config": &client.LensConfig{},
"replicator": &client.Replicator{},
"ccip_request": &CCIPRequest{},
"ccip_response": &CCIPResponse{},
"patch_schema_request": &patchSchemaRequest{},
"add_view_request": &addViewRequest{},
"add_policy_request": &AddPolicyRequest{},
"migrate_request": &migrateRequest{},
"set_migration_request": &setMigrationRequest{},
}
Expand Down Expand Up @@ -134,6 +136,10 @@ func NewOpenAPISpec() (*openapi3.T, error) {
Name: "p2p",
Description: "Peer-to-peer network operations",
},
&openapi3.Tag{
Name: "acp",
Description: "Access control policy operations",
},
&openapi3.Tag{
Name: "transaction",
Description: "Database transaction operations",
Expand Down

0 comments on commit 7b586d7

Please sign in to comment.