Skip to content

Commit

Permalink
refactor: use CheckAuthorization instead of IsAuthorized (#2319)
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD authored Jun 20, 2024
1 parent 2bb6f7b commit ca9b90f
Show file tree
Hide file tree
Showing 84 changed files with 1,358 additions and 1,214 deletions.
115 changes: 58 additions & 57 deletions changelog.md

Large diffs are not rendered by default.

32 changes: 19 additions & 13 deletions cmd/zetacored/parsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"os"
"testing"

"github.com/cometbft/cometbft/crypto"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/app"
"github.com/zeta-chain/zetacore/testutil/sample"
)

func TestParsefileToObserverMapper(t *testing.T) {
Expand All @@ -20,31 +19,38 @@ func TestParsefileToObserverMapper(t *testing.T) {
require.NoError(t, err)
}(t, file)
app.SetConfig()
createObserverList(file)

observerAddress := sample.AccAddress()
commonGrantAddress := sample.AccAddress()
validatorAddress := sample.AccAddress()

createObserverList(file, observerAddress, commonGrantAddress, validatorAddress)
obsListReadFromFile, err := ParsefileToObserverDetails(file)
require.NoError(t, err)
for _, obs := range obsListReadFromFile {
require.Equal(
t,
obs.ObserverAddress,
observerAddress,
)
require.Equal(
t,
obs.ZetaClientGranteeAddress,
sdk.AccAddress(crypto.AddressHash([]byte("ObserverGranteeAddress"))).String(),
commonGrantAddress,
)
}
}

