action-audit 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +17 -0
- data/CHANGELOG.md +29 -0
- data/CODE_OF_CONDUCT.md +25 -0
- data/LICENSE +21 -0
- data/README.md +232 -0
- data/Rakefile +12 -0
- data/docs/README.md +25 -0
- data/docs/api-reference.md +434 -0
- data/docs/configuration.md +230 -0
- data/docs/examples.md +609 -0
- data/docs/installation.md +114 -0
- data/docs/migration.md +571 -0
- data/docs/multi-engine.md +413 -0
- data/docs/troubleshooting.md +577 -0
- data/docs/usage.md +347 -0
- data/lib/action-audit.rb +3 -0
- data/lib/action_audit/audit_messages.rb +73 -0
- data/lib/action_audit/engine.rb +19 -0
- data/lib/action_audit/version.rb +5 -0
- data/lib/action_audit.rb +75 -0
- data/lib/generators/action_audit/install_generator.rb +25 -0
- data/lib/generators/action_audit/templates/README +25 -0
- data/lib/generators/action_audit/templates/action_audit.rb +17 -0
- data/lib/generators/action_audit/templates/audit.yml +30 -0
- data/sig/action_audit.rbs +4 -0
- metadata +146 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 15ebeba499703a1b1ac0eca27102c710d6ca91595f5b85faed6214be8f242013
|
|
4
|
+
data.tar.gz: a76826e7286f9226dafe4c6145ea426393c2dc2152a5d2602400ff32e7665c71
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e643aa00da95ca4a38f5e2ec9ec2d14a7b4239c6be6be2289c664dcf35d742b1917f21c9f24854a3835d8cd4087f3fd98a6272283c43410d2aa0195f2f89ef9b
|
|
7
|
+
data.tar.gz: a0911417abf68c6084e21a675d0e0b27ea5cf6b2898dd79eb415a9d9e70b42413635f5ea9673e13a78f7a19a211aed596b365f8cc818d40cf95bf3227e23186e
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
inherit_gem:
|
|
2
|
+
rubocop-rails-omakase: rubocop.yml
|
|
3
|
+
|
|
4
|
+
AllCops:
|
|
5
|
+
NewCops: enable
|
|
6
|
+
TargetRubyVersion: 3.1
|
|
7
|
+
|
|
8
|
+
# Gem-specific customizations
|
|
9
|
+
Metrics/BlockLength:
|
|
10
|
+
Exclude:
|
|
11
|
+
- 'spec/**/*'
|
|
12
|
+
- '*.gemspec'
|
|
13
|
+
|
|
14
|
+
Style/Documentation:
|
|
15
|
+
Exclude:
|
|
16
|
+
- 'spec/**/*'
|
|
17
|
+
- 'lib/action_audit/version.rb'
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
## [Unreleased]
|
|
2
|
+
|
|
3
|
+
## [1.0.0] - 2025-10-07
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- Initial release of ActionAudit gem
|
|
7
|
+
- Automatic auditing of controller actions via `after_action` callback
|
|
8
|
+
- YAML-based audit message configuration with `config/audit.yml` files
|
|
9
|
+
- Multi-engine support - automatically loads audit configurations from all Rails engines
|
|
10
|
+
- Parameter interpolation in audit messages using controller params (e.g., `%{id}`, `%{email}`)
|
|
11
|
+
- Customizable log formatting via `ActionAudit.log_formatter`
|
|
12
|
+
- Customizable log tagging via `ActionAudit.log_tag`
|
|
13
|
+
- Rails engine integration for automatic configuration loading
|
|
14
|
+
- Comprehensive test suite with RSpec
|
|
15
|
+
- Rails generator for easy installation (`rails generate action_audit:install`)
|
|
16
|
+
- Full documentation with examples and usage patterns
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
- **ActiveSupport::Concern** integration for easy inclusion in controllers
|
|
20
|
+
- **Nested controller path support** (e.g., `Manage::AccountsController` → `manage/accounts`)
|
|
21
|
+
- **Error handling** for interpolation failures with graceful fallbacks
|
|
22
|
+
- **Request context preservation** including Rails request_id when available
|
|
23
|
+
- **Development mode support** with automatic configuration reloading
|
|
24
|
+
- **Flexible message registry** similar to I18n for consistent audit message management
|
|
25
|
+
|
|
26
|
+
### Examples
|
|
27
|
+
- Sample audit.yml configurations for various use cases
|
|
28
|
+
- Custom log formatter examples with timestamps and user information
|
|
29
|
+
- Multi-engine setup examples
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Commitment
|
|
4
|
+
|
|
5
|
+
We are committed to providing a friendly, safe, and welcoming environment for all contributors and users of this project.
|
|
6
|
+
|
|
7
|
+
## Expected Behavior
|
|
8
|
+
|
|
9
|
+
* Be respectful and inclusive in your language and actions
|
|
10
|
+
* Accept constructive feedback gracefully
|
|
11
|
+
* Focus on what is best for the community and the project
|
|
12
|
+
* Show empathy towards other community members
|
|
13
|
+
|
|
14
|
+
## Unacceptable Behavior
|
|
15
|
+
|
|
16
|
+
* Harassment, discrimination, or abusive language of any kind
|
|
17
|
+
* Trolling, insulting comments, or personal attacks
|
|
18
|
+
* Publishing private information without permission
|
|
19
|
+
* Any conduct that could reasonably be considered inappropriate in a professional setting
|
|
20
|
+
|
|
21
|
+
## Enforcement
|
|
22
|
+
|
|
23
|
+
Instances of unacceptable behavior may be reported by contacting the project maintainer. All complaints will be reviewed and investigated fairly and promptly.
|
|
24
|
+
|
|
25
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, issues, and other contributions that are not aligned with this Code of Conduct.
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Vinay Uttam Vemparala
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# ActionAudit
|
|
2
|
+
|
|
3
|
+
A Rails gem that provides automatic auditing of controller actions across multiple Rails engines. ActionAudit works similar to `I18n` by loading audit messages from YAML files, but focuses on logging controller actions with customizable formatting and tagging while preserving Rails request context.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Automatic auditing**: Hooks into controller actions via `after_action`
|
|
8
|
+
- **YAML-based configuration**: Load audit messages from `config/audit.yml` files
|
|
9
|
+
- **Multi-engine support**: Automatically loads configurations from all Rails engines
|
|
10
|
+
- **Customizable formatting**: Configure custom log formatters and tags
|
|
11
|
+
- **Parameter interpolation**: Support for dynamic message interpolation using controller parameters
|
|
12
|
+
- **Rails integration**: Preserves request context including `request_id`
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Add this line to your application's Gemfile:
|
|
17
|
+
|
|
18
|
+
```ruby
|
|
19
|
+
gem 'action_audit'
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
And then execute:
|
|
23
|
+
|
|
24
|
+
$ bundle install
|
|
25
|
+
|
|
26
|
+
Or install it yourself as:
|
|
27
|
+
|
|
28
|
+
$ gem install action_audit
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
### 1. Include in Controllers
|
|
33
|
+
|
|
34
|
+
Include the `ActionAudit` module in any controller you want to audit:
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
class Manage::AccountsController < ApplicationController
|
|
38
|
+
include ActionAudit
|
|
39
|
+
|
|
40
|
+
def create
|
|
41
|
+
@account = Account.create!(account_params)
|
|
42
|
+
# audit_request is automatically called after this action
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def update
|
|
46
|
+
@account = Account.find(params[:id])
|
|
47
|
+
@account.update!(account_params)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def account_params
|
|
53
|
+
params.require(:account).permit(:name, :email)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### 2. Configure Audit Messages
|
|
59
|
+
|
|
60
|
+
Create a `config/audit.yml` file in your Rails application or engine:
|
|
61
|
+
|
|
62
|
+
```yaml
|
|
63
|
+
# config/audit.yml
|
|
64
|
+
manage:
|
|
65
|
+
accounts:
|
|
66
|
+
create: "Created account %{id}"
|
|
67
|
+
update: "Updated account %{id} with %{name}"
|
|
68
|
+
destroy: "Deleted account %{id}"
|
|
69
|
+
users:
|
|
70
|
+
invite: "Invited user %{email}"
|
|
71
|
+
create: "Created user %{email}"
|
|
72
|
+
|
|
73
|
+
sessions:
|
|
74
|
+
create: "User logged in with email %{email}"
|
|
75
|
+
destroy: "User logged out"
|
|
76
|
+
|
|
77
|
+
posts:
|
|
78
|
+
create: "Created post '%{title}'"
|
|
79
|
+
update: "Updated post %{id}"
|
|
80
|
+
publish: "Published post %{id}"
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 3. Customize Logging (Optional)
|
|
84
|
+
|
|
85
|
+
Create an initializer to customize the logging behavior:
|
|
86
|
+
|
|
87
|
+
```ruby
|
|
88
|
+
# config/initializers/action_audit.rb
|
|
89
|
+
|
|
90
|
+
# Add a custom tag to all audit logs
|
|
91
|
+
ActionAudit.log_tag = "AUDIT"
|
|
92
|
+
|
|
93
|
+
# Customize the log format
|
|
94
|
+
ActionAudit.log_formatter = lambda do |controller, action, message|
|
|
95
|
+
user_info = defined?(current_user) && current_user ? "User: #{current_user.email}" : "User: anonymous"
|
|
96
|
+
"[#{Time.current.iso8601}] #{controller}/#{action} | #{message} | #{user_info}"
|
|
97
|
+
end
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### 4. Multi-Engine Support
|
|
101
|
+
|
|
102
|
+
ActionAudit automatically loads `config/audit.yml` files from:
|
|
103
|
+
- Your main Rails application
|
|
104
|
+
- All mounted Rails engines
|
|
105
|
+
|
|
106
|
+
Each engine can have its own audit configuration that will be merged together.
|
|
107
|
+
|
|
108
|
+
## Configuration Options
|
|
109
|
+
|
|
110
|
+
### Log Formatter
|
|
111
|
+
|
|
112
|
+
Customize how log messages are formatted:
|
|
113
|
+
|
|
114
|
+
```ruby
|
|
115
|
+
# Simple formatter
|
|
116
|
+
ActionAudit.log_formatter = ->(controller, action, msg) do
|
|
117
|
+
"AUDIT: #{controller}/#{action} - #{msg}"
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Complex formatter with timestamp and user info
|
|
121
|
+
ActionAudit.log_formatter = lambda do |controller, action, message|
|
|
122
|
+
timestamp = Time.current.strftime("%Y-%m-%d %H:%M:%S")
|
|
123
|
+
"[#{timestamp}] AUDIT: #{controller}/#{action} - #{message}"
|
|
124
|
+
end
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Log Tag
|
|
128
|
+
|
|
129
|
+
Add a consistent tag to all audit logs:
|
|
130
|
+
|
|
131
|
+
```ruby
|
|
132
|
+
ActionAudit.log_tag = "AUDIT"
|
|
133
|
+
# or
|
|
134
|
+
ActionAudit.log_tag = "APP_AUDIT"
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Message Interpolation
|
|
138
|
+
|
|
139
|
+
Audit messages support parameter interpolation using controller params:
|
|
140
|
+
|
|
141
|
+
```yaml
|
|
142
|
+
# audit.yml
|
|
143
|
+
accounts:
|
|
144
|
+
create: "Created account %{id} for %{name}"
|
|
145
|
+
update: "Updated account %{id} - changed %{changed_fields}"
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```ruby
|
|
149
|
+
# In your controller
|
|
150
|
+
def create
|
|
151
|
+
@account = Account.create!(name: params[:name])
|
|
152
|
+
# Will log: "Created account 123 for Acme Corp"
|
|
153
|
+
# Uses params[:id] and params[:name] for interpolation
|
|
154
|
+
end
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Example Log Output
|
|
158
|
+
|
|
159
|
+
### Default Format
|
|
160
|
+
```
|
|
161
|
+
manage/accounts/create - Created account 123
|
|
162
|
+
manage/accounts/update - Updated account 123 with Acme Corp
|
|
163
|
+
sessions/create - User logged in with email john@example.com
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### With Custom Formatter and Tag
|
|
167
|
+
```
|
|
168
|
+
[AUDIT] [2025-01-15T10:30:00Z] manage/accounts/create | Created account 123 | User: john@example.com
|
|
169
|
+
[AUDIT] [2025-01-15T10:31:00Z] manage/accounts/update | Updated account 123 with Acme Corp | User: john@example.com
|
|
170
|
+
[AUDIT] [2025-01-15T10:32:00Z] sessions/destroy | User logged out | User: john@example.com
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## API Reference
|
|
174
|
+
|
|
175
|
+
### ActionAudit Module
|
|
176
|
+
|
|
177
|
+
When included in a controller, automatically adds:
|
|
178
|
+
- `after_action :audit_request` - Hooks into action completion
|
|
179
|
+
- `audit_request` - Private method that performs the logging
|
|
180
|
+
- `interpolate_message` - Private method for parameter interpolation
|
|
181
|
+
|
|
182
|
+
### ActionAudit.log_formatter
|
|
183
|
+
|
|
184
|
+
A `Proc` that receives `(controller_path, action_name, interpolated_message)` and returns a formatted string.
|
|
185
|
+
|
|
186
|
+
**Default**: `"#{controller_path}/#{action_name} - #{interpolated_message}"`
|
|
187
|
+
|
|
188
|
+
### ActionAudit.log_tag
|
|
189
|
+
|
|
190
|
+
A string tag to be used with `Rails.logger.tagged()`.
|
|
191
|
+
|
|
192
|
+
**Default**: `nil` (no tagging)
|
|
193
|
+
|
|
194
|
+
### ActionAudit::AuditMessages
|
|
195
|
+
|
|
196
|
+
Registry for audit messages with methods:
|
|
197
|
+
- `.lookup(controller_path, action_name)` - Find a message
|
|
198
|
+
- `.load_from_file(file_path)` - Load messages from YAML file
|
|
199
|
+
- `.load_from_engines` - Load from all Rails engines
|
|
200
|
+
- `.add_message(controller_path, action_name, message)` - Add a message programmatically
|
|
201
|
+
- `.clear!` - Clear all messages
|
|
202
|
+
|
|
203
|
+
## Documentation
|
|
204
|
+
|
|
205
|
+
For comprehensive documentation, see the [docs](docs/) directory:
|
|
206
|
+
|
|
207
|
+
- [Installation Guide](docs/installation.md) - Step-by-step setup instructions
|
|
208
|
+
- [Configuration Guide](docs/configuration.md) - How to configure audit messages and logging
|
|
209
|
+
- [Usage Guide](docs/usage.md) - How to use ActionAudit in your controllers
|
|
210
|
+
- [Multi-Engine Setup](docs/multi-engine.md) - Using ActionAudit across Rails engines
|
|
211
|
+
- [API Reference](docs/api-reference.md) - Complete API documentation
|
|
212
|
+
- [Examples](docs/examples.md) - Real-world usage examples
|
|
213
|
+
- [Troubleshooting](docs/troubleshooting.md) - Common issues and solutions
|
|
214
|
+
- [Migration Guide](docs/migration.md) - Migrating from other audit solutions
|
|
215
|
+
|
|
216
|
+
*Documentation generated by GitHub Copilot*
|
|
217
|
+
|
|
218
|
+
## Development
|
|
219
|
+
|
|
220
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
221
|
+
|
|
222
|
+
The project uses RuboCop Rails Omakase for code style. Run `bundle exec rubocop` to check style, or `bundle exec rake` to run both tests and RuboCop checks.
|
|
223
|
+
|
|
224
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
225
|
+
|
|
226
|
+
## Contributing
|
|
227
|
+
|
|
228
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/action_audit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/action_audit/blob/main/CODE_OF_CONDUCT.md).
|
|
229
|
+
|
|
230
|
+
## Code of Conduct
|
|
231
|
+
|
|
232
|
+
Everyone interacting in the ActionAudit project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/action_audit/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/docs/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# ActionAudit Documentation
|
|
2
|
+
|
|
3
|
+
Welcome to the ActionAudit gem documentation. This directory contains comprehensive guides on how to install, configure, and use ActionAudit in your Rails applications.
|
|
4
|
+
|
|
5
|
+
## Documentation Structure
|
|
6
|
+
|
|
7
|
+
- [**Installation Guide**](installation.md) - Step-by-step installation and setup
|
|
8
|
+
- [**Configuration Guide**](configuration.md) - How to configure audit messages and logging
|
|
9
|
+
- [**Usage Guide**](usage.md) - How to use ActionAudit in your controllers
|
|
10
|
+
- [**Multi-Engine Setup**](multi-engine.md) - Using ActionAudit across multiple Rails engines
|
|
11
|
+
- [**API Reference**](api-reference.md) - Complete API documentation
|
|
12
|
+
- [**Examples**](examples.md) - Real-world usage examples
|
|
13
|
+
- [**Troubleshooting**](troubleshooting.md) - Common issues and solutions
|
|
14
|
+
- [**Migration Guide**](migration.md) - Migrating from other audit solutions
|
|
15
|
+
|
|
16
|
+
## Quick Start
|
|
17
|
+
|
|
18
|
+
For a quick overview, check out the [Installation Guide](installation.md) and [Usage Guide](usage.md).
|
|
19
|
+
|
|
20
|
+
## Support
|
|
21
|
+
|
|
22
|
+
If you encounter any issues or have questions, please:
|
|
23
|
+
1. Check the [Troubleshooting Guide](troubleshooting.md)
|
|
24
|
+
2. Review the [API Reference](api-reference.md)
|
|
25
|
+
3. Open an issue on the [GitHub repository](https://github.com/vinayuttamvemparala/action_audit)
|