From e9d5789dd45ffcda0107c23bd4a90f3de9511c1f Mon Sep 17 00:00:00 2001 From: Gyuho Lee Date: Mon, 30 Apr 2018 15:10:56 -0700 Subject: [PATCH 1/3] auth: remove "strings.Compare == 0" Signed-off-by: Gyuho Lee --- auth/simple_token.go | 2 +- auth/store.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/auth/simple_token.go b/auth/simple_token.go index 9fd4766e8..49797cbd7 100644 --- a/auth/simple_token.go +++ b/auth/simple_token.go @@ -148,7 +148,7 @@ func (t *tokenSimple) invalidateUser(username string) { } t.simpleTokensMu.Lock() for token, name := range t.simpleTokens { - if strings.Compare(name, username) == 0 { + if name == username { delete(t.simpleTokens, token) t.simpleTokenKeeper.deleteSimpleToken(token) } diff --git a/auth/store.go b/auth/store.go index 13e098aba..b2a9ae013 100644 --- a/auth/store.go +++ b/auth/store.go @@ -412,7 +412,7 @@ func (as *authStore) UserAdd(r *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse, } func (as *authStore) UserDelete(r *pb.AuthUserDeleteRequest) (*pb.AuthUserDeleteResponse, error) { - if as.enabled && strings.Compare(r.Name, rootUser) == 0 { + if as.enabled && r.Name == rootUser { if as.lg != nil { as.lg.Warn("cannot delete 'root' user", zap.String("user-name", r.Name)) } else { @@ -518,7 +518,7 @@ func (as *authStore) UserGrantRole(r *pb.AuthUserGrantRoleRequest) (*pb.AuthUser } idx := sort.SearchStrings(user.Roles, r.Role) - if idx < len(user.Roles) && strings.Compare(user.Roles[idx], r.Role) == 0 { + if idx < len(user.Roles) && user.Roles[idx] == r.Role { if as.lg != nil { as.lg.Warn( "ignored grant role request to a user", @@ -583,7 +583,7 @@ func (as *authStore) UserList(r *pb.AuthUserListRequest) (*pb.AuthUserListRespon } func (as *authStore) UserRevokeRole(r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUserRevokeRoleResponse, error) { - if as.enabled && strings.Compare(r.Name, rootUser) == 0 && strings.Compare(r.Role, rootRole) == 0 { + if as.enabled && r.Name == rootUser && r.Role == rootRole { if as.lg != nil { as.lg.Warn( "'root' user cannot revoke 'root' role", @@ -611,7 +611,7 @@ func (as *authStore) UserRevokeRole(r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUs } for _, role := range user.Roles { - if strings.Compare(role, r.Role) != 0 { + if role != r.Role { updatedUser.Roles = append(updatedUser.Roles, role) } } @@ -714,7 +714,7 @@ func (as *authStore) RoleRevokePermission(r *pb.AuthRoleRevokePermissionRequest) } func (as *authStore) RoleDelete(r *pb.AuthRoleDeleteRequest) (*pb.AuthRoleDeleteResponse, error) { - if as.enabled && strings.Compare(r.Role, rootRole) == 0 { + if as.enabled && r.Role == rootRole { if as.lg != nil { as.lg.Warn("cannot delete 'root' role", zap.String("role-name", r.Role)) } else { @@ -742,7 +742,7 @@ func (as *authStore) RoleDelete(r *pb.AuthRoleDeleteRequest) (*pb.AuthRoleDelete } for _, role := range user.Roles { - if strings.Compare(role, r.Role) != 0 { + if role != r.Role { updatedUser.Roles = append(updatedUser.Roles, role) } } From 86cee93d6b592105f7d30a438480295be2e12fa4 Mon Sep 17 00:00:00 2001 From: Gyuho Lee Date: Mon, 30 Apr 2018 15:11:11 -0700 Subject: [PATCH 2/3] etcdctl/ctlv3: remove "strings.Compare == 0" Signed-off-by: Gyuho Lee --- etcdctl/ctlv3/command/printer_simple.go | 6 +++--- etcdctl/ctlv3/command/user_command.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/etcdctl/ctlv3/command/printer_simple.go b/etcdctl/ctlv3/command/printer_simple.go index 99b27b68c..dee958688 100644 --- a/etcdctl/ctlv3/command/printer_simple.go +++ b/etcdctl/ctlv3/command/printer_simple.go @@ -189,12 +189,12 @@ func (s *simplePrinter) RoleGet(role string, r v3.AuthRoleGetResponse) { printRange := func(perm *v3.Permission) { sKey := string(perm.Key) sRangeEnd := string(perm.RangeEnd) - if strings.Compare(sRangeEnd, "\x00") != 0 { + if sRangeEnd != "\x00" { fmt.Printf("\t[%s, %s)", sKey, sRangeEnd) } else { fmt.Printf("\t[%s, ", sKey) } - if strings.Compare(v3.GetPrefixRangeEnd(sKey), sRangeEnd) == 0 { + if v3.GetPrefixRangeEnd(sKey) == sRangeEnd { fmt.Printf(" (prefix %s)", sKey) } fmt.Printf("\n") @@ -240,7 +240,7 @@ func (s *simplePrinter) RoleRevokePermission(role string, key string, end string fmt.Printf("Permission of key %s is revoked from role %s\n", key, role) return } - if strings.Compare(end, "\x00") != 0 { + if end != "\x00" { fmt.Printf("Permission of range [%s, %s) is revoked from role %s\n", key, end, role) } else { fmt.Printf("Permission of range [%s, is revoked from role %s\n", key, role) diff --git a/etcdctl/ctlv3/command/user_command.go b/etcdctl/ctlv3/command/user_command.go index 4df56f720..c243561de 100644 --- a/etcdctl/ctlv3/command/user_command.go +++ b/etcdctl/ctlv3/command/user_command.go @@ -272,7 +272,7 @@ func readPasswordInteractive(name string) string { ExitWithError(ExitBadArgs, fmt.Errorf("failed to ask password: %s.", err2)) } - if strings.Compare(password1, password2) != 0 { + if password1 != password2 { ExitWithError(ExitBadArgs, fmt.Errorf("given passwords are different.")) } From 48f28b9d2707f0cb52a6a5f36a2c760be423e30c Mon Sep 17 00:00:00 2001 From: Gyuho Lee Date: Mon, 30 Apr 2018 15:11:26 -0700 Subject: [PATCH 3/3] lease/leasehttp: remove "strings.Compare != 0" Signed-off-by: Gyuho Lee --- lease/leasehttp/http_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lease/leasehttp/http_test.go b/lease/leasehttp/http_test.go index 367cd8e64..1148111d7 100644 --- a/lease/leasehttp/http_test.go +++ b/lease/leasehttp/http_test.go @@ -19,7 +19,6 @@ import ( "net/http" "net/http/httptest" "os" - "strings" "testing" "time" @@ -110,7 +109,7 @@ func testApplyTimeout(t *testing.T, f func(*lease.Lease, string) error) { if err == nil { t.Fatalf("expected timeout error, got nil") } - if strings.Compare(err.Error(), ErrLeaseHTTPTimeout.Error()) != 0 { + if err.Error() != ErrLeaseHTTPTimeout.Error() { t.Fatalf("expected (%v), got (%v)", ErrLeaseHTTPTimeout.Error(), err.Error()) } }