capistrano-ssh-authorized-keys-github 1.1.0 → 1.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fdeace2093ea8dd5b0326e751fe39daaf0976d9815949063d0a041b579637db4
4
- data.tar.gz: 5dcd586bbab56363ee751a3379354bfb93861e6dff1e645d78194eb530d86945
3
+ metadata.gz: 479fe35c970cbb125646e1cae8aa813ddd06fb9aa9bbb059ca09536e2eb6bf27
4
+ data.tar.gz: 24b01dcc48a3c67c898a39e3dc7f3eda603f36eb4f553c417f9b1970735407c6
5
5
  SHA512:
6
- metadata.gz: f5e563b6bcb8f601cb0d31c846869c8d62bff34d2154fd356c0c6596f695ed2238a152abfb3e50871ab1b20b8d581f5e17734a88a9af2770351c58bacc3f4fa5
7
- data.tar.gz: e740b5181dcf8f7eb9915d9395c7bef55c222249980c3866cb771d2cc151c7ee61ef948b14b081e9562dec467314f21fe5207140b55f298dc359b3a04d5d0fd0
6
+ metadata.gz: 7df5dad8dc08499b0f561bc69a6f57793ef2397bb6642e763620f655e578ca623e431f2abd1adec1a034f9546a500d6a84bc020cc022cfd20351573f358081d9
7
+ data.tar.gz: b91856817052ce95d3f28026031ce674f050045d2946d24c424fe667dc3d35431bf7130139ea25348d170f61cdd895276346da76a3ba84a6d352105b5d6c68cc
@@ -0,0 +1,11 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "bundler"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "weekly"
7
+
8
+ - package-ecosystem: "github-actions"
9
+ directory: "/"
10
+ schedule:
11
+ interval: "weekly"
@@ -0,0 +1,29 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request:
8
+
9
+ jobs:
10
+ build:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ fail-fast: false
14
+ matrix:
15
+ ruby:
16
+ - '3.2'
17
+ - '3.3'
18
+ - '3.4'
19
+ - '4.0'
20
+ name: Ruby ${{ matrix.ruby }}
21
+ steps:
22
+ - uses: actions/checkout@v7
23
+ - uses: ruby/setup-ruby@v1
24
+ with:
25
+ ruby-version: ${{ matrix.ruby }}
26
+ bundler-cache: true
27
+ - run: bundle exec rspec
28
+ # The gemspec packages every tracked file, so catch a broken one before release.
29
+ - run: gem build capistrano-ssh-authorized-keys-github.gemspec
@@ -0,0 +1,106 @@
1
+ name: Publish GEM
2
+
3
+ # Releases are cut from the GitHub Releases UI, so the tag is the source of
4
+ # truth for the version. This workflow reads the version off the tag, writes it
5
+ # into the gemspec, commits that bump to main, then publishes to RubyGems.
6
+ on:
7
+ release:
8
+ types: [published]
9
+
10
+ jobs:
11
+ push:
12
+ runs-on: ubuntu-latest
13
+
14
+ permissions:
15
+ contents: write
16
+ id-token: write
17
+
18
+ env:
19
+ TAG_NAME: ${{ github.event.release.tag_name }}
20
+ GEMSPEC: capistrano-ssh-authorized-keys-github.gemspec
21
+
22
+ steps:
23
+ # Check out main rather than the tag, because the bump is committed to it.
24
+ - uses: actions/checkout@v7
25
+ with:
26
+ ref: main
27
+ fetch-depth: 0
28
+
29
+ # Installs the bundle, which `rubygems/release-gem` needs for `bundle exec rake release`.
30
+ # Gemfile.lock is gitignored, so generating it leaves the tree clean.
31
+ - name: Set up Ruby
32
+ uses: ruby/setup-ruby@v1
33
+ with:
34
+ ruby-version: '3.4'
35
+ bundler-cache: true
36
+
37
+ - name: Derive gem version from the release tag
38
+ run: |
39
+ version="${TAG_NAME#v}"
40
+ if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+(\.[0-9A-Za-z]+)?$ ]]; then
41
+ echo "::error::Release tag '${TAG_NAME}' is not a usable gem version. Expected vMAJOR.MINOR.PATCH[.PRE], for example v1.3.0 or v1.3.0.rc1."
42
+ exit 1
43
+ fi
44
+ echo "VERSION=${version}" >> "$GITHUB_ENV"
45
+
46
+ # The bump has to land on main, so the release must be cut from its head.
47
+ - name: Check the release tag is at the head of main
48
+ run: |
49
+ tag_sha="$(git rev-parse "refs/tags/${TAG_NAME}^{commit}")"
50
+ main_sha="$(git rev-parse HEAD)"
51
+ if [ "$tag_sha" != "$main_sha" ]; then
52
+ echo "::error::Tag ${TAG_NAME} is at ${tag_sha} but main is at ${main_sha}. Delete the release and recreate it against the current head of main."
53
+ exit 1
54
+ fi
55
+ echo "TAG_OBJECT=$(git rev-parse "refs/tags/${TAG_NAME}")" >> "$GITHUB_ENV"
56
+
57
+ - name: Write the version into the gemspec
58
+ run: |
59
+ # \x27 is a single quote: the gemspec writes `spec.version = '1.2.3'`.
60
+ ruby -e '
61
+ path = ENV.fetch("GEMSPEC")
62
+ File.write(path, File.read(path).sub(/^(\s*spec\.version\s*=\s*)\x27[^\x27]*\x27/) { "#{$1}\x27#{ENV.fetch("VERSION")}\x27" })
63
+ '
64
+
65
+ built="$(ruby -e 'print Gem::Specification.load(ARGV[0]).version' "$GEMSPEC")"
66
+ if [ "$built" != "$VERSION" ]; then
67
+ echo "::error::The gemspec reports ${built}, expected ${VERSION}."
68
+ exit 1
69
+ fi
70
+
71
+ - name: Commit the bump to main and move the tag onto it
72
+ run: |
73
+ if git diff --quiet; then
74
+ echo "Already at ${VERSION}; nothing to commit."
75
+ exit 0
76
+ fi
77
+
78
+ git config user.name 'github-actions[bot]'
79
+ git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
80
+ git add "$GEMSPEC"
81
+ git commit -m "Release v${VERSION}"
82
+ git push origin HEAD:main
83
+
84
+ # Point the release tag at the commit we actually publish, so checking
85
+ # out the tag gives you the released version. The lease makes this a
86
+ # no-op unless the tag is still exactly where we validated it above.
87
+ git tag -f -a "$TAG_NAME" -m "Version ${VERSION}"
88
+ git push --force-with-lease="refs/tags/${TAG_NAME}:${TAG_OBJECT}" \
89
+ origin "refs/tags/${TAG_NAME}"
90
+
91
+ # `rubygems/release-gem` runs `bundle exec rake release`, which refuses to
92
+ # publish from a dirty tree. Everything above is committed by now, so this
93
+ # should always pass; if it does not, fail with something readable rather
94
+ # than "There are files that need to be committed first".
95
+ - name: Check the working tree is clean
96
+ run: |
97
+ if [ -n "$(git status --porcelain)" ]; then
98
+ echo "::error::Working tree is dirty; refusing to publish."
99
+ git status --porcelain
100
+ git diff
101
+ exit 1
102
+ fi
103
+
104
+ # Publishes via trusted publishing (OIDC). `rake release` sees the tag
105
+ # already exists, so it skips creating one and just pushes the gem.
106
+ - uses: rubygems/release-gem@v1
data/.gitignore CHANGED
@@ -54,3 +54,6 @@ build-iPhoneSimulator/
54
54
 
