Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events db #20

Merged
merged 11 commits into from
Apr 9, 2019
Merged

Events db #20

merged 11 commits into from
Apr 9, 2019

Conversation

bsoniam
Copy link
Contributor

@bsoniam bsoniam commented Apr 8, 2019

Store audit events in the DB (Maria DB)

@bsoniam bsoniam requested a review from harture April 8, 2019 14:16
@coveralls
Copy link

coveralls commented Apr 8, 2019

Pull Request Test Coverage Report for Build 186

  • 165 of 179 (92.18%) changed or added relevant lines in 3 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+2.08%) to 90.698%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pkg/event/component.go 94 95 98.95%
pkg/event/module.go 71 75 94.67%
internal/keycloakb/eventsdb.go 0 9 0.0%
Files with Coverage Reduction New Missed Lines %
pkg/management/http.go 1 94.94%
Totals Coverage Status
Change from base Build 161: 2.08%
Covered Lines: 1482
Relevant Lines: 1634

💛 - Coveralls

Copy link
Contributor

@harture harture left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some tests failure in Travis CI

@@ -233,20 +234,25 @@ func main() {
//Ping() error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove this commented line ?

@@ -233,20 +234,25 @@ func main() {
//Ping() error
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need Exec, Query and QueryRow in the interface here ? It seems it is not used there.

@@ -118,6 +119,76 @@ func (c *adminComponent) AdminEvent(ctx context.Context, adminEvent *fb.AdminEve
}
}

func addCTtypeToEvent(event map[string]string) map[string]string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could use a switch. I think it would be easier to read and avoid some not needed evaluations.

//ACCOUNT_CREATED
if event["operationType"] == "CREATE" {
// check if the resourcePath starts with prefix users
if strings.HasPrefix(event["resourcePath"], "users") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there multiple possible value ? That's why we use prefix instead of strict equality ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resourcePath is of the format "users/UUID..." or "roles/...."

@@ -150,15 +239,25 @@ func eventToMap(event *fb.Event) map[string]string {
eventMap["ipAddress"] = string(event.IpAddress())
eventMap["error"] = string(event.Error())

var detailsString string
var detailsMap map[string]string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have a warning there as you could do it in one line

//username - username of the user that is impacted by the action
username := "" //
ctEventType := m["ct_event_type"] // the ct event type is established before at the component level
kcEventType := m["type"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment for each next ones would be good


_, err := cm.db.Exec(insertEvent, origin, realmName, agentUserID, agentUsername, userID, username, ctEventType, kcEventType, kcOperationType, clientID, additionalInfo)
// if ctEventType is not "", then record the events in MariaDB
if m["ct_event_type"] != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be easier to read, if you consider the case where == "" , then return nil.

//userId is in the resourcePath
if resourcePath, ok := m["resourcePath"]; ok {
reg := regexp.MustCompile(`[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}`)
if strings.HasPrefix(resourcePath, "users") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to simplify it with? :
if m["resourcePath"] == "users"{
//....
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because resourcePath has as preffix "users" , followed by an UUID. It can also be "roles" followed also by an UUID

err := json.Unmarshal(eventAuthDetails, &h)

if err != nil {
fmt.Println(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fmt should not be used I think. we should use the logger instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right; the logger is TBD

return err
}

// in authdetails part we can retrieve the client id, agent realm id, agent user id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about of the following alternative ? Would it be easier to read/maintain or not

Suggested change
// in authdetails part we can retrieve the client id, agent realm id, agent user id
for k, v := range h {
switch k {
case "clientId":
clientId = h["clientId"]
case "//////":
///////
default:
infoMap[k] = v
}
}

Copy link
Contributor

@harture harture left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code coverage has decreased, it seems some of your new code is not covered mainly in module.go

@harture harture merged commit a085e28 into master Apr 9, 2019
@harture harture deleted the events_db branch April 25, 2019 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants