Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantBirki committed Nov 13, 2023
1 parent 1dac07c commit 19e1afa
Show file tree
Hide file tree
Showing 17 changed files with 526 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @GrantBirki
24 changes: 24 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
version: 2
updates:
- package-ecosystem: bundler
vendor: true
directory: "/"
groups:
ruby-dependencies:
patterns:
- "*"
schedule:
interval: weekly
day: "tuesday"
time: "21:00"
- package-ecosystem: github-actions
directory: "/"
groups:
github-actions:
patterns:
- "*"
schedule:
interval: weekly
day: "tuesday"
time: "21:00"
45 changes: 45 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: build

on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_call:

permissions:
contents: read

jobs:
build:
name: build

strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}

steps:
- name: checkout
uses: actions/checkout@v4

- uses: ruby/setup-ruby@52b8784594ec115fd17094752708121dc5dabb47 # [email protected]
with:
bundler-cache: true

- name: bootstrap
run: script/bootstrap

- name: build
run: |
GEM_NAME=$(ls | grep gemspec | cut -d. -f1)
echo "Attempting to build gem $GEM_NAME..."
gem build $GEM_NAME
if [ $? -eq 0 ]; then
echo "Gem built successfully!"
else
echo "Gem build failed!"
exit 1
fi
45 changes: 45 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CodeQL

on:
push:
branches: [ main ]
# pull_request:
# # The branches below must be a subset of the branches above
# branches: [ main ]
schedule:
- cron: '15 23 * * 5'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'ruby' ]

steps:
- name: checkout
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
26 changes: 26 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: lint

on:
push:
branches:
- main
pull_request:

permissions:
contents: read

jobs:
lint:
name: lint
runs-on: ubuntu-latest

steps:
- name: checkout
uses: actions/checkout@v4

- uses: ruby/setup-ruby@52b8784594ec115fd17094752708121dc5dabb47 # [email protected]
with:
bundler-cache: true

- name: lint
run: bundle exec rubocop -c .rubocop.yml lib/ spec/
61 changes: 61 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: release

on:
workflow_dispatch:
push:
branches:
- main
paths:
- lib/version.rb

permissions:
contents: write
packages: write

jobs:
release:
runs-on: ubuntu-latest

steps:
- name: checkout
uses: actions/checkout@v4

- uses: ruby/setup-ruby@52b8784594ec115fd17094752708121dc5dabb47 # [email protected]
with:
bundler-cache: true

- name: bootstrap
run: script/bootstrap

- name: lint
run: bundle exec rubocop -c .rubocop.yml lib/ spec/

- name: test
run: script/test

- name: set GEM_NAME from gemspec
run: echo "GEM_NAME=$(ls | grep gemspec | cut -d. -f1)" >> $GITHUB_ENV

# builds the gem and saves the version to GITHUB_ENV
- name: build
run: echo "GEM_VERSION=$(gem build ${{ env.GEM_NAME }}.gemspec 2>&1 | grep Version | cut -d':' -f 2 | tr -d " \t\n\r")" >> $GITHUB_ENV

- name: publish to GitHub packages
run: |
export OWNER=$( echo ${{ github.repository }} | cut -d "/" -f 1 )
GEM_HOST_API_KEY=${{ secrets.GITHUB_TOKEN }} gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} ${{ env.GEM_NAME }}-${{ env.GEM_VERSION }}.gem
- name: release
uses: ncipollo/release-action@6c75be85e571768fa31b40abf38de58ba0397db5 # [email protected]
with:
artifacts: "${{ env.GEM_NAME }}-${{ env.GEM_VERSION }}.gem"
tag: "v${{ env.GEM_VERSION }}"
generateReleaseNotes: true

- name: publish to RubyGems
run: |
mkdir -p ~/.gem
echo -e "---\n:rubygems_api_key: ${{ secrets.RUBYGEMS_API_KEY }}" > ~/.gem/credentials
chmod 0600 ~/.gem/credentials
gem push ${{ env.GEM_NAME }}-${{ env.GEM_VERSION }}.gem
rm ~/.gem/credentials
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: test

on:
push:
branches:
- main
pull_request:

permissions:
contents: read

jobs:
test:
name: test
runs-on: ubuntu-latest

steps:
- name: checkout
uses: actions/checkout@v4

