feat: support auth account for etcd (#1174)
parent
c1a8ccda11
commit
59b9687f31
@ -0,0 +1,7 @@
|
|||||||
|
package discov
|
||||||
|
|
||||||
|
import "github.com/tal-tech/go-zero/core/discov/internal"
|
||||||
|
|
||||||
|
func RegisterAccount(endpoints []string, user, pass string) {
|
||||||
|
internal.AddAccount(endpoints, user, pass)
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package discov
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/tal-tech/go-zero/core/discov/internal"
|
||||||
|
"github.com/tal-tech/go-zero/core/stringx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRegisterAccount(t *testing.T) {
|
||||||
|
endpoints := []string{
|
||||||
|
"localhost:2379",
|
||||||
|
}
|
||||||
|
user := "foo" + stringx.Rand()
|
||||||
|
RegisterAccount(endpoints, user, "bar")
|
||||||
|
account, ok := internal.GetAccount(endpoints)
|
||||||
|
assert.True(t, ok)
|
||||||
|
assert.Equal(t, user, account.User)
|
||||||
|
assert.Equal(t, "bar", account.Pass)
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
type Account struct {
|
||||||
|
User string
|
||||||
|
Pass string
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
accounts = make(map[string]Account)
|
||||||
|
lock sync.RWMutex
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddAccount(endpoints []string, user, pass string) {
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
|
||||||
|
accounts[getClusterKey(endpoints)] = Account{
|
||||||
|
User: user,
|
||||||
|
Pass: pass,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAccount(endpoints []string) (Account, bool) {
|
||||||
|
lock.RLock()
|
||||||
|
defer lock.RUnlock()
|
||||||
|
|
||||||
|
account, ok := accounts[getClusterKey(endpoints)]
|
||||||
|
return account, ok
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/tal-tech/go-zero/core/stringx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAccount(t *testing.T) {
|
||||||
|
endpoints := []string{
|
||||||
|
"192.168.0.2:2379",
|
||||||
|
"192.168.0.3:2379",
|
||||||
|
"192.168.0.4:2379",
|
||||||
|
}
|
||||||
|
username := "foo" + stringx.Rand()
|
||||||
|
password := "bar"
|
||||||
|
anotherPassword := "any"
|
||||||
|
|
||||||
|
_, ok := GetAccount(endpoints)
|
||||||
|
assert.False(t, ok)
|
||||||
|
|
||||||
|
AddAccount(endpoints, username, password)
|
||||||
|
account, ok := GetAccount(endpoints)
|
||||||
|
assert.True(t, ok)
|
||||||
|
assert.Equal(t, username, account.User)
|
||||||
|
assert.Equal(t, password, account.Pass)
|
||||||
|
|
||||||
|
AddAccount(endpoints, username, anotherPassword)
|
||||||
|
account, ok = GetAccount(endpoints)
|
||||||
|
assert.True(t, ok)
|
||||||
|
assert.Equal(t, username, account.User)
|
||||||
|
assert.Equal(t, anotherPassword, account.Pass)
|
||||||
|
}
|
Loading…
Reference in New Issue