Skip to content

Commit

Permalink
optimize msp code (erda-project#913)
Browse files Browse the repository at this point in the history
Co-authored-by: erda-bot <[email protected]>
  • Loading branch information
recallsong and erda-bot authored Jul 13, 2021
1 parent bd22763 commit edf05a2
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,14 @@ package utils

import (
"context"
"net/http"

"github.com/erda-project/erda-infra/pkg/transport"
transhttp "github.com/erda-project/erda-infra/pkg/transport/http"
)

func GetHttpRequest(ctx context.Context) *http.Request {
return transhttp.ContextRequest(ctx)
}

func NewContextWithHeader(ctx context.Context) context.Context {
httpRequest := GetHttpRequest(ctx)
header := transport.Header{}
for k := range httpRequest.Header {
header.Set(k, httpRequest.Header.Get(k))
for k, vals := range transport.ContextHeader(ctx) {
header.Append(k, vals...)
}
return transport.WithHeader(context.Background(), header)
}
5 changes: 2 additions & 3 deletions modules/msp/apm/alert/alert.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,8 +858,7 @@ func (a *alertService) CreateAlertRecordIssue(ctx context.Context, request *aler
return nil, errors.NewInternalServerError(err)
}
context := utils.NewContextWithHeader(ctx)
httpRequest := utils.GetHttpRequest(context)
userId := httpRequest.Header.Get("User-ID")
userID := apis.GetUserID(ctx)
getRecordReq := &monitor.GetAlertRecordRequest{
GroupId: request.GroupId,
}
Expand All @@ -885,7 +884,7 @@ func (a *alertService) CreateAlertRecordIssue(ctx context.Context, request *aler
}
if request.Body == nil {
request.Body = make(map[string]*structpb.Value)
request.Body["creator"] = structpb.NewStringValue(userId)
request.Body["creator"] = structpb.NewStringValue(userID)
request.Body["projectID"] = structpb.NewNumberValue(float64(projectId))
}
createIssue := &apistructs.IssueCreateRequest{}
Expand Down
71 changes: 0 additions & 71 deletions modules/msp/apm/alert/permission.go

This file was deleted.

5 changes: 2 additions & 3 deletions modules/msp/apm/checker/plugins/http/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ type config struct {

// +provider
type provider struct {
Cfg *config
Log logs.Logger
traceSampleIndex int64
Cfg *config
Log logs.Logger
}

func (p *provider) Init(ctx servicehub.Context) error { return nil }
Expand Down
10 changes: 2 additions & 8 deletions pkg/common/permission/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"unicode"

"github.com/erda-project/erda-infra/pkg/transport"
transhttp "github.com/erda-project/erda-infra/pkg/transport/http"
"github.com/erda-project/erda-infra/pkg/transport/interceptor"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/pkg/common/apis"
"github.com/erda-project/erda/pkg/common/errors"
)

Expand Down Expand Up @@ -123,13 +123,7 @@ func (p *provider) Check(perms ...*Permission) transport.ServiceOption {
return nil, errors.NewPermissionError(info.Service()+"/"+info.Method(), string(perm.action), fmt.Sprintf("invalid %s id=%q", scope, id))
}

// TODO: get userID from http or grpc
var userID string
httpReq := transhttp.ContextRequest(ctx)
if httpReq != nil {
userID = httpReq.Header.Get("User-ID")
}

userID := apis.GetUserID(ctx)
resp, err := p.bdl.CheckPermission(&apistructs.PermissionCheckRequest{
UserID: userID,
Scope: apistructs.ScopeType(scope),
Expand Down

0 comments on commit edf05a2

Please sign in to comment.