Skip to content

Commit

Permalink
Merge pull request #29 from chilianyi/refactor
Browse files Browse the repository at this point in the history
Fix get uuid failed when pod ip is public ip
  • Loading branch information
chilianyi authored Mar 2, 2019
2 parents c829d85 + c2a3a29 commit da580b0
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 28 deletions.
12 changes: 6 additions & 6 deletions pkg/db/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"openpitrix.io/iam/pkg/constants"
"openpitrix.io/iam/pkg/util/ctxutil"
"openpitrix.io/iam/pkg/util/strutil"
"openpitrix.io/iam/pkg/util/stringutil"
"openpitrix.io/logger"
)

Expand Down Expand Up @@ -133,7 +133,7 @@ func GetDisplayColumns(displayColumns []string, wholeColumns []string) []string
} else {
var newDisplayColumns []string
for _, column := range displayColumns {
if strutil.Contains(wholeColumns, column) {
if stringutil.Contains(wholeColumns, column) {
newDisplayColumns = append(newDisplayColumns, column)
}
}
Expand Down Expand Up @@ -201,14 +201,14 @@ func (c *Chain) getSearchFilter(tableName string, value interface{}, exclude ...
if vs, ok := value.([]string); ok {
for _, v := range vs {
for _, column := range constants.SearchColumns[tableName] {
if strutil.Contains(exclude, column) {
if stringutil.Contains(exclude, column) {
continue
}
// if column suffix is _id, must exact match
if strings.HasSuffix(column, "_id") {
conditions = append(conditions, column+" = '"+v+"'")
} else {
likeV := "%" + strutil.SimplifyString(v) + "%"
likeV := "%" + stringutil.SimplifyString(v) + "%"
conditions = append(conditions, column+" LIKE '"+likeV+"'")
}
}
Expand All @@ -225,14 +225,14 @@ func (c *Chain) buildFilterConditions(req Request, tableName string, exclude ...
column := getFieldName(field)
param := field.Value()
indexedColumns, ok := constants.IndexedColumns[tableName]
if ok && strutil.Contains(indexedColumns, column) {
if ok && stringutil.Contains(indexedColumns, column) {
value := getReqValue(param)
if value != nil {
key := column
c.DB = c.Where(key+" in (?)", value)
}
}
if column == SearchWordColumnName && strutil.Contains(constants.SearchWordColumnTable, tableName) {
if column == SearchWordColumnName && stringutil.Contains(constants.SearchWordColumnTable, tableName) {
value := getReqValue(param)
c.getSearchFilter(tableName, value, exclude...)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/service/am/resource/can_do_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"openpitrix.io/iam/pkg/constants"
"openpitrix.io/iam/pkg/gerr"
"openpitrix.io/iam/pkg/pb"
"openpitrix.io/iam/pkg/util/strutil"
"openpitrix.io/iam/pkg/util/stringutil"
"openpitrix.io/logger"
)

Expand All @@ -33,9 +33,9 @@ func GetDataLevelNum(dataLevel string) int {
}

func CanDo(ctx context.Context, req *pb.CanDoRequest) (*pb.CanDoResponse, error) {
userId := strutil.SimplifyString(req.UserId)
url := strutil.SimplifyString(req.Url)
urlMethod := strings.ToLower(strutil.SimplifyString(req.UrlMethod))
userId := stringutil.SimplifyString(req.UserId)
url := stringutil.SimplifyString(req.Url)
urlMethod := strings.ToLower(stringutil.SimplifyString(req.UrlMethod))

var roleIds []string
if userId == constants.UserSystem {
Expand Down
18 changes: 9 additions & 9 deletions pkg/service/am/resource/role_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"openpitrix.io/iam/pkg/models"
"openpitrix.io/iam/pkg/pb"
"openpitrix.io/iam/pkg/util/ctxutil"
"openpitrix.io/iam/pkg/util/strutil"
"openpitrix.io/iam/pkg/util/stringutil"
"openpitrix.io/logger"
)

Expand Down Expand Up @@ -67,7 +67,7 @@ func GetSenderPortal(ctx context.Context) (string, error) {
}

func CreateRole(ctx context.Context, req *pb.CreateRoleRequest) (*pb.CreateRoleResponse, error) {
if !strutil.Contains(constants.PortalSet, req.Portal) {
if !stringutil.Contains(constants.PortalSet, req.Portal) {
return nil, gerr.New(ctx, gerr.InvalidArgument, gerr.ErrorPortalNotFound, req.Portal)
}

Expand Down Expand Up @@ -177,15 +177,15 @@ func DescribeRoles(ctx context.Context, req *pb.DescribeRolesRequest) (*pb.Descr
return nil, err
}

if !strutil.Contains(constants.PortalSet, senderPortal) {
if !stringutil.Contains(constants.PortalSet, senderPortal) {
return nil, gerr.New(ctx, gerr.InvalidArgument, gerr.ErrorPortalNotFound, senderPortal)
}

req.RoleId = strutil.SimplifyStringList(req.RoleId)
req.RoleName = strutil.SimplifyStringList(req.RoleName)
req.Portal = strutil.SimplifyStringList(req.Portal)
req.Status = strutil.SimplifyStringList(req.Status)
req.UserId = strutil.SimplifyStringList(req.UserId)
req.RoleId = stringutil.SimplifyStringList(req.RoleId)
req.RoleName = stringutil.SimplifyStringList(req.RoleName)
req.Portal = stringutil.SimplifyStringList(req.Portal)
req.Status = stringutil.SimplifyStringList(req.Status)
req.UserId = stringutil.SimplifyStringList(req.UserId)

var addedRoleIds []string
if senderPortal == constants.PortalGlobalAdmin {
Expand All @@ -212,7 +212,7 @@ func DescribeRoles(ctx context.Context, req *pb.DescribeRolesRequest) (*pb.Descr
} else {
var inRoleIds []string
for _, roleId := range req.RoleId {
if strutil.Contains(roleIds, roleId) {
if stringutil.Contains(roleIds, roleId) {
inRoleIds = append(inRoleIds, roleId)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/service/am/resource/role_module_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"openpitrix.io/iam/pkg/global"
"openpitrix.io/iam/pkg/models"
"openpitrix.io/iam/pkg/pb"
"openpitrix.io/iam/pkg/util/strutil"
"openpitrix.io/iam/pkg/util/stringutil"
)

func getModuleTree(moduleApis []*models.ModuleApi) map[string]map[string]map[string][]*models.ModuleApi {
Expand Down Expand Up @@ -162,7 +162,7 @@ func ModifyRoleModule(ctx context.Context, req *pb.ModifyRoleModuleRequest) (*pb
var newEnableActionBundles []*models.EnableActionBundle
for _, moduleElem := range module.ModuleElemSet {
moduleId := moduleElem.ModuleId
if !strutil.Contains(visibilityModuleIds, moduleId) {
if !stringutil.Contains(visibilityModuleIds, moduleId) {
return nil, gerr.New(ctx, gerr.NotFound, gerr.ErrorModuleNotFound, moduleId)
}

Expand Down
64 changes: 59 additions & 5 deletions pkg/util/idutil/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ package idutil

import (
"crypto/rand"
"errors"
"net"

"github.com/sony/sonyflake"
hashids "github.com/speps/go-hashids"

"openpitrix.io/iam/pkg/util/strutil"
"openpitrix.io/iam/pkg/util/stringutil"
)

var sf *sonyflake.Sonyflake
var upperMachineID uint16

func init() {
var st sonyflake.Settings
sf = sonyflake.NewSonyflake(st)
if sf == nil {
sf = sonyflake.NewSonyflake(sonyflake.Settings{
MachineID: lower16BitIP,
})
upperMachineID, _ = upper16BitIP()
}
}

func GetIntId() uint64 {
Expand All @@ -36,12 +45,16 @@ func GetUuid(prefix string) string {
if err != nil {
panic(err)
}
i, err := h.Encode([]int{int(id)})
numbers := []int64{int64(id)}
if upperMachineID != 0 {
numbers = append(numbers, int64(upperMachineID))
}
i, err := h.EncodeInt64(numbers)
if err != nil {
panic(err)
}

return prefix + strutil.Reverse(i)
return prefix + stringutil.Reverse(i)
}

const (
Expand All @@ -58,12 +71,16 @@ func GetUuid36(prefix string) string {
if err != nil {
panic(err)
}
i, err := h.Encode([]int{int(id)})
numbers := []int64{int64(id)}
if upperMachineID != 0 {
numbers = append(numbers, int64(upperMachineID))
}
i, err := h.EncodeInt64(numbers)
if err != nil {
panic(err)
}

return prefix + strutil.Reverse(i)
return prefix + stringutil.Reverse(i)
}

func randString(letters string, n int) string {
Expand Down Expand Up @@ -105,3 +122,40 @@ func GetRefreshToken() string {
func GetAttachmentPrefix() string {
return randString(Alphabet62, 30)
}

func lower16BitIP() (uint16, error) {
ip, err := IPv4()
if err != nil {
return 0, err
}

return uint16(ip[2])<<8 + uint16(ip[3]), nil
}

func upper16BitIP() (uint16, error) {
ip, err := IPv4()
if err != nil {
return 0, err
}

return uint16(ip[0])<<8 + uint16(ip[1]), nil
}

func IPv4() (net.IP, error) {
as, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}

for _, a := range as {
ipnet, ok := a.(*net.IPNet)
if !ok || ipnet.IP.IsLoopback() {
continue
}

ip := ipnet.IP.To4()
return ip, nil

}
return nil, errors.New("no ip address")
}
2 changes: 1 addition & 1 deletion pkg/util/strutil/utils.go → pkg/util/stringutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a Apache license
// that can be found in the LICENSE file.

package strutil
package stringutil

import (
"regexp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a Apache license
// that can be found in the LICENSE file.

package strutil
package stringutil

import (
"testing"
Expand Down

0 comments on commit da580b0

Please sign in to comment.