Skip to content

Commit

Permalink
Initial commit: Setting up gem and basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dmorgan-fa committed Apr 25, 2024
0 parents commit 122d6b5
Show file tree
Hide file tree
Showing 20 changed files with 409 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Ruby

on:
push:
branches:
- main

pull_request:

jobs:
build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- '3.3.0'

steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run the default task
run: bundle exec rake
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status
Gemfile.lock
Brewfile.lock.json
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
20 changes: 20 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
AllCops:
TargetRubyVersion: 3.1.0

NewCops: disable
Exclude:
- 'Brewfile'
- 'vendor/bundle/**/*' # Prevent RuboCop pulling in .rubocop.yml files from vendor directories in GitHub Actions: https://github.com/rubocop/rubocop/issues/9832

Style/StringLiteralsInInterpolation:
Enabled: true
EnforcedStyle: double_quotes

Layout/LineLength:
Max: 120

Metrics/BlockLength:
Enabled: false

Style/Documentation:
Enabled: false
1 change: 1 addition & 0 deletions Brewfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
brew 'lefthook'
18 changes: 18 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in aws_local_config_parser.gemspec
gemspec

group :development, :test do
gem 'debug'
gem 'pastel'
gem 'pry-byebug'
gem 'rake'
gem 'retest'
gem 'rspec'
gem 'rspec-core'
gem 'rspec-expectations'
gem 'rubocop'
end
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Dale Morgan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# AwsLocalConfigParser

Parse your local `"$HOME"/.aws/config` to use it within a Ruby script.

## Up and Running
1. Checkout this repo: `gh repo clone dmorgan-fa/aws_local_config_parser`
1. Run: `bin/setup`
1. Run: `bin/demo`

## Usage
See [`demo`](./bin/demo) for examples on how to use the gem.

Supports parsing and returning:
- Profiles with SSO
- Profiles with Access Key + Secret
- SSO Sessions

``` ruby
require 'aws_local_config_parser'

config = AwsLocalConfigParser.new

# Profile with SSO
my_sso_profile = config.profile('my-sso-profile-name')
my_sso_profile.sso_session
my_sso_profile.sso_account_id
my_sso_profile.sso_role_name
my_sso_profile.region

## Profile with Access Key + Secret
non_sso_profile = config.profile('my-non-sso-profile-name')
non_sso_profile.aws_access_key_id
non_sso_profile.aws_secret_access_key
non_sso_profile.region

## SSO Session
my_sso_session = config.sso_session('sso-session')
my_sso_session.sso_start_url
my_sso_session.sso_registration_scopes
my_sso_session.sso_region

## Listing all names
config.all_profiles
config.all_sso_sessions
```

