From 12f7ae83fd4603e7b4926df5b898e774ea9522a0 Mon Sep 17 00:00:00 2001 From: Lingfei Kong Date: Thu, 8 Jul 2021 07:54:03 +0800 Subject: [PATCH] docs(changelog): add CHANGELOG-1.0.1.md --- CHANGELOG/CHANGELOG-1.0.1.md | 6 +- api/swagger/docs/user_swagger.go | 2 +- api/swagger/swagger.yaml | 2 +- internal/apiserver/api/v1/cache/cache.go | 106 --------- internal/apiserver/api/v1/cache/cache_test.go | 211 ------------------ internal/apiserver/api/v1/policy/create.go | 46 ---- internal/apiserver/api/v1/policy/delete.go | 28 --- .../api/v1/policy/delete_collection.go | 28 --- internal/apiserver/api/v1/policy/doc.go | 6 - internal/apiserver/api/v1/policy/get.go | 28 --- internal/apiserver/api/v1/policy/list.go | 37 --- internal/apiserver/api/v1/policy/policy.go | 24 -- internal/apiserver/api/v1/policy/update.go | 54 ----- internal/apiserver/api/v1/secret/create.go | 68 ------ internal/apiserver/api/v1/secret/delete.go | 27 --- .../api/v1/secret/delete_collection.go | 32 --- internal/apiserver/api/v1/secret/doc.go | 6 - internal/apiserver/api/v1/secret/get.go | 28 --- internal/apiserver/api/v1/secret/list.go | 36 --- internal/apiserver/api/v1/secret/secret.go | 24 -- internal/apiserver/api/v1/secret/update.go | 58 ----- .../apiserver/api/v1/user/change_password.go | 61 ----- .../api/v1/user/change_password_test.go | 77 ------- internal/apiserver/api/v1/user/create.go | 44 ---- internal/apiserver/api/v1/user/create_test.go | 68 ------ internal/apiserver/api/v1/user/delete.go | 27 --- .../api/v1/user/delete_collection.go | 29 --- .../api/v1/user/delete_collection_test.go | 65 ------ internal/apiserver/api/v1/user/delete_test.go | 66 ------ internal/apiserver/api/v1/user/doc.go | 6 - internal/apiserver/api/v1/user/get.go | 27 --- internal/apiserver/api/v1/user/get_test.go | 77 ------- internal/apiserver/api/v1/user/list.go | 37 --- internal/apiserver/api/v1/user/list_test.go | 66 ------ internal/apiserver/api/v1/user/update.go | 56 ----- internal/apiserver/api/v1/user/update_test.go | 93 -------- internal/apiserver/api/v1/user/user.go | 24 -- internal/apiserver/api/v1/user/user_test.go | 49 ---- internal/apiserver/router.go | 6 +- internal/apiserver/server.go | 2 +- 40 files changed, 7 insertions(+), 1730 deletions(-) delete mode 100644 internal/apiserver/api/v1/cache/cache.go delete mode 100644 internal/apiserver/api/v1/cache/cache_test.go delete mode 100644 internal/apiserver/api/v1/policy/create.go delete mode 100644 internal/apiserver/api/v1/policy/delete.go delete mode 100644 internal/apiserver/api/v1/policy/delete_collection.go delete mode 100644 internal/apiserver/api/v1/policy/doc.go delete mode 100644 internal/apiserver/api/v1/policy/get.go delete mode 100644 internal/apiserver/api/v1/policy/list.go delete mode 100644 internal/apiserver/api/v1/policy/policy.go delete mode 100644 internal/apiserver/api/v1/policy/update.go delete mode 100644 internal/apiserver/api/v1/secret/create.go delete mode 100644 internal/apiserver/api/v1/secret/delete.go delete mode 100644 internal/apiserver/api/v1/secret/delete_collection.go delete mode 100644 internal/apiserver/api/v1/secret/doc.go delete mode 100644 internal/apiserver/api/v1/secret/get.go delete mode 100644 internal/apiserver/api/v1/secret/list.go delete mode 100644 internal/apiserver/api/v1/secret/secret.go delete mode 100644 internal/apiserver/api/v1/secret/update.go delete mode 100644 internal/apiserver/api/v1/user/change_password.go delete mode 100644 internal/apiserver/api/v1/user/change_password_test.go delete mode 100644 internal/apiserver/api/v1/user/create.go delete mode 100644 internal/apiserver/api/v1/user/create_test.go delete mode 100644 internal/apiserver/api/v1/user/delete.go delete mode 100644 internal/apiserver/api/v1/user/delete_collection.go delete mode 100644 internal/apiserver/api/v1/user/delete_collection_test.go delete mode 100644 internal/apiserver/api/v1/user/delete_test.go delete mode 100644 internal/apiserver/api/v1/user/doc.go delete mode 100644 internal/apiserver/api/v1/user/get.go delete mode 100644 internal/apiserver/api/v1/user/get_test.go delete mode 100644 internal/apiserver/api/v1/user/list.go delete mode 100644 internal/apiserver/api/v1/user/list_test.go delete mode 100644 internal/apiserver/api/v1/user/update.go delete mode 100644 internal/apiserver/api/v1/user/update_test.go delete mode 100644 internal/apiserver/api/v1/user/user.go delete mode 100644 internal/apiserver/api/v1/user/user_test.go diff --git a/CHANGELOG/CHANGELOG-1.0.1.md b/CHANGELOG/CHANGELOG-1.0.1.md index 8ebbe4a2..d472ee7f 100644 --- a/CHANGELOG/CHANGELOG-1.0.1.md +++ b/CHANGELOG/CHANGELOG-1.0.1.md @@ -1,8 +1,4 @@ -## [v1.0.1](https://github.com/marmotedu/iam/compare/v1...v1.0.1) (2021-05-26) - -### Bug Fixes - -* fix swagger makefile dependence error +## [v1.0.1](https://github.com/marmotedu/iam/compare/v1.0.0...v1.0.1) (2021-07-08) diff --git a/api/swagger/docs/user_swagger.go b/api/swagger/docs/user_swagger.go index 4ac108cd..76322708 100644 --- a/api/swagger/docs/user_swagger.go +++ b/api/swagger/docs/user_swagger.go @@ -8,7 +8,7 @@ import ( v1 "github.com/marmotedu/api/apiserver/v1" metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - "github.com/marmotedu/iam/internal/apiserver/api/v1/user" + "github.com/marmotedu/iam/internal/apiserver/controller/v1/user" ) // swagger:route POST /users users createUserRequest diff --git a/api/swagger/swagger.yaml b/api/swagger/swagger.yaml index 16d42100..c5d0e3c8 100644 --- a/api/swagger/swagger.yaml +++ b/api/swagger/swagger.yaml @@ -58,7 +58,7 @@ definitions: - newPassword title: ChangePasswordRequest defines the ChangePasswordRequest data format. type: object - x-go-package: github.com/marmotedu/iam/internal/apiserver/api/v1/user + x-go-package: github.com/marmotedu/iam/internal/apiserver/controller/v1/user Condition: properties: GetName: diff --git a/internal/apiserver/api/v1/cache/cache.go b/internal/apiserver/api/v1/cache/cache.go deleted file mode 100644 index c71671b6..00000000 --- a/internal/apiserver/api/v1/cache/cache.go +++ /dev/null @@ -1,106 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -// Package cache defines a cache service which can return all secrets and policies. -package cache - -import ( - "context" - "fmt" - "sync" - - pb "github.com/marmotedu/api/proto/apiserver/v1" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - "github.com/marmotedu/errors" - - "github.com/marmotedu/iam/internal/apiserver/store" - "github.com/marmotedu/iam/internal/pkg/code" - "github.com/marmotedu/iam/pkg/log" -) - -// Cache defines a cache service used to list all secrets and policies. -type Cache struct { - store store.Factory -} - -var ( - cacheServer *Cache - once sync.Once -) - -// GetCacheInsOr return cache server instance with given factory. -func GetCacheInsOr(store store.Factory) (*Cache, error) { - if store != nil { - once.Do(func() { - cacheServer = &Cache{store} - }) - } - - if cacheServer == nil { - return nil, fmt.Errorf("got nil cache server") - } - - return cacheServer, nil -} - -// ListSecrets returns all secrets. -func (c *Cache) ListSecrets(ctx context.Context, r *pb.ListSecretsRequest) (*pb.ListSecretsResponse, error) { - log.L(ctx).Info("list secrets function called.") - opts := metav1.ListOptions{ - Offset: r.Offset, - Limit: r.Limit, - } - - secrets, err := c.store.Secrets().List(ctx, "", opts) - if err != nil { - return nil, errors.WithCode(code.ErrDatabase, err.Error()) - } - - items := make([]*pb.SecretInfo, 0) - for _, secret := range secrets.Items { - items = append(items, &pb.SecretInfo{ - SecretId: secret.SecretID, - Username: secret.Username, - SecretKey: secret.SecretKey, - Expires: secret.Expires, - Description: secret.Description, - CreatedAt: secret.CreatedAt.Format("2006-01-02 15:04:05"), - UpdatedAt: secret.UpdatedAt.Format("2006-01-02 15:04:05"), - }) - } - - return &pb.ListSecretsResponse{ - TotalCount: secrets.TotalCount, - Items: items, - }, nil -} - -// ListPolicies returns all policies. -func (c *Cache) ListPolicies(ctx context.Context, r *pb.ListPoliciesRequest) (*pb.ListPoliciesResponse, error) { - log.L(ctx).Info("list policies function called.") - opts := metav1.ListOptions{ - Offset: r.Offset, - Limit: r.Limit, - } - - policies, err := c.store.Policies().List(ctx, "", opts) - if err != nil { - return nil, errors.WithCode(code.ErrDatabase, err.Error()) - } - - items := make([]*pb.PolicyInfo, 0) - for _, pol := range policies.Items { - items = append(items, &pb.PolicyInfo{ - Name: pol.Name, - Username: pol.Username, - PolicyShadow: pol.PolicyShadow, - CreatedAt: pol.CreatedAt.Format("2006-01-02 15:04:05"), - }) - } - - return &pb.ListPoliciesResponse{ - TotalCount: policies.TotalCount, - Items: items, - }, nil -} diff --git a/internal/apiserver/api/v1/cache/cache_test.go b/internal/apiserver/api/v1/cache/cache_test.go deleted file mode 100644 index f3a5af04..00000000 --- a/internal/apiserver/api/v1/cache/cache_test.go +++ /dev/null @@ -1,211 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -// Package cache defines a cache service which can return all secrets and policies. -package cache - -import ( - "context" - "reflect" - "testing" - - "github.com/golang/mock/gomock" - v1 "github.com/marmotedu/api/apiserver/v1" - pb "github.com/marmotedu/api/proto/apiserver/v1" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - - "github.com/marmotedu/iam/internal/apiserver/store" - "github.com/marmotedu/iam/internal/apiserver/store/fake" -) - -func TestGetCacheInsOr(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockFactory := store.NewMockFactory(ctrl) - - type args struct { - store store.Factory - } - tests := []struct { - name string - args args - want *Cache - wantErr bool - }{ - { - name: "default", - args: args{ - store: mockFactory, - }, - want: &Cache{ - store: mockFactory, - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetCacheInsOr(tt.args.store) - if (err != nil) != tt.wantErr { - t.Errorf("GetCacheInsOr() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetCacheInsOr() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestCache_ListSecrets(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockFactory := store.NewMockFactory(ctrl) - mockSecretStore := store.NewMockSecretStore(ctrl) - mockFactory.EXPECT().Secrets().Return(mockSecretStore) - secrets := &v1.SecretList{ - ListMeta: metav1.ListMeta{ - TotalCount: 10, - }, - Items: fake.FakeSecrets(3), - } - - wantItems := make([]*pb.SecretInfo, 0) - for _, secret := range secrets.Items { - wantItems = append(wantItems, &pb.SecretInfo{ - SecretId: secret.SecretID, - Username: secret.Username, - SecretKey: secret.SecretKey, - Expires: secret.Expires, - Description: secret.Description, - CreatedAt: secret.CreatedAt.Format("2006-01-02 15:04:05"), - UpdatedAt: secret.UpdatedAt.Format("2006-01-02 15:04:05"), - }) - } - wantResponse := &pb.ListSecretsResponse{ - TotalCount: secrets.TotalCount, - Items: wantItems, - } - - mockSecretStore.EXPECT().List(gomock.Any(), gomock.Eq(""), gomock.Any()).Return(secrets, nil) - - type fields struct { - store store.Factory - } - type args struct { - ctx context.Context - r *pb.ListSecretsRequest - } - tests := []struct { - name string - fields fields - args args - want *pb.ListSecretsResponse - wantErr bool - }{ - { - name: "default", - fields: fields{ - store: mockFactory, - }, - args: args{ - ctx: context.TODO(), - r: &pb.ListSecretsRequest{}, - }, - want: wantResponse, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - c := &Cache{ - store: tt.fields.store, - } - got, err := c.ListSecrets(tt.args.ctx, tt.args.r) - if (err != nil) != tt.wantErr { - t.Errorf("Cache.ListSecrets() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Cache.ListSecrets() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestCache_ListPolicies(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockFactory := store.NewMockFactory(ctrl) - mockPolicyStore := store.NewMockPolicyStore(ctrl) - mockFactory.EXPECT().Policies().Return(mockPolicyStore) - policies := &v1.PolicyList{ - ListMeta: metav1.ListMeta{ - TotalCount: 10, - }, - Items: fake.FakePolicies(3), - } - - wantItems := make([]*pb.PolicyInfo, 0) - for _, pol := range policies.Items { - wantItems = append(wantItems, &pb.PolicyInfo{ - Name: pol.Name, - Username: pol.Username, - PolicyShadow: pol.PolicyShadow, - CreatedAt: pol.CreatedAt.Format("2006-01-02 15:04:05"), - }) - } - - wantResponse := &pb.ListPoliciesResponse{ - TotalCount: policies.TotalCount, - Items: wantItems, - } - mockPolicyStore.EXPECT().List(gomock.Any(), gomock.Eq(""), gomock.Any()).Return(policies, nil) - - type fields struct { - store store.Factory - } - type args struct { - ctx context.Context - r *pb.ListPoliciesRequest - } - tests := []struct { - name string - fields fields - args args - want *pb.ListPoliciesResponse - wantErr bool - }{ - { - name: "default", - fields: fields{ - store: mockFactory, - }, - args: args{ - ctx: context.TODO(), - r: &pb.ListPoliciesRequest{}, - }, - want: wantResponse, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - c := &Cache{ - store: tt.fields.store, - } - got, err := c.ListPolicies(tt.args.ctx, tt.args.r) - if (err != nil) != tt.wantErr { - t.Errorf("Cache.ListPolicies() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Cache.ListPolicies() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/internal/apiserver/api/v1/policy/create.go b/internal/apiserver/api/v1/policy/create.go deleted file mode 100644 index ebaa179c..00000000 --- a/internal/apiserver/api/v1/policy/create.go +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package policy - -import ( - "github.com/gin-gonic/gin" - v1 "github.com/marmotedu/api/apiserver/v1" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - "github.com/marmotedu/errors" - - "github.com/marmotedu/iam/internal/pkg/code" - "github.com/marmotedu/iam/internal/pkg/middleware" - "github.com/marmotedu/iam/pkg/log" -) - -// Create creates a new ladon policy. -// It will convert the policy to string and store it in the storage. -func (p *PolicyHandler) Create(c *gin.Context) { - log.L(c).Info("create policy function called.") - - var r v1.Policy - if err := c.ShouldBindJSON(&r); err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrBind, err.Error()), nil) - - return - } - - if errs := r.Validate(); len(errs) != 0 { - core.WriteResponse(c, errors.WithCode(code.ErrValidation, errs.ToAggregate().Error()), nil) - - return - } - - r.Username = c.GetString(middleware.UsernameKey) - - if err := p.srv.Policies().Create(c, &r, metav1.CreateOptions{}); err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, r) -} diff --git a/internal/apiserver/api/v1/policy/delete.go b/internal/apiserver/api/v1/policy/delete.go deleted file mode 100644 index 5c9908fe..00000000 --- a/internal/apiserver/api/v1/policy/delete.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package policy - -import ( - "github.com/gin-gonic/gin" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - - "github.com/marmotedu/iam/internal/pkg/middleware" - "github.com/marmotedu/iam/pkg/log" -) - -// Delete deletes the policy by the policy identifier. -func (p *PolicyHandler) Delete(c *gin.Context) { - log.L(c).Info("delete policy function called.") - - if err := p.srv.Policies().Delete(c, c.GetString(middleware.UsernameKey), c.Param("name"), - metav1.DeleteOptions{}); err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, nil) -} diff --git a/internal/apiserver/api/v1/policy/delete_collection.go b/internal/apiserver/api/v1/policy/delete_collection.go deleted file mode 100644 index 5259269f..00000000 --- a/internal/apiserver/api/v1/policy/delete_collection.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package policy - -import ( - "github.com/gin-gonic/gin" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - - "github.com/marmotedu/iam/internal/pkg/middleware" - "github.com/marmotedu/iam/pkg/log" -) - -// DeleteCollection delete policies by policy names. -func (p *PolicyHandler) DeleteCollection(c *gin.Context) { - log.L(c).Info("batch delete policy function called.") - - if err := p.srv.Policies().DeleteCollection(c, c.GetString(middleware.UsernameKey), - c.QueryArray("name"), metav1.DeleteOptions{}); err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, nil) -} diff --git a/internal/apiserver/api/v1/policy/doc.go b/internal/apiserver/api/v1/policy/doc.go deleted file mode 100644 index 813c8e44..00000000 --- a/internal/apiserver/api/v1/policy/doc.go +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -// Package policy implements the policy handlers. -package policy diff --git a/internal/apiserver/api/v1/policy/get.go b/internal/apiserver/api/v1/policy/get.go deleted file mode 100644 index deebd6ea..00000000 --- a/internal/apiserver/api/v1/policy/get.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package policy - -import ( - "github.com/gin-gonic/gin" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - - "github.com/marmotedu/iam/internal/pkg/middleware" - "github.com/marmotedu/iam/pkg/log" -) - -// Get return policy by the policy identifier. -func (p *PolicyHandler) Get(c *gin.Context) { - log.L(c).Info("get policy function called.") - - pol, err := p.srv.Policies().Get(c, c.GetString(middleware.UsernameKey), c.Param("name"), metav1.GetOptions{}) - if err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, pol) -} diff --git a/internal/apiserver/api/v1/policy/list.go b/internal/apiserver/api/v1/policy/list.go deleted file mode 100644 index c72a5db3..00000000 --- a/internal/apiserver/api/v1/policy/list.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package policy - -import ( - "github.com/gin-gonic/gin" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - "github.com/marmotedu/errors" - - "github.com/marmotedu/iam/internal/pkg/code" - "github.com/marmotedu/iam/internal/pkg/middleware" - "github.com/marmotedu/iam/pkg/log" -) - -// List return all policies. -func (p *PolicyHandler) List(c *gin.Context) { - log.L(c).Info("list policy function called.") - - var r metav1.ListOptions - if err := c.ShouldBindQuery(&r); err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrBind, err.Error()), nil) - - return - } - - policies, err := p.srv.Policies().List(c, c.GetString(middleware.UsernameKey), r) - if err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, policies) -} diff --git a/internal/apiserver/api/v1/policy/policy.go b/internal/apiserver/api/v1/policy/policy.go deleted file mode 100644 index 53f7656f..00000000 --- a/internal/apiserver/api/v1/policy/policy.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package policy - -import ( - srvv1 "github.com/marmotedu/iam/internal/apiserver/service/v1" - "github.com/marmotedu/iam/internal/apiserver/store" -) - -// PolicyHandler create a policy handler used to handle request for policy resource. -type PolicyHandler struct { - srv srvv1.Service - store store.Factory -} - -// NewPolicyHandler creates a policy handler. -func NewPolicyHandler(store store.Factory) *PolicyHandler { - return &PolicyHandler{ - srv: srvv1.NewService(store), - store: store, - } -} diff --git a/internal/apiserver/api/v1/policy/update.go b/internal/apiserver/api/v1/policy/update.go deleted file mode 100644 index a41c34dc..00000000 --- a/internal/apiserver/api/v1/policy/update.go +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package policy - -import ( - "github.com/gin-gonic/gin" - v1 "github.com/marmotedu/api/apiserver/v1" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - "github.com/marmotedu/errors" - - "github.com/marmotedu/iam/internal/pkg/code" - "github.com/marmotedu/iam/internal/pkg/middleware" - "github.com/marmotedu/iam/pkg/log" -) - -// Update updates policy by the policy identifier. -func (p *PolicyHandler) Update(c *gin.Context) { - log.L(c).Info("update policy function called.") - - var r v1.Policy - if err := c.ShouldBindJSON(&r); err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrBind, err.Error()), nil) - - return - } - - pol, err := p.store.Policies().Get(c, c.GetString(middleware.UsernameKey), c.Param("name"), metav1.GetOptions{}) - if err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrDatabase, err.Error()), nil) - - return - } - - // only update policy string - pol.Policy = r.Policy - pol.Extend = r.Extend - - if errs := pol.Validate(); len(errs) != 0 { - core.WriteResponse(c, errors.WithCode(code.ErrValidation, errs.ToAggregate().Error()), nil) - - return - } - - if err := p.srv.Policies().Update(c, pol, metav1.UpdateOptions{}); err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, pol) -} diff --git a/internal/apiserver/api/v1/secret/create.go b/internal/apiserver/api/v1/secret/create.go deleted file mode 100644 index 6d5a27e9..00000000 --- a/internal/apiserver/api/v1/secret/create.go +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package secret - -import ( - "github.com/AlekSi/pointer" - "github.com/gin-gonic/gin" - v1 "github.com/marmotedu/api/apiserver/v1" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - "github.com/marmotedu/errors" - - "github.com/marmotedu/iam/internal/pkg/code" - "github.com/marmotedu/iam/internal/pkg/middleware" - "github.com/marmotedu/iam/pkg/log" -) - -const maxSecretCount = 10 - -// Create add new secret key pairs to the storage. -func (s *SecretHandler) Create(c *gin.Context) { - log.L(c).Info("create secret function called.") - - var r v1.Secret - - if err := c.ShouldBindJSON(&r); err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrBind, err.Error()), nil) - - return - } - - if errs := r.Validate(); len(errs) != 0 { - core.WriteResponse(c, errors.WithCode(code.ErrValidation, errs.ToAggregate().Error()), nil) - - return - } - - username := c.GetString(middleware.UsernameKey) - - sec, err := s.store.Secrets().List(c, username, metav1.ListOptions{ - Offset: pointer.ToInt64(0), - Limit: pointer.ToInt64(-1), - }) - if err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrDatabase, err.Error()), nil) - - return - } - - if sec.TotalCount >= maxSecretCount { - core.WriteResponse(c, errors.WithCode(code.ErrReachMaxCount, "secret count: %d", sec.TotalCount), nil) - - return - } - - // must reassign username - r.Username = username - - if err := s.srv.Secrets().Create(c, &r, metav1.CreateOptions{}); err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, r) -} diff --git a/internal/apiserver/api/v1/secret/delete.go b/internal/apiserver/api/v1/secret/delete.go deleted file mode 100644 index ed1a5afe..00000000 --- a/internal/apiserver/api/v1/secret/delete.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package secret - -import ( - "github.com/gin-gonic/gin" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - - "github.com/marmotedu/iam/internal/pkg/middleware" - "github.com/marmotedu/iam/pkg/log" -) - -// Delete delete a secret by the secret identifier. -func (s *SecretHandler) Delete(c *gin.Context) { - log.L(c).Info("delete secret function called.") - opts := metav1.DeleteOptions{Unscoped: true} - if err := s.srv.Secrets().Delete(c, c.GetString(middleware.UsernameKey), c.Param("name"), opts); err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, nil) -} diff --git a/internal/apiserver/api/v1/secret/delete_collection.go b/internal/apiserver/api/v1/secret/delete_collection.go deleted file mode 100644 index 5fb8ab20..00000000 --- a/internal/apiserver/api/v1/secret/delete_collection.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package secret - -import ( - "github.com/gin-gonic/gin" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - - "github.com/marmotedu/iam/internal/pkg/middleware" - "github.com/marmotedu/iam/pkg/log" -) - -// DeleteCollection delete secrets by secret names. -func (s *SecretHandler) DeleteCollection(c *gin.Context) { - log.L(c).Info("batch delete policy function called.") - - if err := s.srv.Policies().DeleteCollection( - c, - c.GetString(middleware.UsernameKey), - c.QueryArray("name"), - metav1.DeleteOptions{}, - ); err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, nil) -} diff --git a/internal/apiserver/api/v1/secret/doc.go b/internal/apiserver/api/v1/secret/doc.go deleted file mode 100644 index dfe9f0d9..00000000 --- a/internal/apiserver/api/v1/secret/doc.go +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -// Package secret implements the secret handlers. -package secret diff --git a/internal/apiserver/api/v1/secret/get.go b/internal/apiserver/api/v1/secret/get.go deleted file mode 100644 index aa066d9b..00000000 --- a/internal/apiserver/api/v1/secret/get.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package secret - -import ( - "github.com/gin-gonic/gin" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - - "github.com/marmotedu/iam/internal/pkg/middleware" - "github.com/marmotedu/iam/pkg/log" -) - -// Get get an policy by the secret identifier. -func (s *SecretHandler) Get(c *gin.Context) { - log.L(c).Info("get secret function called.") - - secret, err := s.srv.Secrets().Get(c, c.GetString(middleware.UsernameKey), c.Param("name"), metav1.GetOptions{}) - if err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, secret) -} diff --git a/internal/apiserver/api/v1/secret/list.go b/internal/apiserver/api/v1/secret/list.go deleted file mode 100644 index 22ae0ff1..00000000 --- a/internal/apiserver/api/v1/secret/list.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package secret - -import ( - "github.com/gin-gonic/gin" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - "github.com/marmotedu/errors" - - "github.com/marmotedu/iam/internal/pkg/code" - "github.com/marmotedu/iam/internal/pkg/middleware" - "github.com/marmotedu/iam/pkg/log" -) - -// List list all the secrets. -func (s *SecretHandler) List(c *gin.Context) { - log.L(c).Info("list secret function called.") - var r metav1.ListOptions - if err := c.ShouldBindQuery(&r); err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrBind, err.Error()), nil) - - return - } - - secrets, err := s.srv.Secrets().List(c, c.GetString(middleware.UsernameKey), r) - if err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, secrets) -} diff --git a/internal/apiserver/api/v1/secret/secret.go b/internal/apiserver/api/v1/secret/secret.go deleted file mode 100644 index e882f271..00000000 --- a/internal/apiserver/api/v1/secret/secret.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package secret - -import ( - srvv1 "github.com/marmotedu/iam/internal/apiserver/service/v1" - "github.com/marmotedu/iam/internal/apiserver/store" -) - -// SecretHandler create a secret handler used to handle request for secret resource. -type SecretHandler struct { - srv srvv1.Service - store store.Factory -} - -// NewSecretHandler creates a secret handler. -func NewSecretHandler(store store.Factory) *SecretHandler { - return &SecretHandler{ - srv: srvv1.NewService(store), - store: store, - } -} diff --git a/internal/apiserver/api/v1/secret/update.go b/internal/apiserver/api/v1/secret/update.go deleted file mode 100644 index 3c62440d..00000000 --- a/internal/apiserver/api/v1/secret/update.go +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package secret - -import ( - "github.com/gin-gonic/gin" - v1 "github.com/marmotedu/api/apiserver/v1" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - "github.com/marmotedu/errors" - - "github.com/marmotedu/iam/internal/pkg/code" - "github.com/marmotedu/iam/internal/pkg/middleware" - "github.com/marmotedu/iam/pkg/log" -) - -// Update update a key by the secret key identifier. -func (s *SecretHandler) Update(c *gin.Context) { - log.L(c).Info("update secret function called.") - - var r v1.Secret - if err := c.ShouldBindJSON(&r); err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrBind, err.Error()), nil) - - return - } - - username := c.GetString(middleware.UsernameKey) - name := c.Param("name") - - secret, err := s.srv.Secrets().Get(c, username, name, metav1.GetOptions{}) - if err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrDatabase, err.Error()), nil) - - return - } - - // only update expires and description - secret.Expires = r.Expires - secret.Description = r.Description - secret.Extend = r.Extend - - if errs := secret.Validate(); len(errs) != 0 { - core.WriteResponse(c, errors.WithCode(code.ErrValidation, errs.ToAggregate().Error()), nil) - - return - } - - if err := s.srv.Secrets().Update(c, secret, metav1.UpdateOptions{}); err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, secret) -} diff --git a/internal/apiserver/api/v1/user/change_password.go b/internal/apiserver/api/v1/user/change_password.go deleted file mode 100644 index 95b39927..00000000 --- a/internal/apiserver/api/v1/user/change_password.go +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "github.com/gin-gonic/gin" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - "github.com/marmotedu/errors" - - "github.com/marmotedu/iam/internal/pkg/code" - "github.com/marmotedu/iam/pkg/log" -) - -// ChangePasswordRequest defines the ChangePasswordRequest data format. -type ChangePasswordRequest struct { - // Old password. - // Required: true - OldPassword string `json:"oldPassword" binding:"omitempty"` - - // New password. - // Required: true - NewPassword string `json:"newPassword" binding:"password"` -} - -// ChangePassword change the user's password by the user identifier. -func (u *UserHandler) ChangePassword(c *gin.Context) { - log.L(c).Info("change password function called.") - - var r ChangePasswordRequest - - if err := c.ShouldBindJSON(&r); err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrBind, err.Error()), nil) - - return - } - - user, err := u.store.Users().Get(c, c.Param("name"), metav1.GetOptions{}) - if err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrDatabase, err.Error()), nil) - - return - } - - if err := user.Compare(r.OldPassword); err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrPasswordIncorrect, err.Error()), nil) - - return - } - - user.Password = r.NewPassword - if err := u.srv.Users().ChangePassword(c, user); err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, nil) -} diff --git a/internal/apiserver/api/v1/user/change_password_test.go b/internal/apiserver/api/v1/user/change_password_test.go deleted file mode 100644 index 45b99724..00000000 --- a/internal/apiserver/api/v1/user/change_password_test.go +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "bytes" - "net/http" - "net/http/httptest" - "testing" - - "github.com/gin-gonic/gin" - "github.com/golang/mock/gomock" - v1 "github.com/marmotedu/api/apiserver/v1" - - srvv1 "github.com/marmotedu/iam/internal/apiserver/service/v1" - "github.com/marmotedu/iam/internal/apiserver/store" - _ "github.com/marmotedu/iam/pkg/validator" -) - -func TestUserHandler_ChangePassword(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - user := &v1.User{ - Password: "$2a$10$KqZhl5WStpa2K.ddEyzyf.zXllEXP4gIG8xQUgMhU1ZvMUn/Ta5um", - } - mockFactory := store.NewMockFactory(ctrl) - mockUserStore := store.NewMockUserStore(ctrl) - mockUserStore.EXPECT().Get(gomock.Any(), gomock.Eq("colin"), gomock.Any()).Return(user, nil) - mockFactory.EXPECT().Users().Return(mockUserStore) - - mockService := srvv1.NewMockService(ctrl) - mockUserSrv := srvv1.NewMockUserSrv(ctrl) - mockUserSrv.EXPECT().ChangePassword(gomock.Any(), gomock.Any()).Return(nil) - mockService.EXPECT().Users().Return(mockUserSrv) - - c, _ := gin.CreateTestContext(httptest.NewRecorder()) - body := bytes.NewBufferString(`{"oldPassword":"Admin@2020","newPassword":"Colin@2021"}`) - c.Request, _ = http.NewRequest("PUT", "/v1/users/colin/change_password", body) - c.Params = []gin.Param{{Key: "name", Value: "colin"}} - c.Request.Header.Set("Content-Type", "application/json") - - type fields struct { - srv srvv1.Service - store store.Factory - } - type args struct { - c *gin.Context - } - tests := []struct { - name string - fields fields - args args - }{ - { - name: "default", - fields: fields{ - srv: mockService, - store: mockFactory, - }, - args: args{ - c: c, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - u := &UserHandler{ - srv: tt.fields.srv, - store: tt.fields.store, - } - u.ChangePassword(tt.args.c) - }) - } -} diff --git a/internal/apiserver/api/v1/user/create.go b/internal/apiserver/api/v1/user/create.go deleted file mode 100644 index 22dcb786..00000000 --- a/internal/apiserver/api/v1/user/create.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "github.com/gin-gonic/gin" - v1 "github.com/marmotedu/api/apiserver/v1" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - "github.com/marmotedu/errors" - - "github.com/marmotedu/iam/internal/pkg/code" - "github.com/marmotedu/iam/pkg/log" -) - -// Create add new user to the storage. -func (u *UserHandler) Create(c *gin.Context) { - log.L(c).Info("user create function called.") - - var r v1.User - - if err := c.ShouldBindJSON(&r); err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrBind, err.Error()), nil) - - return - } - - if errs := r.Validate(); len(errs) != 0 { - core.WriteResponse(c, errors.WithCode(code.ErrValidation, errs.ToAggregate().Error()), nil) - - return - } - - // Insert the user to the storage. - if err := u.srv.Users().Create(c, &r, metav1.CreateOptions{}); err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, r) -} diff --git a/internal/apiserver/api/v1/user/create_test.go b/internal/apiserver/api/v1/user/create_test.go deleted file mode 100644 index e9cffcc5..00000000 --- a/internal/apiserver/api/v1/user/create_test.go +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "bytes" - "net/http" - "net/http/httptest" - "testing" - - "github.com/gin-gonic/gin" - "github.com/golang/mock/gomock" - - srvv1 "github.com/marmotedu/iam/internal/apiserver/service/v1" - "github.com/marmotedu/iam/internal/apiserver/store" -) - -func TestUserHandler_Create(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockService := srvv1.NewMockService(ctrl) - mockUserSrv := srvv1.NewMockUserSrv(ctrl) - mockUserSrv.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) - mockService.EXPECT().Users().Return(mockUserSrv) - mockFactory := store.NewMockFactory(ctrl) - c, _ := gin.CreateTestContext(httptest.NewRecorder()) - body := bytes.NewBufferString( - `{"metadata":{"name":"admin"},"nickname":"admin","email":"aaa@qq.com","password":"Admin@2020","phone":"1812884xxx"}`, - ) - c.Request, _ = http.NewRequest("POST", "/v1/users", body) - c.Request.Header.Set("Content-Type", "application/json") - - type fields struct { - srv srvv1.Service - store store.Factory - } - type args struct { - c *gin.Context - } - tests := []struct { - name string - fields fields - args args - }{ - { - name: "default", - fields: fields{ - srv: mockService, - store: mockFactory, - }, - args: args{ - c: c, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - u := &UserHandler{ - srv: tt.fields.srv, - store: tt.fields.store, - } - u.Create(tt.args.c) - }) - } -} diff --git a/internal/apiserver/api/v1/user/delete.go b/internal/apiserver/api/v1/user/delete.go deleted file mode 100644 index 8ed894b0..00000000 --- a/internal/apiserver/api/v1/user/delete.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "github.com/gin-gonic/gin" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - - "github.com/marmotedu/iam/pkg/log" -) - -// Delete delete an user by the user identifier. -// Only administrator can call this function. -func (u *UserHandler) Delete(c *gin.Context) { - log.L(c).Info("delete user function called.") - - if err := u.srv.Users().Delete(c, c.Param("name"), metav1.DeleteOptions{Unscoped: true}); err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, nil) -} diff --git a/internal/apiserver/api/v1/user/delete_collection.go b/internal/apiserver/api/v1/user/delete_collection.go deleted file mode 100644 index 079e23df..00000000 --- a/internal/apiserver/api/v1/user/delete_collection.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "github.com/gin-gonic/gin" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - - "github.com/marmotedu/iam/pkg/log" -) - -// DeleteCollection batch delete users by multiple usernames. -// Only administrator can call this function. -func (u *UserHandler) DeleteCollection(c *gin.Context) { - log.L(c).Info("batch delete user function called.") - - usernames := c.QueryArray("name") - - if err := u.srv.Users().DeleteCollection(c, usernames, metav1.DeleteOptions{}); err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, nil) -} diff --git a/internal/apiserver/api/v1/user/delete_collection_test.go b/internal/apiserver/api/v1/user/delete_collection_test.go deleted file mode 100644 index 231c65ee..00000000 --- a/internal/apiserver/api/v1/user/delete_collection_test.go +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "net/http" - "net/http/httptest" - "testing" - - "github.com/gin-gonic/gin" - "github.com/golang/mock/gomock" - - srvv1 "github.com/marmotedu/iam/internal/apiserver/service/v1" - "github.com/marmotedu/iam/internal/apiserver/store" -) - -func TestUserHandler_DeleteCollection(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockFactory := store.NewMockFactory(ctrl) - - mockService := srvv1.NewMockService(ctrl) - mockUserSrv := srvv1.NewMockUserSrv(ctrl) - mockUserSrv.EXPECT().DeleteCollection(gomock.Any(), gomock.Eq([]string{"colin", "john"}), gomock.Any()).Return(nil) - mockService.EXPECT().Users().Return(mockUserSrv) - - c, _ := gin.CreateTestContext(httptest.NewRecorder()) - c.Request, _ = http.NewRequest("DELETE", "/v1/users?name=colin&name=john", nil) - - type fields struct { - srv srvv1.Service - store store.Factory - } - type args struct { - c *gin.Context - } - tests := []struct { - name string - fields fields - args args - }{ - { - name: "default", - fields: fields{ - srv: mockService, - store: mockFactory, - }, - args: args{ - c: c, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - u := &UserHandler{ - srv: tt.fields.srv, - store: tt.fields.store, - } - u.DeleteCollection(tt.args.c) - }) - } -} diff --git a/internal/apiserver/api/v1/user/delete_test.go b/internal/apiserver/api/v1/user/delete_test.go deleted file mode 100644 index 6727b6a2..00000000 --- a/internal/apiserver/api/v1/user/delete_test.go +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "net/http" - "net/http/httptest" - "testing" - - "github.com/gin-gonic/gin" - "github.com/golang/mock/gomock" - - srvv1 "github.com/marmotedu/iam/internal/apiserver/service/v1" - "github.com/marmotedu/iam/internal/apiserver/store" -) - -func TestUserHandler_Delete(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockFactory := store.NewMockFactory(ctrl) - - mockService := srvv1.NewMockService(ctrl) - mockUserSrv := srvv1.NewMockUserSrv(ctrl) - mockUserSrv.EXPECT().Delete(gomock.Any(), gomock.Eq("admin"), gomock.Any()).Return(nil) - mockService.EXPECT().Users().Return(mockUserSrv) - - c, _ := gin.CreateTestContext(httptest.NewRecorder()) - c.Request, _ = http.NewRequest("DELETE", "/v1/users/admin", nil) - c.Params = []gin.Param{{Key: "name", Value: "admin"}} - - type fields struct { - srv srvv1.Service - store store.Factory - } - type args struct { - c *gin.Context - } - tests := []struct { - name string - fields fields - args args - }{ - { - name: "default", - fields: fields{ - srv: mockService, - store: mockFactory, - }, - args: args{ - c: c, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - u := &UserHandler{ - srv: tt.fields.srv, - store: tt.fields.store, - } - u.Delete(tt.args.c) - }) - } -} diff --git a/internal/apiserver/api/v1/user/doc.go b/internal/apiserver/api/v1/user/doc.go deleted file mode 100644 index 3312ce31..00000000 --- a/internal/apiserver/api/v1/user/doc.go +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -// Package user implements the user handler. -package user diff --git a/internal/apiserver/api/v1/user/get.go b/internal/apiserver/api/v1/user/get.go deleted file mode 100644 index 7ce94958..00000000 --- a/internal/apiserver/api/v1/user/get.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "github.com/gin-gonic/gin" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - - "github.com/marmotedu/iam/pkg/log" -) - -// Get get an user by the user identifier. -func (u *UserHandler) Get(c *gin.Context) { - log.L(c).Info("get user function called.") - - user, err := u.srv.Users().Get(c, c.Param("name"), metav1.GetOptions{}) - if err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, user) -} diff --git a/internal/apiserver/api/v1/user/get_test.go b/internal/apiserver/api/v1/user/get_test.go deleted file mode 100644 index e762039a..00000000 --- a/internal/apiserver/api/v1/user/get_test.go +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "net/http" - "net/http/httptest" - "testing" - - "github.com/gin-gonic/gin" - "github.com/golang/mock/gomock" - v1 "github.com/marmotedu/api/apiserver/v1" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - - srvv1 "github.com/marmotedu/iam/internal/apiserver/service/v1" - "github.com/marmotedu/iam/internal/apiserver/store" -) - -func TestUserHandler_Get(t *testing.T) { - user := &v1.User{ - ObjectMeta: metav1.ObjectMeta{ - Name: "admin", - ID: 0, - }, - Nickname: "admin", - Password: "Admin@2020", - Email: "admin@foxmail.com", - } - - c, _ := gin.CreateTestContext(httptest.NewRecorder()) - c.Request, _ = http.NewRequest("GET", "/v1/users/colin", nil) - c.Params = []gin.Param{{Key: "name", Value: "admin"}} - - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockFactory := store.NewMockFactory(ctrl) - mockService := srvv1.NewMockService(ctrl) - mockUserSrv := srvv1.NewMockUserSrv(ctrl) - mockUserSrv.EXPECT().Get(gomock.Any(), gomock.Eq("admin"), gomock.Any()).Return(user, nil) - mockService.EXPECT().Users().Return(mockUserSrv) - - type fields struct { - srv srvv1.Service - store store.Factory - } - type args struct { - c *gin.Context - } - tests := []struct { - name string - fields fields - args args - }{ - { - name: "default", - fields: fields{ - srv: mockService, - store: mockFactory, - }, - args: args{ - c: c, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - u := &UserHandler{ - srv: tt.fields.srv, - store: tt.fields.store, - } - u.Get(tt.args.c) - }) - } -} diff --git a/internal/apiserver/api/v1/user/list.go b/internal/apiserver/api/v1/user/list.go deleted file mode 100644 index 8524660c..00000000 --- a/internal/apiserver/api/v1/user/list.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "github.com/gin-gonic/gin" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - "github.com/marmotedu/errors" - - "github.com/marmotedu/iam/internal/pkg/code" - "github.com/marmotedu/iam/pkg/log" -) - -// List list the users in the storage. -// Only administrator can call this function. -func (u *UserHandler) List(c *gin.Context) { - log.L(c).Info("list user function called.") - - var r metav1.ListOptions - if err := c.ShouldBindQuery(&r); err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrBind, err.Error()), nil) - - return - } - - users, err := u.srv.Users().List(c, r) - if err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, users) -} diff --git a/internal/apiserver/api/v1/user/list_test.go b/internal/apiserver/api/v1/user/list_test.go deleted file mode 100644 index a06d2bd6..00000000 --- a/internal/apiserver/api/v1/user/list_test.go +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "net/http" - "net/http/httptest" - "testing" - - "github.com/gin-gonic/gin" - "github.com/golang/mock/gomock" - - srvv1 "github.com/marmotedu/iam/internal/apiserver/service/v1" - "github.com/marmotedu/iam/internal/apiserver/store" -) - -func TestUserHandler_List(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockFactory := store.NewMockFactory(ctrl) - - mockService := srvv1.NewMockService(ctrl) - mockUserSrv := srvv1.NewMockUserSrv(ctrl) - mockUserSrv.EXPECT().List(gomock.Any(), gomock.Any()).Return(nil, nil) - mockService.EXPECT().Users().Return(mockUserSrv) - - c, _ := gin.CreateTestContext(httptest.NewRecorder()) - c.Request, _ = http.NewRequest("GET", "/v1/users", nil) - c.Params = []gin.Param{{Key: "name", Value: "colin"}} - - type fields struct { - srv srvv1.Service - store store.Factory - } - type args struct { - c *gin.Context - } - tests := []struct { - name string - fields fields - args args - }{ - { - name: "default", - fields: fields{ - srv: mockService, - store: mockFactory, - }, - args: args{ - c: c, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - u := &UserHandler{ - srv: tt.fields.srv, - store: tt.fields.store, - } - u.List(tt.args.c) - }) - } -} diff --git a/internal/apiserver/api/v1/user/update.go b/internal/apiserver/api/v1/user/update.go deleted file mode 100644 index 0fb4c112..00000000 --- a/internal/apiserver/api/v1/user/update.go +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "github.com/gin-gonic/gin" - v1 "github.com/marmotedu/api/apiserver/v1" - "github.com/marmotedu/component-base/pkg/core" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - "github.com/marmotedu/errors" - - "github.com/marmotedu/iam/internal/pkg/code" - "github.com/marmotedu/iam/pkg/log" -) - -// Update update a user info by the user identifier. -func (u *UserHandler) Update(c *gin.Context) { - log.L(c).Info("update user function called.") - - var r v1.User - - if err := c.ShouldBindJSON(&r); err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrBind, err.Error()), nil) - - return - } - - user, err := u.store.Users().Get(c, c.Param("name"), metav1.GetOptions{}) - if err != nil { - core.WriteResponse(c, errors.WithCode(code.ErrDatabase, err.Error()), nil) - - return - } - - user.Nickname = r.Nickname - user.Email = r.Email - user.Phone = r.Phone - user.Extend = r.Extend - - if errs := user.ValidateUpdate(); len(errs) != 0 { - core.WriteResponse(c, errors.WithCode(code.ErrValidation, errs.ToAggregate().Error()), nil) - - return - } - - // Save changed fields. - if err := u.srv.Users().Update(c, user, metav1.UpdateOptions{}); err != nil { - core.WriteResponse(c, err, nil) - - return - } - - core.WriteResponse(c, nil, user) -} diff --git a/internal/apiserver/api/v1/user/update_test.go b/internal/apiserver/api/v1/user/update_test.go deleted file mode 100644 index 824a78b9..00000000 --- a/internal/apiserver/api/v1/user/update_test.go +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "bytes" - "net/http" - "net/http/httptest" - "testing" - - "github.com/gin-gonic/gin" - "github.com/golang/mock/gomock" - v1 "github.com/marmotedu/api/apiserver/v1" - metav1 "github.com/marmotedu/component-base/pkg/meta/v1" - - srvv1 "github.com/marmotedu/iam/internal/apiserver/service/v1" - "github.com/marmotedu/iam/internal/apiserver/store" -) - -func TestUserHandler_Update(t *testing.T) { - user := &v1.User{ - ObjectMeta: metav1.ObjectMeta{ - Name: "admin", - ID: 0, - }, - Nickname: "admin", - Password: "Admin@2020", - Email: "admin@foxmail.com", - Phone: "1812884xxxx", - } - - c, _ := gin.CreateTestContext(httptest.NewRecorder()) - body := bytes.NewBufferString(`{"nickname":"admin2","email":"admin2@foxmail.com","phone":"1812885xxx"}`) - c.Request, _ = http.NewRequest("PUT", "/v1/users/admin", body) - c.Params = []gin.Param{{Key: "name", Value: "admin"}} - c.Request.Header.Set("Content-Type", "application/json") - - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockFactory := store.NewMockFactory(ctrl) - mockUserStore := store.NewMockUserStore(ctrl) - mockUserStore.EXPECT().Get(gomock.Any(), gomock.Eq("admin"), gomock.Any()).Return(user, nil) - mockFactory.EXPECT().Users().Return(mockUserStore) - - mockService := srvv1.NewMockService(ctrl) - mockUserSrv := srvv1.NewMockUserSrv(ctrl) - - // deep copy - user2 := new(v1.User) - *user2 = *user - user2.Nickname = "admin2" - user2.Email = "admin2@foxmail.com" - user2.Phone = "1812885xxx" - - mockUserSrv.EXPECT().Update(gomock.Any(), gomock.Eq(user2), gomock.Any()).Return(nil) - mockService.EXPECT().Users().Return(mockUserSrv) - - type fields struct { - srv srvv1.Service - store store.Factory - } - type args struct { - c *gin.Context - } - tests := []struct { - name string - fields fields - args args - }{ - { - name: "default", - fields: fields{ - srv: mockService, - store: mockFactory, - }, - args: args{ - c: c, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - u := &UserHandler{ - srv: tt.fields.srv, - store: tt.fields.store, - } - u.Update(tt.args.c) - }) - } -} diff --git a/internal/apiserver/api/v1/user/user.go b/internal/apiserver/api/v1/user/user.go deleted file mode 100644 index acdcb21c..00000000 --- a/internal/apiserver/api/v1/user/user.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - srvv1 "github.com/marmotedu/iam/internal/apiserver/service/v1" - "github.com/marmotedu/iam/internal/apiserver/store" -) - -// UserHandler create a user handler used to handle request for user resource. -type UserHandler struct { - srv srvv1.Service - store store.Factory -} - -// NewUserHandler creates a user handler. -func NewUserHandler(store store.Factory) *UserHandler { - return &UserHandler{ - srv: srvv1.NewService(store), - store: store, - } -} diff --git a/internal/apiserver/api/v1/user/user_test.go b/internal/apiserver/api/v1/user/user_test.go deleted file mode 100644 index a488c43a..00000000 --- a/internal/apiserver/api/v1/user/user_test.go +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2020 Lingfei Kong . All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -package user - -import ( - "reflect" - "testing" - - "github.com/golang/mock/gomock" - - srvv1 "github.com/marmotedu/iam/internal/apiserver/service/v1" - "github.com/marmotedu/iam/internal/apiserver/store" -) - -func TestNewUserHandler(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockFactory := store.NewMockFactory(ctrl) - - type args struct { - store store.Factory - } - tests := []struct { - name string - args args - want *UserHandler - }{ - { - name: "default", - args: args{ - store: mockFactory, - }, - want: &UserHandler{ - srv: srvv1.NewService(mockFactory), - store: mockFactory, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := NewUserHandler(tt.args.store); !reflect.DeepEqual(got, tt.want) { - t.Errorf("NewUserHandler() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/internal/apiserver/router.go b/internal/apiserver/router.go index 39f4ac18..dde876d6 100644 --- a/internal/apiserver/router.go +++ b/internal/apiserver/router.go @@ -9,9 +9,9 @@ import ( "github.com/marmotedu/component-base/pkg/core" "github.com/marmotedu/errors" - "github.com/marmotedu/iam/internal/apiserver/api/v1/policy" - "github.com/marmotedu/iam/internal/apiserver/api/v1/secret" - "github.com/marmotedu/iam/internal/apiserver/api/v1/user" + "github.com/marmotedu/iam/internal/apiserver/controller/v1/policy" + "github.com/marmotedu/iam/internal/apiserver/controller/v1/secret" + "github.com/marmotedu/iam/internal/apiserver/controller/v1/user" "github.com/marmotedu/iam/internal/apiserver/store/mysql" "github.com/marmotedu/iam/internal/pkg/code" "github.com/marmotedu/iam/internal/pkg/middleware" diff --git a/internal/apiserver/server.go b/internal/apiserver/server.go index 337e9242..e7bb1c5f 100644 --- a/internal/apiserver/server.go +++ b/internal/apiserver/server.go @@ -13,8 +13,8 @@ import ( "google.golang.org/grpc/credentials" "google.golang.org/grpc/reflection" - cachev1 "github.com/marmotedu/iam/internal/apiserver/api/v1/cache" "github.com/marmotedu/iam/internal/apiserver/config" + cachev1 "github.com/marmotedu/iam/internal/apiserver/controller/v1/cache" "github.com/marmotedu/iam/internal/apiserver/store" "github.com/marmotedu/iam/internal/apiserver/store/mysql" genericoptions "github.com/marmotedu/iam/internal/pkg/options"