GORM SQLChaos manipulates DML at program runtime based on gorm

GORM SQLChaos

GORM SQLChaos manipulates DML at program runtime based on gorm callbacks

Motivation

In Financial Business distributed system, account imbalance problems caused by unstable networks or human mistakes may cause serious impacts. We built Imbalance Monitor&Analysis System, so we want to create data imbalance situations between our business systems to verify if our monitor reports these imbalances timely. Also, we want this situation to be controllable and runs periodically to ensure the system works fine.

Yep, Chaos Engineering ;).

So I developed SQLChaos and embedded it into our business systems.

NOTE: if you're looking for SQL injection attack or any related tools, SQLChaos is not what you want.

Features

  • Easy to embed into your code;
  • Modify DML SQL values at program runtime;
  • Support INSERT, UPDATE SQL;

How it works

SQLChaos registers hooks on gorm Before("gorm:update") and Before("gorm:create") callbacks. It will fetch values from *Statement.Dest pointer, which is a staging store before the real operation is performed, and try to match user defined conditions and apply assigments.

Using SQLChaos

Setup

Embed SQLChaos where your gorm.DB setupped.

db, err := gorm.Open(mysql.Open(DSN), &gorm.Config{}, &sqlchaos.Config{
    DBName:     "dummy",
    RuleProvider: sqlchaos.WithSimpleHTTPRuleProvider(),
})
if err != nil {
    fmt.Fprintf(os.Stderr, "connect db failed:%v", err)
    return
}

SQLChaos provides a simple HTTP server for implementing RuleProvider to enable/disable chaos rules at program runtime. You can implement yours and replace it as your need.

SimpleHTTPRuleProvider listens at SQLCHAOS_HTTP.

> SQLCHAOS_HTTP=127.0.0.1:8081 ./your-program

After your program started, you will see SQLChaos enabled. And SQLChaos listens at port 8081.

Enable Chaos Rule

Try to enable a rule,

=1 AND age<50","then":"balance=1024,age=40"}' ">
export DBNAME=dummy;export TABLE=users;
# For dabase dummy table users, before INSERT Statement evaluated,
# set balance to 1024 and age to 40 for every record
# which age is greater then or equals to 1 and less then 50.
> curl -XPOST "http://127.0.0.1:8081/$DBNAME/$TABLE" -H'Content-Type: application/json' \
    -d'{"dml":"INSERT","when":"age>=1 AND age<50","then":"balance=1024,age=40"}'

After enabled, every record you created using gorm which matches age>=1 and age<50 condition, the balance will be set to 1024 and age will be 40 before it inserted into database.

To disable the table chaos rule,

> curl -XDELETE "http://127.0.0.1:8081/$DBNAME/$TABLE" 

For more practical examples, please check ./example.

Limits

  • gorm Save may not envoke hooks, so SQLChaos only be called in Create, Update, Updates;
  • when condition supports AND only, and operators like = < <= > >= are supported;
  • Only read values from gorm Statement.Dest which is basically same as the argument you pass to Create Update Updates functions. Ensure values which when needed are present where you call on Create, Update, Updates. Record will not be matched if values when required are absent.
Owner
Similar Resources

A fast data generator that's multi-table aware and supports multi-row DML.

A fast data generator that's multi-table aware and supports multi-row DML.

If you need to generate a lot of random data for your database tables but don't want to spend hours configuring a custom tool for the job, then datage

Dec 26, 2022

Identify containers at runtime and observe them. No container runtime required. Read only access to the kernel.

Linux Telemetry The Double Slit Experiment Taken from an interesting physics anomaly where the behavior of a physical system mutates simply by being o

Sep 18, 2022

golang-runtime-di is a framework for runtime dependency injection in go

golang-runtime-di description golang-runtime-di is a framework for runtime dependency injection in go. usage quickstart add it to your go.mod: go get

Aug 1, 2022

Simple to do list API with Gin and Gorm (with Postgres)Simple to do list API with Gin and Gorm (with Postgres)

go-todo Simple to do list API with Gin and Gorm (with Postgres) Docker Clone this repository and run: docker-compose up You can then hit the followin

Aug 29, 2022

Gorm-sharding - High performance table sharding plugin for Gorm

Gorm Sharding This project has moved to Gorm offical organization: https://githu

Nov 18, 2022

A program to build, run, and restart a Go program on code change

devrun A program to build, run, and restart a Go program on code change. It also supports watching all your Go imports too. So if you change the code

Apr 4, 2022

Open Source runtime scanner for Linux containers (LXD), It performs security audit checks based on CIS Linux containers Benchmark specification

Open Source runtime scanner for Linux containers (LXD), It performs security audit checks based on CIS Linux containers  Benchmark specification

lxd-probe Scan your Linux container runtime !! Lxd-Probe is an open source audit scanner who perform audit check on a linux container manager and outp

Dec 26, 2022

Write controller-runtime based k8s controllers that read/write to git, not k8s

Git Backed Controller The basic idea is to write a k8s controller that runs against git and not k8s apiserver. So the controller is reading and writin

Dec 10, 2021

Open Source runtime scanner for OpenShift cluster and perform security audit checks based on CIS RedHat OpenShift Benchmark specification

Open Source runtime scanner for OpenShift cluster and perform security audit checks based on CIS RedHat OpenShift Benchmark specification

OpenShift-Ordeal Scan your Openshift cluster !! OpenShift-Ordeal is an open source audit scanner who perform audit check on OpenShift Cluster and outp