- uses: ruby/setup-ruby@52b8784594ec115fd17094752708121dc5dabb47 # [email protected]
with:
bundler-cache: true

- name: test
run: script/test -k
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.bundle
bin
coverage
logs
tmp
vendor/gems
.idea
.byebug_history
.local/
.DS_Store
.lesshst
*.pem
*.key
*.crt
*.csr
*.secret
21 changes: 21 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
inherit_gem:
rubocop-github:
- config/default.yml

AllCops:
NewCops: enable
SuggestExtensions: false
DisplayCopNames: true
TargetRubyVersion: 3.2
Exclude:
- 'bin/*'
- 'spec/acceptance/fixtures/**/*'
- 'spec/unit/fixtures/**/*'
- 'vendor/**/*'
- '.github/**/*'

Style/HashSyntax:
Enabled: false

Metrics/CyclomaticComplexity:
Max: 15
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.2.2
16 changes: 16 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem "logger", "~> 1.6"

group :development do
gem "irb", "~> 1.8"
gem "rspec", "~> 3.12"
gem "rubocop", "~> 1.56"
gem "rubocop-github", "~> 0.20"
gem "rubocop-performance", "~> 1.19"
gem "rubocop-rspec", "~> 2.24"
gem "simplecov", "~> 0.22"
gem "simplecov-erb", "~> 1.0"
end
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,40 @@
# RedactingLogger

A redacting Ruby logger to prevent the leaking of secrets via logs

## Installation 💎

You can download this Gem from either [RubyGems](https://rubygems.org/gems/RedactingLogger) or [GitHub Packages](https://github.com/GrantBirki/RedactingLogger/pkgs/rubygems/RedactingLogger)

RubyGems (Recommended):

```bash
gem install RedactingLogger
```

> RubyGems [link](https://rubygems.org/gems/RedactingLogger)
Via a Gemfile:

```ruby
# frozen_string_literal: true

source "https://rubygems.org"

gem "RedactingLogger", "~> X.X.X" # Replace X.X.X with the latest version
```

## Usage 💻

```ruby
require "RedactingLogger"

logger = RedactingLogger.new([/REDACTED_PATTERN1/, /REDACTED_PATTERN2/], STDOUT)
logger.info("This is a message with a REDACTED_PATTERN1 and REDACTED_PATTERN2 in it.")
```

This will output:

```text
I, [timestamp] INFO -- : This is a message with a [REDACTED] and [REDACTED] in it.
```
31 changes: 31 additions & 0 deletions RedactingLogger.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require_relative "lib/version"

Gem::Specification.new do |spec|
spec.name = "RedactingLogger"
spec.version = RedactingLogger::Version::VERSION
spec.authors = ["Grant Birkinbine"]
spec.email = "[email protected]"
spec.license = "MIT"

spec.summary = "A redacting Ruby logger to prevent the leaking of secrets via logs"
spec.description = <<~SPEC_DESC
A redacting Ruby logger to prevent the leaking of secrets via logs
SPEC_DESC

spec.homepage = "https://github.com/grantbirki/RedactingLogger"
spec.metadata = {
"source_code_uri" => "https://github.com/grantbirki/RedactingLogger",
"documentation_uri" => "https://github.com/grantbirki/RedactingLogger",
"bug_tracker_uri" => "https://github.com/grantbirki/RedactingLogger/issues"
}

spec.add_dependency "logger", "~> 1.6"

spec.required_ruby_version = Gem::Requirement.new(">= 3.0.0")

spec.files = %w[LICENSE README.md RedactingLogger.gemspec]
spec.files += Dir.glob("lib/**/*.rb")
spec.require_paths = ["lib"]
end
26 changes: 26 additions & 0 deletions lib/redacting_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "logger"

class RedactingLogger < Logger
def initialize(redact_patterns, *args)
super(*args)
@redact_patterns = redact_patterns
end

def add(severity, message = nil, progname = nil)
if message
@redact_patterns.each do |pattern|
message = message.to_s.gsub(pattern, '[REDACTED]')
end
end

if progname
@redact_patterns.each do |pattern|
progname = progname.to_s.gsub(pattern, '[REDACTED]')
end
end

super(severity, message, progname)
end
end
Loading

0 comments on commit 19e1afa

Please sign in to comment.