knowngithub 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
+ SHA1:
3
+ metadata.gz: 1558981157ccbd8a9a6813f322c35ca067144616
4
+ data.tar.gz: 2f7725e9cf5b14c844d68dab2034587608674ec8
5
+ SHA512:
6
+ metadata.gz: 7b725ce302e80464a62e7520f2a3a6bb597895bc25d34e37b9c97d21a29a4f8335cfb00dd3c3d77f5745faf07c91f03e4270521b723cfede4633ff6ae1e50e94
7
+ data.tar.gz: a2ab935cb1cc4c7ca0cb10b22ab60b72264e0abd70c2bd43321c06f5ef9ec4ccda3d07c4513dcb13681e979990e53cc5eddb8414d69185487675616365ad8847
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in knowngithub.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ knowngithub (0.1.0)
5
+ net-ssh (~> 4.2)
6
+ nokogiri (~> 1.8)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ mini_portile2 (2.3.0)
12
+ net-ssh (4.2.0)
13
+ nokogiri (1.8.1)
14
+ mini_portile2 (~> 2.3.0)
15
+ rake (10.5.0)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ bundler (~> 1.16)
22
+ knowngithub!
23
+ rake (~> 10.0)
24
+
25
+ BUNDLED WITH
26
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Alexis Vanier
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,53 @@
1
+ # Knowngithub
2
+
3
+ > ### _“Some people think this is paranoia, but it isn't. Paranoids only think everyone is out to get them. Wizards know it.”_
4
+ >
5
+ > _— Terry Pratchett, Sourcery_
6
+
7
+ Have you hit the problem where you're trying to bootstrap new containers or VMs and `git` will refuse to clone stuff because it doesn't automatically trust GitHub?
8
+
9
+ This gem answers the scenario where you believe that manually burning the `known_hosts` into whatever you're making doesn't scale, and blindly trusting any host to provide you with legitimiate code is insufficient.
10
+
11
+ While the solution is not elegant, and the implementation is fragile, I'm hoping it proves the following point : a diagonal chain of trust is better than blind trust.
12
+
13
+ The idea behind this gem is that by calling GitHub's web pages and API through https, the answers will be certified through it's CA and cannot be tampered with unbeknownst to us. Thus, one can call GitHub's SSH endpoint, and verify its key fingerprint is valid against a dynamic trustable source of truth to protect against man-in-the-middle attacks on `git` operations.
14
+
15
+ This gem will return one or a list of trusted hosts in a format appropriate for `.ssh/known_hosts` files.
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'knowngithub'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install knowngithub
32
+
33
+ ## Usage
34
+
35
+ The main goal of this gem is for use in automation scripts, specifically [Chef](https://www.chef.io/chef/) cookbooks.
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Honorable mentions
44
+
45
+ Huge thanks to [Brent Smith](https://github.com/BrentOnRails) for the scraping and Nokogiri code.
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/avanier/knowngithub.
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "knowngithub"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "knowngithub/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "knowngithub"
7
+ spec.version = Knowngithub::VERSION
8
+ spec.authors = ["Alexis Vanier"]
9
+ spec.email = ["alexis@amonoid.io"]
10
+
11
+ spec.summary = "Don't blindly trust GitHub hosts"
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/avanier/knowngithub"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = 'https://rubygems.org'
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.16"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_runtime_dependency "net-ssh", "~> 4.2"
35
+ spec.add_runtime_dependency "nokogiri", "~> 1.8"
36
+ end
@@ -0,0 +1,86 @@
1
+ require 'ipaddr'
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'uri'
5
+
6
+ require 'net/ssh'
7
+ require 'nokogiri'
8
+ require "knowngithub/version"
9
+
10
+ module Knowngithub
11
+ # Make a call enforcing the strict use of SSL.
12
+ # @param [String] url A full url like `https://help.github.com`
13
+ # @return [Net::HTTP] Return the full Net::HTTP object of the response.
14
+ # @since 0.1.0
15
+ def self.safe_call(url)
16
+ uri = URI.parse(url)
17
+ http = Net::HTTP.new(uri.host, uri.port)
18
+ http.use_ssl = true
19
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
20
+
21
+ request = Net::HTTP::Get.new(uri.request_uri)
22
+
23
+ http.request(request)
24
+ end
25
+
26
+ # Queries and parses the GitHub help page for the SSH key fingerprints.
27
+ # @return [Array<String>] SSH key fingerprints as an array of strings.
28
+ # @since 0.1.0
29
+ def self.fingerprints
30
+ pattern = /^(sha256:[a-z0-9\+\/]{43})|([0-9a-f\:]{32,47})$/i
31
+ res = self.safe_call('https://help.github.com/articles/github-s-ssh-key-fingerprints/')
32
+ output = Nokogiri::HTML(res.body)
33
+ fields = output.xpath("//code")
34
+ return fields.children.map(&:content).select{ |x| pattern =~ x }
35
+ end
36
+
37
+ # Instantiates a Net::SSH session with GitHub to get the host key and closes it.
38
+ # @return [Net:SSH] Returns a closed Net::SSH session
39
+ # @since 0.1.0
40
+ def self.session
41
+ s = Net::SSH::Transport::Session.new('github.com', { :verify_host_key => true })
42
+ s.close
43
+ s
44
+ end
45
+
46
+ # Composes a hash with the properties required for composing a known host entry
47
+ # @return [Hash] Returns a hash object with all of the needed components to compose a `known_hosts` file
48
+ # @raise [SecurityError] If the host keys fail validation or if the https call fails, this will be raised.
49
+ # @since 0.1.0
50
+ def self.host
51
+ s = self.session
52
+ if self.fingerprints.any?{|f| f == s.host_keys.first.fingerprint}
53
+ base64_key = [Net::SSH::Buffer.from(:key, s.host_keys.first).to_s].pack("m*").gsub(/\s/, "")
54
+ return {
55
+ "host_as_string" => s.host_as_string,
56
+ "ssh_type" => s.host_keys.first.ssh_type,
57
+ "base64_key" => base64_key
58
+ }
59
+ else
60
+ raise SecurityError # while this is inappropriate, it sounds cool
61
+ end
62
+ end
63
+
64
+ # Composes a known_hosts entry for the fqdn only
65
+ # @return [String] Returns a `known_hosts` entry for the fqdn only with no ip address binding as a string.
66
+ # @since 0.1.0
67
+ def self.known_host
68
+ h = self.host
69
+ return [ h["host_as_string"], h["ssh_type"], h["base64_key"] ].join(' ')
70
+ end
71
+
72
+ # Composes a block of text ready for appending to a known_hosts file
73
+ # @return [String] Returns a `known_hosts` multiline string enunciating every known IP GitHub may serve SSH on.
74
+ # @since 0.1.0
75
+ def self.known_hosts
76
+ h = self.host
77
+ cidr_ranges = JSON.parse(self.safe_call('https://api.github.com/meta').body)["git"]
78
+ known_hosts = []
79
+ cidr_ranges.each do |range|
80
+ IPAddr.new(range).to_range.to_a.map { |a| a.to_s }.each do |ip|
81
+ known_hosts << 'github.com,' + ip + ' ' + h["base64_key"]
82
+ end
83
+ end
84
+ return known_hosts
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module Knowngithub
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knowngithub
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexis Vanier
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: net-ssh
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.8'
69
+ description: Don't blindly trust GitHub hosts
70
+ email:
71
+ - alexis@amonoid.io
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - knowngithub.gemspec
85
+ - lib/knowngithub.rb
86
+ - lib/knowngithub/version.rb
87
+ homepage: https://github.com/avanier/knowngithub
88
+ licenses:
89
+ - MIT
90
+ metadata:
91
+ allowed_push_host: https://rubygems.org
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.6.11
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Don't blindly trust GitHub hosts
112
+ test_files: []