MSP-7042 Add http_method to access_list
This commit is contained in:
1 parent
4932568c64
commit
e865549fdc
12 files changed
+251
-44
No files matched your search
@@ -16,6 +16,7 @@ type AccessListService interface {
|
||||
SetOne(ctx context.Context, request domain.AccessListSetOneRequest) (*domain.AccessListSetOneResponse, error)
|
||||
SetList(ctx context.Context, req domain.AccessListSetListRequest) ([]domain.MethodInfo, error)
|
||||
DeleteList(ctx context.Context, req domain.AccessListDeleteListRequest) error
|
||||
DeleteListWithMethods(ctx context.Context, req domain.AccessListDeleteV2ListRequest) error
|
||||
}
|
||||
|
||||
type AccessList struct {
|
||||
@@ -141,3 +142,30 @@ func (c AccessList) DeleteList(ctx context.Context, req domain.AccessListDeleteL
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteListWithMethods godoc
|
||||
//
|
||||
// @Tags accessList
|
||||
// @Summary Удалить список доступных методов для приложения
|
||||
// @Description Удаляет заданный список методов для приложения
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.AccessListDeleteV2ListRequest false "тело запроса"
|
||||
// @Success 200 {object} any
|
||||
// @Failure 404 {object} apierrors.Error
|
||||
// @Failure 500 {object} apierrors.Error
|
||||
// @Router /access_list/delete_list_with_methods [POST]
|
||||
func (c AccessList) DeleteListWithMethods(ctx context.Context, req domain.AccessListDeleteV2ListRequest) error {
|
||||
err := c.service.DeleteListWithMethods(ctx, req)
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrApplicationNotFound):
|
||||
return apierrors.New(
|
||||
codes.NotFound,
|
||||
domain.ErrCodeApplicationNotFound,
|
||||
fmt.Sprintf("application with id %d not found", req.AppId),
|
||||
err,
|
||||
)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
type SecureService interface {
|
||||
Authenticate(ctx context.Context, token string) (*domain.AuthData, error)
|
||||
Authorize(ctx context.Context, appId int, endpoint string) (bool, error)
|
||||
Authorize(ctx context.Context, req domain.AuthorizeRequest) (bool, error)
|
||||
}
|
||||
|
||||
type Secure struct {
|
||||
@@ -69,7 +69,7 @@ func (c Secure) Authenticate(ctx context.Context, req domain.AuthenticateRequest
|
||||
// @Failure 500 {object} apierrors.Error
|
||||
// @Router /secure/authorize [POST]
|
||||
func (c Secure) Authorize(ctx context.Context, req domain.AuthorizeRequest) (*domain.AuthorizeResponse, error) {
|
||||
result, err := c.service.Authorize(ctx, req.ApplicationId, req.Endpoint)
|
||||
result, err := c.service.Authorize(ctx, req)
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrAccessListNotFound):
|
||||
return &domain.AuthorizeResponse{
|
||||
|
||||
@@ -23,6 +23,19 @@ definitions:
|
||||
- appId
|
||||
- methods
|
||||
type: object
|
||||
domain.AccessListDeleteV2ListRequest:
|
||||
properties:
|
||||
appId:
|
||||
type: integer
|
||||
methods:
|
||||
items:
|
||||
$ref: '#/definitions/domain.Method'
|
||||
minItems: 1
|
||||
type: array
|
||||
required:
|
||||
- appId
|
||||
- methods
|
||||
type: object
|
||||
domain.AccessListSetListRequest:
|
||||
properties:
|
||||
appId:
|
||||
@@ -40,6 +53,8 @@ definitions:
|
||||
properties:
|
||||
appId:
|
||||
type: integer
|
||||
httpMethod:
|
||||
type: string
|
||||
method:
|
||||
type: string
|
||||
value:
|
||||
@@ -162,6 +177,8 @@ definitions:
|
||||
type: integer
|
||||
endpoint:
|
||||
type: string
|
||||
httpMethod:
|
||||
type: string
|
||||
required:
|
||||
- applicationId
|
||||
- endpoint
|
||||
@@ -276,8 +293,19 @@ definitions:
|
||||
required:
|
||||
- id
|
||||
type: object
|
||||
domain.Method:
|
||||
properties:
|
||||
httpMethod:
|
||||
type: string
|
||||
method:
|
||||
type: string
|
||||
required:
|
||||
- method
|
||||
type: object
|
||||
domain.MethodInfo:
|
||||
properties:
|
||||
httpMethod:
|
||||
type: string
|
||||
method:
|
||||
type: string
|
||||
value:
|
||||
@@ -432,6 +460,34 @@ paths:
|
||||
summary: Удалить список доступных методов для приложения
|
||||
tags:
|
||||
- accessList
|
||||
/access_list/delete_list_with_methods:
|
||||
post:
|
||||
consumes:
|
||||
- application/json
|
||||
description: Удаляет заданный список методов для приложения
|
||||
parameters:
|
||||
- description: тело запроса
|
||||
in: body
|
||||
name: body
|
||||
schema:
|
||||
$ref: '#/definitions/domain.AccessListDeleteV2ListRequest'
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema: {}
|
||||
"404":
|
||||
description: Not Found
|
||||
schema:
|
||||
$ref: '#/definitions/apierrors.Error'
|
||||
"500":
|
||||
description: Internal Server Error
|
||||
schema:
|
||||
$ref: '#/definitions/apierrors.Error'
|
||||
summary: Удалить список доступных методов для приложения
|
||||
tags:
|
||||
- accessList
|
||||
/access_list/get_by_id:
|
||||
post:
|
||||
consumes:
|
||||
|
||||
+17
-5
@@ -1,9 +1,10 @@
|
||||
package domain
|
||||
|
||||
type AccessListSetOneRequest struct {
|
||||
AppId int `validate:"required"`
|
||||
Method string `validate:"required"`
|
||||
Value bool
|
||||
AppId int `validate:"required"`
|
||||
HttpMethod string
|
||||
Method string `validate:"required"`
|
||||
Value bool
|
||||
}
|
||||
|
||||
type AccessListSetOneResponse struct {
|
||||
@@ -17,11 +18,22 @@ type AccessListSetListRequest struct {
|
||||
}
|
||||
|
||||
type MethodInfo struct {
|
||||
Method string
|
||||
Value bool
|
||||
HttpMethod string
|
||||
Method string
|
||||
Value bool
|
||||
}
|
||||
|
||||
type AccessListDeleteListRequest struct {
|
||||
AppId int `validate:"required"`
|
||||
Methods []string `validate:"required,min=1"`
|
||||
}
|
||||
|
||||
type AccessListDeleteV2ListRequest struct {
|
||||
AppId int `validate:"required"`
|
||||
Methods []Method `validate:"required,min=1"`
|
||||
}
|
||||
|
||||
type Method struct {
|
||||
HttpMethod string
|
||||
Method string `validate:"required"`
|
||||
}
|
||||
+2
-1
@@ -19,7 +19,8 @@ type AuthData struct {
|
||||
}
|
||||
|
||||
type AuthorizeRequest struct {
|
||||
ApplicationId int `validate:"required"`
|
||||
ApplicationId int `validate:"required"`
|
||||
HttpMethod string
|
||||
Endpoint string `validate:"required"`
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package entity
|
||||
|
||||
type AccessList struct {
|
||||
AppId int
|
||||
Method string
|
||||
Value bool
|
||||
AppId int
|
||||
HttpMethod string
|
||||
Method string
|
||||
Value bool
|
||||
}
|
||||
|
||||
type Method struct {
|
||||
HttpMethod string
|
||||
Method string
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE access_list_new
|
||||
(
|
||||
app_id INT,
|
||||
http_method VARCHAR(255) NOT NULL DEFAULT '',
|
||||
method VARCHAR(255),
|
||||
value BOOLEAN NOT NULL,
|
||||
PRIMARY KEY (app_id, http_method, method),
|
||||
CONSTRAINT FK_app_id__application_id FOREIGN KEY (app_id) REFERENCES application (id) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
INSERT INTO access_list_new (app_id, method, value)
|
||||
SELECT app_id, method, value
|
||||
FROM access_list
|
||||
WHERE value = true;
|
||||
|
||||
DROP TABLE access_list;
|
||||
ALTER TABLE access_list_new RENAME TO access_list;
|
||||
|
||||
-- +goose Down
|
||||
CREATE TABLE access_list_old
|
||||
(
|
||||
app_id INT,
|
||||
method VARCHAR(255),
|
||||
value BOOLEAN NOT NULL,
|
||||
PRIMARY KEY (app_id, method),
|
||||
CONSTRAINT FK_app_id__application_id FOREIGN KEY (app_id) REFERENCES application (id) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
INSERT INTO access_list_old (app_id, method, value)
|
||||
SELECT app_id, method, value
|
||||
FROM access_list
|
||||
WHERE value = true;
|
||||
|
||||
DROP TABLE access_list;
|
||||
ALTER TABLE access_list_old RENAME TO access_list;
|
||||
+32
-19
@@ -24,16 +24,20 @@ func NewAccessList(db db.DB) AccessList {
|
||||
}
|
||||
}
|
||||
|
||||
func (r AccessList) GetAccessListByAppIdAndMethod(ctx context.Context, appId int, method string) (*entity.AccessList, error) {
|
||||
func (r AccessList) GetAccessListByAppIdAndMethod(ctx context.Context, appId int, httpMethod string, method string) (*entity.AccessList, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "AccessList.GetAccessListByAppIdAndMethod")
|
||||
|
||||
q := `
|
||||
SELECT app_id, method, value
|
||||
FROM access_list
|
||||
WHERE app_id = $1 AND method = $2
|
||||
SELECT app_id, http_method, method, value
|
||||
FROM access_list
|
||||
WHERE app_id = $1
|
||||
AND method = $2
|
||||
AND http_method IN ($3, '')
|
||||
ORDER BY (http_method = $3) DESC
|
||||
LIMIT 1;
|
||||
`
|
||||
result := entity.AccessList{}
|
||||
err := r.db.SelectRow(ctx, &result, q, appId, method)
|
||||
err := r.db.SelectRow(ctx, &result, q, appId, method, httpMethod)
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, domain.ErrAccessListNotFound
|
||||
@@ -48,7 +52,7 @@ func (r AccessList) GetAccessListByAppId(ctx context.Context, appId int) ([]enti
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "AccessList.GetAccessListByAppId")
|
||||
|
||||
q := `
|
||||
SELECT app_id, method, value
|
||||
SELECT app_id, http_method, method, value
|
||||
FROM access_list
|
||||
WHERE app_id = $1
|
||||
`
|
||||
@@ -65,7 +69,7 @@ func (r AccessList) GetAccessListByAppIdList(ctx context.Context, appIdList []in
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "AccessList.GetAccessListByAppIdList")
|
||||
|
||||
q, args, err := query.New().
|
||||
Select("app_id", "method", "value").
|
||||
Select("app_id", "http_method", "method", "value").
|
||||
From("access_list").
|
||||
Where(squirrel.Eq{"app_id": appIdList}).
|
||||
ToSql()
|
||||
@@ -87,9 +91,9 @@ func (r AccessList) InsertArrayAccessList(ctx context.Context, entity []entity.A
|
||||
|
||||
qBuilder := query.New().
|
||||
Insert("access_list").
|
||||
Columns("app_id", "method", "value")
|
||||
Columns("app_id", "http_method", "method", "value")
|
||||
for _, e := range entity {
|
||||
qBuilder = qBuilder.Values(e.AppId, e.Method, e.Value)
|
||||
qBuilder = qBuilder.Values(e.AppId, e.HttpMethod, e.Method, e.Value)
|
||||
}
|
||||
q, args, err := qBuilder.ToSql()
|
||||
if err != nil {
|
||||
@@ -109,13 +113,13 @@ func (r AccessList) UpsertAccessList(ctx context.Context, e entity.AccessList) (
|
||||
|
||||
q := `
|
||||
INSERT INTO access_list
|
||||
(app_id, method, value)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (app_id, method)
|
||||
(app_id, http_method, method, value)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (app_id, http_method, method)
|
||||
DO UPDATE
|
||||
SET (value) = (SELECT EXCLUDED.value)
|
||||
`
|
||||
result, err := r.db.Exec(ctx, q, e.AppId, e.Method, e.Value)
|
||||
result, err := r.db.Exec(ctx, q, e.AppId, e.HttpMethod, e.Method, e.Value)
|
||||
if err != nil {
|
||||
return 0, errors.WithMessagef(err, "exec query %s", q)
|
||||
}
|
||||
@@ -128,16 +132,25 @@ func (r AccessList) UpsertAccessList(ctx context.Context, e entity.AccessList) (
|
||||
return int(rowsAffected), nil
|
||||
}
|
||||
|
||||
func (r AccessList) DeleteAccessList(ctx context.Context, appId int, methods []string) error {
|
||||
func (r AccessList) DeleteAccessList(ctx context.Context, appId int, methods []entity.Method) error {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "AccessList.DeleteAccessList")
|
||||
|
||||
methodsClause := squirrel.Or{}
|
||||
for _, method := range methods {
|
||||
methodsClause = append(methodsClause, squirrel.And{
|
||||
squirrel.Eq{"method": method.Method},
|
||||
squirrel.Eq{"http_method": method.HttpMethod},
|
||||
})
|
||||
}
|
||||
|
||||
q, args, err := query.New().
|
||||
Delete("access_list").
|
||||
Where(squirrel.Eq{
|
||||
"app_id": appId,
|
||||
"method": methods,
|
||||
Where(squirrel.And{
|
||||
squirrel.Eq{
|
||||
"app_id": appId,
|
||||
},
|
||||
methodsClause,
|
||||
}).
|
||||
Suffix("RETURNING app_id, method, value").
|
||||
ToSql()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "build query")
|
||||
@@ -157,7 +170,7 @@ func (r AccessList) DeleteAccessListByAppId(ctx context.Context, appId int) ([]e
|
||||
q := `
|
||||
DELETE FROM access_list
|
||||
WHERE app_id = $1
|
||||
RETURNING app_id, method, value
|
||||
RETURNING app_id, http_method, method, value
|
||||
`
|
||||
result := make([]entity.AccessList, 0)
|
||||
err := r.db.Select(ctx, &result, q, appId)
|
||||
|
||||
@@ -79,6 +79,11 @@ func accessListCluster(c Controllers) []cluster.EndpointDescriptor {
|
||||
Inner: true,
|
||||
Handler: c.AccessList.DeleteList,
|
||||
},
|
||||
{
|
||||
Path: "system/access_list/delete_list_with_methods",
|
||||
Inner: true,
|
||||
Handler: c.AccessList.DeleteListWithMethods,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+32
-11
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
type AccessListRepo interface {
|
||||
GetAccessListByAppId(ctx context.Context, appId int) ([]entity.AccessList, error)
|
||||
DeleteAccessList(ctx context.Context, appId int, methods []string) error
|
||||
DeleteAccessList(ctx context.Context, appId int, methods []entity.Method) error
|
||||
}
|
||||
|
||||
type AccessListSetOneTx interface {
|
||||
@@ -21,7 +21,7 @@ type AccessListSetOneTx interface {
|
||||
type AccessListSetListTx interface {
|
||||
GetAccessListByAppId(ctx context.Context, appId int) ([]entity.AccessList, error)
|
||||
InsertArrayAccessList(ctx context.Context, entity []entity.AccessList) error
|
||||
DeleteAccessList(ctx context.Context, appId int, methods []string) error
|
||||
DeleteAccessList(ctx context.Context, appId int, methods []entity.Method) error
|
||||
DeleteAccessListByAppId(ctx context.Context, appId int) ([]entity.AccessList, error)
|
||||
}
|
||||
|
||||
@@ -79,9 +79,10 @@ func (s AccessList) SetOne(ctx context.Context, request domain.AccessListSetOneR
|
||||
var resp int
|
||||
err = s.tx.AccessListSetOneTx(ctx, func(ctx context.Context, tx AccessListSetOneTx) error {
|
||||
resp, err = tx.UpsertAccessList(ctx, entity.AccessList{
|
||||
AppId: request.AppId,
|
||||
Method: request.Method,
|
||||
Value: request.Value,
|
||||
AppId: request.AppId,
|
||||
HttpMethod: request.HttpMethod,
|
||||
Method: request.Method,
|
||||
Value: request.Value,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "upsert access list")
|
||||
@@ -113,13 +114,17 @@ func (s AccessList) SetList(ctx context.Context, req domain.AccessListSetListReq
|
||||
}
|
||||
|
||||
newAccessList := make([]entity.AccessList, len(req.Methods))
|
||||
updateMethods := make([]string, len(req.Methods))
|
||||
updateMethods := make([]entity.Method, len(req.Methods))
|
||||
for i, m := range req.Methods {
|
||||
updateMethods[i] = m.Method
|
||||
updateMethods[i] = entity.Method{
|
||||
HttpMethod: m.HttpMethod,
|
||||
Method: m.Method,
|
||||
}
|
||||
newAccessList[i] = entity.AccessList{
|
||||
AppId: req.AppId,
|
||||
Method: m.Method,
|
||||
Value: m.Value,
|
||||
AppId: req.AppId,
|
||||
HttpMethod: m.HttpMethod,
|
||||
Method: m.Method,
|
||||
Value: m.Value,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,11 +163,27 @@ func (s AccessList) SetList(ctx context.Context, req domain.AccessListSetListReq
|
||||
}
|
||||
|
||||
func (s AccessList) DeleteList(ctx context.Context, req domain.AccessListDeleteListRequest) error {
|
||||
methods := make([]domain.Method, 0, len(req.Methods))
|
||||
for _, method := range req.Methods {
|
||||
methods = append(methods, domain.Method{Method: method})
|
||||
}
|
||||
return s.DeleteListWithMethods(ctx, domain.AccessListDeleteV2ListRequest{
|
||||
AppId: req.AppId,
|
||||
Methods: methods,
|
||||
})
|
||||
}
|
||||
|
||||
func (s AccessList) DeleteListWithMethods(ctx context.Context, req domain.AccessListDeleteV2ListRequest) error {
|
||||
_, err := s.appRepo.GetApplicationById(ctx, req.AppId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get application by id")
|
||||
}
|
||||
err = s.accessListRepo.DeleteAccessList(ctx, req.AppId, req.Methods)
|
||||
|
||||
methods := make([]entity.Method, 0, len(req.Methods))
|
||||
for _, method := range req.Methods {
|
||||
methods = append(methods, entity.Method{Method: method.Method, HttpMethod: method.HttpMethod})
|
||||
}
|
||||
err = s.accessListRepo.DeleteAccessList(ctx, req.AppId, methods)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "delete access_list")
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ type TokenRep interface {
|
||||
}
|
||||
|
||||
type AccessListRep interface {
|
||||
GetAccessListByAppIdAndMethod(ctx context.Context, appId int, method string) (*entity.AccessList, error)
|
||||
GetAccessListByAppIdAndMethod(ctx context.Context, appId int, httpMethod string, method string) (*entity.AccessList, error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
@@ -53,8 +53,13 @@ func (s Service) Authenticate(ctx context.Context, token string) (*domain.AuthDa
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s Service) Authorize(ctx context.Context, appId int, endpoint string) (bool, error) {
|
||||
accessList, err := s.accessListRep.GetAccessListByAppIdAndMethod(ctx, appId, endpoint)
|
||||
func (s Service) Authorize(ctx context.Context, req domain.AuthorizeRequest) (bool, error) {
|
||||
accessList, err := s.accessListRep.GetAccessListByAppIdAndMethod(
|
||||
ctx,
|
||||
req.ApplicationId,
|
||||
req.HttpMethod,
|
||||
req.Endpoint,
|
||||
)
|
||||
if err != nil {
|
||||
return false, errors.WithMessage(err, "get access list by app_id and method")
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -196,3 +197,26 @@ func (s *SecureSuite) TestAuthorize_NotFound() {
|
||||
Authorized: false,
|
||||
}, result)
|
||||
}
|
||||
|
||||
func (s *SecureSuite) TestAuthorize_WithHttpMethod_Success_True() {
|
||||
InsertAccessList(s.testDb, entity.AccessList{
|
||||
AppId: 7,
|
||||
HttpMethod: http.MethodGet,
|
||||
Method: "endpoint/available_http",
|
||||
Value: true,
|
||||
})
|
||||
|
||||
result := domain.AuthorizeResponse{}
|
||||
err := s.api.Invoke("system/secure/authorize").
|
||||
JsonRequestBody(domain.AuthorizeRequest{
|
||||
ApplicationId: 7,
|
||||
HttpMethod: http.MethodGet,
|
||||
Endpoint: "endpoint/available_http",
|
||||
}).
|
||||
JsonResponseBody(&result).
|
||||
Do(s.T().Context())
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(domain.AuthorizeResponse{
|
||||
Authorized: true,
|
||||
}, result)
|
||||
}
|
||||
Reference in new issue
Block a user