Skip to content

Commit

Permalink
[CLOUDTRUST-1324] Fix reading of NULL values from audit table
Browse files Browse the repository at this point in the history
  • Loading branch information
fperot74 authored and harture committed Jun 7, 2019
1 parent f1e7759 commit 975f36c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
47 changes: 47 additions & 0 deletions api/events/api.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package events_api

import "database/sql"

// AuditEventsRepresentation is the type of the GetEvents response
type AuditEventsRepresentation struct {
Events []AuditRepresentation `json:"events"`
Expand All @@ -24,9 +26,54 @@ type AuditRepresentation struct {
AdditionalInfo string `json:"additionalInfo,omitempty"`
}

// DbAuditRepresentation is a non serializable AuditRepresentation read from database
type DbAuditRepresentation struct {
AuditID int64
AuditTime int64
Origin sql.NullString
RealmName sql.NullString
AgentUserID sql.NullString
AgentUsername sql.NullString
AgentRealmName sql.NullString
UserID sql.NullString
Username sql.NullString
CtEventType sql.NullString
KcEventType sql.NullString
KcOperationType sql.NullString
ClientID sql.NullString
AdditionalInfo sql.NullString
}

// EventSummaryRepresentation elements returned by GetEventsSummary
type EventSummaryRepresentation struct {
Origins []string `json:"origins,omitempty"`
Realms []string `json:"realms,omitempty"`
CtEventTypes []string `json:"ctEventTypes,omitempty"`
}

func toString(sqlValue sql.NullString) string {
if sqlValue.Valid {
return sqlValue.String
}
return ""
}

// ToAuditRepresentation converts a DbAuditRepresentation to a serializable value
func (dba *DbAuditRepresentation) ToAuditRepresentation() AuditRepresentation {
return AuditRepresentation{
AuditID: dba.AuditID,
AuditTime: dba.AuditTime,
Origin: toString(dba.Origin),
RealmName: toString(dba.RealmName),
AgentUserID: toString(dba.AgentUserID),
AgentUsername: toString(dba.AgentUsername),
AgentRealmName: toString(dba.AgentRealmName),
UserID: toString(dba.UserID),
Username: toString(dba.Username),
CtEventType: toString(dba.CtEventType),
KcEventType: toString(dba.KcEventType),
KcOperationType: toString(dba.KcOperationType),
ClientID: toString(dba.ClientID),
AdditionalInfo: toString(dba.AdditionalInfo),
}
}
21 changes: 21 additions & 0 deletions api/events/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package events_api

import (
"database/sql"
"testing"

"github.com/stretchr/testify/assert"
)

func TestToAuditRepresentation(t *testing.T) {
var dba = DbAuditRepresentation{
AuditID: 45,
AuditTime: 46,
Origin: sql.NullString{String: "Origin", Valid: true},
AdditionalInfo: sql.NullString{String: "Additional", Valid: false},
}
var audit = dba.ToAuditRepresentation()

assert.Equal(t, "Origin", audit.Origin)
assert.Equal(t, "", audit.AdditionalInfo)
}
5 changes: 0 additions & 5 deletions configs/authorization.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@
"master": {
"*": {}
}
},
"ST_GetStatistics": {
"master": {
"*": {}
}
}
},
"integrator_manager":{
Expand Down
8 changes: 4 additions & 4 deletions internal/keycloakb/eventsdbmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ func (cm *eventsDBModule) GetEvents(_ context.Context, m map[string]string) ([]a
defer rows.Close()

for rows.Next() {
var audit api.AuditRepresentation
err = rows.Scan(&audit.AuditID, &audit.AuditTime, &audit.Origin, &audit.RealmName, &audit.AgentUserID, &audit.AgentUsername, &audit.AgentRealmName,
&audit.UserID, &audit.Username, &audit.CtEventType, &audit.KcEventType, &audit.KcOperationType, &audit.ClientID, &audit.AdditionalInfo)
var dba api.DbAuditRepresentation
err = rows.Scan(&dba.AuditID, &dba.AuditTime, &dba.Origin, &dba.RealmName, &dba.AgentUserID, &dba.AgentUsername, &dba.AgentRealmName,
&dba.UserID, &dba.Username, &dba.CtEventType, &dba.KcEventType, &dba.KcOperationType, &dba.ClientID, &dba.AdditionalInfo)
if err != nil {
return res, err
}
res = append(res, audit)
res = append(res, dba.ToAuditRepresentation())
}

// Return an error from rows if any error was encountered by Rows.Scan
Expand Down

0 comments on commit 975f36c

Please sign in to comment.