Skip to content

Commit 6993cd6

Browse files
committed
start fixing policy tests
1 parent 152e7f5 commit 6993cd6

File tree

7 files changed

+41
-159
lines changed

7 files changed

+41
-159
lines changed

models/add_policy_request.go

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

models/policy.go

Lines changed: 4 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

models/policy_statement.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

restapi/admin_policies.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ func listPolicies(ctx context.Context, client MinioAdmin) ([]*models.Policy, err
8686
if err != nil {
8787
return nil, err
8888
}
89-
for _, policy := range policyMap {
90-
policy := parsePolicy(policy)
89+
for name, policy := range policyMap {
90+
policy, err := parsePolicy(name, policy)
91+
if err != nil {
92+
return nil, err
93+
}
9194
policies = append(policies, policy)
9295
}
9396
return policies, nil
@@ -154,7 +157,7 @@ func getRemovePolicyResponse(params admin_api.RemovePolicyParams) error {
154157
// addPolicy() takes name and policy in string format, policy
155158
// policy must be string in json format, in the future this will change
156159
// to a Policy struct{} - https://github.com/minio/minio/issues/9171
157-
func addPolicy(ctx context.Context, client MinioAdmin, name string, policy *models.Policy) (*models.Policy, error) {
160+
func addPolicy(ctx context.Context, client MinioAdmin, name, policy string) (*models.Policy, error) {
158161
policyB, err := json.Marshal(policy)
159162
if err != nil {
160163
return nil, err
@@ -189,7 +192,7 @@ func getAddPolicyResponse(params *models.AddPolicyRequest) (*models.Policy, erro
189192
// create a MinIO Admin Client interface implementation
190193
// defining the client to be used
191194
adminClient := adminClient{client: mAdmin}
192-
policy, err := addPolicy(ctx, adminClient, *params.Name, params.Policy)
195+
policy, err := addPolicy(ctx, adminClient, *params.Name, *params.Policy)
193196
if err != nil {
194197
log.Println("error adding policy")
195198
return nil, err
@@ -206,7 +209,10 @@ func policyInfo(ctx context.Context, client MinioAdmin, name string) (*models.Po
206209
if err != nil {
207210
return nil, err
208211
}
209-
policy := parsePolicy(policyRaw)
212+
policy, err := parsePolicy(name, policyRaw)
213+
if err != nil {
214+
return nil, err
215+
}
210216
return policy, nil
211217
}
212218

@@ -265,23 +271,14 @@ func getSetPolicyResponse(name string, params *models.SetPolicyRequest) error {
265271
}
266272

267273
// parsePolicy() converts from *rawPolicy to *models.Policy
268-
// Iterates over the raw statements and copied them to models.policy
269-
func parsePolicy(rawPolicy *iampolicy.Policy) *models.Policy {
270-
var statements []*models.PolicyStatement
271-
for _, rawStatement := range rawPolicy.Statements {
272-
statement := &models.PolicyStatement{
273-
Sid: string(rawStatement.SID),
274-
Action: rawStatement.Actions.String(),
275-
Effect: string(rawStatement.Effect),
276-
Resource: rawStatement.Resources.String(),
277-
Condition: rawStatement.Conditions.String(),
278-
}
279-
statements = append(statements, statement)
274+
func parsePolicy(name string, rawPolicy *iampolicy.Policy) (*models.Policy, error) {
275+
stringPolicy, err := json.Marshal(rawPolicy)
276+
if err != nil {
277+
return nil, err
280278
}
281279
policy := &models.Policy{
282-
ID: string(rawPolicy.ID),
283-
Version: rawPolicy.Version,
284-
Statements: statements,
280+
Name: name,
281+
Policy: string(stringPolicy),
285282
}
286-
return policy
283+
return policy, nil
287284
}

restapi/admin_policies_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ import (
2929
)
3030

3131
// assigning mock at runtime instead of compile time
32-
var minioListPoliciesMock func() (map[string][]byte, error)
33-
var minioGetPolicyMock func(name string) ([]byte, error)
32+
var minioListPoliciesMock func() (map[string]*iampolicy.Policy, error)
33+
var minioGetPolicyMock func(name string) (*iampolicy.Policy , error)
3434
var minioRemovePolicyMock func(name string) error
35-
var minioAddPolicyMock func(name, policy string) error
35+
var minioAddPolicyMock func(name string, policy *iampolicy.Policy) error
3636
var minioSetPolicyMock func(policyName, entityName string, isGroup bool) error
3737

3838
// mock function of listPolicies()
@@ -84,7 +84,7 @@ func TestListPolicies(t *testing.T) {
8484
},
8585
}
8686
// mock function response from listPolicies()
87-
minioListPoliciesMock = func() (map[string][]byte, error) {
87+
minioListPoliciesMock = func() (map[string]*iampolicy.Policy, error) {
8888
return mockPoliciesList, nil
8989
}
9090
// Test-1 : listPolicies() Get response from minio client with three Canned Policies and return the same number on listPolicies()
@@ -106,7 +106,7 @@ func TestListPolicies(t *testing.T) {
106106
assert.Equal(policy.Policy, assertPolicy.Policy)
107107
}
108108
// Test-3 : listPolicies() Return error and see that the error is handled correctly and returned
109-
minioListPoliciesMock = func() (map[string][]byte, error) {
109+
minioListPoliciesMock = func() (map[string]*iampolicy.Policy, error) {
110110
return nil, errors.New("error")
111111
}
112112
_, err = listPolicies(ctx, adminClient)
@@ -144,10 +144,10 @@ func TestAddPolicy(t *testing.T) {
144144
adminClient := adminClientMock{}
145145
policyName := "new-policy"
146146
policyDefinition := "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"s3:GetBucketLocation\",\"s3:GetObject\",\"s3:ListAllMyBuckets\"],\"Resource\":[\"arn:aws:s3:::*\"]}]}"
147-
minioAddPolicyMock = func(name, policy string) error {
147+
minioAddPolicyMock = func(name string, policy *iampolicy.Policy) error {
148148
return nil
149149
}
150-
minioGetPolicyMock = func(name string) (bytes []byte, err error) {
150+
minioGetPolicyMock = func(name string) (*iampolicy.Policy , error) {
151151
return []byte(policyDefinition), nil
152152
}
153153
assertPolicy := models.Policy{
@@ -163,7 +163,7 @@ func TestAddPolicy(t *testing.T) {
163163
assert.Equal(policy.Name, assertPolicy.Name)
164164
assert.Equal(policy.Policy, assertPolicy.Policy)
165165
// Test-2 : addPolicy() got an error while adding policy
166-
minioAddPolicyMock = func(name, policy string) error {
166+
minioAddPolicyMock = func(name string, policy *iampolicy.Policy) error {
167167
return errors.New("error")
168168
}
169169
if _, err := addPolicy(ctx, adminClient, policyName, policyDefinition); assert.Error(err) {

restapi/embedded_spec.go

Lines changed: 6 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swagger.yml

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -955,28 +955,9 @@ definitions:
955955
policy:
956956
type: object
957957
properties:
958-
ID:
959-
type: string
960-
Version:
961-
type: string
962-
Statements:
963-
$ref: "#/definitions/policyStatements"
964-
policyStatements:
965-
type: array
966-
items:
967-
$ref: "#/definitions/policyStatement"
968-
policyStatement:
969-
type: object
970-
properties:
971-
Sid:
972-
type: string
973-
Effect:
974-
type: string
975-
Action:
976-
type: string
977-
Resource:
958+
name:
978959
type: string
979-
Condition:
960+
policy:
980961
type: string
981962
policyEntity:
982963
type: string
@@ -1003,7 +984,7 @@ definitions:
1003984
name:
1004985
type: string
1005986
policy:
1006-
$ref: "#/definitions/policy"
987+
type: string
1007988
listPoliciesResponse:
1008989
type: object
1009990
properties:

0 commit comments

Comments
 (0)