From 57004ad78076e9d5947d2f4393487c461c93cfab Mon Sep 17 00:00:00 2001 From: harture <31417989+harture@users.noreply.github.com> Date: Mon, 2 Nov 2020 09:47:40 +0100 Subject: [PATCH] [CLOUDTRUST-2796] Remove event-receiver (#258) --- Gopkg.lock | 24 +- README.md | 19 +- api/event/event.fbs | 177 -------- api/event/fb/AdminEvent.go | 180 -------- api/event/fb/AuthDetails.go | 89 ---- api/event/fb/Event.go | 171 -------- api/event/fb/EventType.go | 185 --------- api/event/fb/OperationType.go | 19 - api/event/fb/ResourceType.go | 75 ---- api/event/fb/Tuple.go | 56 --- cmd/keycloakb/keycloak_bridge.go | 141 +------ configs/keycloak_bridge.yml | 4 - pkg/event/component.go | 351 ---------------- pkg/event/component_test.go | 687 ------------------------------- pkg/event/endpoint.go | 27 -- pkg/event/endpoint_test.go | 43 -- pkg/event/http.go | 120 ------ pkg/event/http_test.go | 184 --------- pkg/event/instrumenting.go | 167 -------- pkg/event/instrumenting_test.go | 159 ------- pkg/event/logging.go | 162 -------- pkg/event/logging_test.go | 152 ------- pkg/event/mock_test.go | 8 - pkg/event/module.go | 64 --- pkg/event/module_test.go | 42 -- pkg/event/tracing.go | 183 -------- pkg/event/tracing_test.go | 164 -------- pkg/event/tracking.go | 47 --- pkg/event/tracking_test.go | 56 --- scripts/build.sh | 15 - 30 files changed, 24 insertions(+), 3747 deletions(-) delete mode 100755 api/event/event.fbs delete mode 100644 api/event/fb/AdminEvent.go delete mode 100644 api/event/fb/AuthDetails.go delete mode 100644 api/event/fb/Event.go delete mode 100644 api/event/fb/EventType.go delete mode 100644 api/event/fb/OperationType.go delete mode 100644 api/event/fb/ResourceType.go delete mode 100644 api/event/fb/Tuple.go delete mode 100755 pkg/event/component.go delete mode 100755 pkg/event/component_test.go delete mode 100755 pkg/event/endpoint.go delete mode 100644 pkg/event/endpoint_test.go delete mode 100755 pkg/event/http.go delete mode 100755 pkg/event/http_test.go delete mode 100644 pkg/event/instrumenting.go delete mode 100644 pkg/event/instrumenting_test.go delete mode 100644 pkg/event/logging.go delete mode 100644 pkg/event/logging_test.go delete mode 100644 pkg/event/mock_test.go delete mode 100644 pkg/event/module.go delete mode 100644 pkg/event/module_test.go delete mode 100644 pkg/event/tracing.go delete mode 100644 pkg/event/tracing_test.go delete mode 100644 pkg/event/tracking.go delete mode 100644 pkg/event/tracking_test.go diff --git a/Gopkg.lock b/Gopkg.lock index 913e6eb3..206008f9 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -143,14 +143,6 @@ revision = "4846b58453b3708320bdb524f25cc5a1d9cda4d4" version = "v1.4.3" -[[projects]] - digest = "1:0d14ca4e58b26ba21372d2a300dca2702b32b492f21d910c940557bb738c95cf" - name = "github.com/google/flatbuffers" - packages = ["go"] - pruneopts = "UT" - revision = "6df40a2471737b27271bdd9b900ab5f3aec746c7" - version = "v1.12.0" - [[projects]] digest = "1:6078383dbb7405bfa3ce16957f37564e3b716afa29b6eb45ffdb6674624d1e0d" name = "github.com/gorilla/mux" @@ -219,12 +211,12 @@ version = "v1.3.3" [[projects]] - digest = "1:84d14c215fb7eabd4e1e89a62f7bf5ed5fee929ed9dffeb53f46daf3a895ba16" + digest = "1:fe2538db8d9ee15bf6777c74dfd9be3d8e29b07843bc009d8c8d65de8d60f116" name = "github.com/nyaruka/phonenumbers" packages = ["."] pruneopts = "UT" - revision = "41576efa1939bbdc62033e460938e740debdf245" - version = "v1.0.58" + revision = "a5776ac9657d930a5f974ef2b5c91e6847bc7b36" + version = "v1.0.59" [[projects]] digest = "1:86fbd18c1942728ab9cebc391bfe31266a27e2d2a0cf05c3bce8fc605e5f44c2" @@ -393,7 +385,7 @@ "pbkdf2", ] pruneopts = "UT" - revision = "84dcc777aaee85807c99afb056f496a3a8b3d8de" + revision = "9e8e0b390897c84cad53ebe9ed2d1d331a5394d9" [[projects]] branch = "master" @@ -406,7 +398,7 @@ "publicsuffix", ] pruneopts = "UT" - revision = "4f7140c49acb4c7e276e0b67dea953d5344ae064" + revision = "f5854403a9740e74b2e9e725e6cd7c8a57711905" [[projects]] branch = "master" @@ -421,14 +413,14 @@ [[projects]] branch = "master" - digest = "1:fa5ad75709f4697315e1be021a03c77c7388f981001dff22717591015c4d9214" + digest = "1:178adb4fd56b97a2dd6ecc5fd38a5f3070ad5465ddb231568816a33545ac4b63" name = "golang.org/x/sys" packages = [ "internal/unsafeheader", "unix", ] pruneopts = "UT" - revision = "e3ed0017c21142d3345308d92dcc7ad7020040c6" + revision = "6e5568b54d1aaee5899c0083c5776f88b8c5b907" [[projects]] digest = "1:fa940333c48808b0d86ef21f412ffcfd0e5084a82f13905c028a404803b1908f" @@ -602,9 +594,7 @@ "github.com/go-kit/kit/transport/http", "github.com/go-sql-driver/mysql", "github.com/golang/mock/gomock", - "github.com/google/flatbuffers/go", "github.com/gorilla/mux", - "github.com/influxdata/influxdb/client/v2", "github.com/lib/pq", "github.com/pkg/errors", "github.com/rs/cors", diff --git a/README.md b/README.md index c05f40e6..46e29608 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,12 @@ # Keycloak bridge [![Build Status][ci-img]][ci] [![Coverage Status][cov-img]][cov] [![GoDoc][godoc-img]][godoc] [![Go Report Card][report-img]][report] [![OpenTracing Badge][opentracing-img]][opentracing] -The keycloak bridge has two purposes. All our interactions (administration) with keycloak pass through it, and keycloak sends all events (i.e. login, user creation,...) to the bridge, so that they can be processed, stored,... +The keycloak bridge has one purpose, being a bridge for all the interactions with keycloak. The service includes logging, metrics, tracing, and error tracking. The logs are written to stdout. Metrics such as time tracking,... are collected and saved to an InfluxDB Time Series Database. Jaeger is used for distributed tracing and error tracking is managed with Sentry. ## Build - -The service uses [FlatBuffers](https://google.github.io/flatbuffers/) for data serialisation. Make sure you have FlatBuffers installed and up to date with ```flatc --version```. It was tested with "flatc version 1.8.0 (Nov 22 2017)". - Build the service for the environment \: ```bash @@ -18,11 +15,6 @@ Build the service for the environment \: Note: \ is used for versioning. -## Container - -The keycloak bridge is intended to run in a container with keycloak (including the [event-emitter](https://github.com/cloudtrust/event-emitter) module). -See the repository [keycloak-service](https://github.com/cloudtrust/keycloak-service). - ## Configuration Configuration is done with a YAML file, e.g. ```./configs/keycloak_bridge.yml```. @@ -85,25 +77,18 @@ CT_BRIDGE_DB_USERS_PASSWORD | db-users-password CT_BRIDGE_INFLUX_USERNAME | influx-username CT_BRIDGE_INFLUX_PASSWORD | influx-password CT_BRIDGE_SENTRY_DSN | sentry-dsn -CT_BRIDGE_EVENT_BASIC_AUTH | event-basic-auth-token ## Usage Launch the keycloak bridge: ```bash -./bin/keycloak_bridge --config-file --authorization-file +./bin/keycloak_bridge --config-file ``` It is recommended to always provides an absolute path to the configuration file when the service is started, even though absolute and relative paths are supported. If no configuration file is passed, the service will try to load the default config file at ```./configs/keycloak_bridge.yml```, and if it fails it launches the service with the default parameters. -It is manadatory to load an authorization JSON file, if no authorization file is passed, the service will try to load the default file at ```./configs/authorization.json```, and if it fails the bridge refuse to start. - -### Keycloak events - -The keycloak event-emitter module sends all events to the bridge's event endpoint. The event emitter use HTTP with flatbuffers. - ### Monitoring of keycloak-bridge An endpoint allows to get a status of the Bridge and its components health. diff --git a/api/event/event.fbs b/api/event/event.fbs deleted file mode 100755 index 98e5f9fd..00000000 --- a/api/event/event.fbs +++ /dev/null @@ -1,177 +0,0 @@ -// IDL file for events. - -namespace fb; - -enum EventType:byte { - LOGIN, - LOGIN_ERROR, - REGISTER, - REGISTER_ERROR, - LOGOUT, - LOGOUT_ERROR, - CODE_TO_TOKEN, - CODE_TO_TOKEN_ERROR, - CLIENT_LOGIN, - CLIENT_LOGIN_ERROR, - REFRESH_TOKEN, - REFRESH_TOKEN_ERROR, - VALIDATE_ACCESS_TOKEN, - VALIDATE_ACCESS_TOKEN_ERROR, - INTROSPECT_TOKEN, - INTROSPECT_TOKEN_ERROR, - FEDERATED_IDENTITY_LINK, - FEDERATED_IDENTITY_LINK_ERROR, - REMOVE_FEDERATED_IDENTITY, - REMOVE_FEDERATED_IDENTITY_ERROR, - UPDATE_EMAIL, - UPDATE_EMAIL_ERROR, - UPDATE_PROFILE, - UPDATE_PROFILE_ERROR, - UPDATE_PASSWORD, - UPDATE_PASSWORD_ERROR, - UPDATE_TOTP, - UPDATE_TOTP_ERROR, - VERIFY_EMAIL, - VERIFY_EMAIL_ERROR, - REMOVE_TOTP, - REMOVE_TOTP_ERROR, - REVOKE_GRANT, - REVOKE_GRANT_ERROR, - SEND_VERIFY_EMAIL, - SEND_VERIFY_EMAIL_ERROR, - SEND_RESET_PASSWORD, - SEND_RESET_PASSWORD_ERROR, - SEND_IDENTITY_PROVIDER_LINK, - SEND_IDENTITY_PROVIDER_LINK_ERROR, - RESET_PASSWORD, - RESET_PASSWORD_ERROR, - RESTART_AUTHENTICATION, - RESTART_AUTHENTICATION_ERROR, - INVALID_SIGNATURE, - INVALID_SIGNATURE_ERROR, - REGISTER_NODE, - REGISTER_NODE_ERROR, - UNREGISTER_NODE, - UNREGISTER_NODE_ERROR, - USER_INFO_REQUEST, - USER_INFO_REQUEST_ERROR, - IDENTITY_PROVIDER_LINK_ACCOUNT, - IDENTITY_PROVIDER_LINK_ACCOUNT_ERROR, - IDENTITY_PROVIDER_LOGIN, - IDENTITY_PROVIDER_LOGIN_ERROR, - IDENTITY_PROVIDER_FIRST_LOGIN, - IDENTITY_PROVIDER_FIRST_LOGIN_ERROR, - IDENTITY_PROVIDER_POST_LOGIN, - IDENTITY_PROVIDER_POST_LOGIN_ERROR, - IDENTITY_PROVIDER_RESPONSE, - IDENTITY_PROVIDER_RESPONSE_ERROR, - IDENTITY_PROVIDER_RETRIEVE_TOKEN, - IDENTITY_PROVIDER_RETRIEVE_TOKEN_ERROR, - IMPERSONATE, - IMPERSONATE_ERROR, - CUSTOM_REQUIRED_ACTION, - CUSTOM_REQUIRED_ACTION_ERROR, - EXECUTE_ACTIONS, - EXECUTE_ACTIONS_ERROR, - EXECUTE_ACTION_TOKEN, - EXECUTE_ACTION_TOKEN_ERROR, - CLIENT_INFO, - CLIENT_INFO_ERROR, - CLIENT_REGISTER, - CLIENT_REGISTER_ERROR, - CLIENT_UPDATE, - CLIENT_UPDATE_ERROR, - CLIENT_DELETE, - CLIENT_DELETE_ERROR, - CLIENT_INITIATED_ACCOUNT_LINKING, - CLIENT_INITIATED_ACCOUNT_LINKING_ERROR, - TOKEN_EXCHANGE, - TOKEN_EXCHANGE_ERROR, - PERMISSION_TOKEN, - PERMISSION_TOKEN_ERROR, - UNKNOWN // Default case -} - -enum ResourceType:byte { - REALM, - REALM_ROLE, - REALM_ROLE_MAPPING, - REALM_SCOPE_MAPPING, - AUTH_FLOW, - AUTH_EXECUTION_FLOW, - AUTH_EXECUTION, - AUTHENTICATOR_CONFIG, - REQUIRED_ACTION, - IDENTITY_PROVIDER, - IDENTITY_PROVIDER_MAPPER, - PROTOCOL_MAPPER, - USER, - USER_LOGIN_FAILURE, - USER_SESSION, - USER_FEDERATION_PROVIDER, - USER_FEDERATION_MAPPER, - GROUP, - GROUP_MEMBERSHIP, - CLIENT, - CLIENT_INITIAL_ACCESS_MODEL, - CLIENT_ROLE, - CLIENT_ROLE_MAPPING, - CLIENT_SCOPE, - CLIENT_SCOPE_MAPPING, - CLUSTER_NODE, - COMPONENT, - AUTHORIZATION_RESOURCE_SERVER, - AUTHORIZATION_RESOURCE, - AUTHORIZATION_SCOPE, - AUTHORIZATION_POLICY, - UNKNOWN // Default case -} - -enum OperationType:byte { - CREATE, - UPDATE, - DELETE, - ACTION -} - -table Tuple { - key:string; - value:string; -} - -table AuthDetails { - realmId:string; - clientId:string; - userId:string; - username:string; - ipAddress:string; -} - -table Event { - uid:long; - time:long; - type:EventType; - realmId:string; - clientId:string; - userId:string; - sessionId:string; - ipAddress:string; - error:string; - details:[Tuple]; -} - -table AdminEvent { - uid:long; - time:long; - realmId:string; - authDetails:AuthDetails; - details:[Tuple]; - resourceType:ResourceType; - operationType:OperationType; - resourcePath:string; - representation:string; - error:string; -} - -root_type Event; -root_type AdminEvent; \ No newline at end of file diff --git a/api/event/fb/AdminEvent.go b/api/event/fb/AdminEvent.go deleted file mode 100644 index 8c9e2423..00000000 --- a/api/event/fb/AdminEvent.go +++ /dev/null @@ -1,180 +0,0 @@ -// Code generated by the FlatBuffers compiler. DO NOT EDIT. - -package fb - -import ( - flatbuffers "github.com/google/flatbuffers/go" -) - -type AdminEvent struct { - _tab flatbuffers.Table -} - -func GetRootAsAdminEvent(buf []byte, offset flatbuffers.UOffsetT) *AdminEvent { - n := flatbuffers.GetUOffsetT(buf[offset:]) - x := &AdminEvent{} - x.Init(buf, n+offset) - return x -} - -func (rcv *AdminEvent) Init(buf []byte, i flatbuffers.UOffsetT) { - rcv._tab.Bytes = buf - rcv._tab.Pos = i -} - -func (rcv *AdminEvent) Table() flatbuffers.Table { - return rcv._tab -} - -func (rcv *AdminEvent) Uid() int64 { - o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) - if o != 0 { - return rcv._tab.GetInt64(o + rcv._tab.Pos) - } - return 0 -} - -func (rcv *AdminEvent) MutateUid(n int64) bool { - return rcv._tab.MutateInt64Slot(4, n) -} - -func (rcv *AdminEvent) Time() int64 { - o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) - if o != 0 { - return rcv._tab.GetInt64(o + rcv._tab.Pos) - } - return 0 -} - -func (rcv *AdminEvent) MutateTime(n int64) bool { - return rcv._tab.MutateInt64Slot(6, n) -} - -func (rcv *AdminEvent) RealmId() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(8)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func (rcv *AdminEvent) AuthDetails(obj *AuthDetails) *AuthDetails { - o := flatbuffers.UOffsetT(rcv._tab.Offset(10)) - if o != 0 { - x := rcv._tab.Indirect(o + rcv._tab.Pos) - if obj == nil { - obj = new(AuthDetails) - } - obj.Init(rcv._tab.Bytes, x) - return obj - } - return nil -} - -func (rcv *AdminEvent) Details(obj *Tuple, j int) bool { - o := flatbuffers.UOffsetT(rcv._tab.Offset(12)) - if o != 0 { - x := rcv._tab.Vector(o) - x += flatbuffers.UOffsetT(j) * 4 - x = rcv._tab.Indirect(x) - obj.Init(rcv._tab.Bytes, x) - return true - } - return false -} - -func (rcv *AdminEvent) DetailsLength() int { - o := flatbuffers.UOffsetT(rcv._tab.Offset(12)) - if o != 0 { - return rcv._tab.VectorLen(o) - } - return 0 -} - -func (rcv *AdminEvent) ResourceType() ResourceType { - o := flatbuffers.UOffsetT(rcv._tab.Offset(14)) - if o != 0 { - return rcv._tab.GetInt8(o + rcv._tab.Pos) - } - return 0 -} - -func (rcv *AdminEvent) MutateResourceType(n ResourceType) bool { - return rcv._tab.MutateInt8Slot(14, n) -} - -func (rcv *AdminEvent) OperationType() OperationType { - o := flatbuffers.UOffsetT(rcv._tab.Offset(16)) - if o != 0 { - return rcv._tab.GetInt8(o + rcv._tab.Pos) - } - return 0 -} - -func (rcv *AdminEvent) MutateOperationType(n OperationType) bool { - return rcv._tab.MutateInt8Slot(16, n) -} - -func (rcv *AdminEvent) ResourcePath() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(18)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func (rcv *AdminEvent) Representation() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(20)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func (rcv *AdminEvent) Error() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(22)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func AdminEventStart(builder *flatbuffers.Builder) { - builder.StartObject(10) -} -func AdminEventAddUid(builder *flatbuffers.Builder, uid int64) { - builder.PrependInt64Slot(0, uid, 0) -} -func AdminEventAddTime(builder *flatbuffers.Builder, time int64) { - builder.PrependInt64Slot(1, time, 0) -} -func AdminEventAddRealmId(builder *flatbuffers.Builder, realmId flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(2, flatbuffers.UOffsetT(realmId), 0) -} -func AdminEventAddAuthDetails(builder *flatbuffers.Builder, authDetails flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(authDetails), 0) -} -func AdminEventAddDetails(builder *flatbuffers.Builder, details flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(4, flatbuffers.UOffsetT(details), 0) -} -func AdminEventStartDetailsVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { - return builder.StartVector(4, numElems, 4) -} -func AdminEventAddResourceType(builder *flatbuffers.Builder, resourceType int8) { - builder.PrependInt8Slot(5, resourceType, 0) -} -func AdminEventAddOperationType(builder *flatbuffers.Builder, operationType int8) { - builder.PrependInt8Slot(6, operationType, 0) -} -func AdminEventAddResourcePath(builder *flatbuffers.Builder, resourcePath flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(7, flatbuffers.UOffsetT(resourcePath), 0) -} -func AdminEventAddRepresentation(builder *flatbuffers.Builder, representation flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(8, flatbuffers.UOffsetT(representation), 0) -} -func AdminEventAddError(builder *flatbuffers.Builder, error flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(9, flatbuffers.UOffsetT(error), 0) -} -func AdminEventEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { - return builder.EndObject() -} diff --git a/api/event/fb/AuthDetails.go b/api/event/fb/AuthDetails.go deleted file mode 100644 index 4c55c791..00000000 --- a/api/event/fb/AuthDetails.go +++ /dev/null @@ -1,89 +0,0 @@ -// Code generated by the FlatBuffers compiler. DO NOT EDIT. - -package fb - -import ( - flatbuffers "github.com/google/flatbuffers/go" -) - -type AuthDetails struct { - _tab flatbuffers.Table -} - -func GetRootAsAuthDetails(buf []byte, offset flatbuffers.UOffsetT) *AuthDetails { - n := flatbuffers.GetUOffsetT(buf[offset:]) - x := &AuthDetails{} - x.Init(buf, n+offset) - return x -} - -func (rcv *AuthDetails) Init(buf []byte, i flatbuffers.UOffsetT) { - rcv._tab.Bytes = buf - rcv._tab.Pos = i -} - -func (rcv *AuthDetails) Table() flatbuffers.Table { - return rcv._tab -} - -func (rcv *AuthDetails) RealmId() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func (rcv *AuthDetails) ClientId() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func (rcv *AuthDetails) UserId() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(8)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func (rcv *AuthDetails) Username() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(10)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func (rcv *AuthDetails) IpAddress() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(12)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func AuthDetailsStart(builder *flatbuffers.Builder) { - builder.StartObject(5) -} -func AuthDetailsAddRealmId(builder *flatbuffers.Builder, realmId flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(realmId), 0) -} -func AuthDetailsAddClientId(builder *flatbuffers.Builder, clientId flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(clientId), 0) -} -func AuthDetailsAddUserId(builder *flatbuffers.Builder, userId flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(2, flatbuffers.UOffsetT(userId), 0) -} -func AuthDetailsAddUsername(builder *flatbuffers.Builder, username flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(username), 0) -} -func AuthDetailsAddIpAddress(builder *flatbuffers.Builder, ipAddress flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(4, flatbuffers.UOffsetT(ipAddress), 0) -} -func AuthDetailsEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { - return builder.EndObject() -} diff --git a/api/event/fb/Event.go b/api/event/fb/Event.go deleted file mode 100644 index 505ece75..00000000 --- a/api/event/fb/Event.go +++ /dev/null @@ -1,171 +0,0 @@ -// Code generated by the FlatBuffers compiler. DO NOT EDIT. - -package fb - -import ( - flatbuffers "github.com/google/flatbuffers/go" -) - -type Event struct { - _tab flatbuffers.Table -} - -func GetRootAsEvent(buf []byte, offset flatbuffers.UOffsetT) *Event { - n := flatbuffers.GetUOffsetT(buf[offset:]) - x := &Event{} - x.Init(buf, n+offset) - return x -} - -func (rcv *Event) Init(buf []byte, i flatbuffers.UOffsetT) { - rcv._tab.Bytes = buf - rcv._tab.Pos = i -} - -func (rcv *Event) Table() flatbuffers.Table { - return rcv._tab -} - -func (rcv *Event) Uid() int64 { - o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) - if o != 0 { - return rcv._tab.GetInt64(o + rcv._tab.Pos) - } - return 0 -} - -func (rcv *Event) MutateUid(n int64) bool { - return rcv._tab.MutateInt64Slot(4, n) -} - -func (rcv *Event) Time() int64 { - o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) - if o != 0 { - return rcv._tab.GetInt64(o + rcv._tab.Pos) - } - return 0 -} - -func (rcv *Event) MutateTime(n int64) bool { - return rcv._tab.MutateInt64Slot(6, n) -} - -func (rcv *Event) Type() EventType { - o := flatbuffers.UOffsetT(rcv._tab.Offset(8)) - if o != 0 { - return rcv._tab.GetInt8(o + rcv._tab.Pos) - } - return 0 -} - -func (rcv *Event) MutateType(n EventType) bool { - return rcv._tab.MutateInt8Slot(8, n) -} - -func (rcv *Event) RealmId() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(10)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func (rcv *Event) ClientId() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(12)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func (rcv *Event) UserId() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(14)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func (rcv *Event) SessionId() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(16)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func (rcv *Event) IpAddress() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(18)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func (rcv *Event) Error() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(20)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func (rcv *Event) Details(obj *Tuple, j int) bool { - o := flatbuffers.UOffsetT(rcv._tab.Offset(22)) - if o != 0 { - x := rcv._tab.Vector(o) - x += flatbuffers.UOffsetT(j) * 4 - x = rcv._tab.Indirect(x) - obj.Init(rcv._tab.Bytes, x) - return true - } - return false -} - -func (rcv *Event) DetailsLength() int { - o := flatbuffers.UOffsetT(rcv._tab.Offset(22)) - if o != 0 { - return rcv._tab.VectorLen(o) - } - return 0 -} - -func EventStart(builder *flatbuffers.Builder) { - builder.StartObject(10) -} -func EventAddUid(builder *flatbuffers.Builder, uid int64) { - builder.PrependInt64Slot(0, uid, 0) -} -func EventAddTime(builder *flatbuffers.Builder, time int64) { - builder.PrependInt64Slot(1, time, 0) -} -func EventAddType(builder *flatbuffers.Builder, type_ int8) { - builder.PrependInt8Slot(2, type_, 0) -} -func EventAddRealmId(builder *flatbuffers.Builder, realmId flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(realmId), 0) -} -func EventAddClientId(builder *flatbuffers.Builder, clientId flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(4, flatbuffers.UOffsetT(clientId), 0) -} -func EventAddUserId(builder *flatbuffers.Builder, userId flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(5, flatbuffers.UOffsetT(userId), 0) -} -func EventAddSessionId(builder *flatbuffers.Builder, sessionId flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(6, flatbuffers.UOffsetT(sessionId), 0) -} -func EventAddIpAddress(builder *flatbuffers.Builder, ipAddress flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(7, flatbuffers.UOffsetT(ipAddress), 0) -} -func EventAddError(builder *flatbuffers.Builder, error flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(8, flatbuffers.UOffsetT(error), 0) -} -func EventAddDetails(builder *flatbuffers.Builder, details flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(9, flatbuffers.UOffsetT(details), 0) -} -func EventStartDetailsVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { - return builder.StartVector(4, numElems, 4) -} -func EventEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { - return builder.EndObject() -} diff --git a/api/event/fb/EventType.go b/api/event/fb/EventType.go deleted file mode 100644 index befeb59e..00000000 --- a/api/event/fb/EventType.go +++ /dev/null @@ -1,185 +0,0 @@ -// Code generated by the FlatBuffers compiler. DO NOT EDIT. - -package fb - -type EventType = int8 -const ( - EventTypeLOGIN EventType = 0 - EventTypeLOGIN_ERROR EventType = 1 - EventTypeREGISTER EventType = 2 - EventTypeREGISTER_ERROR EventType = 3 - EventTypeLOGOUT EventType = 4 - EventTypeLOGOUT_ERROR EventType = 5 - EventTypeCODE_TO_TOKEN EventType = 6 - EventTypeCODE_TO_TOKEN_ERROR EventType = 7 - EventTypeCLIENT_LOGIN EventType = 8 - EventTypeCLIENT_LOGIN_ERROR EventType = 9 - EventTypeREFRESH_TOKEN EventType = 10 - EventTypeREFRESH_TOKEN_ERROR EventType = 11 - EventTypeVALIDATE_ACCESS_TOKEN EventType = 12 - EventTypeVALIDATE_ACCESS_TOKEN_ERROR EventType = 13 - EventTypeINTROSPECT_TOKEN EventType = 14 - EventTypeINTROSPECT_TOKEN_ERROR EventType = 15 - EventTypeFEDERATED_IDENTITY_LINK EventType = 16 - EventTypeFEDERATED_IDENTITY_LINK_ERROR EventType = 17 - EventTypeREMOVE_FEDERATED_IDENTITY EventType = 18 - EventTypeREMOVE_FEDERATED_IDENTITY_ERROR EventType = 19 - EventTypeUPDATE_EMAIL EventType = 20 - EventTypeUPDATE_EMAIL_ERROR EventType = 21 - EventTypeUPDATE_PROFILE EventType = 22 - EventTypeUPDATE_PROFILE_ERROR EventType = 23 - EventTypeUPDATE_PASSWORD EventType = 24 - EventTypeUPDATE_PASSWORD_ERROR EventType = 25 - EventTypeUPDATE_TOTP EventType = 26 - EventTypeUPDATE_TOTP_ERROR EventType = 27 - EventTypeVERIFY_EMAIL EventType = 28 - EventTypeVERIFY_EMAIL_ERROR EventType = 29 - EventTypeREMOVE_TOTP EventType = 30 - EventTypeREMOVE_TOTP_ERROR EventType = 31 - EventTypeREVOKE_GRANT EventType = 32 - EventTypeREVOKE_GRANT_ERROR EventType = 33 - EventTypeSEND_VERIFY_EMAIL EventType = 34 - EventTypeSEND_VERIFY_EMAIL_ERROR EventType = 35 - EventTypeSEND_RESET_PASSWORD EventType = 36 - EventTypeSEND_RESET_PASSWORD_ERROR EventType = 37 - EventTypeSEND_IDENTITY_PROVIDER_LINK EventType = 38 - EventTypeSEND_IDENTITY_PROVIDER_LINK_ERROR EventType = 39 - EventTypeRESET_PASSWORD EventType = 40 - EventTypeRESET_PASSWORD_ERROR EventType = 41 - EventTypeRESTART_AUTHENTICATION EventType = 42 - EventTypeRESTART_AUTHENTICATION_ERROR EventType = 43 - EventTypeINVALID_SIGNATURE EventType = 44 - EventTypeINVALID_SIGNATURE_ERROR EventType = 45 - EventTypeREGISTER_NODE EventType = 46 - EventTypeREGISTER_NODE_ERROR EventType = 47 - EventTypeUNREGISTER_NODE EventType = 48 - EventTypeUNREGISTER_NODE_ERROR EventType = 49 - EventTypeUSER_INFO_REQUEST EventType = 50 - EventTypeUSER_INFO_REQUEST_ERROR EventType = 51 - EventTypeIDENTITY_PROVIDER_LINK_ACCOUNT EventType = 52 - EventTypeIDENTITY_PROVIDER_LINK_ACCOUNT_ERROR EventType = 53 - EventTypeIDENTITY_PROVIDER_LOGIN EventType = 54 - EventTypeIDENTITY_PROVIDER_LOGIN_ERROR EventType = 55 - EventTypeIDENTITY_PROVIDER_FIRST_LOGIN EventType = 56 - EventTypeIDENTITY_PROVIDER_FIRST_LOGIN_ERROR EventType = 57 - EventTypeIDENTITY_PROVIDER_POST_LOGIN EventType = 58 - EventTypeIDENTITY_PROVIDER_POST_LOGIN_ERROR EventType = 59 - EventTypeIDENTITY_PROVIDER_RESPONSE EventType = 60 - EventTypeIDENTITY_PROVIDER_RESPONSE_ERROR EventType = 61 - EventTypeIDENTITY_PROVIDER_RETRIEVE_TOKEN EventType = 62 - EventTypeIDENTITY_PROVIDER_RETRIEVE_TOKEN_ERROR EventType = 63 - EventTypeIMPERSONATE EventType = 64 - EventTypeIMPERSONATE_ERROR EventType = 65 - EventTypeCUSTOM_REQUIRED_ACTION EventType = 66 - EventTypeCUSTOM_REQUIRED_ACTION_ERROR EventType = 67 - EventTypeEXECUTE_ACTIONS EventType = 68 - EventTypeEXECUTE_ACTIONS_ERROR EventType = 69 - EventTypeEXECUTE_ACTION_TOKEN EventType = 70 - EventTypeEXECUTE_ACTION_TOKEN_ERROR EventType = 71 - EventTypeCLIENT_INFO EventType = 72 - EventTypeCLIENT_INFO_ERROR EventType = 73 - EventTypeCLIENT_REGISTER EventType = 74 - EventTypeCLIENT_REGISTER_ERROR EventType = 75 - EventTypeCLIENT_UPDATE EventType = 76 - EventTypeCLIENT_UPDATE_ERROR EventType = 77 - EventTypeCLIENT_DELETE EventType = 78 - EventTypeCLIENT_DELETE_ERROR EventType = 79 - EventTypeCLIENT_INITIATED_ACCOUNT_LINKING EventType = 80 - EventTypeCLIENT_INITIATED_ACCOUNT_LINKING_ERROR EventType = 81 - EventTypeTOKEN_EXCHANGE EventType = 82 - EventTypeTOKEN_EXCHANGE_ERROR EventType = 83 - EventTypePERMISSION_TOKEN EventType = 84 - EventTypePERMISSION_TOKEN_ERROR EventType = 85 - EventTypeUNKNOWN EventType = 86 -) - -var EnumNamesEventType = map[EventType]string{ - EventTypeLOGIN:"LOGIN", - EventTypeLOGIN_ERROR:"LOGIN_ERROR", - EventTypeREGISTER:"REGISTER", - EventTypeREGISTER_ERROR:"REGISTER_ERROR", - EventTypeLOGOUT:"LOGOUT", - EventTypeLOGOUT_ERROR:"LOGOUT_ERROR", - EventTypeCODE_TO_TOKEN:"CODE_TO_TOKEN", - EventTypeCODE_TO_TOKEN_ERROR:"CODE_TO_TOKEN_ERROR", - EventTypeCLIENT_LOGIN:"CLIENT_LOGIN", - EventTypeCLIENT_LOGIN_ERROR:"CLIENT_LOGIN_ERROR", - EventTypeREFRESH_TOKEN:"REFRESH_TOKEN", - EventTypeREFRESH_TOKEN_ERROR:"REFRESH_TOKEN_ERROR", - EventTypeVALIDATE_ACCESS_TOKEN:"VALIDATE_ACCESS_TOKEN", - EventTypeVALIDATE_ACCESS_TOKEN_ERROR:"VALIDATE_ACCESS_TOKEN_ERROR", - EventTypeINTROSPECT_TOKEN:"INTROSPECT_TOKEN", - EventTypeINTROSPECT_TOKEN_ERROR:"INTROSPECT_TOKEN_ERROR", - EventTypeFEDERATED_IDENTITY_LINK:"FEDERATED_IDENTITY_LINK", - EventTypeFEDERATED_IDENTITY_LINK_ERROR:"FEDERATED_IDENTITY_LINK_ERROR", - EventTypeREMOVE_FEDERATED_IDENTITY:"REMOVE_FEDERATED_IDENTITY", - EventTypeREMOVE_FEDERATED_IDENTITY_ERROR:"REMOVE_FEDERATED_IDENTITY_ERROR", - EventTypeUPDATE_EMAIL:"UPDATE_EMAIL", - EventTypeUPDATE_EMAIL_ERROR:"UPDATE_EMAIL_ERROR", - EventTypeUPDATE_PROFILE:"UPDATE_PROFILE", - EventTypeUPDATE_PROFILE_ERROR:"UPDATE_PROFILE_ERROR", - EventTypeUPDATE_PASSWORD:"UPDATE_PASSWORD", - EventTypeUPDATE_PASSWORD_ERROR:"UPDATE_PASSWORD_ERROR", - EventTypeUPDATE_TOTP:"UPDATE_TOTP", - EventTypeUPDATE_TOTP_ERROR:"UPDATE_TOTP_ERROR", - EventTypeVERIFY_EMAIL:"VERIFY_EMAIL", - EventTypeVERIFY_EMAIL_ERROR:"VERIFY_EMAIL_ERROR", - EventTypeREMOVE_TOTP:"REMOVE_TOTP", - EventTypeREMOVE_TOTP_ERROR:"REMOVE_TOTP_ERROR", - EventTypeREVOKE_GRANT:"REVOKE_GRANT", - EventTypeREVOKE_GRANT_ERROR:"REVOKE_GRANT_ERROR", - EventTypeSEND_VERIFY_EMAIL:"SEND_VERIFY_EMAIL", - EventTypeSEND_VERIFY_EMAIL_ERROR:"SEND_VERIFY_EMAIL_ERROR", - EventTypeSEND_RESET_PASSWORD:"SEND_RESET_PASSWORD", - EventTypeSEND_RESET_PASSWORD_ERROR:"SEND_RESET_PASSWORD_ERROR", - EventTypeSEND_IDENTITY_PROVIDER_LINK:"SEND_IDENTITY_PROVIDER_LINK", - EventTypeSEND_IDENTITY_PROVIDER_LINK_ERROR:"SEND_IDENTITY_PROVIDER_LINK_ERROR", - EventTypeRESET_PASSWORD:"RESET_PASSWORD", - EventTypeRESET_PASSWORD_ERROR:"RESET_PASSWORD_ERROR", - EventTypeRESTART_AUTHENTICATION:"RESTART_AUTHENTICATION", - EventTypeRESTART_AUTHENTICATION_ERROR:"RESTART_AUTHENTICATION_ERROR", - EventTypeINVALID_SIGNATURE:"INVALID_SIGNATURE", - EventTypeINVALID_SIGNATURE_ERROR:"INVALID_SIGNATURE_ERROR", - EventTypeREGISTER_NODE:"REGISTER_NODE", - EventTypeREGISTER_NODE_ERROR:"REGISTER_NODE_ERROR", - EventTypeUNREGISTER_NODE:"UNREGISTER_NODE", - EventTypeUNREGISTER_NODE_ERROR:"UNREGISTER_NODE_ERROR", - EventTypeUSER_INFO_REQUEST:"USER_INFO_REQUEST", - EventTypeUSER_INFO_REQUEST_ERROR:"USER_INFO_REQUEST_ERROR", - EventTypeIDENTITY_PROVIDER_LINK_ACCOUNT:"IDENTITY_PROVIDER_LINK_ACCOUNT", - EventTypeIDENTITY_PROVIDER_LINK_ACCOUNT_ERROR:"IDENTITY_PROVIDER_LINK_ACCOUNT_ERROR", - EventTypeIDENTITY_PROVIDER_LOGIN:"IDENTITY_PROVIDER_LOGIN", - EventTypeIDENTITY_PROVIDER_LOGIN_ERROR:"IDENTITY_PROVIDER_LOGIN_ERROR", - EventTypeIDENTITY_PROVIDER_FIRST_LOGIN:"IDENTITY_PROVIDER_FIRST_LOGIN", - EventTypeIDENTITY_PROVIDER_FIRST_LOGIN_ERROR:"IDENTITY_PROVIDER_FIRST_LOGIN_ERROR", - EventTypeIDENTITY_PROVIDER_POST_LOGIN:"IDENTITY_PROVIDER_POST_LOGIN", - EventTypeIDENTITY_PROVIDER_POST_LOGIN_ERROR:"IDENTITY_PROVIDER_POST_LOGIN_ERROR", - EventTypeIDENTITY_PROVIDER_RESPONSE:"IDENTITY_PROVIDER_RESPONSE", - EventTypeIDENTITY_PROVIDER_RESPONSE_ERROR:"IDENTITY_PROVIDER_RESPONSE_ERROR", - EventTypeIDENTITY_PROVIDER_RETRIEVE_TOKEN:"IDENTITY_PROVIDER_RETRIEVE_TOKEN", - EventTypeIDENTITY_PROVIDER_RETRIEVE_TOKEN_ERROR:"IDENTITY_PROVIDER_RETRIEVE_TOKEN_ERROR", - EventTypeIMPERSONATE:"IMPERSONATE", - EventTypeIMPERSONATE_ERROR:"IMPERSONATE_ERROR", - EventTypeCUSTOM_REQUIRED_ACTION:"CUSTOM_REQUIRED_ACTION", - EventTypeCUSTOM_REQUIRED_ACTION_ERROR:"CUSTOM_REQUIRED_ACTION_ERROR", - EventTypeEXECUTE_ACTIONS:"EXECUTE_ACTIONS", - EventTypeEXECUTE_ACTIONS_ERROR:"EXECUTE_ACTIONS_ERROR", - EventTypeEXECUTE_ACTION_TOKEN:"EXECUTE_ACTION_TOKEN", - EventTypeEXECUTE_ACTION_TOKEN_ERROR:"EXECUTE_ACTION_TOKEN_ERROR", - EventTypeCLIENT_INFO:"CLIENT_INFO", - EventTypeCLIENT_INFO_ERROR:"CLIENT_INFO_ERROR", - EventTypeCLIENT_REGISTER:"CLIENT_REGISTER", - EventTypeCLIENT_REGISTER_ERROR:"CLIENT_REGISTER_ERROR", - EventTypeCLIENT_UPDATE:"CLIENT_UPDATE", - EventTypeCLIENT_UPDATE_ERROR:"CLIENT_UPDATE_ERROR", - EventTypeCLIENT_DELETE:"CLIENT_DELETE", - EventTypeCLIENT_DELETE_ERROR:"CLIENT_DELETE_ERROR", - EventTypeCLIENT_INITIATED_ACCOUNT_LINKING:"CLIENT_INITIATED_ACCOUNT_LINKING", - EventTypeCLIENT_INITIATED_ACCOUNT_LINKING_ERROR:"CLIENT_INITIATED_ACCOUNT_LINKING_ERROR", - EventTypeTOKEN_EXCHANGE:"TOKEN_EXCHANGE", - EventTypeTOKEN_EXCHANGE_ERROR:"TOKEN_EXCHANGE_ERROR", - EventTypePERMISSION_TOKEN:"PERMISSION_TOKEN", - EventTypePERMISSION_TOKEN_ERROR:"PERMISSION_TOKEN_ERROR", - EventTypeUNKNOWN:"UNKNOWN", -} - diff --git a/api/event/fb/OperationType.go b/api/event/fb/OperationType.go deleted file mode 100644 index bbd14130..00000000 --- a/api/event/fb/OperationType.go +++ /dev/null @@ -1,19 +0,0 @@ -// Code generated by the FlatBuffers compiler. DO NOT EDIT. - -package fb - -type OperationType = int8 -const ( - OperationTypeCREATE OperationType = 0 - OperationTypeUPDATE OperationType = 1 - OperationTypeDELETE OperationType = 2 - OperationTypeACTION OperationType = 3 -) - -var EnumNamesOperationType = map[OperationType]string{ - OperationTypeCREATE:"CREATE", - OperationTypeUPDATE:"UPDATE", - OperationTypeDELETE:"DELETE", - OperationTypeACTION:"ACTION", -} - diff --git a/api/event/fb/ResourceType.go b/api/event/fb/ResourceType.go deleted file mode 100644 index 34b398e3..00000000 --- a/api/event/fb/ResourceType.go +++ /dev/null @@ -1,75 +0,0 @@ -// Code generated by the FlatBuffers compiler. DO NOT EDIT. - -package fb - -type ResourceType = int8 -const ( - ResourceTypeREALM ResourceType = 0 - ResourceTypeREALM_ROLE ResourceType = 1 - ResourceTypeREALM_ROLE_MAPPING ResourceType = 2 - ResourceTypeREALM_SCOPE_MAPPING ResourceType = 3 - ResourceTypeAUTH_FLOW ResourceType = 4 - ResourceTypeAUTH_EXECUTION_FLOW ResourceType = 5 - ResourceTypeAUTH_EXECUTION ResourceType = 6 - ResourceTypeAUTHENTICATOR_CONFIG ResourceType = 7 - ResourceTypeREQUIRED_ACTION ResourceType = 8 - ResourceTypeIDENTITY_PROVIDER ResourceType = 9 - ResourceTypeIDENTITY_PROVIDER_MAPPER ResourceType = 10 - ResourceTypePROTOCOL_MAPPER ResourceType = 11 - ResourceTypeUSER ResourceType = 12 - ResourceTypeUSER_LOGIN_FAILURE ResourceType = 13 - ResourceTypeUSER_SESSION ResourceType = 14 - ResourceTypeUSER_FEDERATION_PROVIDER ResourceType = 15 - ResourceTypeUSER_FEDERATION_MAPPER ResourceType = 16 - ResourceTypeGROUP ResourceType = 17 - ResourceTypeGROUP_MEMBERSHIP ResourceType = 18 - ResourceTypeCLIENT ResourceType = 19 - ResourceTypeCLIENT_INITIAL_ACCESS_MODEL ResourceType = 20 - ResourceTypeCLIENT_ROLE ResourceType = 21 - ResourceTypeCLIENT_ROLE_MAPPING ResourceType = 22 - ResourceTypeCLIENT_SCOPE ResourceType = 23 - ResourceTypeCLIENT_SCOPE_MAPPING ResourceType = 24 - ResourceTypeCLUSTER_NODE ResourceType = 25 - ResourceTypeCOMPONENT ResourceType = 26 - ResourceTypeAUTHORIZATION_RESOURCE_SERVER ResourceType = 27 - ResourceTypeAUTHORIZATION_RESOURCE ResourceType = 28 - ResourceTypeAUTHORIZATION_SCOPE ResourceType = 29 - ResourceTypeAUTHORIZATION_POLICY ResourceType = 30 - ResourceTypeUNKNOWN ResourceType = 31 -) - -var EnumNamesResourceType = map[ResourceType]string{ - ResourceTypeREALM:"REALM", - ResourceTypeREALM_ROLE:"REALM_ROLE", - ResourceTypeREALM_ROLE_MAPPING:"REALM_ROLE_MAPPING", - ResourceTypeREALM_SCOPE_MAPPING:"REALM_SCOPE_MAPPING", - ResourceTypeAUTH_FLOW:"AUTH_FLOW", - ResourceTypeAUTH_EXECUTION_FLOW:"AUTH_EXECUTION_FLOW", - ResourceTypeAUTH_EXECUTION:"AUTH_EXECUTION", - ResourceTypeAUTHENTICATOR_CONFIG:"AUTHENTICATOR_CONFIG", - ResourceTypeREQUIRED_ACTION:"REQUIRED_ACTION", - ResourceTypeIDENTITY_PROVIDER:"IDENTITY_PROVIDER", - ResourceTypeIDENTITY_PROVIDER_MAPPER:"IDENTITY_PROVIDER_MAPPER", - ResourceTypePROTOCOL_MAPPER:"PROTOCOL_MAPPER", - ResourceTypeUSER:"USER", - ResourceTypeUSER_LOGIN_FAILURE:"USER_LOGIN_FAILURE", - ResourceTypeUSER_SESSION:"USER_SESSION", - ResourceTypeUSER_FEDERATION_PROVIDER:"USER_FEDERATION_PROVIDER", - ResourceTypeUSER_FEDERATION_MAPPER:"USER_FEDERATION_MAPPER", - ResourceTypeGROUP:"GROUP", - ResourceTypeGROUP_MEMBERSHIP:"GROUP_MEMBERSHIP", - ResourceTypeCLIENT:"CLIENT", - ResourceTypeCLIENT_INITIAL_ACCESS_MODEL:"CLIENT_INITIAL_ACCESS_MODEL", - ResourceTypeCLIENT_ROLE:"CLIENT_ROLE", - ResourceTypeCLIENT_ROLE_MAPPING:"CLIENT_ROLE_MAPPING", - ResourceTypeCLIENT_SCOPE:"CLIENT_SCOPE", - ResourceTypeCLIENT_SCOPE_MAPPING:"CLIENT_SCOPE_MAPPING", - ResourceTypeCLUSTER_NODE:"CLUSTER_NODE", - ResourceTypeCOMPONENT:"COMPONENT", - ResourceTypeAUTHORIZATION_RESOURCE_SERVER:"AUTHORIZATION_RESOURCE_SERVER", - ResourceTypeAUTHORIZATION_RESOURCE:"AUTHORIZATION_RESOURCE", - ResourceTypeAUTHORIZATION_SCOPE:"AUTHORIZATION_SCOPE", - ResourceTypeAUTHORIZATION_POLICY:"AUTHORIZATION_POLICY", - ResourceTypeUNKNOWN:"UNKNOWN", -} - diff --git a/api/event/fb/Tuple.go b/api/event/fb/Tuple.go deleted file mode 100644 index 8d884a59..00000000 --- a/api/event/fb/Tuple.go +++ /dev/null @@ -1,56 +0,0 @@ -// Code generated by the FlatBuffers compiler. DO NOT EDIT. - -package fb - -import ( - flatbuffers "github.com/google/flatbuffers/go" -) - -type Tuple struct { - _tab flatbuffers.Table -} - -func GetRootAsTuple(buf []byte, offset flatbuffers.UOffsetT) *Tuple { - n := flatbuffers.GetUOffsetT(buf[offset:]) - x := &Tuple{} - x.Init(buf, n+offset) - return x -} - -func (rcv *Tuple) Init(buf []byte, i flatbuffers.UOffsetT) { - rcv._tab.Bytes = buf - rcv._tab.Pos = i -} - -func (rcv *Tuple) Table() flatbuffers.Table { - return rcv._tab -} - -func (rcv *Tuple) Key() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func (rcv *Tuple) Value() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func TupleStart(builder *flatbuffers.Builder) { - builder.StartObject(2) -} -func TupleAddKey(builder *flatbuffers.Builder, key flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(key), 0) -} -func TupleAddValue(builder *flatbuffers.Builder, value flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(value), 0) -} -func TupleEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { - return builder.EndObject() -} diff --git a/cmd/keycloakb/keycloak_bridge.go b/cmd/keycloakb/keycloak_bridge.go index 045b7541..f9a3d691 100644 --- a/cmd/keycloakb/keycloak_bridge.go +++ b/cmd/keycloakb/keycloak_bridge.go @@ -32,7 +32,6 @@ import ( "github.com/cloudtrust/common-service/tracking" "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" "github.com/cloudtrust/keycloak-bridge/pkg/account" - "github.com/cloudtrust/keycloak-bridge/pkg/event" "github.com/cloudtrust/keycloak-bridge/pkg/events" "github.com/cloudtrust/keycloak-bridge/pkg/export" "github.com/cloudtrust/keycloak-bridge/pkg/kyc" @@ -73,7 +72,6 @@ const ( pathHealthCheck = "/health/check" RateKeyAccount = iota - RateKeyEvent = iota RateKeyEvents = iota RateKeyKYC = iota RateKeyManagement = iota @@ -93,7 +91,6 @@ const ( cfgTimeout = "keycloak-timeout" cfgAudienceRequired = "audience-required" cfgMobileAudienceRequired = "mobile-audience-required" - cfgEventBasicAuthToken = "event-basic-auth-token" cfgValidationBasicAuthToken = "validation-basic-auth-token" cfgPprofRouteEnabled = "pprof-route-enabled" cfgInfluxWriteInterval = "influx-write-interval" @@ -104,7 +101,6 @@ const ( cfgConfigRoDbParams = "db-config-ro" cfgUsersRwDbParams = "db-users-rw" cfgRateKeyValidation = "rate-validation" - cfgRateKeyEvent = "rate-event" cfgRateKeyAccount = "rate-account" cfgRateKeyMobile = "rate-mobile" cfgRateKeyManagement = "rate-management" @@ -216,7 +212,6 @@ func main() { // Rate limiting rateLimit = map[RateKey]int{ RateKeyValidation: c.GetInt(cfgRateKeyValidation), - RateKeyEvent: c.GetInt(cfgRateKeyEvent), RateKeyAccount: c.GetInt(cfgRateKeyAccount), RateKeyMobile: c.GetInt(cfgRateKeyMobile), RateKeyManagement: c.GetInt(cfgRateKeyManagement), @@ -301,17 +296,6 @@ func main() { } } - // Security - Basic AuthN token to protect internal/event endpoint - var eventExpectedAuthToken string - { - eventExpectedAuthToken = c.GetString(cfgEventBasicAuthToken) - - if eventExpectedAuthToken == "" { - logger.Error(ctx, "msg", "password for event endpoint (event-basic-auth-token) cannot be empty") - return - } - } - var validationExpectedAuthToken string { validationExpectedAuthToken = c.GetString(cfgValidationBasicAuthToken) @@ -503,90 +487,13 @@ func main() { } } - // Event service. - var eventEndpoints = event.Endpoints{} - { - var eventLogger = log.With(logger, "svc", "event") - - var consoleModule event.ConsoleModule - { - consoleModule = event.NewConsoleModule(log.With(eventLogger, "module", "console")) - consoleModule = event.MakeConsoleModuleInstrumentingMW(influxMetrics.NewHistogram("console_module"))(consoleModule) - consoleModule = event.MakeConsoleModuleLoggingMW(log.With(eventLogger, "mw", "module", "unit", "console"))(consoleModule) - consoleModule = event.MakeConsoleModuleTracingMW(tracer)(consoleModule) - } - - var statisticModule event.StatisticModule - { - statisticModule = event.NewStatisticModule(influxMetrics) - statisticModule = event.MakeStatisticModuleInstrumentingMW(influxMetrics.NewHistogram("statistic_module"))(statisticModule) - statisticModule = event.MakeStatisticModuleLoggingMW(log.With(eventLogger, "mw", "module", "unit", "statistic"))(statisticModule) - statisticModule = event.MakeStatisticModuleTracingMW(tracer)(statisticModule) - } - - // new module for sending the events to the DB - var eventsDBModule database.EventsDBModule - { - eventsDBModule = database.NewEventsDBModule(eventsDBConn) - eventsDBModule = event.MakeEventsDBModuleInstrumentingMW(influxMetrics.NewHistogram("eventsDB_module"))(eventsDBModule) - eventsDBModule = event.MakeEventsDBModuleLoggingMW(log.With(eventLogger, "mw", "module", "unit", "eventsDB"))(eventsDBModule) - eventsDBModule = event.MakeEventsDBModuleTracingMW(tracer)(eventsDBModule) - } - - var eventAdminComponent event.AdminComponent - { - var fns = []event.FuncEvent{consoleModule.Print, statisticModule.Stats, eventsDBModule.Store} - eventAdminComponent = event.NewAdminComponent(fns, fns, fns, fns) - eventAdminComponent = event.MakeAdminComponentInstrumentingMW(influxMetrics.NewHistogram("admin_component"))(eventAdminComponent) - eventAdminComponent = event.MakeAdminComponentLoggingMW(log.With(eventLogger, "mw", "component", "unit", "admin_event"))(eventAdminComponent) - eventAdminComponent = event.MakeAdminComponentTracingMW(tracer)(eventAdminComponent) - } - - var eventComponent event.Component - { - var fns = []event.FuncEvent{consoleModule.Print, statisticModule.Stats, eventsDBModule.Store} - eventComponent = event.NewComponent(fns, fns) - eventComponent = event.MakeComponentInstrumentingMW(influxMetrics.NewHistogram("component"))(eventComponent) - eventComponent = event.MakeComponentLoggingMW(log.With(eventLogger, "mw", "component", "unit", "event"))(eventComponent) - eventComponent = event.MakeComponentTracingMW(tracer)(eventComponent) - } - - // add ct_type - - var muxComponent event.MuxComponent - { - muxComponent = event.NewMuxComponent(eventComponent, eventAdminComponent) - muxComponent = event.MakeMuxComponentInstrumentingMW(influxMetrics.NewHistogram("mux_component"))(muxComponent) - muxComponent = event.MakeMuxComponentLoggingMW(log.With(eventLogger, "mw", "component", "unit", "mux"))(muxComponent) - muxComponent = event.MakeMuxComponentTracingMW(tracer)(muxComponent) - muxComponent = event.MakeMuxComponentTrackingMW(sentryClient, log.With(eventLogger, "mw", "component"))(muxComponent) - } - - var eventEndpoint cs.Endpoint - { - eventEndpoint = event.MakeEventEndpoint(muxComponent) - eventEndpoint = middleware.MakeEndpointInstrumentingMW(influxMetrics, "event_endpoint")(eventEndpoint) - eventEndpoint = middleware.MakeEndpointLoggingMW(log.With(eventLogger, "mw", "endpoint"))(eventEndpoint) - eventEndpoint = tracer.MakeEndpointTracingMW("event_endpoint")(eventEndpoint) - } - - eventEndpoints = event.Endpoints{ - Endpoint: keycloakb.LimitRate(eventEndpoint, rateLimit[RateKeyEvent]), - } - } - - baseEventsDBModule := database.NewEventsDBModule(eventsDBConn) - - // new module for reading events from the DB - eventsRODBModule := keycloakb.NewEventsDBModule(eventsRODBConn) - // Validation service. var validationEndpoints validation.Endpoints { var validationLogger = log.With(logger, "svc", "validation") - // module to store validation API calls - eventsDBModule := configureEventsDbModule(baseEventsDBModule, influxMetrics, validationLogger, tracer) + // module to store validation events API calls + eventsDBModule := database.NewEventsDBModule(eventsDBConn) // module for storing and retrieving details of the users var usersDBModule = keycloakb.NewUsersDetailsDBModule(usersRwDBConn, aesEncryption, validationLogger) @@ -613,6 +520,9 @@ func main() { { var statisticsLogger = log.With(logger, "svc", "statistics") + //module for reading events from the DB + eventsRODBModule := keycloakb.NewEventsDBModule(eventsRODBConn) + statisticsComponent := statistics.NewComponent(eventsRODBModule, keycloakClient, statisticsLogger) statisticsComponent = statistics.MakeAuthorizationManagementComponentMW(log.With(statisticsLogger, "mw", "endpoint"), authorizationManager)(statisticsComponent) @@ -634,9 +544,12 @@ func main() { var eventsLogger = log.With(logger, "svc", "events") // module to store API calls of the back office to the DB - eventsDBModule := configureEventsDbModule(baseEventsDBModule, influxMetrics, eventsLogger, tracer) + eventsRWDBModule := database.NewEventsDBModule(eventsDBConn) - eventsComponent := events.NewComponent(eventsRODBModule, eventsDBModule, eventsLogger) + //module for reading events from the DB + eventsRODBModule := keycloakb.NewEventsDBModule(eventsRODBConn) + + eventsComponent := events.NewComponent(eventsRODBModule, eventsRWDBModule, eventsLogger) eventsComponent = events.MakeAuthorizationManagementComponentMW(log.With(eventsLogger, "mw", "endpoint"), authorizationManager)(eventsComponent) var rateLimitEvents = rateLimit[RateKeyEvents] @@ -654,7 +567,7 @@ func main() { var managementLogger = log.With(logger, "svc", "management") // module to store API calls of the back office to the DB - eventsDBModule := configureEventsDbModule(baseEventsDBModule, influxMetrics, managementLogger, tracer) + eventsDBModule := database.NewEventsDBModule(eventsDBConn) // module for storing and retrieving the custom configuration var configDBModule = createConfigurationDBModule(configurationRwDBConn, influxMetrics, managementLogger) @@ -741,7 +654,7 @@ func main() { var accountLogger = log.With(logger, "svc", "account") // Configure events db module - eventsDBModule := configureEventsDbModule(baseEventsDBModule, influxMetrics, accountLogger, tracer) + eventsDBModule := database.NewEventsDBModule(eventsDBConn) // module for retrieving the custom configuration var configDBModule keycloakb.ConfigurationDBModule @@ -806,7 +719,7 @@ func main() { var registerLogger = log.With(logger, "svc", "register") // Configure events db module - eventsDBModule := configureEventsDbModule(baseEventsDBModule, influxMetrics, registerLogger, tracer) + eventsDBModule := database.NewEventsDBModule(eventsDBConn) // module for storing and retrieving the custom configuration var configDBModule = createConfigurationDBModule(configurationRwDBConn, influxMetrics, registerLogger) @@ -849,7 +762,7 @@ func main() { var kycLogger = log.With(logger, "svc", "kyc") // Configure events db module - eventsDBModule := configureEventsDbModule(baseEventsDBModule, influxMetrics, kycLogger, tracer) + eventsDBModule := database.NewEventsDBModule(eventsDBConn) // module for storing and retrieving details of the users var usersDBModule = keycloakb.NewUsersDetailsDBModule(usersRwDBConn, aesEncryption, kycLogger) @@ -885,7 +798,7 @@ func main() { errorhandler.SetEmitter(keycloakb.ComponentName) - // HTTP Internal Call Server (Event reception from Keycloak & Export API). + // HTTP Internal Call Server (Export & Validation API). go func() { var logger = log.With(logger, "transport", "http") logger.Info(ctx, "addr", httpAddrInternal) @@ -896,18 +809,6 @@ func main() { route.Handle("/", commonhttp.MakeVersionHandler(keycloakb.ComponentName, ComponentID, keycloakb.Version, Environment, GitCommit)) route.Handle(pathHealthCheck, healthChecker.MakeHandler()) - // Event. - var eventSubroute = route.PathPrefix("/event").Subrouter() - - var eventHandler http.Handler - { - eventHandler = event.MakeHTTPEventHandler(eventEndpoints.Endpoint, logger) - eventHandler = middleware.MakeHTTPCorrelationIDMW(idGenerator, tracer, logger, keycloakb.ComponentName, ComponentID)(eventHandler) - eventHandler = tracer.MakeHTTPTracingMW(keycloakb.ComponentName, "http_server_event")(eventHandler) - eventHandler = middleware.MakeHTTPBasicAuthenticationMW(eventExpectedAuthToken, logger)(eventHandler) - } - eventSubroute.Handle("/receiver", eventHandler) - // Export. route.Handle("/export", export.MakeHTTPExportHandler(exportEndpoint)).Methods("GET") route.Handle("/export", export.MakeHTTPExportHandler(exportSaveAndExportEndpoint)).Methods("POST") @@ -1316,7 +1217,6 @@ func config(ctx context.Context, logger log.Logger) *viper.Viper { // Security - Audience check v.SetDefault(cfgAudienceRequired, "") v.SetDefault(cfgMobileAudienceRequired, "") - v.SetDefault(cfgEventBasicAuthToken, "") v.SetDefault(cfgTrustIDGroups, []string{ "l1_support_agent", @@ -1359,7 +1259,6 @@ func config(ctx context.Context, logger log.Logger) *viper.Viper { // Rate limiting (in requests/second) v.SetDefault(cfgRateKeyValidation, 1000) - v.SetDefault(cfgRateKeyEvent, 1000) v.SetDefault(cfgRateKeyAccount, 1000) v.SetDefault(cfgRateKeyMobile, 1000) v.SetDefault(cfgRateKeyManagement, 1000) @@ -1437,9 +1336,6 @@ func config(ctx context.Context, logger log.Logger) *viper.Viper { v.BindEnv(cfgSentryDsn, "CT_BRIDGE_SENTRY_DSN") censoredParameters[cfgSentryDsn] = true - v.BindEnv(cfgEventBasicAuthToken, "CT_BRIDGE_EVENT_BASIC_AUTH") - censoredParameters[cfgEventBasicAuthToken] = true - v.BindEnv(cfgValidationBasicAuthToken, "CT_BRIDGE_VALIDATION_BASIC_AUTH") censoredParameters[cfgValidationBasicAuthToken] = true @@ -1611,13 +1507,6 @@ func createConfigurationDBModule(configDBConn sqltypes.CloudtrustDB, influxMetri return configDBModule } -func configureEventsDbModule(baseEventsDBModule database.EventsDBModule, influxMetrics metrics.Metrics, logger log.Logger, tracer tracing.OpentracingClient) database.EventsDBModule { - eventsDBModule := event.MakeEventsDBModuleInstrumentingMW(influxMetrics.NewHistogram("eventsDB_module"))(baseEventsDBModule) - eventsDBModule = event.MakeEventsDBModuleLoggingMW(log.With(logger, "mw", "module", "unit", "eventsDB"))(eventsDBModule) - eventsDBModule = event.MakeEventsDBModuleTracingMW(tracer)(eventsDBModule) - return eventsDBModule -} - func prepareEndpoint(e cs.Endpoint, endpointName string, influxMetrics metrics.Metrics, logger log.Logger, tracer tracing.OpentracingClient, rateLimit int) endpoint.Endpoint { e = middleware.MakeEndpointInstrumentingMW(influxMetrics, endpointName)(e) e = middleware.MakeEndpointLoggingMW(log.With(logger, "mw", endpointName))(e) diff --git a/configs/keycloak_bridge.yml b/configs/keycloak_bridge.yml index 220d07b7..4c132c08 100644 --- a/configs/keycloak_bridge.yml +++ b/configs/keycloak_bridge.yml @@ -44,9 +44,6 @@ cors-debug: true audience-required: "account" mobile-audience-required: "mobile" -## Password used to protect /internal/event endpoint -event-basic-auth-token: "superpasswordverylongandstrong" - ## Password used to protect /internal/validation endpoint validation-basic-auth-token: "idnowsuperpasswordverylongandstrong" @@ -144,7 +141,6 @@ db-users-rw-connection-check: false # Rate limiting in requests/second. rate-validation: 1000 -rate-event: 1000 rate-account: 1000 rate-management: 1000 rate-statistics: 1000 diff --git a/pkg/event/component.go b/pkg/event/component.go deleted file mode 100755 index 14186674..00000000 --- a/pkg/event/component.go +++ /dev/null @@ -1,351 +0,0 @@ -package event - -import ( - "context" - "encoding/json" - "fmt" - "strings" - "sync" - "time" - - "github.com/cloudtrust/common-service/database" - "github.com/cloudtrust/keycloak-bridge/api/event/fb" -) - -const ( - timeFormat = "2006-01-02 15:04:05.000" -) - -// MuxComponent is the Mux component interface. -type MuxComponent interface { - Event(ctx context.Context, eventType string, obj []byte) error -} - -type muxComponent struct { - component Component - adminComponent AdminComponent -} - -// NewMuxComponent returns a Mux component. -func NewMuxComponent(component Component, adminComponent AdminComponent) MuxComponent { - return &muxComponent{ - component: component, - adminComponent: adminComponent, - } -} - -func (c *muxComponent) Event(ctx context.Context, eventType string, obj []byte) error { - switch eventType { - case "Event": - var event = fb.GetRootAsEvent(obj, 0) - return c.component.Event(ctx, event) - case "AdminEvent": - var adminEvent = fb.GetRootAsAdminEvent(obj, 0) - return c.adminComponent.AdminEvent(ctx, adminEvent) - default: - return ErrInvalidArgument{InvalidParam: "Type"} - } -} - -// Component is the event component interface. -type Component interface { - Event(ctx context.Context, event *fb.Event) error -} - -type component struct { - fStdEvent []FuncEvent - fErrEvent []FuncEvent -} - -// NewComponent returns an event component. -func NewComponent(modulesToCallForStandardEvent []FuncEvent, - modulesToCallForErrorEvent []FuncEvent) Component { - return &component{ - fStdEvent: modulesToCallForStandardEvent, - fErrEvent: modulesToCallForErrorEvent, - } -} - -func (c *component) Event(ctx context.Context, event *fb.Event) error { - var eventType = int8(event.Type()) - var eventTypeName = fb.EnumNamesEventType[eventType] - var eventMap = eventToMap(event) - - if strings.HasSuffix(eventTypeName, "_ERROR") { - return apply(ctx, c.fErrEvent, eventMap) - } - - return apply(ctx, c.fStdEvent, eventMap) -} - -// AdminComponent is the admin event component interface. -type AdminComponent interface { - AdminEvent(ctx context.Context, adminEvent *fb.AdminEvent) error -} - -// FuncEvent is the function to call for a given event. -type FuncEvent = func(context.Context, map[string]string) error - -type adminComponent struct { - modulesToCallForCreate []FuncEvent - modulesToCallForUpdate []FuncEvent - modulesToCallForDelete []FuncEvent - modulesToCallForAction []FuncEvent -} - -// NewAdminComponent returns an admin event component. -func NewAdminComponent(modulesToCallForCreate []FuncEvent, - modulesToCallForUpdate []FuncEvent, - modulesToCallForDelete []FuncEvent, - modulesToCallForAction []FuncEvent) AdminComponent { - return &adminComponent{ - modulesToCallForCreate: modulesToCallForCreate, - modulesToCallForUpdate: modulesToCallForUpdate, - modulesToCallForDelete: modulesToCallForDelete, - modulesToCallForAction: modulesToCallForAction, - } -} - -func (c *adminComponent) AdminEvent(ctx context.Context, adminEvent *fb.AdminEvent) error { - var adminEventMap = adminEventToMap(adminEvent) - switch operationType := adminEvent.OperationType(); operationType { - case fb.OperationTypeCREATE: - return apply(ctx, c.modulesToCallForCreate, adminEventMap) - case fb.OperationTypeUPDATE: - return apply(ctx, c.modulesToCallForUpdate, adminEventMap) - case fb.OperationTypeDELETE: - return apply(ctx, c.modulesToCallForDelete, adminEventMap) - case fb.OperationTypeACTION: - return apply(ctx, c.modulesToCallForAction, adminEventMap) - default: - return ErrInvalidArgument{InvalidParam: "OperationType"} - } -} - -func addCTtypeToEvent(event map[string]string) map[string]string { - // add the ct_event_type - - addInfo := []byte(event[database.CtEventAdditionalInfo]) - var f map[string]string - _ = json.Unmarshal(addInfo, &f) - - if result := addCTtypeToEventFromOperationType(event, f); result != nil { - return result - } - if result := addCTtypeToEventFromEventType(event, f); result != nil { - return result - } - - // for all those events that don't have set the ct_event_type, we assign an empty ct_event_type - if _, ok := event[database.CtEventType]; !ok { - event[database.CtEventType] = "" - } - - return event -} - -func addCTtypeToEventFromOperationType(event map[string]string, f map[string]string) map[string]string { - switch opType := event[database.CtEventKcOperationType]; opType { - case "CREATE": - //ACCOUNT_CREATED - // check if the resourcePath starts with prefix users - if f["resource_type"] == "USER" { - event[database.CtEventType] = "ACCOUNT_CREATED" - return event - } - case "ACTION": - //ACTIVATION_EMAIL_SENT - // check if the resourcePath ends with suffix send-verify-email - if strings.HasSuffix(f["resource_path"], "send-verify-email") { - event[database.CtEventType] = "ACTIVATION_EMAIL_SENT" - return event - } - default: - // Nothing to do here - } - return nil -} - -func addCTtypeToEventFromEventType(event map[string]string, f map[string]string) map[string]string { - switch t := event[database.CtEventKcEventType]; t { - case "CUSTOM_REQUIRED_ACTION", "EXECUTE_ACTION_TOKEN": - //EMAIL_CONFIRMED - if f["custom_required_action"] == "VERIFY_EMAIL" { - event[database.CtEventType] = "EMAIL_CONFIRMED" - return event - } - case "EXECUTE_ACTION_TOKEN_ERROR": - //CONFIRM_EMAIL_EXPIRED - if f["error"] == "expired_code" { - event[database.CtEventType] = "CONFIRM_EMAIL_EXPIRED" - return event - } - case "UPDATE_PASSWORD": - //PASSWORD_RESET - if f["custom_required_action"] == "sms-password-set" { - event[database.CtEventType] = "PASSWORD_RESET" - return event - } - case "LOGIN": - //LOGON_OK - event[database.CtEventType] = "LOGON_OK" - return event - - case "LOGIN_ERROR": - if f["error"] == "user_temporarily_disabled" { - //TEMPORARILY_LOCKED - event[database.CtEventType] = "TEMPORARILY_LOCKED" - } else { - //LOGON_ERROR - event[database.CtEventType] = "LOGON_ERROR" - } - - return event - case "LOGOUT": - //LOGOUT - event[database.CtEventType] = "LOGOUT" - return event - default: - // Nothing to do here - } - return nil -} - -func adminEventToMap(adminEvent *fb.AdminEvent) map[string]string { - var adminEventMap = make(map[string]string) - var addInfo = make(map[string]string) - - addInfo["uid"] = fmt.Sprint(adminEvent.Uid()) - - //TZ set to UTC and insert the time as a string - timeEvent := epochMilliToTime(adminEvent.Time()).UTC() - adminEventMap[database.CtEventAuditTime] = timeEvent.Format(timeFormat) //audit_time - - adminEventMap[database.CtEventRealmName] = string(adminEvent.RealmId()) //realm_name - adminEventMap[database.CtEventOrigin] = "keycloak" //origin - - authDetails := adminEvent.AuthDetails(nil) - - adminEventMap[database.CtEventClientID] = string(authDetails.ClientId()) //client_id - addInfo["ip_address"] = string(authDetails.IpAddress()) - adminEventMap[database.CtEventAgentRealmName] = string(authDetails.RealmId()) // agent_realm_name - adminEventMap[database.CtEventAgentUserID] = string(authDetails.UserId()) //agent_user_id - adminEventMap[database.CtEventAgentUsername] = string(authDetails.Username()) //agent_username - - //details contains the user_id and the username of the user affected by the action - var detailsLength = adminEvent.DetailsLength() - for i := 0; i < detailsLength; i++ { - var tuple = new(fb.Tuple) - adminEvent.Details(tuple, i) - if string(tuple.Key()) == database.CtEventUsername || string(tuple.Key()) == database.CtEventUserID { - adminEventMap[string(tuple.Key())] = string(tuple.Value()) - } - - } - - addInfo["resource_type"] = fb.EnumNamesResourceType[int8(adminEvent.ResourceType())] - adminEventMap[database.CtEventKcOperationType] = fb.EnumNamesOperationType[int8(adminEvent.OperationType())] //kc_operation_type - addInfo["resource_path"] = string(adminEvent.ResourcePath()) - - addInfo["representation"] = string(adminEvent.Representation()) - addInfo["error"] = string(adminEvent.Error()) - //all the admin events have, by default, the ct_event_type set to admin - adminEventMap[database.CtEventType] = "ADMIN" - - // BE AWARE: error is not treated - infoJSON, _ := json.Marshal(addInfo) - adminEventMap[database.CtEventAdditionalInfo] = string(infoJSON) - - //set the correct ct_event_type for actions like create_account, etc. - adminEventMap = addCTtypeToEvent(adminEventMap) - - return adminEventMap -} - -func eventToMap(event *fb.Event) map[string]string { - var eventMap = make(map[string]string) - var addInfo = make(map[string]string) - // if an event has the ct_event_type set already, the flag avoids rewriting it - var doNotSetCTEventType = false - - addInfo["uid"] = fmt.Sprint(event.Uid()) - - //TZ set to UTC and insert the time as a string - timeEvent := epochMilliToTime(event.Time()).UTC() - eventMap[database.CtEventAuditTime] = timeEvent.Format(timeFormat) //audit_time - - eventMap[database.CtEventKcEventType] = fb.EnumNamesEventType[int8(event.Type())] // kc_event_type - eventMap[database.CtEventRealmName] = string(event.RealmId()) //realm_name - eventMap[database.CtEventClientID] = string(event.ClientId()) //client_id - eventMap[database.CtEventAgentUserID] = string(event.UserId()) //agent_user_id - eventMap[database.CtEventUserID] = string(event.UserId()) //user_id - //Note: we make the assumption that the agent and the user are the same in the case of the events that are not admin events - - addInfo["session_id"] = string(event.SessionId()) - addInfo["ip_address"] = string(event.IpAddress()) - addInfo["error"] = string(event.Error()) - eventMap[database.CtEventOrigin] = "keycloak" //origin - - var detailsLength = event.DetailsLength() - for i := 0; i < detailsLength; i++ { - var tuple = new(fb.Tuple) - event.Details(tuple, i) - if string(tuple.Key()) == database.CtEventType { - eventMap[string(tuple.Key())] = string(tuple.Value()) - doNotSetCTEventType = true - } else { - if string(tuple.Key()) == database.CtEventUsername { - eventMap[database.CtEventAgentUsername] = string(tuple.Value()) //agent_username - eventMap[string(tuple.Key())] = string(tuple.Value()) //username - } else { - addInfo[string(tuple.Key())] = string(tuple.Value()) - } - - } - } - - // BE AWARE: error is not treated - infoJSON, _ := json.Marshal(addInfo) - eventMap[database.CtEventAdditionalInfo] = string(infoJSON) - - if !doNotSetCTEventType { - eventMap = addCTtypeToEvent(eventMap) - } - - return eventMap -} - -func apply(ctx context.Context, fs [](FuncEvent), param map[string]string) error { - var errors = make(chan error, len(fs)) - var wg sync.WaitGroup - - // Wait for all fs. - wg.Add(len(fs)) - - for _, f := range fs { - go func(wg *sync.WaitGroup, f FuncEvent) { - defer wg.Done() - - var err = f(ctx, param) - if err != nil { - errors <- err - } - }(&wg, f) - } - - wg.Wait() - - select { - case err, ok := <-errors: - if ok { - return err - } - default: - return nil - } - return nil -} - -func epochMilliToTime(milli int64) time.Time { - return time.Unix(0, milli*1000000) -} diff --git a/pkg/event/component_test.go b/pkg/event/component_test.go deleted file mode 100755 index 21f73916..00000000 --- a/pkg/event/component_test.go +++ /dev/null @@ -1,687 +0,0 @@ -package event - -import ( - "context" - "encoding/json" - "errors" - "strconv" - "testing" - "time" - - "github.com/cloudtrust/common-service/database" - "github.com/cloudtrust/keycloak-bridge/api/event/fb" - flatbuffers "github.com/google/flatbuffers/go" - "github.com/stretchr/testify/assert" -) - -func TestMuxComponent(t *testing.T) { - var ch = make(chan string, 1) - - var fnEvent = func(ctx context.Context, eventMap map[string]string) error { - ch <- "Event" - return nil - } - - var fnAdminEvent = func(ctx context.Context, eventMap map[string]string) error { - ch <- "AdminEvent" - return nil - } - - var tEvent = []FuncEvent{fnEvent} - var tAdminEvent = []FuncEvent{fnAdminEvent} - - var eventComponent = NewComponent(tEvent, tEvent) - var adminEventService = NewAdminComponent(tAdminEvent, tAdminEvent, tAdminEvent, tAdminEvent) - - var muxComponent = NewMuxComponent(eventComponent, adminEventService) - - var event = createEventBytes(fb.EventTypeCLIENT_DELETE, 1234, "realm") - var err = muxComponent.Event(context.Background(), "Event", event) - assert.Equal(t, "Event", <-ch) - assert.Nil(t, err) - - var adminEvent = createAdminEventBytes(fb.OperationTypeDELETE, 1234) - var err2 = muxComponent.Event(context.Background(), "AdminEvent", adminEvent) - assert.Equal(t, "AdminEvent", <-ch) - assert.Nil(t, err2) -} -func TestComponent(t *testing.T) { - var eventComponent Component - { - var fnStd = func(ctx context.Context, eventMap map[string]string) error { - return nil - } - - var fnErr = func(ctx context.Context, eventMap map[string]string) error { - return errors.New("Failed") - } - - var tStd = []FuncEvent{fnStd} - var tErr = []FuncEvent{fnErr} - eventComponent = NewComponent(tStd, tErr) - } - - { - var eventStd = createEvent(fb.EventTypeCLIENT_DELETE, 1234, "realm") - var err = eventComponent.Event(nil, eventStd) - assert.Nil(t, err) - } - - { - var eventErr = createEvent(fb.EventTypeCLIENT_DELETE_ERROR, 1234, "realm") - var err = eventComponent.Event(nil, eventErr) - assert.NotNil(t, err) - } -} -func TestAdminComponent(t *testing.T) { - var adminEventComponent AdminComponent - var ch = make(chan string, 1) - { - var fnCreate = func(ctx context.Context, eventMap map[string]string) error { - ch <- "CREATE" - return nil - } - - var fnUpdate = func(ctx context.Context, eventMap map[string]string) error { - ch <- "UPDATE" - return nil - } - - var fnDelete = func(ctx context.Context, eventMap map[string]string) error { - ch <- "DELETE" - return nil - } - - var fnAction = func(ctx context.Context, eventMap map[string]string) error { - ch <- "ACTION" - return nil - } - - var tCreate = [](FuncEvent){fnCreate} - var tUpdate = [](FuncEvent){fnUpdate} - var tDelete = [](FuncEvent){fnDelete} - var tAction = [](FuncEvent){fnAction} - adminEventComponent = NewAdminComponent(tCreate, tUpdate, tDelete, tAction) - } - - var fn = func(operationType int8) { - var adminEvt *fb.AdminEvent = createAdminEvent(fb.OperationTypeCREATE, 1234) - var err = adminEventComponent.AdminEvent(nil, adminEvt) - - assert.Equal(t, getOperationTypeName(fb.OperationTypeCREATE), <-ch) - assert.Nil(t, err) - } - - var operationTypes = [4]int8{fb.OperationTypeCREATE, - fb.OperationTypeUPDATE, - fb.OperationTypeDELETE, - fb.OperationTypeACTION} - - for _, element := range operationTypes { - fn(element) - } -} - -func TestEventToMap(t *testing.T) { - var uid int64 = 1234 - var epoch = int64(1547127600485) - var etype int8 = 6 - var realmID = "realm" - var clientID = "client" - var userID = "user" - var sessionID = "session" - var ipAddr = "ipAddress" - var error = "error" - var username = "test_username" - - var event *fb.Event - { - var builder = flatbuffers.NewBuilder(0) - - var realm = builder.CreateString(realmID) - var clientID = builder.CreateString(clientID) - var userID = builder.CreateString(userID) - var sessionID = builder.CreateString(sessionID) - var ipAddress = builder.CreateString(ipAddr) - var error = builder.CreateString(error) - - var key1 = builder.CreateString("username") - var value1 = builder.CreateString(username) - fb.TupleStart(builder) - fb.TupleAddKey(builder, key1) - fb.TupleAddValue(builder, value1) - var detail1 = fb.TupleEnd(builder) - - var key2 = builder.CreateString("key2") - var value2 = builder.CreateString("value2") - fb.TupleStart(builder) - fb.TupleAddKey(builder, key2) - fb.TupleAddValue(builder, value2) - var detail2 = fb.TupleEnd(builder) - - fb.EventStartDetailsVector(builder, 2) - builder.PrependUOffsetT(detail1) - builder.PrependUOffsetT(detail2) - var details = builder.EndVector(2) - - fb.EventStart(builder) - fb.EventAddUid(builder, uid) - fb.EventAddTime(builder, epoch) - fb.EventAddType(builder, etype) - fb.EventAddRealmId(builder, realm) - fb.EventAddClientId(builder, clientID) - fb.EventAddUserId(builder, userID) - fb.EventAddSessionId(builder, sessionID) - fb.EventAddIpAddress(builder, ipAddress) - fb.EventAddError(builder, error) - fb.EventAddDetails(builder, details) - var eventOffset = fb.EventEnd(builder) - builder.Finish(eventOffset) - event = fb.GetRootAsEvent(builder.FinishedBytes(), 0) - } - - var m = eventToMap(event) - assert.Equal(t, time.Unix(0, epoch*1000000).UTC().Format("2006-01-02 15:04:05.000"), m["audit_time"]) - assert.Equal(t, fb.EnumNamesEventType[int8(etype)], m["kc_event_type"]) - assert.Equal(t, realmID, m["realm_name"]) - assert.Equal(t, clientID, m["client_id"]) - assert.Equal(t, userID, m["user_id"]) - assert.Equal(t, username, m["username"]) - var f = make(map[string]string) - err := json.Unmarshal([]byte(m["additional_info"]), &f) - assert.Nil(t, err) - assert.Equal(t, strconv.FormatInt(uid, 10), f["uid"]) - assert.Equal(t, sessionID, f["session_id"]) - assert.Equal(t, ipAddr, f["ip_address"]) - assert.Equal(t, error, f["error"]) - assert.Equal(t, "", m["ct_event_type"]) - -} - -func TestEventToMapNewCTEvent(t *testing.T) { - var customEvent = "CUSTOM_EVENT" - var etype int8 = 6 - - var event *fb.Event - { - var builder = flatbuffers.NewBuilder(0) - var key1 = builder.CreateString(database.CtEventType) - var value1 = builder.CreateString(customEvent) - fb.TupleStart(builder) - fb.TupleAddKey(builder, key1) - fb.TupleAddValue(builder, value1) - var detail1 = fb.TupleEnd(builder) - - fb.EventStartDetailsVector(builder, 1) - builder.PrependUOffsetT(detail1) - var details = builder.EndVector(1) - - fb.EventStart(builder) - fb.EventAddDetails(builder, details) - fb.EventAddType(builder, etype) - var eventOffset = fb.EventEnd(builder) - builder.Finish(eventOffset) - event = fb.GetRootAsEvent(builder.FinishedBytes(), 0) - } - - var m = eventToMap(event) - assert.Equal(t, customEvent, m[database.CtEventType]) - -} - -func TestEventToMapLogon(t *testing.T) { - var etype int8 - - var event *fb.Event - { - var builder = flatbuffers.NewBuilder(0) - - fb.EventStart(builder) - fb.EventAddType(builder, etype) - var eventOffset = fb.EventEnd(builder) - builder.Finish(eventOffset) - event = fb.GetRootAsEvent(builder.FinishedBytes(), 0) - } - - var m = eventToMap(event) - assert.Equal(t, "LOGON_OK", m[database.CtEventType]) - -} - -func TestEventToMapLogonError(t *testing.T) { - var etype int8 = 1 - - var event *fb.Event - { - var builder = flatbuffers.NewBuilder(0) - - fb.EventStart(builder) - fb.EventAddType(builder, etype) - var eventOffset = fb.EventEnd(builder) - builder.Finish(eventOffset) - event = fb.GetRootAsEvent(builder.FinishedBytes(), 0) - } - - var m = eventToMap(event) - assert.Equal(t, "LOGON_ERROR", m[database.CtEventType]) - -} - -func TestEventToMapLogonErrorLocked(t *testing.T) { - var etype int8 = 1 - - var event *fb.Event - { - var builder = flatbuffers.NewBuilder(0) - var error = builder.CreateString("user_temporarily_disabled") - fb.EventStart(builder) - fb.EventAddType(builder, etype) - fb.EventAddError(builder, error) - var eventOffset = fb.EventEnd(builder) - builder.Finish(eventOffset) - event = fb.GetRootAsEvent(builder.FinishedBytes(), 0) - } - - var m = eventToMap(event) - assert.Equal(t, "TEMPORARILY_LOCKED", m[database.CtEventType]) - -} - -func TestEventToMapLogonErrorCustom(t *testing.T) { - var etype int8 = 1 - - var event *fb.Event - { - var builder = flatbuffers.NewBuilder(0) - var error = builder.CreateString("custom_error") - fb.EventStart(builder) - fb.EventAddType(builder, etype) - fb.EventAddError(builder, error) - var eventOffset = fb.EventEnd(builder) - builder.Finish(eventOffset) - event = fb.GetRootAsEvent(builder.FinishedBytes(), 0) - } - - var m = eventToMap(event) - assert.Equal(t, "LOGON_ERROR", m[database.CtEventType]) - -} - -func TestEventToMapLogout(t *testing.T) { - var etype int8 = 4 - - var event *fb.Event - { - var builder = flatbuffers.NewBuilder(0) - - fb.EventStart(builder) - fb.EventAddType(builder, etype) - var eventOffset = fb.EventEnd(builder) - builder.Finish(eventOffset) - event = fb.GetRootAsEvent(builder.FinishedBytes(), 0) - } - - var m = eventToMap(event) - assert.Equal(t, "LOGOUT", m[database.CtEventType]) - -} - -func TestEventToMapEmailConfirmed(t *testing.T) { - var etype int8 = 66 - - var event *fb.Event - { - var builder = flatbuffers.NewBuilder(0) - var key1 = builder.CreateString("username") - var value1 = builder.CreateString("test_username") - fb.TupleStart(builder) - fb.TupleAddKey(builder, key1) - fb.TupleAddValue(builder, value1) - var detail1 = fb.TupleEnd(builder) - - var key2 = builder.CreateString("custom_required_action") - var value2 = builder.CreateString("VERIFY_EMAIL") - fb.TupleStart(builder) - fb.TupleAddKey(builder, key2) - fb.TupleAddValue(builder, value2) - var detail2 = fb.TupleEnd(builder) - - fb.EventStartDetailsVector(builder, 2) - builder.PrependUOffsetT(detail1) - builder.PrependUOffsetT(detail2) - var details = builder.EndVector(2) - - fb.EventStart(builder) - fb.EventAddType(builder, etype) - fb.EventAddDetails(builder, details) - - var eventOffset = fb.EventEnd(builder) - builder.Finish(eventOffset) - event = fb.GetRootAsEvent(builder.FinishedBytes(), 0) - } - - var m = eventToMap(event) - assert.Equal(t, "EMAIL_CONFIRMED", m[database.CtEventType]) - -} - -func TestEventToMapConfirmEmailExpired(t *testing.T) { - var etype int8 = 71 - var error1 = "expired_code" - - var event *fb.Event - { - var builder = flatbuffers.NewBuilder(0) - var error = builder.CreateString(error1) - var key1 = builder.CreateString("username") - var value1 = builder.CreateString("test_username") - fb.TupleStart(builder) - fb.TupleAddKey(builder, key1) - fb.TupleAddValue(builder, value1) - var detail1 = fb.TupleEnd(builder) - - var key2 = builder.CreateString("key") - var value2 = builder.CreateString("value") - fb.TupleStart(builder) - fb.TupleAddKey(builder, key2) - fb.TupleAddValue(builder, value2) - var detail2 = fb.TupleEnd(builder) - - fb.EventStartDetailsVector(builder, 2) - builder.PrependUOffsetT(detail1) - builder.PrependUOffsetT(detail2) - var details = builder.EndVector(2) - - fb.EventStart(builder) - fb.EventAddType(builder, etype) - fb.EventAddDetails(builder, details) - fb.EventAddError(builder, error) - var eventOffset = fb.EventEnd(builder) - builder.Finish(eventOffset) - event = fb.GetRootAsEvent(builder.FinishedBytes(), 0) - } - - var m = eventToMap(event) - assert.Equal(t, "CONFIRM_EMAIL_EXPIRED", m[database.CtEventType]) - -} - -func TestEventToMapPasswordReset(t *testing.T) { - var etype int8 = 24 - var error1 = "expired_code" - - var event *fb.Event - { - var builder = flatbuffers.NewBuilder(0) - var error = builder.CreateString(error1) - var key1 = builder.CreateString("username") - var value1 = builder.CreateString("test_username") - fb.TupleStart(builder) - fb.TupleAddKey(builder, key1) - fb.TupleAddValue(builder, value1) - var detail1 = fb.TupleEnd(builder) - - var key2 = builder.CreateString("custom_required_action") - var value2 = builder.CreateString("sms-password-set") - fb.TupleStart(builder) - fb.TupleAddKey(builder, key2) - fb.TupleAddValue(builder, value2) - var detail2 = fb.TupleEnd(builder) - - fb.EventStartDetailsVector(builder, 2) - builder.PrependUOffsetT(detail1) - builder.PrependUOffsetT(detail2) - var details = builder.EndVector(2) - - fb.EventStart(builder) - fb.EventAddType(builder, etype) - fb.EventAddDetails(builder, details) - fb.EventAddError(builder, error) - var eventOffset = fb.EventEnd(builder) - builder.Finish(eventOffset) - event = fb.GetRootAsEvent(builder.FinishedBytes(), 0) - } - - var m = eventToMap(event) - assert.Equal(t, "PASSWORD_RESET", m[database.CtEventType]) - -} - -func TestAdminEventToMap(t *testing.T) { - var uid int64 = 1234 - var epoch = int64(1547127600485) - var resourcetype int8 - var resourcePath = "" - var optype int8 - var realmID = "realm" - var representation = "representation" - var error = "error" - var agentUsername = "agent_test_username" - var agentUserID = "agent_test_id" - var userID = "user_id" - var username = "username" - var ipAddr = "127.0.0.1" - var clientID = "clientid_test" - - var adminEvent *fb.AdminEvent - { - var builder = flatbuffers.NewBuilder(0) - - var realm = builder.CreateString(realmID) - var representation = builder.CreateString(representation) - var resourceP = builder.CreateString(resourcePath) - var error = builder.CreateString(error) - - var agentUsrValue = builder.CreateString(agentUsername) - var agentIDValue = builder.CreateString(agentUserID) - var agentRealmValue = builder.CreateString(realmID) - var ipValue = builder.CreateString(ipAddr) - var clientIDValue = builder.CreateString(clientID) - - fb.AuthDetailsStart(builder) - fb.AuthDetailsAddUserId(builder, agentIDValue) - fb.AuthDetailsAddUsername(builder, agentUsrValue) - fb.AuthDetailsAddRealmId(builder, agentRealmValue) - fb.AuthDetailsAddIpAddress(builder, ipValue) - fb.AuthDetailsAddClientId(builder, clientIDValue) - var authDetails = fb.AuthDetailsEnd(builder) - - var key3 = builder.CreateString(database.CtEventUsername) - var value3 = builder.CreateString(username) - fb.TupleStart(builder) - fb.TupleAddKey(builder, key3) - fb.TupleAddValue(builder, value3) - var detail3 = fb.TupleEnd(builder) - - var key4 = builder.CreateString(database.CtEventUserID) - var value4 = builder.CreateString(userID) - fb.TupleStart(builder) - fb.TupleAddKey(builder, key4) - fb.TupleAddValue(builder, value4) - var detail4 = fb.TupleEnd(builder) - - fb.EventStartDetailsVector(builder, 2) - builder.PrependUOffsetT(detail3) - builder.PrependUOffsetT(detail4) - var details = builder.EndVector(2) - - fb.AdminEventStart(builder) - fb.AdminEventAddUid(builder, uid) - fb.AdminEventAddTime(builder, epoch) - fb.AdminEventAddResourceType(builder, resourcetype) - fb.AdminEventAddRealmId(builder, realm) - fb.AdminEventAddRepresentation(builder, representation) - fb.AdminEventAddOperationType(builder, optype) - fb.AdminEventAddResourcePath(builder, resourceP) - fb.AdminEventAddError(builder, error) - fb.AdminEventAddAuthDetails(builder, authDetails) - fb.AdminEventAddDetails(builder, details) - var eventOffset = fb.EventEnd(builder) - builder.Finish(eventOffset) - adminEvent = fb.GetRootAsAdminEvent(builder.FinishedBytes(), 0) - } - - var m = adminEventToMap(adminEvent) - - assert.Equal(t, time.Unix(0, epoch*1000000).UTC().Format("2006-01-02 15:04:05.000"), m[database.CtEventAuditTime]) - assert.Equal(t, fb.EnumNamesOperationType[int8(optype)], m[database.CtEventKcOperationType]) - assert.Equal(t, realmID, m[database.CtEventRealmName]) - assert.Equal(t, username, m[database.CtEventUsername]) - assert.Equal(t, userID, m[database.CtEventUserID]) - assert.Equal(t, agentUsername, m[database.CtEventAgentUsername]) - assert.Equal(t, agentUserID, m[database.CtEventAgentUserID]) - assert.Equal(t, clientID, m[database.CtEventClientID]) - assert.Equal(t, realmID, m[database.CtEventAgentRealmName]) - var f = make(map[string]string) - err := json.Unmarshal([]byte(m[database.CtEventAdditionalInfo]), &f) - assert.Nil(t, err) - assert.Equal(t, strconv.FormatInt(uid, 10), f["uid"]) - assert.Equal(t, resourcePath, f["resource_path"]) - assert.Equal(t, representation, f["representation"]) - assert.Equal(t, ipAddr, f["ip_address"]) - assert.Equal(t, error, f["error"]) - assert.Equal(t, "ADMIN", m[database.CtEventType]) - -} - -func TestAdminEventToMapAccountCreated(t *testing.T) { - var resourcePath = "users/8caefab3-90d1-492e-87e0-1bf6cecc76ea/role-mappings/realm " - var optype int8 - var agentUsername = "agent_test_username" - var agentUserID = "agent_test_id" - var ipAddr = "127.0.0.1" - var clientID = "clientid_test" - var realmID = "realm" - - var adminEvent *fb.AdminEvent - { - var builder = flatbuffers.NewBuilder(0) - var resourceP = builder.CreateString(resourcePath) - - var agentUsrValue = builder.CreateString(agentUsername) - var agentIDValue = builder.CreateString(agentUserID) - var agentRealmValue = builder.CreateString(realmID) - var ipValue = builder.CreateString(ipAddr) - var clientIDValue = builder.CreateString(clientID) - - fb.AuthDetailsStart(builder) - fb.AuthDetailsAddUserId(builder, agentIDValue) - fb.AuthDetailsAddUsername(builder, agentUsrValue) - fb.AuthDetailsAddRealmId(builder, agentRealmValue) - fb.AuthDetailsAddIpAddress(builder, ipValue) - fb.AuthDetailsAddClientId(builder, clientIDValue) - var authDetails = fb.AuthDetailsEnd(builder) - - fb.AdminEventStart(builder) - fb.AdminEventAddOperationType(builder, optype) - fb.AdminEventAddResourceType(builder, fb.ResourceTypeUSER) - fb.AdminEventAddResourcePath(builder, resourceP) - fb.AdminEventAddAuthDetails(builder, authDetails) - var eventOffset = fb.EventEnd(builder) - builder.Finish(eventOffset) - adminEvent = fb.GetRootAsAdminEvent(builder.FinishedBytes(), 0) - } - - var m = adminEventToMap(adminEvent) - assert.Equal(t, "ACCOUNT_CREATED", m[database.CtEventType]) - -} - -func TestAdminEventToMapActivationEmailSent(t *testing.T) { - var resourcePath = "users/8caefab3-90d1-492e-87e0-1bf6cecc76ea/send-verify-email" - var optype int8 = 3 - var agentUsername = "agent_test_username" - var agentUserID = "agent_test_id" - var ipAddr = "127.0.0.1" - var clientID = "clientid_test" - var realmID = "realm" - - var adminEvent *fb.AdminEvent - { - var builder = flatbuffers.NewBuilder(0) - var resourceP = builder.CreateString(resourcePath) - - var agentUsrValue = builder.CreateString(agentUsername) - var agentIDValue = builder.CreateString(agentUserID) - var agentRealmValue = builder.CreateString(realmID) - var ipValue = builder.CreateString(ipAddr) - var clientIDValue = builder.CreateString(clientID) - - fb.AuthDetailsStart(builder) - fb.AuthDetailsAddUserId(builder, agentIDValue) - fb.AuthDetailsAddUsername(builder, agentUsrValue) - fb.AuthDetailsAddRealmId(builder, agentRealmValue) - fb.AuthDetailsAddIpAddress(builder, ipValue) - fb.AuthDetailsAddClientId(builder, clientIDValue) - var authDetails = fb.AuthDetailsEnd(builder) - - fb.AdminEventStart(builder) - fb.AdminEventAddOperationType(builder, optype) - fb.AdminEventAddResourcePath(builder, resourceP) - fb.AdminEventAddAuthDetails(builder, authDetails) - var eventOffset = fb.EventEnd(builder) - builder.Finish(eventOffset) - adminEvent = fb.GetRootAsAdminEvent(builder.FinishedBytes(), 0) - } - - var m = adminEventToMap(adminEvent) - assert.Equal(t, "ACTIVATION_EMAIL_SENT", m[database.CtEventType]) - -} - -func createEvent(eventType int8, uid int64, realm string) *fb.Event { - return fb.GetRootAsEvent(createEventBytes(eventType, uid, realm), 0) -} - -func createEventBytes(eventType int8, uid int64, realm string) []byte { - var builder = flatbuffers.NewBuilder(0) - var realmStr = builder.CreateString(realm) - fb.EventStart(builder) - fb.EventAddUid(builder, uid) - fb.EventAddTime(builder, time.Now().Unix()) - fb.EventAddType(builder, eventType) - fb.EventAddRealmId(builder, realmStr) - var eventOffset = fb.EventEnd(builder) - builder.Finish(eventOffset) - return builder.FinishedBytes() -} - -func createAdminEvent(operationType int8, uid int64) *fb.AdminEvent { - return fb.GetRootAsAdminEvent(createAdminEventBytes(operationType, uid), 0) -} - -func createAdminEventBytes(operationType int8, uid int64) []byte { - var builder = flatbuffers.NewBuilder(0) - var agentUsername = "agent_test_username" - var agentUserID = "agent_test_id" - var ipAddr = "127.0.0.1" - var clientID = "clientid_test" - var realmID = "realm" - - var agentUsrValue = builder.CreateString(agentUsername) - var agentIDValue = builder.CreateString(agentUserID) - var agentRealmValue = builder.CreateString(realmID) - var ipValue = builder.CreateString(ipAddr) - var clientIDValue = builder.CreateString(clientID) - - fb.AuthDetailsStart(builder) - fb.AuthDetailsAddUserId(builder, agentIDValue) - fb.AuthDetailsAddUsername(builder, agentUsrValue) - fb.AuthDetailsAddRealmId(builder, agentRealmValue) - fb.AuthDetailsAddIpAddress(builder, ipValue) - fb.AuthDetailsAddClientId(builder, clientIDValue) - var authDetails = fb.AuthDetailsEnd(builder) - - fb.AdminEventStart(builder) - fb.AdminEventAddTime(builder, time.Now().Unix()) - fb.AdminEventAddUid(builder, uid) - fb.AdminEventAddOperationType(builder, operationType) - fb.AdminEventAddAuthDetails(builder, authDetails) - var adminEventOffset = fb.AdminEventEnd(builder) - builder.Finish(adminEventOffset) - return builder.FinishedBytes() -} - -func getOperationTypeName(key int8) string { - return fb.EnumNamesOperationType[int8(key)] -} diff --git a/pkg/event/endpoint.go b/pkg/event/endpoint.go deleted file mode 100755 index 23cd9f2f..00000000 --- a/pkg/event/endpoint.go +++ /dev/null @@ -1,27 +0,0 @@ -package event - -import ( - "context" - "fmt" - - cs "github.com/cloudtrust/common-service" - msg "github.com/cloudtrust/keycloak-bridge/internal/constants" - "github.com/go-kit/kit/endpoint" -) - -// Endpoints wraps a service behind a set of endpoints. -type Endpoints struct { - Endpoint endpoint.Endpoint -} - -// MakeEventEndpoint makes the event endpoint. -func MakeEventEndpoint(c MuxComponent) cs.Endpoint { - return func(ctx context.Context, req interface{}) (interface{}, error) { - switch r := req.(type) { - case Request: - return nil, c.Event(ctx, r.Type, r.Object) - default: - return nil, fmt.Errorf(msg.MsgErrWrongTypeRequest+".%T", req) - } - } -} diff --git a/pkg/event/endpoint_test.go b/pkg/event/endpoint_test.go deleted file mode 100644 index 3282b034..00000000 --- a/pkg/event/endpoint_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package event - -import ( - "context" - "math/rand" - "strconv" - "testing" - "time" - - cs "github.com/cloudtrust/common-service" - "github.com/cloudtrust/keycloak-bridge/api/event/fb" - "github.com/cloudtrust/keycloak-bridge/pkg/event/mock" - "github.com/golang/mock/gomock" - "github.com/stretchr/testify/assert" -) - -func TestEventEndpoint(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockMuxComponent = mock.NewMuxComponent(mockCtrl) - - var e = MakeEventEndpoint(mockMuxComponent) - - // Context with correlation ID. - rand.Seed(time.Now().UnixNano()) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var uid = rand.Int63() - var req = Request{ - Type: "Event", - Object: createEventBytes(fb.EventTypeCLIENT_DELETE, uid, "realm"), - } - - // Event. - mockMuxComponent.EXPECT().Event(ctx, req.Type, req.Object).Return(nil).Times(1) - var rep, err = e(ctx, req) - assert.Nil(t, err) - assert.Nil(t, rep) - - // Bad parameters - _, err = e(ctx, "string") - assert.NotNil(t, err) -} diff --git a/pkg/event/http.go b/pkg/event/http.go deleted file mode 100755 index f08e88ed..00000000 --- a/pkg/event/http.go +++ /dev/null @@ -1,120 +0,0 @@ -package event - -import ( - "context" - "encoding/base64" - "encoding/json" - "fmt" - "net/http" - - cs "github.com/cloudtrust/common-service" - "github.com/cloudtrust/common-service/log" - msg "github.com/cloudtrust/keycloak-bridge/internal/constants" - "github.com/go-kit/kit/endpoint" - http_transport "github.com/go-kit/kit/transport/http" - "github.com/pkg/errors" -) - -// MakeHTTPEventHandler makes a HTTP handler for the event endpoint. -func MakeHTTPEventHandler(e endpoint.Endpoint, logger log.Logger) *http_transport.Server { - return http_transport.NewServer(e, - decodeHTTPRequest, - encodeHTTPReply, - http_transport.ServerErrorEncoder(errorHandler(logger)), - http_transport.ServerBefore(fetchHTTPCorrelationID), - ) -} - -// fetchHTTPCorrelationID reads the correlation ID from the http header "X-Correlation-ID". -// If the ID is not zero, we put it in the context. -func fetchHTTPCorrelationID(ctx context.Context, req *http.Request) context.Context { - var correlationID = req.Header.Get("X-Correlation-ID") - if correlationID != "" { - ctx = context.WithValue(ctx, cs.CtContextCorrelationID, correlationID) - } - return ctx -} - -// KeycloakRequest is the Request for KeycloakEventReceiver endpoint. -type KeycloakRequest struct { - Type string - Object string `json:"Obj"` -} - -// Request has the fields Type and Object. -type Request struct { - Type string - Object []byte -} - -// decodeHTTPRequest decodes the http event request. -func decodeHTTPRequest(_ context.Context, r *http.Request) (res interface{}, err error) { - var request KeycloakRequest - { - var err = json.NewDecoder(r.Body).Decode(&request) - if err != nil { - return nil, errors.Wrap(err, msg.MsgErrInvalidJSONRequest) - } - } - - var bEvent []byte - { - var err error - bEvent, err = base64.StdEncoding.DecodeString(request.Object) - - if err != nil { - return nil, errors.Wrap(err, msg.MsgErrInvalidBase64Object) - } - } - - var objType = request.Type - { - if !(objType == "AdminEvent" || objType == "Event") { - var err = ErrInvalidArgument{InvalidParam: "type"} - return nil, errors.Wrap(err, msg.MsgErrInvalidBase64Object) - } - } - - // Check valid buffer (at least 4 bytes) - if len(bEvent) < 4 { - var err = ErrInvalidArgument{InvalidParam: "obj"} - return nil, errors.Wrap(err, msg.MsgErrInvalidLength+"."+msg.Flatbuffer) - } - - return Request{ - Type: objType, - Object: bEvent, - }, nil -} - -// encodeHTTPReply encodes the http event reply. -func encodeHTTPReply(_ context.Context, w http.ResponseWriter, _ interface{}) error { - w.WriteHeader(http.StatusOK) - return nil -} - -// ErrInvalidArgument is returned when one or more arguments are invalid. -type ErrInvalidArgument struct { - InvalidParam string -} - -func (e ErrInvalidArgument) Error() string { - return fmt.Sprintf("invalidArgument.%s", e.InvalidParam) -} - -// errorHandler encodes the reply when there is an error. -func errorHandler(logger log.Logger) func(ctx context.Context, err error, w http.ResponseWriter) { - return func(ctx context.Context, err error, w http.ResponseWriter) { - w.Header().Set("Content-Type", "application/json; charset=utf-8") - switch errors.Cause(err).(type) { - case ErrInvalidArgument: - logger.Error(ctx, "errorHandler", http.StatusBadRequest, "msg", err.Error()) - w.WriteHeader(http.StatusBadRequest) - default: - logger.Error(ctx, "errorHandler", http.StatusInternalServerError, "msg", err.Error()) - w.WriteHeader(http.StatusInternalServerError) - } - - json.NewEncoder(w).Encode(map[string]interface{}{"error": err.Error()}) - } -} diff --git a/pkg/event/http_test.go b/pkg/event/http_test.go deleted file mode 100755 index 59cbec3f..00000000 --- a/pkg/event/http_test.go +++ /dev/null @@ -1,184 +0,0 @@ -package event - -import ( - "context" - "encoding/base64" - "fmt" - "io/ioutil" - "math/rand" - "net/http" - "net/http/httptest" - "strconv" - "strings" - "testing" - "time" - - cs "github.com/cloudtrust/common-service" - "github.com/cloudtrust/common-service/log" - "github.com/cloudtrust/keycloak-bridge/api/event/fb" - "github.com/cloudtrust/keycloak-bridge/internal/keycloakb" - "github.com/cloudtrust/keycloak-bridge/pkg/event/mock" - "github.com/golang/mock/gomock" - "github.com/pkg/errors" - "github.com/stretchr/testify/assert" -) - -func TestHTTPEventHandler(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockComponent = mock.NewMuxComponent(mockCtrl) - var mockLogger = log.NewNopLogger() - - var eventHandler = MakeHTTPEventHandler(keycloakb.ToGoKitEndpoint(MakeEventEndpoint(mockComponent)), mockLogger) - - rand.Seed(time.Now().UnixNano()) - var uid = rand.Int63() - var eventByte = createEventBytes(fb.OperationTypeCREATE, uid, "realm") - var eventString = base64.StdEncoding.EncodeToString(eventByte) - - // HTTP request. - var body = strings.NewReader(fmt.Sprintf(`{"type": "Event", "Obj": "%s"}`, eventString)) - var httpReq = httptest.NewRequest("POST", "http://localhost:8888/event/id", body) - var w = httptest.NewRecorder() - - // Event. - { - mockComponent.EXPECT().Event(context.Background(), "Event", eventByte).Return(nil).Times(1) - eventHandler.ServeHTTP(w, httpReq) - var res = w.Result() - var body, err = ioutil.ReadAll(res.Body) - assert.Nil(t, err) - assert.Equal(t, http.StatusOK, res.StatusCode) - assert.Equal(t, "", res.Header.Get("Content-Type")) - assert.Equal(t, 0, len(body)) - } -} -func TestHTTPErrorHandler(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockComponent = mock.NewMuxComponent(mockCtrl) - var mockLogger = log.NewNopLogger() - - var eventHandler = MakeHTTPEventHandler(keycloakb.ToGoKitEndpoint(MakeEventEndpoint(mockComponent)), mockLogger) - - rand.Seed(time.Now().UnixNano()) - var uid = rand.Int63() - var eventByte = createEventBytes(fb.OperationTypeCREATE, uid, "realm") - var eventString = base64.StdEncoding.EncodeToString(eventByte) - - // Internal server error. - { - // HTTP request. - var body = strings.NewReader(fmt.Sprintf(`{"type": "Event", "Obj": "%s"}`, eventString)) - var httpReq = httptest.NewRequest("POST", "http://localhost:8888/event/id", body) - var w = httptest.NewRecorder() - - mockComponent.EXPECT().Event(context.Background(), "Event", eventByte).Return(fmt.Errorf("fail")).Times(1) - eventHandler.ServeHTTP(w, httpReq) - var res = w.Result() - var data, err = ioutil.ReadAll(res.Body) - assert.Nil(t, err) - assert.Equal(t, http.StatusInternalServerError, res.StatusCode) - assert.Equal(t, "application/json; charset=utf-8", res.Header.Get("Content-Type")) - assert.NotZero(t, string(data)) - } - - // Bad request. - { - // Bad HTTP request. - var body = strings.NewReader(fmt.Sprintf(`{"type": "Unknown", "Obj": "%s"}`, eventString)) - var httpReq = httptest.NewRequest("POST", "http://localhost:8888/event/id", body) - var w = httptest.NewRecorder() - - eventHandler.ServeHTTP(w, httpReq) - var res = w.Result() - var data, err = ioutil.ReadAll(res.Body) - assert.Nil(t, err) - assert.Equal(t, http.StatusBadRequest, res.StatusCode) - assert.Equal(t, "application/json; charset=utf-8", res.Header.Get("Content-Type")) - assert.NotZero(t, string(data)) - } -} - -func TestDecodeValidAdminEvent(t *testing.T) { - rand.Seed(time.Now().UnixNano()) - var uid = rand.Int63() - var eventByte = createAdminEventBytes(fb.OperationTypeACTION, uid) - var eventString = base64.StdEncoding.EncodeToString(eventByte) - var body = strings.NewReader(fmt.Sprintf(`{"type": "AdminEvent", "Obj": "%s"}`, eventString)) - var req = httptest.NewRequest("POST", "http://localhost:8888/event/id", body) - - var res, err = decodeHTTPRequest(context.Background(), req) - assert.Nil(t, err) - - var r, ok = res.(Request) - assert.True(t, ok) - assert.Equal(t, "AdminEvent", r.Type) - assert.Equal(t, eventByte, r.Object) -} - -func TestDecodeValidEvent(t *testing.T) { - rand.Seed(time.Now().UnixNano()) - var uid = rand.Int63() - var eventByte = createEventBytes(fb.OperationTypeCREATE, uid, "realm") - var eventString = base64.StdEncoding.EncodeToString(eventByte) - var body = strings.NewReader(fmt.Sprintf(`{"type": "Event", "Obj": "%s"}`, eventString)) - var req = httptest.NewRequest("POST", "http://localhost:8888/event/id", body) - - var res, err = decodeHTTPRequest(context.Background(), req) - assert.Nil(t, err) - - var r, ok = res.(Request) - assert.True(t, ok) - assert.Equal(t, "Event", r.Type) - assert.Equal(t, eventByte, r.Object) -} - -func TestDecodeUnknownEvent(t *testing.T) { - rand.Seed(time.Now().UnixNano()) - var uid = rand.Int63() - var eventByte = createEventBytes(fb.OperationTypeCREATE, uid, "realm") - var eventString = base64.StdEncoding.EncodeToString(eventByte) - var body = strings.NewReader(fmt.Sprintf(`{"type": "Unknown", "Obj": "%s"}`, eventString)) - var req = httptest.NewRequest("POST", "http://localhost:8888/event/id", body) - - var res, err = decodeHTTPRequest(context.Background(), req) - assert.NotNil(t, err) - assert.IsType(t, ErrInvalidArgument{}, errors.Cause(err)) - assert.Nil(t, res) -} - -func TestDecodeInvalidObject(t *testing.T) { - var body = strings.NewReader(`{"type": "Event", "Obj": "test"}`) - var req = httptest.NewRequest("POST", "http://localhost:8888/event/id", body) - - var res, err = decodeHTTPRequest(context.Background(), req) - assert.NotNil(t, err) - assert.IsType(t, ErrInvalidArgument{}, errors.Cause(err)) - assert.Nil(t, res) -} - -func TestFetchHTTPCorrelationID(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockComponent = mock.NewMuxComponent(mockCtrl) - var mockLogger = log.NewNopLogger() - - var eventHandler = MakeHTTPEventHandler(keycloakb.ToGoKitEndpoint(MakeEventEndpoint(mockComponent)), mockLogger) - - rand.Seed(time.Now().UnixNano()) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var uid = rand.Int63() - var eventByte = createEventBytes(fb.OperationTypeCREATE, uid, "realm") - var eventString = base64.StdEncoding.EncodeToString(eventByte) - - // HTTP request. - var body = strings.NewReader(fmt.Sprintf(`{"type": "Event", "Obj": "%s"}`, eventString)) - var httpReq = httptest.NewRequest("POST", "http://localhost:8888/event/id", body) - httpReq.Header.Add("X-Correlation-ID", corrID) - var w = httptest.NewRecorder() - - mockComponent.EXPECT().Event(ctx, "Event", eventByte).Return(nil).Times(1) - eventHandler.ServeHTTP(w, httpReq) -} diff --git a/pkg/event/instrumenting.go b/pkg/event/instrumenting.go deleted file mode 100644 index 34bcc55d..00000000 --- a/pkg/event/instrumenting.go +++ /dev/null @@ -1,167 +0,0 @@ -package event - -import ( - "context" - "time" - - cs "github.com/cloudtrust/common-service" - "github.com/cloudtrust/common-service/database" - "github.com/cloudtrust/common-service/metrics" - "github.com/cloudtrust/keycloak-bridge/api/event/fb" -) - -const ( - // KeyCorrelationID is histogram field for correlation ID - KeyCorrelationID = "correlation_id" -) - -// Instrumenting middleware for the mux component. -type muxComponentInstrumentingMW struct { - h metrics.Histogram - next MuxComponent -} - -// MakeMuxComponentInstrumentingMW makes an instrumenting middleware for the mux component. -func MakeMuxComponentInstrumentingMW(h metrics.Histogram) func(MuxComponent) MuxComponent { - return func(next MuxComponent) MuxComponent { - return &muxComponentInstrumentingMW{ - h: h, - next: next, - } - } -} - -// muxComponentInstrumentingMW implements MuxComponent. -func (m *muxComponentInstrumentingMW) Event(ctx context.Context, eventType string, obj []byte) error { - defer func(begin time.Time) { - m.h.With(KeyCorrelationID, ctx.Value(cs.CtContextCorrelationID).(string)).Observe(time.Since(begin).Seconds()) - }(time.Now()) - return m.next.Event(ctx, eventType, obj) -} - -// Instrumenting middleware for the event component. -type componentInstrumentingMW struct { - h metrics.Histogram - next Component -} - -// MakeComponentInstrumentingMW makes an instrumenting middleware for the event component. -func MakeComponentInstrumentingMW(h metrics.Histogram) func(Component) Component { - return func(next Component) Component { - return &componentInstrumentingMW{ - h: h, - next: next, - } - } -} - -// componentInstrumentingMW implements Component. -func (m *componentInstrumentingMW) Event(ctx context.Context, event *fb.Event) error { - defer func(begin time.Time) { - m.h.With(KeyCorrelationID, ctx.Value(cs.CtContextCorrelationID).(string)).Observe(time.Since(begin).Seconds()) - }(time.Now()) - return m.next.Event(ctx, event) -} - -// Instrumenting middleware for the admin event component. -type adminComponentInstrumentingMW struct { - h metrics.Histogram - next AdminComponent -} - -// MakeAdminComponentInstrumentingMW makes a Instrumenting middleware for the admin event component. -func MakeAdminComponentInstrumentingMW(h metrics.Histogram) func(AdminComponent) AdminComponent { - return func(next AdminComponent) AdminComponent { - return &adminComponentInstrumentingMW{ - h: h, - next: next, - } - } -} - -// adminComponentInstrumentingMW implements AdminComponent. -func (m *adminComponentInstrumentingMW) AdminEvent(ctx context.Context, adminEvent *fb.AdminEvent) error { - defer func(begin time.Time) { - m.h.With(KeyCorrelationID, ctx.Value(cs.CtContextCorrelationID).(string)).Observe(time.Since(begin).Seconds()) - }(time.Now()) - return m.next.AdminEvent(ctx, adminEvent) -} - -// Instrumenting middleware at module level. -type consoleModuleInstrumentingMW struct { - h metrics.Histogram - next ConsoleModule -} - -// MakeConsoleModuleInstrumentingMW makes an instrumenting middleware at module level. -func MakeConsoleModuleInstrumentingMW(h metrics.Histogram) func(ConsoleModule) ConsoleModule { - return func(next ConsoleModule) ConsoleModule { - return &consoleModuleInstrumentingMW{ - h: h, - next: next, - } - } -} - -// consoleModuleInstrumentingMW implements Module. -func (m *consoleModuleInstrumentingMW) Print(ctx context.Context, mp map[string]string) error { - defer func(begin time.Time) { - m.h.With(KeyCorrelationID, ctx.Value(cs.CtContextCorrelationID).(string)).Observe(time.Since(begin).Seconds()) - }(time.Now()) - return m.next.Print(ctx, mp) -} - -// Instrumenting middleware at module level. -type statisticModuleInstrumentingMW struct { - h metrics.Histogram - next StatisticModule -} - -// MakeStatisticModuleInstrumentingMW makes an instrumenting middleware at module level. -func MakeStatisticModuleInstrumentingMW(h metrics.Histogram) func(StatisticModule) StatisticModule { - return func(next StatisticModule) StatisticModule { - return &statisticModuleInstrumentingMW{ - h: h, - next: next, - } - } -} - -// consoleModuleInstrumentingMW implements Module. -func (m *statisticModuleInstrumentingMW) Stats(ctx context.Context, mp map[string]string) error { - defer func(begin time.Time) { - m.h.With(KeyCorrelationID, ctx.Value(cs.CtContextCorrelationID).(string)).Observe(time.Since(begin).Seconds()) - }(time.Now()) - return m.next.Stats(ctx, mp) -} - -// Instrumenting middleware at module level. -type eventsDBModuleInstrumentingMW struct { - h metrics.Histogram - next database.EventsDBModule -} - -// MakeEventsDBModuleInstrumentingMW makes an instrumenting middleware at module level. -func MakeEventsDBModuleInstrumentingMW(h metrics.Histogram) func(database.EventsDBModule) database.EventsDBModule { - return func(next database.EventsDBModule) database.EventsDBModule { - return &eventsDBModuleInstrumentingMW{ - h: h, - next: next, - } - } -} - -// consoleModuleInstrumentingMW implements Module. -func (m *eventsDBModuleInstrumentingMW) Store(ctx context.Context, mp map[string]string) error { - defer func(begin time.Time) { - m.h.With(KeyCorrelationID, ctx.Value(cs.CtContextCorrelationID).(string)).Observe(time.Since(begin).Seconds()) - }(time.Now()) - return m.next.Store(ctx, mp) -} - -func (m *eventsDBModuleInstrumentingMW) ReportEvent(ctx context.Context, apiCall string, origin string, values ...string) error { - defer func(begin time.Time) { - m.h.With(KeyCorrelationID, ctx.Value(cs.CtContextCorrelationID).(string)).Observe(time.Since(begin).Seconds()) - }(time.Now()) - return m.next.ReportEvent(ctx, apiCall, origin, values...) -} diff --git a/pkg/event/instrumenting_test.go b/pkg/event/instrumenting_test.go deleted file mode 100644 index 2880cd2f..00000000 --- a/pkg/event/instrumenting_test.go +++ /dev/null @@ -1,159 +0,0 @@ -package event - -import ( - "context" - "math/rand" - "strconv" - "testing" - "time" - - cs "github.com/cloudtrust/common-service" - "github.com/cloudtrust/keycloak-bridge/api/event/fb" - "github.com/cloudtrust/keycloak-bridge/pkg/event/mock" - "github.com/golang/mock/gomock" - "github.com/stretchr/testify/assert" -) - -func TestMuxComponentInstrumentingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockMuxComponent = mock.NewMuxComponent(mockCtrl) - var mockHistogram = mock.NewHistogram(mockCtrl) - - var m = MakeMuxComponentInstrumentingMW(mockHistogram)(mockMuxComponent) - - rand.Seed(time.Now().UnixNano()) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var uid = rand.Int63() - var event = createEventBytes(fb.EventTypeCLIENT_DELETE, uid, "realm") - - // Event. - mockMuxComponent.EXPECT().Event(ctx, "Event", event).Return(nil).Times(1) - mockHistogram.EXPECT().With("correlation_id", corrID).Return(mockHistogram).Times(1) - mockHistogram.EXPECT().Observe(gomock.Any()).Return().Times(1) - m.Event(ctx, "Event", event) - - // Event without correlation ID. - mockMuxComponent.EXPECT().Event(context.Background(), "Event", event).Return(nil).Times(1) - var f = func() { - m.Event(context.Background(), "Event", event) - } - assert.Panics(t, f) -} - -func TestComponentInstrumentingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockComponent = mock.NewComponent(mockCtrl) - var mockHistogram = mock.NewHistogram(mockCtrl) - - var m = MakeComponentInstrumentingMW(mockHistogram)(mockComponent) - - rand.Seed(time.Now().UnixNano()) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var uid = rand.Int63() - var event = createEvent(fb.EventTypeCLIENT_INFO, uid, "realm") - - // Event. - mockComponent.EXPECT().Event(ctx, event).Return(nil).Times(1) - mockHistogram.EXPECT().With("correlation_id", corrID).Return(mockHistogram).Times(1) - mockHistogram.EXPECT().Observe(gomock.Any()).Return().Times(1) - m.Event(ctx, event) - - // Event without correlation ID. - mockComponent.EXPECT().Event(context.Background(), event).Return(nil).Times(1) - var f = func() { - m.Event(context.Background(), event) - } - assert.Panics(t, f) -} - -func TestAdminComponentInstrumentingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockAdminComponent = mock.NewAdminComponent(mockCtrl) - var mockHistogram = mock.NewHistogram(mockCtrl) - - var m = MakeAdminComponentInstrumentingMW(mockHistogram)(mockAdminComponent) - - rand.Seed(time.Now().UnixNano()) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var uid = rand.Int63() - var event = createAdminEvent(fb.OperationTypeCREATE, uid) - - // Event. - mockAdminComponent.EXPECT().AdminEvent(ctx, event).Return(nil).Times(1) - mockHistogram.EXPECT().With("correlation_id", corrID).Return(mockHistogram).Times(1) - mockHistogram.EXPECT().Observe(gomock.Any()).Return().Times(1) - m.AdminEvent(ctx, event) - - // Event without correlation ID. - mockAdminComponent.EXPECT().AdminEvent(context.Background(), event).Return(nil).Times(1) - var f = func() { - m.AdminEvent(context.Background(), event) - } - assert.Panics(t, f) -} - -func TestConsoleModuleInstrumentingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockConsoleModule = mock.NewConsoleModule(mockCtrl) - var mockHistogram = mock.NewHistogram(mockCtrl) - - var m = MakeConsoleModuleInstrumentingMW(mockHistogram)(mockConsoleModule) - - rand.Seed(time.Now().UnixNano()) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var mp = map[string]string{"key": "val"} - - // Print. - mockConsoleModule.EXPECT().Print(ctx, mp).Return(nil).Times(1) - mockHistogram.EXPECT().With("correlation_id", corrID).Return(mockHistogram).Times(1) - mockHistogram.EXPECT().Observe(gomock.Any()).Return().Times(1) - m.Print(ctx, mp) -} - -func TestStatisticModuleInstrumentingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockStatisticModule = mock.NewStatisticModule(mockCtrl) - var mockHistogram = mock.NewHistogram(mockCtrl) - - var m = MakeStatisticModuleInstrumentingMW(mockHistogram)(mockStatisticModule) - - rand.Seed(time.Now().UnixNano()) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var mp = map[string]string{"key": "val"} - - // Stats. - mockStatisticModule.EXPECT().Stats(ctx, mp).Return(nil).Times(1) - mockHistogram.EXPECT().With("correlation_id", corrID).Return(mockHistogram).Times(1) - mockHistogram.EXPECT().Observe(gomock.Any()).Return().Times(1) - m.Stats(ctx, mp) -} - -func TestEventsDBModuleInstrumentingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockEventsDBModule = mock.NewEventsDBModule(mockCtrl) - var mockHistogram = mock.NewHistogram(mockCtrl) - - var m = MakeEventsDBModuleInstrumentingMW(mockHistogram)(mockEventsDBModule) - - rand.Seed(time.Now().UnixNano()) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var mp = map[string]string{"key": "val"} - - // Stats. - mockEventsDBModule.EXPECT().Store(ctx, mp).Return(nil).Times(1) - mockHistogram.EXPECT().With("correlation_id", corrID).Return(mockHistogram).Times(1) - mockHistogram.EXPECT().Observe(gomock.Any()).Return().Times(1) - m.Store(ctx, mp) -} diff --git a/pkg/event/logging.go b/pkg/event/logging.go deleted file mode 100644 index 633bf9c4..00000000 --- a/pkg/event/logging.go +++ /dev/null @@ -1,162 +0,0 @@ -package event - -import ( - "context" - "time" - - cs "github.com/cloudtrust/common-service" - "github.com/cloudtrust/common-service/database" - "github.com/cloudtrust/common-service/log" - "github.com/cloudtrust/keycloak-bridge/api/event/fb" -) - -// Logging middleware for the mux component. -type muxComponentLoggingMW struct { - logger log.Logger - next MuxComponent -} - -// MakeMuxComponentLoggingMW makes a logging middleware for the mux component. -func MakeMuxComponentLoggingMW(log log.Logger) func(MuxComponent) MuxComponent { - return func(next MuxComponent) MuxComponent { - return &muxComponentLoggingMW{ - logger: log, - next: next, - } - } -} - -// muxComponentLoggingMW implements MuxComponent. -func (m *muxComponentLoggingMW) Event(ctx context.Context, eventType string, obj []byte) error { - defer func(begin time.Time) { - m.logger.Info(ctx, "unit", "Event", "type", eventType, "correlation_id", ctx.Value(cs.CtContextCorrelationID).(string), "took", time.Since(begin)) - }(time.Now()) - return m.next.Event(ctx, eventType, obj) -} - -// Logging middleware for the event component. -type componentLoggingMW struct { - logger log.Logger - next Component -} - -// MakeComponentLoggingMW makes a logging middleware for the event component. -func MakeComponentLoggingMW(log log.Logger) func(Component) Component { - return func(next Component) Component { - return &componentLoggingMW{ - logger: log, - next: next, - } - } -} - -// componentLoggingMW implements Component. -func (m *componentLoggingMW) Event(ctx context.Context, event *fb.Event) error { - defer func(begin time.Time) { - m.logger.Info(ctx, "unit", "Event", "correlation_id", ctx.Value(cs.CtContextCorrelationID).(string), "took", time.Since(begin)) - }(time.Now()) - return m.next.Event(ctx, event) -} - -// Logging middleware for the admin event component. -type adminComponentLoggingMW struct { - logger log.Logger - next AdminComponent -} - -// MakeAdminComponentLoggingMW makes a logging middleware for the admin event component. -func MakeAdminComponentLoggingMW(log log.Logger) func(AdminComponent) AdminComponent { - return func(next AdminComponent) AdminComponent { - return &adminComponentLoggingMW{ - logger: log, - next: next, - } - } -} - -// adminComponentLoggingMW implements AdminComponent. -func (m *adminComponentLoggingMW) AdminEvent(ctx context.Context, adminEvent *fb.AdminEvent) error { - defer func(begin time.Time) { - m.logger.Info(ctx, "unit", "AdminEvent", "correlation_id", ctx.Value(cs.CtContextCorrelationID).(string), "took", time.Since(begin)) - }(time.Now()) - return m.next.AdminEvent(ctx, adminEvent) -} - -// Logging middleware for the console module. -type consoleModuleLoggingMW struct { - logger log.Logger - next ConsoleModule -} - -// MakeConsoleModuleLoggingMW makes a logging middleware for the console module. -func MakeConsoleModuleLoggingMW(log log.Logger) func(ConsoleModule) ConsoleModule { - return func(next ConsoleModule) ConsoleModule { - return &consoleModuleLoggingMW{ - logger: log, - next: next, - } - } -} - -// consoleModuleLoggingMW implements ConsoleModule. -func (m *consoleModuleLoggingMW) Print(ctx context.Context, mp map[string]string) error { - defer func(begin time.Time) { - m.logger.Debug(ctx, "method", "Print", "args", mp, "took", time.Since(begin)) - }(time.Now()) - return m.next.Print(ctx, mp) -} - -// Logging middleware for the statistic module. -type statisticModuleLoggingMW struct { - logger log.Logger - next StatisticModule -} - -// MakeStatisticModuleLoggingMW makes a logging middleware for the statistic module. -func MakeStatisticModuleLoggingMW(log log.Logger) func(StatisticModule) StatisticModule { - return func(next StatisticModule) StatisticModule { - return &statisticModuleLoggingMW{ - logger: log, - next: next, - } - } -} - -// statisticModuleLoggingMW implements StatisticModule. -func (m *statisticModuleLoggingMW) Stats(ctx context.Context, mp map[string]string) error { - defer func(begin time.Time) { - m.logger.Debug(ctx, "method", "Stats", "args", mp, "took", time.Since(begin)) - }(time.Now()) - return m.next.Stats(ctx, mp) -} - -// Logging middleware for the statistic module. -type eventsDBModuleLoggingMW struct { - logger log.Logger - next database.EventsDBModule -} - -// MakeEventsDBModuleLoggingMW makes a logging middleware for the statistic module. -func MakeEventsDBModuleLoggingMW(log log.Logger) func(database.EventsDBModule) database.EventsDBModule { - return func(next database.EventsDBModule) database.EventsDBModule { - return &eventsDBModuleLoggingMW{ - logger: log, - next: next, - } - } -} - -// statisticModuleLoggingMW implements StatisticModule. -func (m *eventsDBModuleLoggingMW) Store(ctx context.Context, mp map[string]string) error { - defer func(begin time.Time) { - m.logger.Debug(ctx, "method", "Store", "args", mp, "took", time.Since(begin)) - }(time.Now()) - return m.next.Store(ctx, mp) -} - -func (m *eventsDBModuleLoggingMW) ReportEvent(ctx context.Context, apiCall string, origin string, values ...string) error { - defer func(begin time.Time) { - m.logger.Info(ctx, "method", "ReportEvent", "args", apiCall, origin, values, "took", time.Since(begin)) - }(time.Now()) - return m.next.ReportEvent(ctx, apiCall, origin, values...) -} diff --git a/pkg/event/logging_test.go b/pkg/event/logging_test.go deleted file mode 100644 index 8b54c281..00000000 --- a/pkg/event/logging_test.go +++ /dev/null @@ -1,152 +0,0 @@ -package event - -import ( - "context" - "math/rand" - "strconv" - "testing" - "time" - - cs "github.com/cloudtrust/common-service" - "github.com/cloudtrust/keycloak-bridge/api/event/fb" - "github.com/cloudtrust/keycloak-bridge/pkg/event/mock" - "github.com/golang/mock/gomock" - "github.com/stretchr/testify/assert" -) - -func TestMuxComponentLoggingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockMuxComponent = mock.NewMuxComponent(mockCtrl) - var mockLogger = mock.NewLogger(mockCtrl) - - var m = MakeMuxComponentLoggingMW(mockLogger)(mockMuxComponent) - - rand.Seed(time.Now().UnixNano()) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var uid = rand.Int63() - var event = createEventBytes(fb.EventTypeCLIENT_DELETE, uid, "realm") - - // Event. - mockMuxComponent.EXPECT().Event(ctx, "Event", event).Return(nil).Times(1) - mockLogger.EXPECT().Info(ctx, "unit", "Event", "type", "Event", "correlation_id", corrID, "took", gomock.Any()).Times(1) - m.Event(ctx, "Event", event) - - // Event without correlation ID. - mockMuxComponent.EXPECT().Event(context.Background(), "Event", event).Return(nil).Times(1) - var f = func() { - m.Event(context.Background(), "Event", event) - } - assert.Panics(t, f) -} - -func TestComponentLoggingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockComponent = mock.NewComponent(mockCtrl) - var mockLogger = mock.NewLogger(mockCtrl) - - var m = MakeComponentLoggingMW(mockLogger)(mockComponent) - - rand.Seed(time.Now().UnixNano()) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var uid = rand.Int63() - var event = createEvent(fb.EventTypeCLIENT_INFO, uid, "realm") - - // Event. - mockComponent.EXPECT().Event(ctx, event).Return(nil).Times(1) - mockLogger.EXPECT().Info(ctx, "unit", "Event", "correlation_id", corrID, "took", gomock.Any()).Times(1) - m.Event(ctx, event) - - // Event without correlation ID. - mockComponent.EXPECT().Event(context.Background(), event).Return(nil).Times(1) - var f = func() { - m.Event(context.Background(), event) - } - assert.Panics(t, f) -} - -func TestAdminComponentLoggingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockAdminComponent = mock.NewAdminComponent(mockCtrl) - var mockLogger = mock.NewLogger(mockCtrl) - - var m = MakeAdminComponentLoggingMW(mockLogger)(mockAdminComponent) - - rand.Seed(time.Now().UnixNano()) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var uid = rand.Int63() - var event = createAdminEvent(fb.OperationTypeCREATE, uid) - - // Event. - mockAdminComponent.EXPECT().AdminEvent(ctx, event).Return(nil).Times(1) - mockLogger.EXPECT().Info(ctx, "unit", "AdminEvent", "correlation_id", corrID, "took", gomock.Any()).Times(1) - m.AdminEvent(ctx, event) - - // Event without correlation ID. - mockAdminComponent.EXPECT().AdminEvent(context.Background(), event).Return(nil).Times(1) - var f = func() { - m.AdminEvent(context.Background(), event) - } - assert.Panics(t, f) -} - -func TestConsoleModuleLoggingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockConsoleModule = mock.NewConsoleModule(mockCtrl) - var mockLogger = mock.NewLogger(mockCtrl) - - var m = MakeConsoleModuleLoggingMW(mockLogger)(mockConsoleModule) - - rand.Seed(time.Now().UnixNano()) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var mp = map[string]string{"key": "val"} - - // Print. - mockConsoleModule.EXPECT().Print(ctx, mp).Return(nil).Times(1) - mockLogger.EXPECT().Debug(ctx, "method", "Print", "args", mp, "took", gomock.Any()).Times(1) - m.Print(ctx, mp) -} - -func TestStatisticModuleLoggingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockStatisticModule = mock.NewStatisticModule(mockCtrl) - var mockLogger = mock.NewLogger(mockCtrl) - - var m = MakeStatisticModuleLoggingMW(mockLogger)(mockStatisticModule) - - rand.Seed(time.Now().UnixNano()) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var mp = map[string]string{"key": "val"} - - // Stats. - mockStatisticModule.EXPECT().Stats(ctx, mp).Return(nil).Times(1) - mockLogger.EXPECT().Debug(ctx, "method", "Stats", "args", mp, "took", gomock.Any()).Times(1) - m.Stats(ctx, mp) -} - -func TestEventsDBModuleLoggingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockEventsDBModule = mock.NewEventsDBModule(mockCtrl) - var mockLogger = mock.NewLogger(mockCtrl) - - var m = MakeEventsDBModuleLoggingMW(mockLogger)(mockEventsDBModule) - - var corrID = "store-corrid-123456789" - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var mp = map[string]string{"key": "val"} - - // Store events. - mockEventsDBModule.EXPECT().Store(ctx, mp).Return(nil).Times(1) - mockLogger.EXPECT().Debug(ctx, "method", "Store", "args", mp, "took", gomock.Any()).Times(1) - m.Store(ctx, mp) -} diff --git a/pkg/event/mock_test.go b/pkg/event/mock_test.go deleted file mode 100644 index d462eabe..00000000 --- a/pkg/event/mock_test.go +++ /dev/null @@ -1,8 +0,0 @@ -package event - -//go:generate mockgen -destination=./mock/event.go -package=mock -mock_names=MuxComponent=MuxComponent,Component=Component,AdminComponent=AdminComponent,ConsoleModule=ConsoleModule,StatisticModule=StatisticModule github.com/cloudtrust/keycloak-bridge/pkg/event MuxComponent,Component,AdminComponent,ConsoleModule,StatisticModule -//go:generate mockgen -destination=./mock/dbmodule.go -package=mock -mock_names=EventsDBModule=EventsDBModule github.com/cloudtrust/common-service/database EventsDBModule -//go:generate mockgen -destination=./mock/instrumenting.go -package=mock -mock_names=Histogram=Histogram,Metrics=Metrics github.com/cloudtrust/common-service/metrics Histogram,Metrics -//go:generate mockgen -destination=./mock/logging.go -package=mock -mock_names=Logger=Logger github.com/cloudtrust/common-service/log Logger -//go:generate mockgen -destination=./mock/tracing.go -package=mock -mock_names=OpentracingClient=OpentracingClient,Finisher=Finisher github.com/cloudtrust/common-service/tracing OpentracingClient,Finisher -//go:generate mockgen -destination=./mock/tracking.go -package=mock -mock_names=SentryTracking=SentryTracking github.com/cloudtrust/common-service/tracking SentryTracking diff --git a/pkg/event/module.go b/pkg/event/module.go deleted file mode 100644 index 62a61478..00000000 --- a/pkg/event/module.go +++ /dev/null @@ -1,64 +0,0 @@ -package event - -import ( - "context" - - "github.com/cloudtrust/common-service/log" - "github.com/cloudtrust/common-service/metrics" - influx "github.com/influxdata/influxdb/client/v2" -) - -// ConsoleModule is the interface of the console module. -type ConsoleModule interface { - Print(context.Context, map[string]string) error -} - -type consoleModule struct { - logger log.Logger -} - -// NewConsoleModule returns a Console module. -func NewConsoleModule(logger log.Logger) ConsoleModule { - return &consoleModule{ - logger: logger, - } -} - -func (cm *consoleModule) Print(ctx context.Context, m map[string]string) error { - // Log - for k, v := range m { - cm.logger.Info(ctx, k, v) - } - return nil -} - -// StatisticModule is the interface of the keycloak statistic module. -type StatisticModule interface { - Stats(context.Context, map[string]string) error -} - -// Influx is the influx DB interface. -type Influx interface { - Write(bp influx.BatchPoints) error - Close() -} - -type statisticModule struct { - influx metrics.Metrics -} - -//NewStatisticModule returns a Statistic module. -func NewStatisticModule(influx metrics.Metrics) StatisticModule { - return &statisticModule{ - influx: influx, - } -} - -func (sm *statisticModule) Stats(ctx context.Context, m map[string]string) error { - // Create a point and add to batch - var tags = map[string]string{"type": m["type"], "realm": m["realmId"], "userId": m["userId"]} - var fields = map[string]interface{}{ - "uid": m["uid"], - } - return sm.influx.Stats(ctx, "event_statistics", tags, fields) -} diff --git a/pkg/event/module_test.go b/pkg/event/module_test.go deleted file mode 100644 index 5dcedad1..00000000 --- a/pkg/event/module_test.go +++ /dev/null @@ -1,42 +0,0 @@ -package event - -import ( - "context" - "testing" - - "github.com/cloudtrust/keycloak-bridge/pkg/event/mock" - "github.com/cloudtrust/common-service/log" - "github.com/golang/mock/gomock" - "github.com/stretchr/testify/assert" -) - -func TestConsoleModule(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockLogger = log.NewNopLogger() - - var ( - uid = "uid" - m = map[string]string{ - "uid": uid, - "time": "123314", - "componentName": "component_name", - "componentID": "component_id", - } - ) - var consoleModule = NewConsoleModule(mockLogger) - - var err = consoleModule.Print(context.Background(), m) - assert.Nil(t, err) -} - -func TestStatisticsModule(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockInflux = mock.NewMetrics(mockCtrl) - - var statisticModule = NewStatisticModule(mockInflux) - mockInflux.EXPECT().Stats(gomock.Any(), "event_statistics", gomock.Any(), gomock.Any()).Times(1) - var err = statisticModule.Stats(context.Background(), map[string]string{"type": "val"}) - assert.Nil(t, err) -} diff --git a/pkg/event/tracing.go b/pkg/event/tracing.go deleted file mode 100644 index c75f8b10..00000000 --- a/pkg/event/tracing.go +++ /dev/null @@ -1,183 +0,0 @@ -package event - -import ( - "context" - - cs "github.com/cloudtrust/common-service" - "github.com/cloudtrust/common-service/database" - "github.com/cloudtrust/common-service/tracing" - "github.com/cloudtrust/keycloak-bridge/api/event/fb" -) - -// Tracing middleware at component level. -type muxComponentTracingMW struct { - tracer tracing.OpentracingClient - next MuxComponent -} - -// MakeMuxComponentTracingMW makes a tracing middleware at component level. -func MakeMuxComponentTracingMW(tracer tracing.OpentracingClient) func(MuxComponent) MuxComponent { - return func(next MuxComponent) MuxComponent { - return &muxComponentTracingMW{ - tracer: tracer, - next: next, - } - } -} - -// muxComponentTracingMW implements MuxComponent. -func (m *muxComponentTracingMW) Event(ctx context.Context, eventType string, obj []byte) error { - var f tracing.Finisher - ctx, f = m.tracer.TryStartSpanWithTag(ctx, "mux_component", KeyCorrelationID, ctx.Value(cs.CtContextCorrelationID).(string)) - if f != nil { - defer f.Finish() - } - - return m.next.Event(ctx, eventType, obj) -} - -// Tracing middleware at component level. -type componentTracingMW struct { - tracer tracing.OpentracingClient - next Component -} - -// MakeComponentTracingMW makes a tracing middleware at component level. -func MakeComponentTracingMW(tracer tracing.OpentracingClient) func(Component) Component { - return func(next Component) Component { - return &componentTracingMW{ - tracer: tracer, - next: next, - } - } -} - -// componentTracingMW implements Component. -func (m *componentTracingMW) Event(ctx context.Context, event *fb.Event) error { - var f tracing.Finisher - ctx, f = m.tracer.TryStartSpanWithTag(ctx, "event_component", KeyCorrelationID, ctx.Value(cs.CtContextCorrelationID).(string)) - if f != nil { - defer f.Finish() - } - - return m.next.Event(ctx, event) -} - -// Tracing middleware at component level. -type adminComponentTracingMW struct { - tracer tracing.OpentracingClient - next AdminComponent -} - -// MakeAdminComponentTracingMW makes a tracing middleware at component level. -func MakeAdminComponentTracingMW(tracer tracing.OpentracingClient) func(AdminComponent) AdminComponent { - return func(next AdminComponent) AdminComponent { - return &adminComponentTracingMW{ - tracer: tracer, - next: next, - } - } -} - -// adminComponentTracingMW implements Component. -func (m *adminComponentTracingMW) AdminEvent(ctx context.Context, adminEvent *fb.AdminEvent) error { - var f tracing.Finisher - ctx, f = m.tracer.TryStartSpanWithTag(ctx, "admin_event_component", KeyCorrelationID, ctx.Value(cs.CtContextCorrelationID).(string)) - if f != nil { - defer f.Finish() - } - - return m.next.AdminEvent(ctx, adminEvent) -} - -// Tracing middleware at module level. -type consoleModuleTracingMW struct { - tracer tracing.OpentracingClient - next ConsoleModule -} - -// MakeConsoleModuleTracingMW makes a tracing middleware at component level. -func MakeConsoleModuleTracingMW(tracer tracing.OpentracingClient) func(ConsoleModule) ConsoleModule { - return func(next ConsoleModule) ConsoleModule { - return &consoleModuleTracingMW{ - tracer: tracer, - next: next, - } - } -} - -// consoleModuleTracingMW implements ConsoleModule. -func (m *consoleModuleTracingMW) Print(ctx context.Context, mp map[string]string) error { - var f tracing.Finisher - ctx, f = m.tracer.TryStartSpanWithTag(ctx, "console_module", KeyCorrelationID, ctx.Value(cs.CtContextCorrelationID).(string)) - if f != nil { - defer f.Finish() - } - - return m.next.Print(ctx, mp) -} - -// Tracing middleware at module level. -type statisticModuleTracingMW struct { - tracer tracing.OpentracingClient - next StatisticModule -} - -// MakeStatisticModuleTracingMW makes a tracing middleware at component level. -func MakeStatisticModuleTracingMW(tracer tracing.OpentracingClient) func(StatisticModule) StatisticModule { - return func(next StatisticModule) StatisticModule { - return &statisticModuleTracingMW{ - tracer: tracer, - next: next, - } - } -} - -// statisticModuleTracingMW implements StatisticModule. -func (m *statisticModuleTracingMW) Stats(ctx context.Context, mp map[string]string) error { - var f tracing.Finisher - ctx, f = m.tracer.TryStartSpanWithTag(ctx, "statistic_module", KeyCorrelationID, ctx.Value(cs.CtContextCorrelationID).(string)) - if f != nil { - defer f.Finish() - } - - return m.next.Stats(ctx, mp) -} - -// Tracing middleware at module level. -type eventsDBModuleTracingMW struct { - tracer tracing.OpentracingClient - next database.EventsDBModule -} - -// MakeEventsDBModuleTracingMW makes a tracing middleware at component level. -func MakeEventsDBModuleTracingMW(tracer tracing.OpentracingClient) func(database.EventsDBModule) database.EventsDBModule { - return func(next database.EventsDBModule) database.EventsDBModule { - return &eventsDBModuleTracingMW{ - tracer: tracer, - next: next, - } - } -} - -// statisticModuleTracingMW implements StatisticModule. -func (m *eventsDBModuleTracingMW) Store(ctx context.Context, mp map[string]string) error { - var f tracing.Finisher - ctx, f = m.tracer.TryStartSpanWithTag(ctx, "eventsDB_module", KeyCorrelationID, ctx.Value(cs.CtContextCorrelationID).(string)) - if f != nil { - defer f.Finish() - } - - return m.next.Store(ctx, mp) -} - -// statisticModuleTracingMW implements StatisticModule. -func (m *eventsDBModuleTracingMW) ReportEvent(ctx context.Context, apiCall string, origin string, values ...string) error { - var f tracing.Finisher - ctx, f = m.tracer.TryStartSpanWithTag(ctx, "eventsDB_module", KeyCorrelationID, ctx.Value(cs.CtContextCorrelationID).(string)) - if f != nil { - defer f.Finish() - } - - return m.next.ReportEvent(ctx, apiCall, origin, values...) -} diff --git a/pkg/event/tracing_test.go b/pkg/event/tracing_test.go deleted file mode 100644 index f6a61861..00000000 --- a/pkg/event/tracing_test.go +++ /dev/null @@ -1,164 +0,0 @@ -package event - -import ( - "context" - "math/rand" - "strconv" - "testing" - - cs "github.com/cloudtrust/common-service" - "github.com/cloudtrust/keycloak-bridge/api/event/fb" - "github.com/cloudtrust/keycloak-bridge/pkg/event/mock" - "github.com/golang/mock/gomock" -) - -func TestMuxComponentTracingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockMuxComponent = mock.NewMuxComponent(mockCtrl) - var mockTracer = mock.NewOpentracingClient(mockCtrl) - var mockFinisher = mock.NewFinisher(mockCtrl) - - var m = MakeMuxComponentTracingMW(mockTracer)(mockMuxComponent) - //ctx = opentracing.ContextWithSpan(ctx, mockSpan) - //var uid = rand.Int63() - //var event = createEventBytes(fb.EventTypeCLIENT_DELETE, uid, "realm") - var corrID = "123-456-789" - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - - // Event / Spawn - mockMuxComponent.EXPECT().Event(gomock.Any(), "Event", gomock.Any()).Return(nil).Times(1) - mockTracer.EXPECT().TryStartSpanWithTag(ctx, "mux_component", "correlation_id", corrID).Return(ctx, mockFinisher).Times(1) - mockFinisher.EXPECT().Finish().Times(1) - m.Event(ctx, "Event", []byte{}) - - // Event / Not spawn - mockMuxComponent.EXPECT().Event(gomock.Any(), "Event", gomock.Any()).Return(nil).Times(1) - mockTracer.EXPECT().TryStartSpanWithTag(ctx, "mux_component", "correlation_id", corrID).Return(ctx, nil).Times(1) - mockFinisher.EXPECT().Finish().Times(0) - m.Event(ctx, "Event", []byte{}) -} - -func TestComponentTracingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockComponent = mock.NewComponent(mockCtrl) - var mockTracer = mock.NewOpentracingClient(mockCtrl) - var mockFinisher = mock.NewFinisher(mockCtrl) - - var m = MakeComponentTracingMW(mockTracer)(mockComponent) - //ctx = opentracing.ContextWithSpan(ctx, mockSpan) - //var uid = rand.Int63() - //var event = createEvent(fb.EventTypeCLIENT_INFO, uid, "realm") - var corrID = "456-789-123" - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - - // Event. - mockComponent.EXPECT().Event(gomock.Any(), gomock.Any()).Return(nil).Times(1) - mockTracer.EXPECT().TryStartSpanWithTag(ctx, "event_component", "correlation_id", corrID).Return(ctx, mockFinisher).Times(1) - mockFinisher.EXPECT().Finish().Times(1) - m.Event(ctx, &fb.Event{}) -} - -func TestAdminComponentTracingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockAdminComponent = mock.NewAdminComponent(mockCtrl) - var mockTracer = mock.NewOpentracingClient(mockCtrl) - var mockFinisher = mock.NewFinisher(mockCtrl) - - var m = MakeAdminComponentTracingMW(mockTracer)(mockAdminComponent) - - //ctx = opentracing.ContextWithSpan(ctx, mockSpan) - //var uid = rand.Int63() - //var event = createAdminEvent(fb.OperationTypeCREATE, uid) - var corrID = "789-123-456" - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var event = &fb.AdminEvent{} - - // Spawn - mockAdminComponent.EXPECT().AdminEvent(gomock.Any(), event).Return(nil).Times(1) - mockTracer.EXPECT().TryStartSpanWithTag(ctx, "admin_event_component", "correlation_id", corrID).Return(ctx, mockFinisher).Times(1) - mockFinisher.EXPECT().Finish().Times(1) - m.AdminEvent(ctx, event) - - // Not spawn - mockAdminComponent.EXPECT().AdminEvent(gomock.Any(), event).Return(nil).Times(1) - mockTracer.EXPECT().TryStartSpanWithTag(ctx, "admin_event_component", "correlation_id", corrID).Return(ctx, nil).Times(1) - m.AdminEvent(ctx, event) -} - -func TestConsoleModuleTracingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockConsoleModule = mock.NewConsoleModule(mockCtrl) - var mockTracer = mock.NewOpentracingClient(mockCtrl) - var mockFinisher = mock.NewFinisher(mockCtrl) - - var m = MakeConsoleModuleTracingMW(mockTracer)(mockConsoleModule) - - //ctx = opentracing.ContextWithSpan(ctx, mockSpan) - var corrID = "987-654-321" - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var mp = map[string]string{"key": "val"} - - // Spawn - mockConsoleModule.EXPECT().Print(gomock.Any(), mp).Return(nil).Times(1) - mockTracer.EXPECT().TryStartSpanWithTag(ctx, "console_module", "correlation_id", corrID).Return(ctx, mockFinisher).Times(1) - mockFinisher.EXPECT().Finish().Times(1) - m.Print(ctx, mp) - - // Not spawn - mockConsoleModule.EXPECT().Print(gomock.Any(), mp).Return(nil).Times(1) - mockTracer.EXPECT().TryStartSpanWithTag(ctx, "console_module", "correlation_id", corrID).Return(ctx, nil).Times(1) - m.Print(ctx, mp) -} - -func TestStatisticModuleTracingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockStatisticModule = mock.NewStatisticModule(mockCtrl) - var mockTracer = mock.NewOpentracingClient(mockCtrl) - var mockFinisher = mock.NewFinisher(mockCtrl) - - var m = MakeStatisticModuleTracingMW(mockTracer)(mockStatisticModule) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - //ctx = opentracing.ContextWithSpan(ctx, mockSpan) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var mp = map[string]string{"key": "val"} - - // Spawn - mockStatisticModule.EXPECT().Stats(gomock.Any(), mp).Return(nil).Times(1) - mockTracer.EXPECT().TryStartSpanWithTag(ctx, "statistic_module", "correlation_id", corrID).Return(ctx, mockFinisher).Times(1) - mockFinisher.EXPECT().Finish().Times(1) - m.Stats(ctx, mp) - - // Not spawn - mockStatisticModule.EXPECT().Stats(gomock.Any(), mp).Return(nil).Times(1) - mockTracer.EXPECT().TryStartSpanWithTag(ctx, "statistic_module", "correlation_id", corrID).Return(ctx, nil).Times(1) - m.Stats(ctx, mp) -} - -func TestEventsDBModuleTracingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockEventsDBModule = mock.NewEventsDBModule(mockCtrl) - var mockTracer = mock.NewOpentracingClient(mockCtrl) - var mockFinisher = mock.NewFinisher(mockCtrl) - - var m = MakeEventsDBModuleTracingMW(mockTracer)(mockEventsDBModule) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var mp = map[string]string{"key": "val"} - - // Spawn - mockEventsDBModule.EXPECT().Store(gomock.Any(), mp).Return(nil).Times(1) - mockTracer.EXPECT().TryStartSpanWithTag(ctx, "eventsDB_module", "correlation_id", corrID).Return(ctx, mockFinisher).Times(1) - mockFinisher.EXPECT().Finish().Times(1) - m.Store(ctx, mp) - - // Not spawn - mockEventsDBModule.EXPECT().Store(gomock.Any(), mp).Return(nil).Times(1) - mockTracer.EXPECT().TryStartSpanWithTag(ctx, "eventsDB_module", "correlation_id", corrID).Return(ctx, nil).Times(1) - m.Store(ctx, mp) -} diff --git a/pkg/event/tracking.go b/pkg/event/tracking.go deleted file mode 100644 index 1fd6b895..00000000 --- a/pkg/event/tracking.go +++ /dev/null @@ -1,47 +0,0 @@ -package event - -import ( - "context" - "encoding/base64" - - cs "github.com/cloudtrust/common-service" - "github.com/cloudtrust/common-service/log" - "github.com/cloudtrust/common-service/tracking" -) - -// Tracking middleware at component level. -type trackingMuxComponentMW struct { - sentry tracking.SentryTracking - logger log.Logger - next MuxComponent -} - -// MakeMuxComponentTrackingMW makes an error tracking middleware, where the errors are sent to Sentry. -func MakeMuxComponentTrackingMW(sentry tracking.SentryTracking, logger log.Logger) func(MuxComponent) MuxComponent { - return func(next MuxComponent) MuxComponent { - return &trackingMuxComponentMW{ - sentry: sentry, - logger: logger, - next: next, - } - } -} - -// trackingComponentMW implements MuxComponent. -func (m *trackingMuxComponentMW) Event(ctx context.Context, eventType string, obj []byte) error { - var err = m.next.Event(ctx, eventType, obj) - if err != nil { - var corrID = ctx.Value(cs.CtContextCorrelationID).(string) - var b64Obj = base64.StdEncoding.EncodeToString(obj) - - var tags = map[string]string{ - "correlation_id": corrID, - "event_type": eventType, - "obj": b64Obj, - } - - m.sentry.CaptureError(err, tags) - m.logger.Debug(ctx, "unit", "Event", "correlation_id", corrID, "event_type", eventType, "obj", b64Obj, "error", err.Error()) - } - return err -} diff --git a/pkg/event/tracking_test.go b/pkg/event/tracking_test.go deleted file mode 100644 index bab055d7..00000000 --- a/pkg/event/tracking_test.go +++ /dev/null @@ -1,56 +0,0 @@ -package event - -import ( - "context" - "encoding/base64" - "fmt" - "math/rand" - "strconv" - "testing" - "time" - - cs "github.com/cloudtrust/common-service" - "github.com/cloudtrust/keycloak-bridge/api/event/fb" - "github.com/cloudtrust/keycloak-bridge/pkg/event/mock" - "github.com/golang/mock/gomock" - "github.com/stretchr/testify/assert" -) - -func TestComponentTrackingMW(t *testing.T) { - var mockCtrl = gomock.NewController(t) - defer mockCtrl.Finish() - var mockMuxComponent = mock.NewMuxComponent(mockCtrl) - var mockSentry = mock.NewSentryTracking(mockCtrl) - var mockLogger = mock.NewLogger(mockCtrl) - - var m = MakeMuxComponentTrackingMW(mockSentry, mockLogger)(mockMuxComponent) - - rand.Seed(time.Now().UnixNano()) - var corrID = strconv.FormatUint(rand.Uint64(), 10) - var ctx = context.WithValue(context.Background(), cs.CtContextCorrelationID, corrID) - var uid = rand.Int63() - var event = createEventBytes(fb.EventTypeCLIENT_DELETE, uid, "realm") - - // Event. - mockMuxComponent.EXPECT().Event(ctx, "Event", event).Return(nil).Times(1) - m.Event(ctx, "Event", event) - - // Event error. - var expected = map[string]string{ - "correlation_id": corrID, - "event_type": "Event", - "obj": base64.StdEncoding.EncodeToString(event), - } - mockMuxComponent.EXPECT().Event(ctx, "Event", event).Return(fmt.Errorf("fail")).Times(1) - mockSentry.EXPECT().CaptureError(gomock.Any(), expected).Return("").Times(1) - mockLogger.EXPECT().Debug(ctx, "unit", "Event", "correlation_id", corrID, "event_type", "Event", "obj", gomock.Any(), "error", "fail").Times(1) - - m.Event(ctx, "Event", event) - - // Event without correlation ID. - mockMuxComponent.EXPECT().Event(context.Background(), "Event", event).Return(fmt.Errorf("fail")).Times(1) - var f = func() { - m.Event(context.Background(), "Event", event) - } - assert.Panics(t, f) -} diff --git a/scripts/build.sh b/scripts/build.sh index caddf931..b67a2b22 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -38,21 +38,6 @@ if [ -z ${ENV} ] || [ -z ${VERSION} ]; then exit 1 fi -# Directories flatbuffer. -FB_EVENT_DIR="./api/event" - -# Delete the old dirs. -echo "==> Removing old directories..." -rm -f bin/* -mkdir -p bin/ -rm -f "$FB_EVENT_DIR"/fb/* - -# Flatbuffers. -echo -echo "==> Flatbuffers:" -flatc --grpc --go -o "$FB_EVENT_DIR" "$FB_EVENT_DIR"/event.fbs -ls -hl "$FB_EVENT_DIR"/fb - # Build. echo echo "==> Build:"