Class: Fetchers::Url
- Inherits:
-
Object
- Object
- Fetchers::Url
- Defined in:
- lib/fetchers/url.rb
Direct Known Subclasses
Constant Summary collapse
- MIME_TYPES =
{ "application/x-zip-compressed" => ".zip", "application/zip" => ".zip", "application/x-gzip" => ".tar.gz", "application/gzip" => ".tar.gz", }.freeze
- GITHUB_URL_REGEX =
Transforms a browser github/bitbucket url to github/bitbucket tar url We distinguish between three different Github/Bitbucket URL types:
- Master URL - Branch URL - Commit URL
master url: github.com/nathenharvey/tmp_compliance_profile/ is transformed to github.com/nathenharvey/tmp_compliance_profile/archive/master.tar.gz bitbucket.org/username/repo is transformed to bitbucket.org/username/repo/get/master.tar.gz
branch: github.com/hardening-io/tests-os-hardening/tree/2.0 is transformed to github.com/hardening-io/tests-os-hardening/archive/2.0.tar.gz bitbucket.org/username/repo/branch/branchname is transformed to bitbucket.org/username/repo/get/newbranch.tar.gz
commit: github.com/hardening-io/tests-os-hardening/tree/48bd4388ddffde68badd83aefa654e7af3231876 is transformed to github.com/hardening-io/tests-os-hardening/archive/48bd4388ddffde68badd83aefa654e7af3231876.tar.gz bitbucket.org/username/repo/commits/95ce1f83d5bbe9eec34c5973f6894617e8d6d8cc is transformed to bitbucket.org/username/repo/get/95ce1f83d5bbe9eec34c5973f6894617e8d6d8cc.tar.gz
%r{^https?://(www\.)?github\.com/(?<user>[\w-]+)/(?<repo>[\w.-]+?)(\.git)?(/)?$}.freeze
- GITHUB_URL_WITH_TREE_REGEX =
%r{^https?://(www\.)?github\.com/(?<user>[\w-]+)/(?<repo>[\w.-]+)/tree/(?<commit>[\w\.]+)(/)?$}.freeze
- BITBUCKET_URL_REGEX =
%r{^https?://(www\.)?bitbucket\.org/(?<user>[\w-]+)/(?<repo>[\w-]+)(\.git)?(/)?$}.freeze
- BITBUCKET_URL_BRANCH_REGEX =
%r{^https?://(www\.)?bitbucket\.org/(?<user>[\w-]+)/(?<repo>[\w-]+)/branch/(?<branch>[\w\.]+)(/)?$}.freeze
- BITBUCKET_URL_COMMIT_REGEX =
%r{^https?://(www\.)?bitbucket\.org/(?<user>[\w-]+)/(?<repo>[\w-]+)/commits/(?<commit>[\w\.]+)(/)?$}.freeze
Instance Attribute Summary collapse
-
#archive_path ⇒ Object
readonly
Returns the value of attribute archive_path.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
Class Method Summary collapse
- .resolve(target, opts = {}) ⇒ Object
- .resolve_from_string(target, opts, username = nil, password = nil) ⇒ Object
- .transform(target) ⇒ Object
Instance Method Summary collapse
- #cache_key ⇒ Object
- #fetch(path) ⇒ Object
-
#initialize(url, opts) ⇒ Url
constructor
A new instance of Url.
- #resolved_source ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(url, opts) ⇒ Url
Returns a new instance of Url.
95 96 97 98 99 100 101 102 103 |
# File 'lib/fetchers/url.rb', line 95 def initialize(url, opts) @target = url.to_s @target_uri = url.is_a?(URI) ? url : parse_uri(url) @insecure = opts["insecure"] @token = opts["token"] @config = opts @archive_path = nil @temp_archive_path = nil end |
Instance Attribute Details
#archive_path ⇒ Object (readonly)
Returns the value of attribute archive_path.
93 94 95 |
# File 'lib/fetchers/url.rb', line 93 def archive_path @archive_path end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
93 94 95 |
# File 'lib/fetchers/url.rb', line 93 def files @files end |
Class Method Details
.resolve(target, opts = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/fetchers/url.rb', line 18 def self.resolve(target, opts = {}) if target.is_a?(Hash) && target.key?(:url) resolve_from_string(target[:url], opts, target[:username], target[:password]) elsif target.is_a?(String) resolve_from_string(target, opts) elsif target.is_a?(URI) resolve_from_string(target.to_s, opts) end end |
.resolve_from_string(target, opts, username = nil, password = nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fetchers/url.rb', line 28 def self.resolve_from_string(target, opts, username = nil, password = nil) uri = URI.parse(target) return nil if uri.nil? || uri.scheme.nil? return nil unless %{ http https }.include? uri.scheme target = transform(target) opts[:username] = username if username opts[:password] = password if password new(target, opts) rescue URI::Error nil end |
.transform(target) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/fetchers/url.rb', line 72 def self.transform(target) transformed_target = if m = GITHUB_URL_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition "https://github.com/#{m[:user]}/#{m[:repo]}/archive/master.tar.gz" elsif m = GITHUB_URL_WITH_TREE_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition "https://github.com/#{m[:user]}/#{m[:repo]}/archive/#{m[:commit]}.tar.gz" elsif m = BITBUCKET_URL_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition "https://bitbucket.org/#{m[:user]}/#{m[:repo]}/get/master.tar.gz" elsif m = BITBUCKET_URL_BRANCH_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition "https://bitbucket.org/#{m[:user]}/#{m[:repo]}/get/#{m[:branch]}.tar.gz" elsif m = BITBUCKET_URL_COMMIT_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition "https://bitbucket.org/#{m[:user]}/#{m[:repo]}/get/#{m[:commit]}.tar.gz" end if transformed_target Inspec::Log.warn("URL target #{target} transformed to #{transformed_target}. Consider using the git fetcher") transformed_target else target end end |
Instance Method Details
#cache_key ⇒ Object
113 114 115 |
# File 'lib/fetchers/url.rb', line 113 def cache_key @archive_shasum ||= sha256 end |
#fetch(path) ⇒ Object
105 106 107 |
# File 'lib/fetchers/url.rb', line 105 def fetch(path) @archive_path ||= download_archive(path) end |
#resolved_source ⇒ Object
109 110 111 |
# File 'lib/fetchers/url.rb', line 109 def resolved_source @resolved_source ||= { url: @target, sha256: sha256 } end |
#to_s ⇒ Object
117 118 119 |
# File 'lib/fetchers/url.rb', line 117 def to_s @target end |