func createObserverList(fp string) {
func createObserverList(fp string, observerAddress, commonGrantAddress, validatorAddress string) {
var listReader []ObserverInfoReader
commonGrantAddress := sdk.AccAddress(crypto.AddressHash([]byte("ObserverGranteeAddress")))
observerAddress := sdk.AccAddress(crypto.AddressHash([]byte("ObserverAddress")))
validatorAddress := sdk.ValAddress(crypto.AddressHash([]byte("ValidatorAddress")))
info := ObserverInfoReader{
ObserverAddress: observerAddress.String(),
ZetaClientGranteeAddress: commonGrantAddress.String(),
StakingGranteeAddress: commonGrantAddress.String(),
ObserverAddress: observerAddress,
ZetaClientGranteeAddress: commonGrantAddress,
StakingGranteeAddress: commonGrantAddress,
StakingMaxTokens: "100000000",
StakingValidatorAllowList: []string{validatorAddress.String()},
StakingValidatorAllowList: []string{validatorAddress},
SpendMaxTokens: "100000000",
GovGranteeAddress: commonGrantAddress.String(),
GovGranteeAddress: commonGrantAddress,
ZetaClientGranteePubKey: "zetapub1addwnpepqggtjvkmj6apcqr6ynyc5edxf2mpf5fxp2d3kwupemxtfwvg6gm7qv79fw0",
}
listReader = append(listReader, info)
Expand Down
2 changes: 1 addition & 1 deletion rpc/backend/mocks/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions testutil/keeper/authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func AuthorityKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
return &k, ctx
}

// MockIsAuthorized mocks the IsAuthorized method of an authority keeper mock
func MockIsAuthorized(m *mock.Mock, address string, policyType types.PolicyType, isAuthorized bool) {
m.On("IsAuthorized", mock.Anything, address, policyType).Return(isAuthorized).Once()
// MockCheckAuthorization mocks the CheckAuthorization method of the authority keeper.
func MockCheckAuthorization(m *mock.Mock, msg sdk.Msg, authorizationResult error) {
m.On("CheckAuthorization", mock.Anything, msg).Return(authorizationResult).Once()
}

func SetAdminPolicies(ctx sdk.Context, ak *keeper.Keeper) string {
Expand Down
17 changes: 8 additions & 9 deletions testutil/keeper/mocks/crosschain/authority.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 8 additions & 9 deletions testutil/keeper/mocks/fungible/authority.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 8 additions & 9 deletions testutil/keeper/mocks/lightclient/authority.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions testutil/keeper/mocks/observer/authority.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 1 addition & 16 deletions x/authority/keeper/authorization_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,7 @@ func (k Keeper) GetAuthorizationList(ctx sdk.Context) (val types.AuthorizationLi
return val, true
}

// IsAuthorized checks if the address is authorized for the given policy type
func (k Keeper) IsAuthorized(ctx sdk.Context, address string, policyType types.PolicyType) bool {
policies, found := k.GetPolicies(ctx)
if !found {
return false
}
for _, policy := range policies.Items {
if policy.Address == address && policy.PolicyType == policyType {
return true
}
}
return false
}

// CheckAuthorization checks if the signer is authorized to sign the message
// It uses both the authorization list and the policies to check if the signer is authorized
// CheckAuthorization uses both the authorization list and the policies to check if the signer is authorized
func (k Keeper) CheckAuthorization(ctx sdk.Context, msg sdk.Msg) error {
// Policy transactions must have only one signer
if len(msg.GetSigners()) != 1 {
Expand Down
11 changes: 5 additions & 6 deletions x/authority/keeper/msg_server_add_authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ func (k msgServer) AddAuthorization(
) (*types.MsgAddAuthorizationResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

if !k.IsAuthorized(ctx, msg.Creator, types.PolicyType_groupAdmin) {
return nil, errorsmod.Wrap(
types.ErrUnauthorized,
"AddAuthorization can only be executed by the admin policy account",
)
// check if the caller is authorized to add an authorization
err := k.CheckAuthorization(ctx, msg)
if err != nil {
return nil, errorsmod.Wrap(types.ErrUnauthorized, err.Error())
}

authorizationList, found := k.GetAuthorizationList(ctx)
Expand All @@ -31,7 +30,7 @@ func (k msgServer) AddAuthorization(
authorizationList.SetAuthorization(types.Authorization{MsgUrl: msg.MsgUrl, AuthorizedPolicy: msg.AuthorizedPolicy})

// validate the authorization list after adding the authorization as a precautionary measure.
err := authorizationList.Validate()
err = authorizationList.Validate()
if err != nil {
return nil, errorsmod.Wrap(err, "authorization list is invalid")
}
Expand Down
71 changes: 38 additions & 33 deletions x/authority/keeper/msg_server_add_authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (

func TestMsgServer_AddAuthorization(t *testing.T) {
const url = "/zetachain.zetacore.sample.ABC"
var AddAuthorization = types.Authorization{
MsgUrl: "/zetachain.zetacore.authority.MsgAddAuthorization",
AuthorizedPolicy: types.PolicyType_groupAdmin,
}
t.Run("successfully add authorization of type admin to existing authorization list", func(t *testing.T) {
k, ctx := keepertest.AuthorityKeeper(t)
admin := keepertest.SetAdminPolicies(ctx, k)
Expand Down Expand Up @@ -86,35 +90,41 @@ func TestMsgServer_AddAuthorization(t *testing.T) {
require.Equal(t, prevLen+1, len(authorizationList.Authorizations))
})

t.Run("successfully add authorization to empty authorization list", func(t *testing.T) {
k, ctx := keepertest.AuthorityKeeper(t)
admin := keepertest.SetAdminPolicies(ctx, k)
k.SetAuthorizationList(ctx, types.AuthorizationList{})
msgServer := keeper.NewMsgServerImpl(*k)

msg := &types.MsgAddAuthorization{
Creator: admin,
MsgUrl: url,
AuthorizedPolicy: types.PolicyType_groupAdmin,
}

_, err := msgServer.AddAuthorization(sdk.WrapSDKContext(ctx), msg)
require.NoError(t, err)

authorizationList, found := k.GetAuthorizationList(ctx)
require.True(t, found)
policy, err := authorizationList.GetAuthorizedPolicy(url)
require.NoError(t, err)
require.Equal(t, types.PolicyType_groupAdmin, policy)
require.Equal(t, 1, len(authorizationList.Authorizations))
})
t.Run(
"successfully add authorization to list containing only authorization for AddAuthorization",
func(t *testing.T) {
k, ctx := keepertest.AuthorityKeeper(t)
admin := keepertest.SetAdminPolicies(ctx, k)
k.SetAuthorizationList(ctx, types.AuthorizationList{
Authorizations: []types.Authorization{
AddAuthorization,
},
})
msgServer := keeper.NewMsgServerImpl(*k)

msg := &types.MsgAddAuthorization{
Creator: admin,
MsgUrl: url,
AuthorizedPolicy: types.PolicyType_groupAdmin,
}

_, err := msgServer.AddAuthorization(sdk.WrapSDKContext(ctx), msg)
require.NoError(t, err)

authorizationList, found := k.GetAuthorizationList(ctx)
require.True(t, found)
policy, err := authorizationList.GetAuthorizedPolicy(url)
require.NoError(t, err)
require.Equal(t, types.PolicyType_groupAdmin, policy)
require.Equal(t, 2, len(authorizationList.Authorizations))
},
)

t.Run("successfully set authorization when list is not found ", func(t *testing.T) {
t.Run("unable to add authorization to empty authorization list", func(t *testing.T) {
k, ctx := keepertest.AuthorityKeeper(t)
admin := keepertest.SetAdminPolicies(ctx, k)
k.SetAuthorizationList(ctx, types.AuthorizationList{})
msgServer := keeper.NewMsgServerImpl(*k)
authorizationList, found := k.GetAuthorizationList(ctx)
require.False(t, found)

msg := &types.MsgAddAuthorization{
Creator: admin,
Expand All @@ -123,14 +133,7 @@ func TestMsgServer_AddAuthorization(t *testing.T) {
}

_, err := msgServer.AddAuthorization(sdk.WrapSDKContext(ctx), msg)
require.NoError(t, err)

authorizationList, found = k.GetAuthorizationList(ctx)
require.True(t, found)
policy, err := authorizationList.GetAuthorizedPolicy(url)
require.NoError(t, err)
require.Equal(t, types.PolicyType_groupAdmin, policy)
require.Equal(t, 1, len(authorizationList.Authorizations))
require.ErrorIs(t, err, types.ErrUnauthorized)
})

t.Run("update existing authorization", func(t *testing.T) {
Expand All @@ -141,6 +144,7 @@ func TestMsgServer_AddAuthorization(t *testing.T) {
MsgUrl: "/zetachain.zetacore.sample.ABC",
AuthorizedPolicy: types.PolicyType_groupOperational,
},
AddAuthorization,
},
}
k.SetAuthorizationList(ctx, authorizationList)
Expand Down Expand Up @@ -198,6 +202,7 @@ func TestMsgServer_AddAuthorization(t *testing.T) {
MsgUrl: url,
AuthorizedPolicy: types.PolicyType_groupOperational,
},
AddAuthorization,
}}
k.SetAuthorizationList(ctx, authorizationList)
prevLen := len(authorizationList.Authorizations)
Expand Down
Loading

0 comments on commit ca9b90f

Please sign in to comment.