## Making a commit
[Lefthook](https://github.com/evilmartians/lefthook/) has [been configured](./lefthook.yml) with pre-commit checks to:
- run `rubocop` for any `ruby` files

If for some reason it's necessary, it's possible to temporarily skip `lefthook` with: `LEFTHOOK=0 git commit`.
12 changes: 12 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

require 'rubocop/rake_task'

RuboCop::RakeTask.new

task default: %i[spec rubocop]
28 changes: 28 additions & 0 deletions aws_local_config_parser.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require_relative 'lib/aws_local_config_parser/version'

Gem::Specification.new do |spec|
spec.name = 'aws_local_config_parser'
spec.version = AwsLocalConfigParser::VERSION
spec.authors = ['Dale Morgan']
spec.email = ['[email protected]']

spec.summary = 'A simple gem to parse your local AWS config file.'
spec.description = "Parse your local '\"$HOME\"/.aws/config' file."
spec.homepage = 'https://github.com/dmorgan-fa/aws_local_config_parser'
spec.license = 'MIT'
spec.required_ruby_version = '>= 3.1.0'

spec.metadata['allowed_push_host'] = 'https://rubygems.pkg.github.com/fac'

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = spec.homepage
spec.metadata['changelog_uri'] = "#{spec.homepage}/releases"

spec.files = Dir['{lib}/**/*.rb', 'bin/*', 'LICENSE.txt', '*.md']

spec.require_paths = ['lib']

spec.add_dependency 'inifile', '~> 3'
end
13 changes: 13 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'aws_local_config_parser'
require 'debug'
require 'pry'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

require 'irb'
IRB.start(__FILE__)
62 changes: 62 additions & 0 deletions bin/demo
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env ruby

# frozen_string_literal: true

require_relative '../lib/aws_local_config_parser'

require 'debug'
require 'pastel'

config = AwsLocalConfigParser.new
pastel = Pastel.new

puts pastel.bold.blue('Profile:')

puts pastel.bold.magenta('fa-ci-stage') + pastel.bold.green(' [SSO Profile]')
stage = config.profile('fa-ci-stage')
puts "\tsso_session: #{stage.sso_session}"
puts "\tsso_account_id: #{stage.sso_account_id}"
puts "\tsso_role_name: #{stage.sso_role_name}"
puts "\tregion: #{stage.region}"

begin
puts "\n"
puts pastel.bold.magenta('component-finder-uploader') + pastel.bold.green(' [Access Key+Secret Profile]')
non_sso_profile = config.profile('component-finder-uploader')
puts "\taws_access_key_id: #{non_sso_profile.aws_access_key_id}"
puts "\taws_secret_access_key: #{non_sso_profile.aws_secret_access_key}"
puts "\tregion: #{non_sso_profile.region}"
rescue AwsLocalConfigParser::NoMatchingProfile => e
puts pastel.bold.red 'No local Access Key+Secret Profile to demo'
puts pastel.bold.red e.message.to_s
end

puts "\n"
puts pastel.bold.blue('SSO Session:')
puts pastel.bold.magenta('fa-sso')
sso_session = config.sso_session('fa-sso')
puts "\tsso_start_url: #{sso_session.sso_start_url}"
puts "\tsso_registration_scopes: #{sso_session.sso_registration_scopes}"
puts "\tsso_region: #{sso_session.sso_region}"

puts "\n"
puts pastel.bold.blue('Custom Errors:')
begin
puts config.profile('non-existant-profile')
rescue AwsLocalConfigParser::NoMatchingProfile => e
puts pastel.bold.red "🚨 #{e.message}"
end

begin
puts config.sso_session('non-existant-sso-session')
rescue AwsLocalConfigParser::NoMatchingSsoSession => e
puts pastel.bold.red "🚨 #{e.message}"
end

puts "\n"
puts pastel.bold.blue('SSO Sessions:')
puts config.all_sso_sessions

puts "\n"
puts pastel.bold.blue('Profiles:')
puts config.all_profiles
5 changes: 5 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

bundle check || bundle install
lefthook install -f
brew bundle
8 changes: 8 additions & 0 deletions lefthook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# https://github.com/evilmartians/lefthook/blob/master/docs/configuration.md

pre-commit:
parallel: true
commands:
ruby-lint:
glob: "*.rb"
run: bundle exec rubocop {staged_files}
48 changes: 48 additions & 0 deletions lib/aws_local_config_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require_relative 'aws_local_config_parser/version'
require_relative 'aws_local_config_parser/errors'

require 'inifile'
require 'ostruct'

class AwsLocalConfigParser
attr_accessor :config, :config_path

def initialize(config_path: "#{ENV["HOME"]}/.aws/config")
@config_path = config_path
@config = read_config_file
end

def read_config_file
raise AwsLocalConfigParser::NoAwsConfig.new(config_path:) unless File.exist?(config_path)

IniFile.load(config_path)
end

def config_hash
config.to_h
end

def profile(profile_name)
raise NoMatchingProfile.new(profile_name:, config_path:) unless config.has_section?("profile #{profile_name}")

OpenStruct.new(config_hash["profile #{profile_name}"])
end

def sso_session(sso_session)
raise NoMatchingSsoSession.new(sso_session:, config_path:) unless config.has_section?("sso-session #{sso_session}")

OpenStruct.new(config_hash["sso-session #{sso_session}"])
end

def all_sso_sessions
list_of_sso_sessions = config_hash.keys.select { |entry| entry.start_with?('sso-session') }.sort
list_of_sso_sessions.map { |sso_session| sso_session.gsub('sso-session ', '') }
end

def all_profiles
list_of_profiles = config_hash.keys.select { |entry| entry.start_with?('profile') }.sort
list_of_profiles.map { |profile| profile.gsub('profile ', '') }
end
end
44 changes: 44 additions & 0 deletions lib/aws_local_config_parser/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

class AwsLocalConfigParser
class NoAwsConfig < StandardError
attr_reader :config_path

def initialize(config_path:)
@config_path = config_path
super(msg: message)
end

def message
"No AWS config file at: '#{config_path}'."
end
end

class NoMatchingProfile < StandardError
attr_reader :profile_name, :config_path

def initialize(profile_name:, config_path:)
@profile_name = profile_name
@config_path = config_path
super(msg: message)
end

def message
"No matching profile, '#{profile_name}', in '#{config_path}'."
end
end

class NoMatchingSsoSession < StandardError
attr_reader :sso_session, :config_path

def initialize(sso_session:, config_path:)
@sso_session = sso_session
@config_path = config_path
super(msg: message)
end

def message
"No matching sso-session, '#{sso_session}', in '#{config_path}'."
end
end
end
5 changes: 5 additions & 0 deletions lib/aws_local_config_parser/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class AwsLocalConfigParser
VERSION = '0.1.0'
end
Loading

0 comments on commit 122d6b5

Please sign in to comment.