Skip to content

Commit f892711

Browse files
authored
Merge pull request #83 from runwaylab/pass-through-auth
Pass through auth
2 parents dcc4edd + b239f1a commit f892711

File tree

7 files changed

+79
-16
lines changed

7 files changed

+79
-16
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
issue-db (1.1.0)
4+
issue-db (1.2.0)
55
faraday-retry (~> 2.2, >= 2.2.1)
66
jwt (>= 2.9.3, < 4.0)
77
octokit (>= 9.2, < 11.0)

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,14 @@ This section will go into detail around how you can configure the `issue-db` gem
197197

198198
## Authentication 🔒
199199

200-
The `issue-db` gem uses the [`Octokit.rb`](https://github.com/octokit/octokit.rb) library under the hood for interactions with the GitHub API. You have three options for authentication when using the `issue-db` gem:
200+
The `issue-db` gem uses the [`Octokit.rb`](https://github.com/octokit/octokit.rb) library under the hood for interactions with the GitHub API. You have four options for authentication when using the `issue-db` gem:
201201

202202
> Note: The order displayed below is also the order of priority that this Gem uses to authenticate.
203203
204204
1. Pass in your own authenticated `Octokit.rb` instance to the `IssueDB.new` method
205-
2. Use a GitHub App by setting the `ISSUE_DB_GITHUB_APP_ID`, `ISSUE_DB_GITHUB_APP_INSTALLATION_ID`, and `ISSUE_DB_GITHUB_APP_KEY` environment variables
206-
3. Use a GitHub personal access token by setting the `ISSUE_DB_GITHUB_TOKEN` environment variable
205+
2. Pass GitHub App authentication parameters directly to the `IssueDB.new` method
206+
3. Use a GitHub App by setting the `ISSUE_DB_GITHUB_APP_ID`, `ISSUE_DB_GITHUB_APP_INSTALLATION_ID`, and `ISSUE_DB_GITHUB_APP_KEY` environment variables
207+
4. Use a GitHub personal access token by setting the `ISSUE_DB_GITHUB_TOKEN` environment variable
207208

208209
> Using a GitHub App is the suggested method
209210
@@ -218,7 +219,31 @@ require "issue_db"
218219
db = IssueDB.new("<org>/<repo>") # THAT'S IT! 🎉
219220
```
220221

221-
### Using a GitHub App
222+
### Using GitHub App Parameters Directly
223+
224+
You can now pass GitHub App authentication parameters directly to the `IssueDB.new` method. This is especially useful when you want to manage authentication credentials in your application code or when you have multiple GitHub Apps for different purposes:
225+
226+
```ruby
227+
require "issue_db"
228+
229+
# Pass GitHub App credentials directly to IssueDB.new
230+
db = IssueDB.new(
231+
"<org>/<repo>",
232+
app_id: 12345, # Your GitHub App ID
233+
installation_id: 56789, # Your GitHub App Installation ID
234+
app_key: "-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----", # Your GitHub App private key
235+
app_algo: "RS256" # Optional: defaults to RS256
236+
)
237+
```
238+
239+
**Parameters:**
240+
241+
- `app_id` (Integer) - Your GitHub App ID (found on the App's settings page)
242+
- `installation_id` (Integer) - Your GitHub App Installation ID (found in the installation URL: `https://github.com/organizations/<org>/settings/installations/<installation_id>`)
243+
- `app_key` (String) - Your GitHub App private key (can be the key content as a string or a file path ending in `.pem`)
244+
- `app_algo` (String, optional) - The algorithm to use for JWT signing (defaults to "RS256")
245+
246+
### Using a GitHub App with Environment Variables
222247

223248
This is the single best way to use the `issue-db` gem because GitHub Apps have increased rate limits, fine-grained permissions, and are more secure than using a personal access token. All you have to do is provide three environment variables and the `issue-db` gem will take care of the rest:
224249

lib/issue_db.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@ class Client
3030
# :param label: The label to use for issues managed in the datastore by this library
3131
# :param cache_expiry: The number of seconds to cache issues in memory (default: 60)
3232
# :param init: Whether or not to initialize the database on object creation (default: true) - idempotent
33+
# :param app_id: GitHub App ID (for GitHub App authentication)
34+
# :param installation_id: GitHub App Installation ID (for GitHub App authentication)
35+
# :param app_key: GitHub App private key (for GitHub App authentication)
36+
# :param app_algo: GitHub App algorithm (for GitHub App authentication, defaults to RS256)
3337
# :return: A new IssueDB::Client object
34-
def initialize(repo, log: nil, octokit_client: nil, label: nil, cache_expiry: nil, init: true)
38+
def initialize(repo, log: nil, octokit_client: nil, label: nil, cache_expiry: nil, init: true, app_id: nil, installation_id: nil, app_key: nil, app_algo: nil)
3539
@log = log || RedactingLogger.new($stdout, level: ENV.fetch("LOG_LEVEL", "INFO").upcase)
3640
@version = VERSION
37-
@client = Authentication.login(octokit_client, @log)
41+
@client = Authentication.login(octokit_client, @log, app_id:, installation_id:, app_key:, app_algo:)
3842
@repo = Repository.new(repo)
3943
@label = label || ENV.fetch("ISSUE_DB_LABEL", "issue-db")
4044
@cache_expiry = cache_expiry || ENV.fetch("ISSUE_DB_CACHE_EXPIRY", 60).to_i

lib/issue_db/authentication.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,28 @@ module IssueDB
77
class AuthenticationError < StandardError; end
88

99
module Authentication
10-
def self.login(client = nil, log = nil)
10+
def self.login(client = nil, log = nil, app_id: nil, installation_id: nil, app_key: nil, app_algo: nil)
1111
# if the client is not nil, use the pre-provided client
1212
unless client.nil?
1313
log.debug("using pre-provided client") if log
1414
return client
1515
end
1616

17-
# if the client is nil, check for GitHub App env vars first
17+
# if GitHub App parameters are provided, use them first
18+
if app_id && installation_id && app_key
19+
log.debug("using provided github app authentication parameters") if log
20+
return IssueDB::Utils::GitHub.new(log:, app_id:, installation_id:, app_key:, app_algo:)
21+
end
22+
23+
# if the client is nil, check for GitHub App env vars
1824
# first, check if all three of the following env vars are set and have values
1925
# ISSUE_DB_GITHUB_APP_ID, ISSUE_DB_GITHUB_APP_INSTALLATION_ID, ISSUE_DB_GITHUB_APP_KEY
20-
app_id = ENV.fetch("ISSUE_DB_GITHUB_APP_ID", nil)
21-
installation_id = ENV.fetch("ISSUE_DB_GITHUB_APP_INSTALLATION_ID", nil)
22-
app_key = ENV.fetch("ISSUE_DB_GITHUB_APP_KEY", nil)
23-
if app_id && installation_id && app_key
24-
log.debug("using github app authentication") if log
25-
return IssueDB::Utils::GitHub.new(log:, app_id:, installation_id:, app_key:)
26+
env_app_id = ENV.fetch("ISSUE_DB_GITHUB_APP_ID", nil)
27+
env_installation_id = ENV.fetch("ISSUE_DB_GITHUB_APP_INSTALLATION_ID", nil)
28+
env_app_key = ENV.fetch("ISSUE_DB_GITHUB_APP_KEY", nil)
29+
if env_app_id && env_installation_id && env_app_key
30+
log.debug("using github app authentication from environment variables") if log
31+
return IssueDB::Utils::GitHub.new(log:, app_id: env_app_id, installation_id: env_installation_id, app_key: env_app_key)
2632
end
2733

2834
# if the client is nil and no GitHub App env vars were found, check for the ISSUE_DB_GITHUB_TOKEN

lib/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module IssueDB
44
module Version
5-
VERSION = "1.1.0"
5+
VERSION = "1.2.0"
66
end
77
end

spec/lib/issue_db/authentication_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@
3535
expect(IssueDB::Authentication.login).to eq(client)
3636
end
3737

38+
it "returns a hydrated octokit client from provided GitHub App parameters" do
39+
expect(IssueDB::Utils::GitHub).to receive(:new)
40+
.with(log: nil, app_id: "123", installation_id: "456", app_key: "-----KEY-----", app_algo: "RS256")
41+
.and_return(client)
42+
expect(IssueDB::Authentication.login(nil, nil, app_id: "123", installation_id: "456", app_key: "-----KEY-----", app_algo: "RS256")).to eq(client)
43+
end
44+
45+
it "prioritizes provided GitHub App parameters over environment variables" do
46+
# Set up environment variables that should be ignored
47+
expect(ENV).not_to receive(:fetch).with("ISSUE_DB_GITHUB_APP_ID", nil)
48+
expect(ENV).not_to receive(:fetch).with("ISSUE_DB_GITHUB_APP_INSTALLATION_ID", nil)
49+
expect(ENV).not_to receive(:fetch).with("ISSUE_DB_GITHUB_APP_KEY", nil)
50+
51+
expect(IssueDB::Utils::GitHub).to receive(:new)
52+
.with(log: nil, app_id: "789", installation_id: "101112", app_key: "-----PROVIDED-KEY-----", app_algo: nil)
53+
.and_return(client)
54+
expect(IssueDB::Authentication.login(nil, nil, app_id: "789", installation_id: "101112", app_key: "-----PROVIDED-KEY-----")).to eq(client)
55+
end
56+
3857
it "raises an authentication error when no auth methods pass for octokit" do
3958
expect(ENV).to receive(:fetch).with("ISSUE_DB_GITHUB_APP_ID", nil).and_return(nil)
4059
expect(ENV).to receive(:fetch).with("ISSUE_DB_GITHUB_APP_INSTALLATION_ID", nil).and_return(nil)

spec/lib/issue_db_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525
expect(instance).to be_a(IssueDB::Client)
2626
end
2727

28+
it "allows module-level instantiation with GitHub App parameters" do
29+
expect(IssueDB::Authentication).to receive(:login)
30+
.with(nil, log, app_id: "123", installation_id: "456", app_key: "-----KEY-----", app_algo: "RS256")
31+
.and_return(client)
32+
33+
instance = IssueDB.new(REPO, log: log, app_id: "123", installation_id: "456", app_key: "-----KEY-----", app_algo: "RS256")
34+
expect(instance).to be_a(IssueDB::Client)
35+
end
36+
2837
it "is a valid version string" do
2938
expect(subject.version).to match(/\A\d+\.\d+\.\d+(\.\w+)?\z/)
3039
end

0 commit comments

Comments
 (0)