From 08415182f8245efb85d73d258ea6457c7c110564 Mon Sep 17 00:00:00 2001 From: braginini Date: Mon, 6 Jun 2022 09:54:14 +0200 Subject: [PATCH 1/7] Add IDP cache warmup --- management/server/account.go | 41 ++++++- management/server/idp/auth0.go | 218 +++++++++++++++++++++++++++++++++ management/server/idp/idp.go | 1 + 3 files changed, 256 insertions(+), 4 deletions(-) diff --git a/management/server/account.go b/management/server/account.go index 6805f17dbd7..1f8e2127311 100644 --- a/management/server/account.go +++ b/management/server/account.go @@ -13,6 +13,7 @@ import ( log "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "math/rand" "reflect" "strings" "sync" @@ -20,9 +21,11 @@ import ( ) const ( - PublicCategory = "public" - PrivateCategory = "private" - UnknownCategory = "unknown" + PublicCategory = "public" + PrivateCategory = "private" + UnknownCategory = "unknown" + CacheExpirationMax = 7 * 24 * 3600 * time.Second // 7 days + CacheExpirationMin = 3 * 24 * 3600 * time.Second // 3 days ) type AccountManager interface { @@ -173,14 +176,44 @@ func BuildManager( } } - gocacheClient := gocache.New(7*24*time.Hour, 30*time.Minute) + gocacheClient := gocache.New(CacheExpirationMax, 30*time.Minute) gocacheStore := cacheStore.NewGoCache(gocacheClient, nil) am.cacheManager = cache.NewLoadable(am.loadFromCache, cache.New(gocacheStore)) + + go func() { + err := am.warmupIDPCache() + if err != nil { + log.Warnf("failed warming up cache due to error: %v", err) + //todo retry? + return + } + }() + return am, nil } +func (am *DefaultAccountManager) warmupIDPCache() error { + userData, err := am.idpManager.GetAllAccounts() + if err != nil { + return err + } + + for accountID, users := range userData { + rand.Seed(time.Now().UnixNano()) + + r := rand.Intn(int(CacheExpirationMax.Milliseconds()-CacheExpirationMin.Milliseconds())) + int(CacheExpirationMin.Milliseconds()) + expiration := time.Duration(r) * time.Millisecond + err = am.cacheManager.Set(am.ctx, accountID, users, &cacheStore.Options{Expiration: expiration}) + if err != nil { + return err + } + } + log.Infof("warmed up IDP cache with %d entries", len(userData)) + return nil +} + // AddSetupKey generates a new setup key with a given name and type, and adds it to the specified account func (am *DefaultAccountManager) AddSetupKey( accountId string, diff --git a/management/server/idp/auth0.go b/management/server/idp/auth0.go index a10d9220f25..6ffaae2bf5f 100644 --- a/management/server/idp/auth0.go +++ b/management/server/idp/auth0.go @@ -1,6 +1,9 @@ package idp import ( + "bytes" + "compress/gzip" + "context" "encoding/json" "fmt" "io" @@ -51,6 +54,38 @@ type Auth0Credentials struct { mux sync.Mutex } +type UserExportJobResponse struct { + Type string `json:"type"` + Status string `json:"status"` + ConnectionId string `json:"connection_id"` + Format string `json:"format"` + Limit int `json:"limit"` + Connection string `json:"connection"` + CreatedAt time.Time `json:"created_at"` + Id string `json:"id"` +} + +type ExportJobStatusResponse struct { + Type string `json:"type"` + Status string `json:"status"` + ConnectionId string `json:"connection_id"` + Format string `json:"format"` + Limit int `json:"limit"` + Location string `json:"location"` + Connection string `json:"connection"` + CreatedAt time.Time `json:"created_at"` + Id string `json:"id"` +} + +type Auth0Profile struct { + AccountId string `json:"wt_account_id"` + UserID string `json:"user_id"` + Name string `json:"name"` + Email string `json:"email"` + CreatedAt string `json:"created_at"` + LastLogin string `json:"last_login"` +} + // NewAuth0Manager creates a new instance of the Auth0Manager func NewAuth0Manager(config Auth0ClientConfig) (*Auth0Manager, error) { @@ -359,3 +394,186 @@ func (am *Auth0Manager) UpdateUserAppMetadata(userId string, appMetadata AppMeta return nil } + +// GetAllAccounts gets all registered accounts with corresponding user data. +// It returns a list of users indexed by accountID. +func (am *Auth0Manager) GetAllAccounts() (map[string][]*UserData, error) { + jwtToken, err := am.credentials.Authenticate() + if err != nil { + return nil, err + } + + reqURL := am.authIssuer + "/api/v2/jobs/users-exports" + + payloadString := fmt.Sprintf("{\"format\": \"json\"," + + "\"fields\": [{\"name\": \"created_at\"}, {\"name\": \"last_login\"},{\"name\": \"user_id\"}, {\"name\": \"email\"}, {\"name\": \"name\"}, {\"name\": \"app_metadata.wt_account_id\", \"export_as\": \"wt_account_id\"}]}") + + payload := strings.NewReader(payloadString) + + exportJobReq, err := http.NewRequest("POST", reqURL, payload) + if err != nil { + return nil, err + } + exportJobReq.Header.Add("authorization", "Bearer "+jwtToken.AccessToken) + exportJobReq.Header.Add("content-type", "application/json") + + jobResp, err := am.httpClient.Do(exportJobReq) + if err != nil { + log.Debugf("Couldn't get job response %v", err) + return nil, err + } + + defer func() { + err = jobResp.Body.Close() + if err != nil { + log.Errorf("error while closing update user app metadata response body: %v", err) + } + }() + if jobResp.StatusCode != 200 { + return nil, fmt.Errorf("unable to update the appMetadata, statusCode %d", jobResp.StatusCode) + } + + var exportJobResp UserExportJobResponse + + body, err := ioutil.ReadAll(jobResp.Body) + if err != nil { + log.Debugf("Coudln't read export job response; %v", err) + return nil, err + } + + err = am.helper.Unmarshal(body, &exportJobResp) + if err != nil { + log.Debugf("Coudln't unmarshal export job response; %v", err) + return nil, err + } + + if exportJobResp.Id == "" { + return nil, fmt.Errorf("couldn't get an batch id status %d, %s, response body: %v", jobResp.StatusCode, jobResp.Status, exportJobResp) + } + + log.Debugf("batch id status %d, %s, response body: %v", jobResp.StatusCode, jobResp.Status, exportJobResp) + + done, downloadLink, err := am.checkExportJobStatus(exportJobResp.Id) + if err != nil { + log.Debugf("Failed at getting status checks from exportJob; %v", err) + return nil, err + } + + if done { + return am.downloadProfileExport(downloadLink) + } + + return nil, fmt.Errorf("failed extracting user profiles from auth0") +} + +// checkExportJobStatus checks the status of the job created at CreateExportUsersJob. +// If the status is "completed", then return the downloadLink +func (am *Auth0Manager) checkExportJobStatus(jobId string) (bool, string, error) { + ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second) + defer cancel() + retry := time.NewTicker(10 * time.Second) + for { + select { + case <-ctx.Done(): + log.Debugf("Export job status stopped...\n") + return false, "", ctx.Err() + case <-retry.C: + jwtToken, err := am.credentials.Authenticate() + if err != nil { + return false, "", err + } + + statusUrl := am.authIssuer + "/api/v2/jobs/" + jobId + body, err := doGetReq(am.httpClient, statusUrl, jwtToken.AccessToken) + if err != nil { + return false, "", err + } + + var status ExportJobStatusResponse + err = am.helper.Unmarshal(body, &status) + if err != nil { + return false, "", err + } + + log.Debugf("current export job status is %v", status.Status) + + if status.Status != "completed" { + continue + } + + return true, status.Location, nil + } + } +} + +// downloadProfileExport downloads user profiles from auth0 batch job +func (am *Auth0Manager) downloadProfileExport(location string) (map[string][]*UserData, error) { + body, err := doGetReq(am.httpClient, location, "") + if err != nil { + return nil, err + } + + bodyReader := bytes.NewReader(body) + + gzipReader, err := gzip.NewReader(bodyReader) + if err != nil { + return nil, err + } + + decoder := json.NewDecoder(gzipReader) + + res := make(map[string][]*UserData) + for decoder.More() { + profile := Auth0Profile{} + err = decoder.Decode(&profile) + if err != nil { + return nil, err + } + if profile.AccountId != "" { + if _, ok := res[profile.AccountId]; !ok { + res[profile.AccountId] = []*UserData{} + } + res[profile.AccountId] = append(res[profile.AccountId], + &UserData{ + ID: profile.UserID, + Name: profile.Name, + Email: profile.Email, + }) + } + } + + return res, nil +} + +// Boilerplate implementation for Get Requests. +func doGetReq(client ManagerHTTPClient, url, accessToken string) ([]byte, error) { + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, err + } + + if accessToken != "" { + req.Header.Add("authorization", "Bearer "+accessToken) + } + + res, err := client.Do(req) + if err != nil { + return nil, err + } + + defer func() { + err = res.Body.Close() + if err != nil { + log.Errorf("error while closing body for url %s: %v", url, err) + } + }() + if res.StatusCode != 200 { + return nil, fmt.Errorf("unable to get %s, statusCode %d", url, res.StatusCode) + } + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + return body, nil +} diff --git a/management/server/idp/idp.go b/management/server/idp/idp.go index 6991b893a08..672fbc3b642 100644 --- a/management/server/idp/idp.go +++ b/management/server/idp/idp.go @@ -12,6 +12,7 @@ type Manager interface { UpdateUserAppMetadata(userId string, appMetadata AppMetadata) error GetUserDataByID(userId string, appMetadata AppMetadata) (*UserData, error) GetBatchedUserData(accountId string) ([]*UserData, error) + GetAllAccounts() (map[string][]*UserData, error) } // Config an idp configuration struct to be loaded from management server's config file From a2505cd5ab0c01653404c09e79b783d2815d9dc2 Mon Sep 17 00:00:00 2001 From: braginini Date: Mon, 6 Jun 2022 09:55:02 +0200 Subject: [PATCH 2/7] Rename IDP manager method to get Account data --- management/server/account.go | 2 +- management/server/idp/auth0.go | 2 +- management/server/idp/idp.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/management/server/account.go b/management/server/account.go index 1f8e2127311..72706a1c457 100644 --- a/management/server/account.go +++ b/management/server/account.go @@ -365,7 +365,7 @@ func mergeLocalAndQueryUser(queried idp.UserData, local User) *UserInfo { } func (am *DefaultAccountManager) loadFromCache(ctx context.Context, accountID interface{}) (interface{}, error) { - return am.idpManager.GetBatchedUserData(fmt.Sprintf("%v", accountID)) + return am.idpManager.GetAccount(fmt.Sprintf("%v", accountID)) } func (am *DefaultAccountManager) lookupCache(accountUsers map[string]*User, accountID string) ([]*idp.UserData, error) { diff --git a/management/server/idp/auth0.go b/management/server/idp/auth0.go index 6ffaae2bf5f..0e748840256 100644 --- a/management/server/idp/auth0.go +++ b/management/server/idp/auth0.go @@ -240,7 +240,7 @@ func requestByUserIdUrl(authIssuer, userId string) string { } // GetBatchedUserData requests users in batches from Auth0 -func (am *Auth0Manager) GetBatchedUserData(accountId string) ([]*UserData, error) { +func (am *Auth0Manager) GetAccount(accountId string) ([]*UserData, error) { jwtToken, err := am.credentials.Authenticate() if err != nil { return nil, err diff --git a/management/server/idp/idp.go b/management/server/idp/idp.go index 672fbc3b642..f6bd942241d 100644 --- a/management/server/idp/idp.go +++ b/management/server/idp/idp.go @@ -11,7 +11,7 @@ import ( type Manager interface { UpdateUserAppMetadata(userId string, appMetadata AppMetadata) error GetUserDataByID(userId string, appMetadata AppMetadata) (*UserData, error) - GetBatchedUserData(accountId string) ([]*UserData, error) + GetAccount(accountId string) ([]*UserData, error) GetAllAccounts() (map[string][]*UserData, error) } From 31e781cab222029e54230b20f7574092d9784130 Mon Sep 17 00:00:00 2001 From: braginini Date: Mon, 6 Jun 2022 09:59:29 +0200 Subject: [PATCH 3/7] Run IDP cache warmup only when IDP manager is not nil --- management/server/account.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/management/server/account.go b/management/server/account.go index 72706a1c457..4b94713d6c7 100644 --- a/management/server/account.go +++ b/management/server/account.go @@ -181,14 +181,16 @@ func BuildManager( am.cacheManager = cache.NewLoadable(am.loadFromCache, cache.New(gocacheStore)) - go func() { - err := am.warmupIDPCache() - if err != nil { - log.Warnf("failed warming up cache due to error: %v", err) - //todo retry? - return - } - }() + if !isNil(am.idpManager) { + go func() { + err := am.warmupIDPCache() + if err != nil { + log.Warnf("failed warming up cache due to error: %v", err) + //todo retry? + return + } + }() + } return am, nil From d8e34b8ce7db8b39a3ea5451e81f8e12a7b0c325 Mon Sep 17 00:00:00 2001 From: braginini Date: Mon, 6 Jun 2022 10:19:07 +0200 Subject: [PATCH 4/7] Refactor export job request - make it struct --- management/server/idp/auth0.go | 36 +++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/management/server/idp/auth0.go b/management/server/idp/auth0.go index 0e748840256..f36a6c3357a 100644 --- a/management/server/idp/auth0.go +++ b/management/server/idp/auth0.go @@ -54,6 +54,11 @@ type Auth0Credentials struct { mux sync.Mutex } +type UserExportJobRequest struct { + Format string `json:"format"` + Fields []map[string]string `json:"fields"` +} + type UserExportJobResponse struct { Type string `json:"type"` Status string `json:"status"` @@ -395,6 +400,30 @@ func (am *Auth0Manager) UpdateUserAppMetadata(userId string, appMetadata AppMeta return nil } +func buildUserExportRequest() (string, error) { + req := &UserExportJobRequest{} + fields := make([]map[string]string, 0) + + for _, field := range []string{"created_at", "last_login", "user_id", "email", "name"} { + fields = append(fields, map[string]string{"name": field}) + } + + fields = append(fields, map[string]string{ + "name": "app_metadata.wt_account_id", + "export_as": "wt_account_id", + }) + + req.Format = "json" + req.Fields = fields + + str, err := json.Marshal(req) + if err != nil { + return "", err + } + + return string(str), nil +} + // GetAllAccounts gets all registered accounts with corresponding user data. // It returns a list of users indexed by accountID. func (am *Auth0Manager) GetAllAccounts() (map[string][]*UserData, error) { @@ -405,9 +434,10 @@ func (am *Auth0Manager) GetAllAccounts() (map[string][]*UserData, error) { reqURL := am.authIssuer + "/api/v2/jobs/users-exports" - payloadString := fmt.Sprintf("{\"format\": \"json\"," + - "\"fields\": [{\"name\": \"created_at\"}, {\"name\": \"last_login\"},{\"name\": \"user_id\"}, {\"name\": \"email\"}, {\"name\": \"name\"}, {\"name\": \"app_metadata.wt_account_id\", \"export_as\": \"wt_account_id\"}]}") - + payloadString, err := buildUserExportRequest() + if err != nil { + return nil, err + } payload := strings.NewReader(payloadString) exportJobReq, err := http.NewRequest("POST", reqURL, payload) From 2fa032bbfcef50eec6008bda70e391b4606be627 Mon Sep 17 00:00:00 2001 From: braginini Date: Mon, 6 Jun 2022 11:31:44 +0200 Subject: [PATCH 5/7] Add docs to public structs --- management/server/idp/auth0.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/management/server/idp/auth0.go b/management/server/idp/auth0.go index f36a6c3357a..0a9dd95f448 100644 --- a/management/server/idp/auth0.go +++ b/management/server/idp/auth0.go @@ -54,11 +54,13 @@ type Auth0Credentials struct { mux sync.Mutex } +// UserExportJobRequest is a user export request struct type UserExportJobRequest struct { Format string `json:"format"` Fields []map[string]string `json:"fields"` } +// UserExportJobResponse is a user export response struct type UserExportJobResponse struct { Type string `json:"type"` Status string `json:"status"` @@ -70,7 +72,8 @@ type UserExportJobResponse struct { Id string `json:"id"` } -type ExportJobStatusResponse struct { +// UserExportJobStatusResponse is a user export status response struct +type UserExportJobStatusResponse struct { Type string `json:"type"` Status string `json:"status"` ConnectionId string `json:"connection_id"` @@ -82,6 +85,7 @@ type ExportJobStatusResponse struct { Id string `json:"id"` } +// Auth0Profile represents an Auth0 user profile response type Auth0Profile struct { AccountId string `json:"wt_account_id"` UserID string `json:"user_id"` @@ -519,7 +523,7 @@ func (am *Auth0Manager) checkExportJobStatus(jobId string) (bool, string, error) return false, "", err } - var status ExportJobStatusResponse + var status UserExportJobStatusResponse err = am.helper.Unmarshal(body, &status) if err != nil { return false, "", err From bd51b24664a60bfe96e568ba0cde607a0f140c63 Mon Sep 17 00:00:00 2001 From: braginini Date: Mon, 6 Jun 2022 11:36:52 +0200 Subject: [PATCH 6/7] Fix codacy issues --- management/server/idp/auth0.go | 58 +++++++++++++++++----------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/management/server/idp/auth0.go b/management/server/idp/auth0.go index 0a9dd95f448..229ec86374f 100644 --- a/management/server/idp/auth0.go +++ b/management/server/idp/auth0.go @@ -54,26 +54,26 @@ type Auth0Credentials struct { mux sync.Mutex } -// UserExportJobRequest is a user export request struct -type UserExportJobRequest struct { +// userExportJobRequest is a user export request struct +type userExportJobRequest struct { Format string `json:"format"` Fields []map[string]string `json:"fields"` } -// UserExportJobResponse is a user export response struct -type UserExportJobResponse struct { +// userExportJobResponse is a user export response struct +type userExportJobResponse struct { Type string `json:"type"` Status string `json:"status"` - ConnectionId string `json:"connection_id"` + ConnectionID string `json:"connection_id"` Format string `json:"format"` Limit int `json:"limit"` Connection string `json:"connection"` CreatedAt time.Time `json:"created_at"` - Id string `json:"id"` + ID string `json:"id"` } -// UserExportJobStatusResponse is a user export status response struct -type UserExportJobStatusResponse struct { +// userExportJobStatusResponse is a user export status response struct +type userExportJobStatusResponse struct { Type string `json:"type"` Status string `json:"status"` ConnectionId string `json:"connection_id"` @@ -85,9 +85,9 @@ type UserExportJobStatusResponse struct { Id string `json:"id"` } -// Auth0Profile represents an Auth0 user profile response -type Auth0Profile struct { - AccountId string `json:"wt_account_id"` +// auth0Profile represents an Auth0 user profile response +type auth0Profile struct { + AccountID string `json:"wt_account_id"` UserID string `json:"user_id"` Name string `json:"name"` Email string `json:"email"` @@ -230,7 +230,7 @@ func (c *Auth0Credentials) Authenticate() (JWTToken, error) { return c.jwtToken, nil } -func batchRequestUsersUrl(authIssuer, accountId string, page int) (string, url.Values, error) { +func batchRequestUsersUrl(authIssuer, accountID string, page int) (string, url.Values, error) { u, err := url.Parse(authIssuer + "/api/v2/users") if err != nil { return "", nil, err @@ -238,7 +238,7 @@ func batchRequestUsersUrl(authIssuer, accountId string, page int) (string, url.V q := u.Query() q.Set("page", strconv.Itoa(page)) q.Set("search_engine", "v3") - q.Set("q", "app_metadata.wt_account_id:"+accountId) + q.Set("q", "app_metadata.wt_account_id:"+accountID) u.RawQuery = q.Encode() return u.String(), q, nil @@ -248,8 +248,8 @@ func requestByUserIdUrl(authIssuer, userId string) string { return authIssuer + "/api/v2/users/" + userId } -// GetBatchedUserData requests users in batches from Auth0 -func (am *Auth0Manager) GetAccount(accountId string) ([]*UserData, error) { +// GetAccount returns all the users for a given profile. Calls Auth0 API. +func (am *Auth0Manager) GetAccount(accountID string) ([]*UserData, error) { jwtToken, err := am.credentials.Authenticate() if err != nil { return nil, err @@ -260,7 +260,7 @@ func (am *Auth0Manager) GetAccount(accountId string) ([]*UserData, error) { // https://auth0.com/docs/manage-users/user-search/retrieve-users-with-get-users-endpoint#limitations // auth0 limitation of 1000 users via this endpoint for page := 0; page < 20; page++ { - reqURL, query, err := batchRequestUsersUrl(am.authIssuer, accountId, page) + reqURL, query, err := batchRequestUsersUrl(am.authIssuer, accountID, page) if err != nil { return nil, err } @@ -405,7 +405,7 @@ func (am *Auth0Manager) UpdateUserAppMetadata(userId string, appMetadata AppMeta } func buildUserExportRequest() (string, error) { - req := &UserExportJobRequest{} + req := &userExportJobRequest{} fields := make([]map[string]string, 0) for _, field := range []string{"created_at", "last_login", "user_id", "email", "name"} { @@ -467,7 +467,7 @@ func (am *Auth0Manager) GetAllAccounts() (map[string][]*UserData, error) { return nil, fmt.Errorf("unable to update the appMetadata, statusCode %d", jobResp.StatusCode) } - var exportJobResp UserExportJobResponse + var exportJobResp userExportJobResponse body, err := ioutil.ReadAll(jobResp.Body) if err != nil { @@ -481,13 +481,13 @@ func (am *Auth0Manager) GetAllAccounts() (map[string][]*UserData, error) { return nil, err } - if exportJobResp.Id == "" { + if exportJobResp.ID == "" { return nil, fmt.Errorf("couldn't get an batch id status %d, %s, response body: %v", jobResp.StatusCode, jobResp.Status, exportJobResp) } log.Debugf("batch id status %d, %s, response body: %v", jobResp.StatusCode, jobResp.Status, exportJobResp) - done, downloadLink, err := am.checkExportJobStatus(exportJobResp.Id) + done, downloadLink, err := am.checkExportJobStatus(exportJobResp.ID) if err != nil { log.Debugf("Failed at getting status checks from exportJob; %v", err) return nil, err @@ -502,7 +502,7 @@ func (am *Auth0Manager) GetAllAccounts() (map[string][]*UserData, error) { // checkExportJobStatus checks the status of the job created at CreateExportUsersJob. // If the status is "completed", then return the downloadLink -func (am *Auth0Manager) checkExportJobStatus(jobId string) (bool, string, error) { +func (am *Auth0Manager) checkExportJobStatus(jobID string) (bool, string, error) { ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second) defer cancel() retry := time.NewTicker(10 * time.Second) @@ -517,13 +517,13 @@ func (am *Auth0Manager) checkExportJobStatus(jobId string) (bool, string, error) return false, "", err } - statusUrl := am.authIssuer + "/api/v2/jobs/" + jobId - body, err := doGetReq(am.httpClient, statusUrl, jwtToken.AccessToken) + statusURL := am.authIssuer + "/api/v2/jobs/" + jobID + body, err := doGetReq(am.httpClient, statusURL, jwtToken.AccessToken) if err != nil { return false, "", err } - var status UserExportJobStatusResponse + var status userExportJobStatusResponse err = am.helper.Unmarshal(body, &status) if err != nil { return false, "", err @@ -558,16 +558,16 @@ func (am *Auth0Manager) downloadProfileExport(location string) (map[string][]*Us res := make(map[string][]*UserData) for decoder.More() { - profile := Auth0Profile{} + profile := auth0Profile{} err = decoder.Decode(&profile) if err != nil { return nil, err } - if profile.AccountId != "" { - if _, ok := res[profile.AccountId]; !ok { - res[profile.AccountId] = []*UserData{} + if profile.AccountID != "" { + if _, ok := res[profile.AccountID]; !ok { + res[profile.AccountID] = []*UserData{} } - res[profile.AccountId] = append(res[profile.AccountId], + res[profile.AccountID] = append(res[profile.AccountID], &UserData{ ID: profile.UserID, Name: profile.Name, From 05e96fdbf3ba6758004f41a387169b6c72fea51b Mon Sep 17 00:00:00 2001 From: braginini Date: Mon, 6 Jun 2022 11:42:55 +0200 Subject: [PATCH 7/7] Fix codacy issues --- management/server/idp/auth0.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/management/server/idp/auth0.go b/management/server/idp/auth0.go index 229ec86374f..bd73343fc7c 100644 --- a/management/server/idp/auth0.go +++ b/management/server/idp/auth0.go @@ -76,13 +76,13 @@ type userExportJobResponse struct { type userExportJobStatusResponse struct { Type string `json:"type"` Status string `json:"status"` - ConnectionId string `json:"connection_id"` + ConnectionID string `json:"connection_id"` Format string `json:"format"` Limit int `json:"limit"` Location string `json:"location"` Connection string `json:"connection"` CreatedAt time.Time `json:"created_at"` - Id string `json:"id"` + ID string `json:"id"` } // auth0Profile represents an Auth0 user profile response @@ -230,7 +230,7 @@ func (c *Auth0Credentials) Authenticate() (JWTToken, error) { return c.jwtToken, nil } -func batchRequestUsersUrl(authIssuer, accountID string, page int) (string, url.Values, error) { +func batchRequestUsersURL(authIssuer, accountID string, page int) (string, url.Values, error) { u, err := url.Parse(authIssuer + "/api/v2/users") if err != nil { return "", nil, err @@ -244,8 +244,8 @@ func batchRequestUsersUrl(authIssuer, accountID string, page int) (string, url.V return u.String(), q, nil } -func requestByUserIdUrl(authIssuer, userId string) string { - return authIssuer + "/api/v2/users/" + userId +func requestByUserIDURL(authIssuer, userID string) string { + return authIssuer + "/api/v2/users/" + userID } // GetAccount returns all the users for a given profile. Calls Auth0 API. @@ -260,7 +260,7 @@ func (am *Auth0Manager) GetAccount(accountID string) ([]*UserData, error) { // https://auth0.com/docs/manage-users/user-search/retrieve-users-with-get-users-endpoint#limitations // auth0 limitation of 1000 users via this endpoint for page := 0; page < 20; page++ { - reqURL, query, err := batchRequestUsersUrl(am.authIssuer, accountID, page) + reqURL, query, err := batchRequestUsersURL(am.authIssuer, accountID, page) if err != nil { return nil, err } @@ -313,13 +313,13 @@ func (am *Auth0Manager) GetAccount(accountID string) ([]*UserData, error) { } // GetUserDataByID requests user data from auth0 via ID -func (am *Auth0Manager) GetUserDataByID(userId string, appMetadata AppMetadata) (*UserData, error) { +func (am *Auth0Manager) GetUserDataByID(userID string, appMetadata AppMetadata) (*UserData, error) { jwtToken, err := am.credentials.Authenticate() if err != nil { return nil, err } - reqURL := requestByUserIdUrl(am.authIssuer, userId) + reqURL := requestByUserIDURL(am.authIssuer, userID) req, err := http.NewRequest(http.MethodGet, reqURL, nil) if err != nil { return nil, err @@ -358,14 +358,14 @@ func (am *Auth0Manager) GetUserDataByID(userId string, appMetadata AppMetadata) } // UpdateUserAppMetadata updates user app metadata based on userId and metadata map -func (am *Auth0Manager) UpdateUserAppMetadata(userId string, appMetadata AppMetadata) error { +func (am *Auth0Manager) UpdateUserAppMetadata(userID string, appMetadata AppMetadata) error { jwtToken, err := am.credentials.Authenticate() if err != nil { return err } - reqURL := am.authIssuer + "/api/v2/users/" + userId + reqURL := am.authIssuer + "/api/v2/users/" + userID data, err := am.helper.Marshal(appMetadata) if err != nil { @@ -383,7 +383,7 @@ func (am *Auth0Manager) UpdateUserAppMetadata(userId string, appMetadata AppMeta req.Header.Add("authorization", "Bearer "+jwtToken.AccessToken) req.Header.Add("content-type", "application/json") - log.Debugf("updating metadata for user %s", userId) + log.Debugf("updating metadata for user %s", userID) res, err := am.httpClient.Do(req) if err != nil {