Sep 6, 2022

RBAC scaffolding based on Gin + Gorm+ Casbin + Wire

RBAC scaffolding based on Gin + Gorm+ Casbin + Wire

Gin Admin 基于 GIN + GORM + CASBIN + WIRE 实现的RBAC权限管理脚手架,目的是提供一套轻量的中后台开发框架,方便、快速的完成业务需求的开发。 特性 遵循 RESTful API 设计规范 & 基于接口的编程规范 基于 GIN 框架,提供了丰富的中间件支持(JWT

Dec 28, 2022

Golang based User creation and Management application. GORM, Fiber, JWT

User Creation and Management app (BACK-END) Auth Features: Create Clients (regular password + 6 one-time passwords + (optional) QR code and Secret for

Dec 2, 2022

A ToDoList Demo based on Vue+Gin+Gorm+mysql

A ToDoList Demo based on Vue+Gin+Gorm+mysql

bubble清单 一个基于gin+gorm开发的练手小项目,通过该项目可初识go web开发该有的姿势。 前端页面基于vue和ElementUI开发,对前端不熟悉的童鞋可直接下载templates和static文件夹下的内容使用。 使用指南 下载 [email protected]:mao888/GoWe

Mar 16, 2022

Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.

Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.

dasel Dasel (short for data-selector) allows you to query and modify data structures using selector strings. Comparable to jq / yq, but supports JSON,

Jan 2, 2023

Strict Runtime Dependency Injection for Golang

wire Wire is runtime depedency injection/wiring for golang. It's designed to be strict to avoid your go application running without proper dependency

Sep 27, 2022

:rocket: Instant live visualization of your Go application runtime statistics (GC, MemStats, etc.) in the browser

:rocket: Instant live visualization of your Go application runtime statistics (GC, MemStats, etc.) in the browser

Statsviz Instant live visualization of your Go application runtime statistics (GC, MemStats, etc.). Import "github.com/arl/statsviz" Register statsviz

Jan 3, 2023

Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.

Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.

dasel Dasel (short for data-selector) allows you to query and modify data structures using selector strings. Comparable to jq / yq, but supports JSON,

Jan 2, 2023

OpenAPI Terraform Provider that configures itself at runtime with the resources exposed by the service provider (defined in a swagger file)

OpenAPI Terraform Provider that configures itself at runtime with the resources exposed by the service provider (defined in a swagger file)

Terraform Provider OpenAPI This terraform provider aims to minimise as much as possible the efforts needed from service providers to create and mainta

Dec 26, 2022

Golang library to act on structure fields at runtime. Similar to Python getattr(), setattr(), hasattr() APIs.

go-attr Golang library to act on structure fields at runtime. Similar to Python getattr(), setattr(), hasattr() APIs. This package provides user frien

Dec 16, 2022

Minict is a minimal container runtime written in Go.

Minict Minict is a minimal container runtime written in Go. It was made mainly for learning purposes and is intended to be as simple as possible.

Oct 31, 2022
auto generate sql from gorm model struct

gorm2sql: auto generate sql from gorm model struct A Swiss Army Knife helps you generate sql from gorm model struct. Installation go get github.com/li

Dec 22, 2022
Clickhouse support for GORM

clickhouse Clickhouse support for GORM Quick Start package main import ( "fmt" "github.com/sweetpotato0/clickhouse" "gorm.io/gorm" ) // User

Oct 18, 2022
SQLFlow is a compiler that compiles a SQL program to a workflow that runs on Kubernetes.
SQLFlow is a compiler that compiles a SQL program to a workflow that runs on Kubernetes.

SQLFlow is a compiler that compiles a SQL program to a workflow that runs on Kubernetes. The input is a SQL program that writt

Jan 9, 2023
A tool I made to quickly store bug bounty program scopes in a local sqlite3 database

GoScope A tool I made to quickly store bug bounty program scopes in a local sqlite3 database. Download or copy a Burpsuite configuration file from the

Nov 18, 2021
Podman based development-only dependency manager for Linux.

Tent is a CLI tool for running development dependencies such as MySQL, Mongo, ElasticSearch etc inside pre-configured containers using simple one

Aug 30, 2022
Query and Provision Cloud Infrastructure using an extensible SQL based grammar
Query and Provision Cloud Infrastructure using an extensible SQL based grammar

Deploy, Manage and Query Cloud Infrastructure using SQL [Documentation] [Developer Guide] Cloud infrastructure coding using SQL InfraQL allows you to

Oct 25, 2022
REST based Redis client built on top of Upstash REST API

An HTTP/REST based Redis client built on top of Upstash REST API.

Jul 31, 2022
A simple Golang-based application that queries a PostgreSQL database

Qwik-E-Mart Demo App A simple Golang-based application that queries a PostgreSQL database named qwikemart to read and return customer data stored in t

Nov 6, 2021
Web-based, zero-config, dependency-free database schema change and version control tool for teams
Web-based, zero-config, dependency-free database schema change and version control tool for teams

Live Demo • Install • Help • Development • Design Doc Bytebase is a web-based, zero-config, dependency-free database schema change and version control

Jan 1, 2023
Mogo: a lightweight browser-based logs analytics and logs search platform for some datasource(ClickHouse, MySQL, etc.)
Mogo: a lightweight browser-based logs analytics and logs search platform for some datasource(ClickHouse, MySQL, etc.)

mogo Mogo is a lightweight browser-based logs analytics and logs search platform

Dec 30, 2022