mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-05 08:54:11 -04:00
WIP idpmanager users_cache by accountId
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
FROM gcr.io/distroless/base:debug
|
FROM gcr.io/distroless/base:debug
|
||||||
ENV WT_LOG_FILE=console
|
ENV WT_LOG_FILE=console
|
||||||
ENTRYPOINT [ "/go/bin/netbird","up"]
|
ENTRYPOINT [ "/go/bin/netbird","up"]
|
||||||
COPY netbird /go/bin/netbird
|
COPY netbird /go/bin/netbird
|
||||||
|
|||||||
@@ -21,11 +21,11 @@ import (
|
|||||||
|
|
||||||
// Auth0Manager auth0 manager client instance
|
// Auth0Manager auth0 manager client instance
|
||||||
type Auth0Manager struct {
|
type Auth0Manager struct {
|
||||||
authIssuer string
|
authIssuer string
|
||||||
httpClient ManagerHTTPClient
|
httpClient ManagerHTTPClient
|
||||||
credentials ManagerCredentials
|
credentials ManagerCredentials
|
||||||
helper ManagerHelper
|
helper ManagerHelper
|
||||||
cachedUsers map[string]Auth0Profile
|
cachedUsersByAccountId map[string][]Auth0Profile
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auth0ClientConfig auth0 manager client configurations
|
// Auth0ClientConfig auth0 manager client configurations
|
||||||
@@ -118,11 +118,11 @@ func NewAuth0Manager(config Auth0ClientConfig) (*Auth0Manager, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &Auth0Manager{
|
return &Auth0Manager{
|
||||||
authIssuer: config.AuthIssuer,
|
authIssuer: config.AuthIssuer,
|
||||||
credentials: credentials,
|
credentials: credentials,
|
||||||
httpClient: httpClient,
|
httpClient: httpClient,
|
||||||
helper: helper,
|
helper: helper,
|
||||||
cachedUsers: make(map[string]Auth0Profile),
|
cachedUsersByAccountId: make(map[string][]Auth0Profile),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,7 +296,7 @@ func (am *Auth0Manager) CreateExportUsersJob(accountId string) error {
|
|||||||
|
|
||||||
jobResp, err := am.httpClient.Do(exportJobReq)
|
jobResp, err := am.httpClient.Do(exportJobReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debug("Couldn't get job response %v", err)
|
log.Debugf("Couldn't get job response %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,13 +314,13 @@ func (am *Auth0Manager) CreateExportUsersJob(accountId string) error {
|
|||||||
|
|
||||||
body, err := ioutil.ReadAll(jobResp.Body)
|
body, err := ioutil.ReadAll(jobResp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debug("Coudln't read export job response; %v", err)
|
log.Debugf("Coudln't read export job response; %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = am.helper.Unmarshal(body, &exportJobResp)
|
err = am.helper.Unmarshal(body, &exportJobResp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debug("Coudln't unmarshal export job response; %v", err)
|
log.Debugf("Coudln't unmarshal export job response; %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,7 +340,7 @@ func (am *Auth0Manager) CreateExportUsersJob(accountId string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if done {
|
if done {
|
||||||
err = am.cacheUsers(downloadLink)
|
err = am.cacheUsers(accountId, downloadLink)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("Failed to cache users via download link; %v", err)
|
log.Debugf("Failed to cache users via download link; %v", err)
|
||||||
}
|
}
|
||||||
@@ -351,7 +351,7 @@ func (am *Auth0Manager) CreateExportUsersJob(accountId string) error {
|
|||||||
|
|
||||||
// Downloads the users from auth0 and caches it in memory
|
// Downloads the users from auth0 and caches it in memory
|
||||||
// We don't need
|
// We don't need
|
||||||
func (am *Auth0Manager) cacheUsers(location string) error {
|
func (am *Auth0Manager) cacheUsers(accountId, location string) error {
|
||||||
body, err := doGetReq(am.httpClient, location, "")
|
body, err := doGetReq(am.httpClient, location, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("Can't download cached users; %v", err)
|
log.Debugf("Can't download cached users; %v", err)
|
||||||
@@ -374,7 +374,8 @@ func (am *Auth0Manager) cacheUsers(location string) error {
|
|||||||
log.Errorf("Couldn't decode profile; %v", err)
|
log.Errorf("Couldn't decode profile; %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
am.cachedUsers[profile.UserID] = profile
|
|
||||||
|
am.cachedUsersByAccountId[accountId] = append(am.cachedUsersByAccountId[accountId], profile)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -383,7 +384,7 @@ func (am *Auth0Manager) cacheUsers(location string) error {
|
|||||||
// This checks the status of the job created at CreateExportUsersJob.
|
// This checks the status of the job created at CreateExportUsersJob.
|
||||||
// If the status is "completed", then return the downloadLink
|
// If the status is "completed", then return the downloadLink
|
||||||
func (am *Auth0Manager) checkExportJobStatus(ctx context.Context, jobId string) (bool, string, error) {
|
func (am *Auth0Manager) checkExportJobStatus(ctx context.Context, jobId string) (bool, string, error) {
|
||||||
retry := time.NewTicker(500 * time.Millisecond)
|
retry := time.NewTicker(time.Second)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
@@ -415,10 +416,15 @@ func (am *Auth0Manager) checkExportJobStatus(ctx context.Context, jobId string)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This recaches every use from account
|
||||||
|
func (am *Auth0Manager) ForceUpdateUserCache(accountId string) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (am *Auth0Manager) GetBatchedUserData(accountId string) ([]*UserData, error) {
|
func (am *Auth0Manager) GetBatchedUserData(accountId string) ([]*UserData, error) {
|
||||||
// first time calling this
|
// first time calling this
|
||||||
// we need to check whether we need to call for users we don't have
|
// we need to check whether we need to call for users we don't have
|
||||||
if len(am.cachedUsers) == 0 {
|
if len(am.cachedUsersByAccountId[accountId]) == 0 {
|
||||||
err := am.CreateExportUsersJob(accountId)
|
err := am.CreateExportUsersJob(accountId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("Couldn't cache users; %v", err)
|
log.Debugf("Couldn't cache users; %v", err)
|
||||||
@@ -428,7 +434,8 @@ func (am *Auth0Manager) GetBatchedUserData(accountId string) ([]*UserData, error
|
|||||||
|
|
||||||
var list []*UserData
|
var list []*UserData
|
||||||
|
|
||||||
for _, val := range am.cachedUsers {
|
cachedUsers := am.cachedUsersByAccountId[accountId]
|
||||||
|
for _, val := range cachedUsers {
|
||||||
list = append(list, &UserData{
|
list = append(list, &UserData{
|
||||||
Name: val.Name,
|
Name: val.Name,
|
||||||
Email: val.Email,
|
Email: val.Email,
|
||||||
|
|||||||
Reference in New Issue
Block a user