Files
tasks/common/authorization/interceptor_test.go
Tim Deeb-SwihartandGitHub dae78a6d83 Rename metricDefinition getters to follow Go's guidelines (#5185)
## What changed?
I renamed the getters on `metricDefinition` to follow go's guidelines.

## Why?
Not only does it save typing, it's better Go.

Quoth [Effective Go](https://go.dev/doc/effective_go#Getters):

> ...it's neither idiomatic nor necessary to put Get into the getter's
name. If you have a field called owner (lower case, unexported), the
getter method should be called Owner (upper case, exported), not
GetOwner.

## How did you test it?
Our current tests are sufficient

## Potential risks
None

## Is hotfix candidate?
No.
2023-11-30 16:34:29 -08:00

214 lines
7.3 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package authorization
import (
"context"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.temporal.io/api/workflowservice/v1"
"go.temporal.io/api/workflowservicemock/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/metrics"
)
const (
testNamespace string = "test-namespace"
)
var (
ctx = context.Background()
describeNamespaceRequest = &workflowservice.DescribeNamespaceRequest{Namespace: testNamespace}
describeNamespaceTarget = &CallTarget{Namespace: testNamespace, Request: describeNamespaceRequest, APIName: "/temporal.api.workflowservice.v1.WorkflowService/DescribeNamespace"}
describeNamespaceInfo = &grpc.UnaryServerInfo{FullMethod: "/temporal.api.workflowservice.v1.WorkflowService/DescribeNamespace"}
startWorkflowExecutionRequest = &workflowservice.StartWorkflowExecutionRequest{Namespace: testNamespace}
startWorkflowExecutionTarget = &CallTarget{Namespace: testNamespace, Request: startWorkflowExecutionRequest, APIName: "/temporal.api.workflowservice.v1.WorkflowService/StartWorkflowExecution"}
startWorkflowExecutionInfo = &grpc.UnaryServerInfo{FullMethod: "/temporal.api.workflowservice.v1.WorkflowService/StartWorkflowExecution"}
)
type (
authorizerInterceptorSuite struct {
suite.Suite
*require.Assertions
controller *gomock.Controller
mockFrontendHandler *workflowservicemock.MockWorkflowServiceServer
mockAuthorizer *MockAuthorizer
mockMetricsHandler *metrics.MockHandler
interceptor grpc.UnaryServerInterceptor
handler grpc.UnaryHandler
mockClaimMapper *MockClaimMapper
}
)
func TestAuthorizerInterceptorSuite(t *testing.T) {
s := new(authorizerInterceptorSuite)
suite.Run(t, s)
}
func (s *authorizerInterceptorSuite) SetupTest() {
s.Assertions = require.New(s.T())
s.controller = gomock.NewController(s.T())
s.mockFrontendHandler = workflowservicemock.NewMockWorkflowServiceServer(s.controller)
s.mockAuthorizer = NewMockAuthorizer(s.controller)
s.mockMetricsHandler = metrics.NewMockHandler(s.controller)
s.mockMetricsHandler.EXPECT().WithTags(metrics.OperationTag(metrics.AuthorizationScope)).Return(s.mockMetricsHandler).AnyTimes()
s.mockMetricsHandler.EXPECT().Timer(metrics.ServiceAuthorizationLatency.Name()).Return(metrics.NoopTimerMetricFunc).AnyTimes()
s.mockClaimMapper = NewMockClaimMapper(s.controller)
s.interceptor = NewAuthorizationInterceptor(
s.mockClaimMapper,
s.mockAuthorizer,
s.mockMetricsHandler,
log.NewNoopLogger(),
nil,
"",
"",
)
s.handler = func(ctx context.Context, req interface{}) (interface{}, error) { return true, nil }
}
func (s *authorizerInterceptorSuite) TearDownTest() {
s.controller.Finish()
}
func (s *authorizerInterceptorSuite) TestIsAuthorized() {
s.mockAuthorizer.EXPECT().Authorize(ctx, nil, describeNamespaceTarget).
Return(Result{Decision: DecisionAllow}, nil)
res, err := s.interceptor(ctx, describeNamespaceRequest, describeNamespaceInfo, s.handler)
s.True(res.(bool))
s.NoError(err)
}
func (s *authorizerInterceptorSuite) TestIsAuthorizedWithNamespace() {
s.mockAuthorizer.EXPECT().Authorize(ctx, nil, startWorkflowExecutionTarget).
Return(Result{Decision: DecisionAllow}, nil)
res, err := s.interceptor(ctx, startWorkflowExecutionRequest, startWorkflowExecutionInfo, s.handler)
s.True(res.(bool))
s.NoError(err)
}
func (s *authorizerInterceptorSuite) TestIsUnauthorized() {
s.mockAuthorizer.EXPECT().Authorize(ctx, nil, describeNamespaceTarget).
Return(Result{Decision: DecisionDeny}, nil)
s.mockMetricsHandler.EXPECT().Counter(metrics.ServiceErrUnauthorizedCounter.Name()).Return(metrics.NoopCounterMetricFunc)
res, err := s.interceptor(ctx, describeNamespaceRequest, describeNamespaceInfo, s.handler)
s.Nil(res)
s.Error(err)
}
func (s *authorizerInterceptorSuite) TestAuthorizationFailed() {
s.mockAuthorizer.EXPECT().Authorize(ctx, nil, describeNamespaceTarget).
Return(Result{Decision: DecisionDeny}, errUnauthorized)
s.mockMetricsHandler.EXPECT().Counter(metrics.ServiceErrAuthorizeFailedCounter.Name()).Return(metrics.NoopCounterMetricFunc)
res, err := s.interceptor(ctx, describeNamespaceRequest, describeNamespaceInfo, s.handler)
s.Nil(res)
s.Error(err)
}
func (s *authorizerInterceptorSuite) TestNoopClaimMapperWithoutTLS() {
admin := &Claims{System: RoleAdmin}
s.mockAuthorizer.EXPECT().Authorize(gomock.Any(), admin, describeNamespaceTarget).
DoAndReturn(func(ctx context.Context, caller *Claims, target *CallTarget) (Result, error) {
// check that claims are present in ctx also
s.Equal(admin, ctx.Value(MappedClaims))
return Result{Decision: DecisionAllow}, nil
})
interceptor := NewAuthorizationInterceptor(
NewNoopClaimMapper(),
s.mockAuthorizer,
s.mockMetricsHandler,
log.NewNoopLogger(),
nil,
"",
"",
)
_, err := interceptor(ctx, describeNamespaceRequest, describeNamespaceInfo, s.handler)
s.NoError(err)
}
func (s *authorizerInterceptorSuite) TestAlternateHeaders() {
interceptor := NewAuthorizationInterceptor(
s.mockClaimMapper,
NewNoopAuthorizer(),
s.mockMetricsHandler,
log.NewNoopLogger(),
nil,
"custom-header",
"custom-extra-header",
)
cases := []struct {
md metadata.MD
authInfo *AuthInfo
}{
{
metadata.Pairs(
"custom-header", "the-token",
"custom-extra-header", "more stuff",
),
&AuthInfo{
AuthToken: "the-token",
ExtraData: "more stuff",
},
},
{
metadata.Pairs(
"custom-header", "the-token",
"authorization-extras", "this gets ignored",
),
&AuthInfo{
AuthToken: "the-token",
},
},
{
metadata.Pairs(
"custom-extra-header", "missing main header, this gets ignored",
),
nil,
},
}
for _, testCase := range cases {
if testCase.authInfo != nil {
s.mockClaimMapper.EXPECT().GetClaims(testCase.authInfo).Return(&Claims{System: RoleAdmin}, nil)
}
inCtx := metadata.NewIncomingContext(ctx, testCase.md)
_, err := interceptor(inCtx, describeNamespaceRequest, describeNamespaceInfo, s.handler)
s.NoError(err)
}
}