Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
small refactor, docs and cleanup
  • Loading branch information
jlangy committed May 24, 2024
1 parent 2b044fd commit 82835a7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 43 deletions.
3 changes: 2 additions & 1 deletion aggregator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ In order to avoid the custom codebase parsing the requests, it relies on `Grafan
A lightweight Go server running scheduled jobs. There are two cronjobs it controls:

1. Deleting old client events.
2. Collecting and clearing client session counts.
2. Collecting client session counts.

## Environment Variables

Expand All @@ -21,6 +21,7 @@ A lightweight Go server running scheduled jobs. There are two cronjobs it contro
- `DB_PASSWORD`: the password to be used for password authentication.
- `RETENTION_PERIOD`: the duration of time to keep the aggregated data.
- please see [Postgres Interval Input](https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-INTERVAL-INPUT) for the unit convention.
- `RC_WEBHOOK`: The url for the rocketchat webhook to use when notifying from the compactor
- `DEV_KEYCLOAK_URL`: The development keycloak base URL
- `DEV_KEYCLOAK_CLIENT_ID`: The development keycloak client id
- `DEV_KEYCLOAK_USERNAME`: The development keycloak username
Expand Down
24 changes: 20 additions & 4 deletions aggregator/keycloak/tokenManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (rm *RequestHandler) GetToken() (string, string, error) {
refreshTokenExpired, err := rm.IsTokenExpired(rm.RefreshToken)

var formData url.Values

// If there is an error or expiry, must get a new set of tokens. Otherwise refresh
if refreshTokenExpired || err != nil {
formData = url.Values{
"grant_type": {"password"},
Expand Down Expand Up @@ -180,8 +180,10 @@ func (tm *RequestHandler) IsTokenExpired(token string) (bool, error) {
}
}

// Method to perform an HTTP request with token management
func (rm *RequestHandler) DoRequest(req *http.Request) (*http.Response, error) {
/*
Method to perform an HTTP request with token management. Returns the body or an error if network failure or non-200 status code.
*/
func (rm *RequestHandler) DoRequest(req *http.Request) ([]byte, error) {
accessToken, refreshToken, err := rm.GetToken()

if err != nil {
Expand All @@ -198,5 +200,19 @@ func (rm *RequestHandler) DoRequest(req *http.Request) (*http.Response, error) {
if err != nil {
return nil, err
}
return resp, err

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
log.Printf("non 200 status code returned from realm request: %v", resp.Status)
return nil, errors.New("non 200 status code returned from realm request")
}

body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()

if err != nil {
log.Printf("Error reading response body: %v", err)
return nil, err
}

return body, err
}
32 changes: 2 additions & 30 deletions aggregator/model/client_sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ package model

import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strings"

"io"

"github.com/go-co-op/gocron"
"sso-dashboard.bcgov.com/aggregator/config"
"sso-dashboard.bcgov.com/aggregator/keycloak"
Expand All @@ -30,28 +27,15 @@ type SessionStats struct {
var RealmErrorMessage = "Error getting realms for env %s: "
var ClientErrorMessage = "Error getting client stats for env %s: "

type SessionInserter func(environment string, realmID string, clientID string, activeSessions string, offlineSessions string) error

func GetRealms(rm *keycloak.RequestHandler) ([]string, error) {
req, err := http.NewRequest("GET", rm.ApiBaseUrl+"/admin/realms", nil)
if err != nil {
log.Printf("Error occurred creating request: %v", err)
return nil, err
}

resp, err := rm.DoRequest(req)

if err != nil {
log.Print("Error occurred making getting realms", err)
return nil, err
}

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
log.Printf("non 200 status code returned from realm request: %v", resp.Status)
return nil, errors.New("non 200 status code returned from realm request")
}
body, err := rm.DoRequest(req)

body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("Error reading response body: %v", err)
return nil, err
Expand Down Expand Up @@ -87,19 +71,7 @@ func GetClientStats(rm *keycloak.RequestHandler, realms []string, env string) bo
continue
}

resp, err := rm.DoRequest(req)
if err != nil {
handleError(fmt.Sprintf("Error occurred creating request: %v", err))
continue
}

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
handleError(fmt.Sprintf("non 200 status code returned from realm request: %v", resp.Status))
continue
}

defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
body, err := rm.DoRequest(req)

if err != nil {
handleError(fmt.Sprintf("Error reading response body: %v", err))
Expand Down
2 changes: 1 addition & 1 deletion aggregator/model/client_sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (m *MockRocketChat) ResetMock() {
m.Messages = [][]string{}
}

func TestClientSessionTokenFailure(t *testing.T) {
func TestTokenFailure(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/realms/master/protocol/openid-connect/token" {
// Fail all token requests
Expand Down
7 changes: 0 additions & 7 deletions aggregator/webhooks/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ import (
"sso-dashboard.bcgov.com/aggregator/utils"
)

type Post struct {
Id int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
UserId int `json:"userId"`
}

type RocketChatNotifier interface {
NotifyRocketChat(text string, title string, body string)
}
Expand Down

0 comments on commit 82835a7

Please sign in to comment.