gaargh 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5be00f9025dc2da224ad903119e28ffe8ff2d2e6ed1817abeec4df7df9db3b89
4
+ data.tar.gz: 82688fb326f5c034221b4d986a01af2dd6779e67df7e45bea859bbc34575024b
5
+ SHA512:
6
+ metadata.gz: b4d5be9570636aa6aa6991f82a09ec359790d43e2ec305cb95276fe349a9f48ec0ec99f98a319ca71f96f9d22ae03612878838e445f4c6b593cd2a2d5dc629df
7
+ data.tar.gz: e3711674b834fab31ba5d2c003cd4049d17cc9455d15226599e5049bb5ee640075282d72931101b0f349f85c38da87287a811bc0162ed41d66aaab9e51a1d03c
data/.editorconfig ADDED
@@ -0,0 +1,11 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
10
+
11
+ max_line_length = 300
@@ -0,0 +1,32 @@
1
+ # See https://pre-commit.com for more information
2
+ # See https://pre-commit.com/hooks.html for more hooks
3
+ repos:
4
+ - repo: https://github.com/gitleaks/gitleaks
5
+ rev: v8.16.1
6
+ hooks:
7
+ - id: gitleaks
8
+ - repo: https://github.com/pre-commit/pre-commit-hooks
9
+ rev: v3.2.0
10
+ hooks:
11
+ # - id: no-commit-to-branch
12
+ # args: ['--branch', 'main']
13
+ - id: trailing-whitespace
14
+ - id: end-of-file-fixer
15
+ - id: check-yaml
16
+ - id: check-json
17
+ - id: check-added-large-files
18
+ - id: detect-private-key
19
+ - id: check-executables-have-shebangs
20
+ - id: check-merge-conflict
21
+ - id: mixed-line-ending
22
+ args:
23
+ - "--fix=lf"
24
+ - id: pretty-format-json
25
+ args:
26
+ - "--autofix"
27
+ - "--indent=4"
28
+ - "--no-sort-keys"
29
+ - repo: https://github.com/mattlqx/pre-commit-ruby
30
+ rev: v1.3.5
31
+ hooks:
32
+ - id: rubocop
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,20 @@
1
+ require:
2
+ - rubocop-performance
3
+
4
+ AllCops:
5
+ TargetRubyVersion: <%= RUBY_VERSION[/^\d+\.\d+/, 0] %>
6
+
7
+ # Disable all cops for now. Only notify on DeprecatedClassMethods
8
+ DisabledByDefault: true
9
+ Layout/LineLength:
10
+ Max: 300
11
+
12
+ Lint/DeprecatedClassMethods:
13
+ Enabled: true
14
+ Style/StringLiterals:
15
+ Enabled: true
16
+
17
+ Style/HashSyntax:
18
+ Enabled: false
19
+ Style/GuardClause:
20
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-07-13
4
+
5
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 do-i-need-a-username
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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Mark Lynch
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Gaargh
2
+
3
+ gaargh is a **G**CP **A**lternative **A**uthentication **R**uby **G**em **H**elper and it's also the noise I make when working with GCP and ruby.
4
+
5
+ This gem was created as a work around for the lack of support for account impersonation using Ruby with GCP.
6
+
7
+ Ruby isn't one of the 4 language that support account impersonation on GCP as documented [here](https://cloud.google.com/docs/authentication/provide-credentials-adc#sa-impersonation).
8
+
9
+ The is a github feature request https://github.com/googleapis/google-auth-library-ruby/issues/353 but there has been no update since Nov 16, 2021.
10
+
11
+ This gem uses the work around discussed in https://github.com/googleapis/google-cloud-ruby/issues/17915 as a work around for dealing with account impersonation.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ bundler:
18
+
19
+ ```ruby
20
+ gem 'gaargh'
21
+ ```
22
+
23
+ Or
24
+
25
+ `gem install 'gaargh'`
26
+
27
+ ## Usage
28
+
29
+ EXample usage with `Google::Cloud::Storage` client.
30
+
31
+ ```ruby
32
+ require 'gaargh'
33
+ require 'google/cloud/storage'
34
+ impersonated_credentials_client = Gaargh.impersonate_service_account(service_account_email: 'my-service-account@my-project-id.iam.gserviceaccount.com')
35
+ storage = Google::Cloud::Storage.new(credentials: impersonated_credentials_client, project_id: 'my-project-id')
36
+ puts storage.buckets.map { |b| b.name}
37
+ token_info = Gaargh.token_expiration_time(access_token: impersonated_credentials_client.access_token)
38
+ pp token_info
39
+ ```
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gaargh.
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
48
+
49
+ ## Creating a Gem
50
+
51
+ ### Create a Gem Skeleton
52
+
53
+ ```bash
54
+ bundle gem json_ruby_logger \
55
+ --mit \
56
+ --linter=rubocop \
57
+ --test=rspec \
58
+ --ci=github \
59
+ --no-coc
60
+ ```
61
+
62
+ ### Build the gem
63
+
64
+ ```bash
65
+ gem build json_ruby_logger.gemspec
66
+ ```
67
+
68
+ ### push the gem to rubygems.org
69
+
70
+ ```bash
71
+ gem push json_ruby_logger-0.1.0.gem
72
+ ```
73
+
74
+ ## Revoke/yank a gem
75
+
76
+ ```bash
77
+ gem yank json_ruby_logger -v 0.1.0
78
+ ```
79
+
80
+ ## Bump Gem Version
81
+
82
+ ```bash
83
+ gem install gem-release
84
+ gem bump patch --skip-ci --push
85
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/gaargh.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/gaargh/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "gaargh"
7
+ spec.version = Gaargh::VERSION
8
+ spec.authors = ["ml"]
9
+ # spec.email = ["redacted@redacted"]
10
+
11
+ spec.summary = "Helper module for impersonating service accounts on GCP."
12
+ spec.description = "GCP account impersonation helper."
13
+ spec.homepage = "https://github.com/do-i-need-a-username/gaargh"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+ spec.metadata["changelog_uri"] = spec.homepage
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (File.expand_path(f) == __FILE__) ||
28
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
29
+ end
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ # Uncomment to register a new dependency of your gem
36
+ spec.add_dependency "google-apis-iamcredentials_v1", "~> 0.17"
37
+
38
+ # For more information and examples about making a new gem, check out our
39
+ # guide at: https://bundler.io/guides/creating_gem.html
40
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gaargh
4
+ VERSION = "0.1.0"
5
+ end
data/lib/gaargh.rb ADDED
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+ require_relative "gaargh/version"
3
+ require 'google/apis/iamcredentials_v1'
4
+ require 'logger'
5
+
6
+ module Gaargh
7
+ class Error < StandardError; end
8
+ class << self
9
+ attr_writer :logger
10
+ end
11
+
12
+ def self.logger
13
+ @logger ||= Logger.new(STDOUT)
14
+ end
15
+
16
+ # Returns an impersonated credentials client to be used with the GCP clients
17
+ # impersonated_credentials_client = Gaargh.impersonate_service_account(service_account_email: 'my-service-account@my-project-id.iam.gserviceaccount.com')
18
+ # e.g. storage = Google::Cloud::Storage.new(credentials: impersonated_credentials_client, project_id: 'your-storage-project-id')
19
+ #
20
+ # @param [String] service_account_email - the email of the service account to impersonate (e.g. 'my-service-account@my-project-id.iam.gserviceaccount.com')
21
+ # @param [String] lifetime - the lifetime of the access token in seconds (default: 3600s)
22
+ # @return [Signet::OAuth2::Client] client - the client to be used with GCP clients
23
+ def self.impersonate_service_account(service_account_email: '', lifetime: '3600s', scopes: ['https://www.googleapis.com/auth/cloud-platform'])
24
+ creds_service = Google::Apis::IamcredentialsV1::IAMCredentialsService.new
25
+
26
+ creds_service.authorization = Google::Auth.get_application_default(scopes)
27
+
28
+ generate_token_request = Google::Apis::IamcredentialsV1::GenerateAccessTokenRequest.new(
29
+ lifetime: lifetime,
30
+ scope: ['https://www.googleapis.com/auth/cloud-platform']
31
+ )
32
+
33
+ impersonated_account = creds_service.generate_service_account_access_token("projects/-/serviceAccounts/#{service_account_email}", generate_token_request)
34
+ client = Signet::OAuth2::Client.new
35
+ client.access_token = impersonated_account.access_token
36
+ return client
37
+ end
38
+
39
+ # Returns information about the access token provided e.g. expiration time, email address etc.
40
+ #
41
+ # @param [String] access_token - the access token to retrieve information about
42
+ # @return [Hash] token_info - a hash containing information about the access token, error informaiton if the request fails
43
+ def self.token_expiration_time(access_token:)
44
+ uri = URI.parse("https://oauth2.googleapis.com/tokeninfo?access_token=#{access_token}")
45
+ http = Net::HTTP.new(uri.host, uri.port)
46
+ http.use_ssl = true
47
+
48
+ request = Net::HTTP::Get.new(uri.request_uri)
49
+
50
+ response = http.request(request)
51
+
52
+ if response.code == '200'
53
+ data = JSON.parse(response.body)
54
+ expiration_time = Time.at(data['exp'].to_i)
55
+ logger.info("Token expires at: #{expiration_time}")
56
+ pp data
57
+ token_info = {
58
+ token_data: data,
59
+ token_expiration_time: expiration_time
60
+ }
61
+ return token_info
62
+ else
63
+ error_hash = {
64
+ error: "Failed to retrieve token information",
65
+ response_code: response.code,
66
+ response_body: response.body
67
+ }
68
+ logger.info("Error retrieving token information: response.code: #{response.code}, response.body: #{response.body}")
69
+ return error_hash
70
+ end
71
+ end
72
+ end
data/sig/gaargh.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Gaargh
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gaargh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ml
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-apis-iamcredentials_v1
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.17'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.17'
27
+ description: GCP account impersonation helper.
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".editorconfig"
34
+ - ".pre-commit-config.yaml"
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - CHANGELOG.md
38
+ - LICENSE
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - gaargh.gemspec
43
+ - lib/gaargh.rb
44
+ - lib/gaargh/version.rb
45
+ - sig/gaargh.rbs
46
+ homepage: https://github.com/do-i-need-a-username/gaargh
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ allowed_push_host: https://rubygems.org
51
+ homepage_uri: https://github.com/do-i-need-a-username/gaargh
52
+ source_code_uri: https://github.com/do-i-need-a-username/gaargh
53
+ changelog_uri: https://github.com/do-i-need-a-username/gaargh
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.6.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.4.13
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Helper module for impersonating service accounts on GCP.
73
+ test_files: []