55
55
  # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
56
  # .rubocop-https?--*
57
+
58
+ # A gem does not commit its lockfile; ignoring it keeps the tree clean for `rake release`.
59
+ Gemfile.lock
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Capistrano Cloudwatch [![Build Status](https://app.travis-ci.com/OLIOEX/capistrano-ssh-authorized-keys-github.svg?branch=main)](https://app.travis-ci.com/olioex/capistrano-ssh-authorized-keys-github) [![Gem Version](https://badge.fury.io/rb/capistrano-ssh-authorized-keys-github.svg)](http://badge.fury.io/rb/capistrano-ssh-authorized-keys-github)
1
+ # Capistrano Cloudwatch [![CI](https://github.com/OLIOEX/capistrano-ssh-authorized-keys-github/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/OLIOEX/capistrano-ssh-authorized-keys-github/actions/workflows/ci.yml) [![Gem Version](https://badge.fury.io/rb/capistrano-ssh-authorized-keys-github.svg)](http://badge.fury.io/rb/capistrano-ssh-authorized-keys-github)
2
2
 
3
3
  Sync organisation SSH public keys to server `authorized_keys` file so they are able to SSH into OS - for [Capistrano v3](https://github.com/capistrano/capistrano).
4
4
 
@@ -32,6 +32,9 @@ And then set the variables in `config/deploy.rb`:
32
32
  # Optional for Github rate limits (oauth application)
33
33
  set :github_app_id, '12345'
34
34
  set :github_app_secret, 'abcdef'
35
+ # Optional: what to do when one member's keys cannot be downloaded
36
+ # :log (default) warns and skips that member; :fail aborts the sync
37
+ set :github_key_fetch_failure, :log
35
38
 
36
39
  The task will run automatically on successful deploy. Alternatively, you can notify of a deploy starting manually by using:
37
40
 
@@ -41,8 +44,17 @@ The task will run automatically on successful deploy. Alternatively, you can not
41
44
 
42
45
  Users in your organisation will need to have their membership public in order for SSH public keys to be sync'd to the server.
43
46
 
47
+ By default, if a member's keys cannot be downloaded (an error response, or a network failure), the failure is logged as a warning for that member, their keys are left out of `authorized_keys`, and the sync carries on with everyone else. A summary of the skipped members is logged at the end. The task still fails if the organisation or its member list cannot be fetched, or if no keys at all were downloaded, so a bad run never replaces the file with an empty one. Set `github_key_fetch_failure` to `:fail` to abort the whole sync on the first member whose keys cannot be downloaded instead.
48
+
44
49
  Github rate limits aren't currently respected. They are limited to 60 per hour. if you have a large number of users and are doing regular syncing from an IP address then you will be rate limited.
45
50
 
51
+ ## Releasing
52
+
53
+ Releases are cut from the GitHub Releases UI. Create a release against the head of `main`, with a tag of the form `vMAJOR.MINOR.PATCH` (or `vMAJOR.MINOR.PATCH.PRE`, for example `v1.3.0.rc1`). The `Publish GEM` workflow then:
54
+
55
+ 1. writes the tag's version into the gemspec and, if it changed, commits that to `main` and moves the tag onto the commit, and
56
+ 2. publishes the gem to RubyGems through trusted publishing.
57
+
46
58
  ## Copyright
47
59
 
48
60
  Copyright (c) 2022 OLIO Exchange Ltd. See LICENSE.txt for further details.
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'capistrano-ssh-authorized-keys-github'
7
- spec.version = '1.1.0'
7
+ spec.version = '1.4.0'
8
8
  spec.authors = ['lloydwatkin']
9
9
  spec.email = ['lloyd@olioex.com']
10
10
  spec.summary = %q{Sync Github organisation public SSH keys to `server authorized_keys` file}
@@ -0,0 +1,21 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+
4
+ module Capistrano
5
+ module SshAuthorizedKeysGithub
6
+ # Fetches one member's public keys. A failure is returned rather than raised, so one
7
+ # unreachable or renamed account does not stop everyone else's keys being synced.
8
+ module MemberKeys
9
+ Result = Struct.new(:keys, :error)
10
+
11
+ def self.fetch(url)
12
+ response = Net::HTTP.get_response(url)
13
+ return Result.new(nil, "HTTP #{response.code} #{response.message}") unless response.is_a?(Net::HTTPSuccess)
14
+
15
+ Result.new(response.body.gsub(/\r\n?/, "\n"), nil)
16
+ rescue StandardError => e
17
+ Result.new(nil, "#{e.class}: #{e.message}")
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,53 +1,74 @@
1
1
  require 'uri'
2
2
  require 'net/http'
3
3
  require "json"
4
+ require_relative '../ssh_authorized_keys_github/member_keys'
4
5
 
5
6
  NO_GITHUB_ORGANISATION_PROVIDED = 'No Github organisations have been set in your configuration'
6
7
  NO_ORGANISATION_MEMBER_KEYS_FOUND = 'There are no public members for this Github organisation'
8
+ KEY_FETCH_FAILURE_MODES = %i[log fail].freeze
7
9
 
8
10
  namespace :security do
9
11
  desc 'Cycle SSH key logins'
10
12
  task :update_ssh_keys do
11
- on roles(:all) do |host|
13
+ run_locally do
12
14
  organisations = fetch(:github_orgs) || fetch(:github_org) || raise(NO_GITHUB_ORGANISATION_PROVIDED)
13
15
  keys = ""
16
+ failed_members = []
17
+ on_fetch_failure = fetch(:github_key_fetch_failure, :log).to_sym
18
+ unless KEY_FETCH_FAILURE_MODES.include?(on_fetch_failure)
19
+ raise "github_key_fetch_failure must be one of #{KEY_FETCH_FAILURE_MODES.join(', ')}, got #{on_fetch_failure.inspect}"
20
+ end
14
21
  authentication = ''
15
22
  if fetch(:github_app_id) && fetch(:github_app_secret)
16
23
  authentication = "#{fetch(:github_app_id)}:#{fetch(:github_app_secret)}@"
17
24
  end
18
25
 
19
26
  [*organisations].each do |organisation|
27
+ info "Fetching keys for #{organisation}"
20
28
  url = URI("https://#{authentication}api.github.com/orgs/#{organisation}")
21
29
  organisation_details = JSON.parse(Net::HTTP.get_response(url).body, symbolize_names: true)
22
30
  members_url = URI(organisation_details[:members_url].gsub("{/member}", "").gsub('https://', "https://#{authentication}"))
23
- members = JSON.parse(Net::HTTP.get_response(members_url).body, symbolize_names: true)
24
- keys += " #
25
- # #{organisation_details[:name]} keys
26
- # #{members_url}
27
- #
28
- # --
29
-
30
- "
31
+ response = Net::HTTP.get_response(members_url)
32
+ raise response.body unless response.kind_of? Net::HTTPSuccess
33
+ members = JSON.parse(response.body, symbolize_names: true)
34
+ keys += "\n#\n"\
35
+ "# #{organisation_details[:name]} keys\n"\
36
+ "# #{members_url}\n"\
37
+ "#\n"
31
38
  member_details = members.map { |member| member[:login].downcase }.sort
32
39
  member_details.each do |member|
40
+ info " - Downloading keys for #{member}"
33
41
  member_keys = URI("https://#{authentication}github.com/#{member}.keys")
34
- info = " #
35
- # @#{member}
36
- # #{member_keys}
37
- #
38
- "
39
- keys += info + Net::HTTP.get_response(member_keys).body.gsub(/\r\n?/, "\n")
42
+ result = Capistrano::SshAuthorizedKeysGithub::MemberKeys.fetch(member_keys)
43
+ if result.error
44
+ raise "Failed to download keys for #{member}: #{result.error}" if on_fetch_failure == :fail
45
+
46
+ warn " - FAILED to download keys for #{member} (#{result.error}), skipping"
47
+ failed_members << "#{organisation}/#{member}"
48
+ next
49
+ end
50
+
51
+ header = "\n #\n"\
52
+ " # @#{member}\n"\
53
+ " # #{member_keys}\n"\
54
+ " #\n"
55
+ keys += header + result.keys
40
56
  end
41
57
  end
58
+ unless failed_members.empty?
59
+ warn "Could not download keys for #{failed_members.size} member(s), who will not be in authorized_keys: " \
60
+ "#{failed_members.join(', ')}"
61
+ end
62
+ raise NO_ORGANISATION_MEMBER_KEYS_FOUND unless keys.scan(/ssh-(rsa|ed25519)/).count > 0
63
+ info "Writing authorized_keys file to ./tmp"
64
+ File.open("./tmp/authorized_keys", "w") do |f|
65
+ f.write(keys)
66
+ end
42
67
 
43
- if keys.scan(/ssh-(rsa|ed25519)/).count > 0
44
- File.open("./tmp/authorized_keys", "w") do |f|
45
- f.write(keys)
46
- end
68
+ info "Uploading updated authorized_keys to servers"
69
+ on roles(:all) do |host|
47
70
  upload! './tmp/authorized_keys', "/tmp/authorized_keys"
48
71
  execute :mv, "/tmp/authorized_keys", "~/.ssh/authorized_keys"
49
- else
50
- raise NO_ORGANISATION_MEMBER_KEYS_FOUND
51
72
  end
52
73
  end
53
74
  end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'capistrano/ssh_authorized_keys_github/member_keys'
3
+
4
+ RSpec.describe Capistrano::SshAuthorizedKeysGithub::MemberKeys do
5
+ let(:url) { URI('https://github.com/alice.keys') }
6
+
7
+ def http_response(klass, code, body, message = 'OK')
8
+ response = klass.new('1.1', code, message)
9
+ allow(response).to receive(:body).and_return(body)
10
+ response
11
+ end
12
+
13
+ it 'returns the keys with line endings normalised' do
14
+ allow(Net::HTTP).to receive(:get_response).with(url)
15
+ .and_return(http_response(Net::HTTPOK, '200', "ssh-ed25519 AAAA\r\nssh-rsa BBBB\r\n"))
16
+
17
+ expect(described_class.fetch(url)).to eq(described_class::Result.new("ssh-ed25519 AAAA\nssh-rsa BBBB\n", nil))
18
+ end
19
+
20
+ it 'reports the status of an unsuccessful response instead of raising' do
21
+ allow(Net::HTTP).to receive(:get_response).with(url)
22
+ .and_return(http_response(Net::HTTPNotFound, '404', '<html>…</html>', 'Not Found'))
23
+
24
+ result = described_class.fetch(url)
25
+
26
+ expect(result.keys).to be_nil
27
+ expect(result.error).to eq('HTTP 404 Not Found')
28
+ end
29
+
30
+ it 'reports a network error instead of raising' do
31
+ allow(Net::HTTP).to receive(:get_response).with(url).and_raise(Net::OpenTimeout, 'execution expired')
32
+
33
+ result = described_class.fetch(url)
34
+
35
+ expect(result.keys).to be_nil
36
+ expect(result.error).to eq('Net::OpenTimeout: execution expired')
37
+ end
38
+ end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-ssh-authorized-keys-github
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - lloydwatkin
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2022-10-28 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: capistrano
@@ -74,22 +73,25 @@ executables: []
74
73
  extensions: []
75
74
  extra_rdoc_files: []
76
75
  files:
76
+ - ".github/dependabot.yml"
77
+ - ".github/workflows/ci.yml"
78
+ - ".github/workflows/publish.yml"
77
79
  - ".gitignore"
78
- - ".travis.yml"
79
80
  - Gemfile
80
81
  - LICENSE
81
82
  - README.md
82
83
  - Rakefile
83
84
  - capistrano-ssh-authorized-keys-github.gemspec
84
85
  - lib/capistrano/ssh-authorized-keys-github.rb
86
+ - lib/capistrano/ssh_authorized_keys_github/member_keys.rb
85
87
  - lib/capistrano/tasks/ssh-authorized-keys-github.cap
86
88
  - lib/ssh-authorized-keys-github.rb
89
+ - spec/member_keys_spec.rb
87
90
  - spec/spec_helper.rb
88
91
  homepage: https://github.com/olioex/capistrano-ssh-authorized-keys-github
89
92
  licenses:
90
93
  - Apache 2.0
91
94
  metadata: {}
92
- post_install_message:
93
95
  rdoc_options: []
94
96
  require_paths:
95
97
  - lib
@@ -104,9 +106,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
106
  - !ruby/object:Gem::Version
105
107
  version: '0'
106
108
  requirements: []
107
- rubygems_version: 3.1.6
108
- signing_key:
109
+ rubygems_version: 3.6.9
109
110
  specification_version: 4
110
111
  summary: Sync Github organisation public SSH keys to `server authorized_keys` file
111
112
  test_files:
113
+ - spec/member_keys_spec.rb
112
114
  - spec/spec_helper.rb
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- before_install:
2
- - gem install bundler
3
- rvm:
4
- - 2.7.0
5
- - 3