-
Notifications
You must be signed in to change notification settings - Fork 14
Add an SSG module for interacting with SSG #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| require 'ssg/downloader' | ||
| require 'ssg/unarchiver' | ||
|
|
||
| module Ssg | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
| require 'net/http' | ||
|
|
||
| module Ssg | ||
| # Downloads SCAP datastreams from the SCAP Security Guide | ||
| # https://github.com/ComplianceAsCode/content | ||
| class Downloader | ||
| RELEASES_API = 'https://api.github.com/repos'\ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How official source is this?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't look good :( (from #openscap on irc.devel.rh):
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've also sent an email to scap-internal-list with the subject "Hosted SSG - A Use Case for cloud.redhat.com" if you're interested in following the discussion there. |
||
| '/ComplianceAsCode/content/releases/' | ||
| SSG_DS_REGEX = /scap-security-guide-(\d+\.)+zip$/ | ||
|
|
||
| def initialize(version = 'latest') | ||
| @release_uri = URI( | ||
| "#{RELEASES_API}#{'tags/' unless version[/^latest$/]}#{version}" | ||
| ) | ||
| end | ||
|
|
||
| def self.download!(versions = []) | ||
| versions.uniq.map do |version| | ||
| [version, new(version).fetch_datastream_file] | ||
| end.to_h | ||
| end | ||
|
|
||
| def fetch_datastream_file | ||
| puts "Fetching #{datastream_filename}" | ||
| get_chunked(datastream_uri) | ||
|
|
||
| datastream_filename | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def datastream_uri | ||
| @datastream_uri ||= URI( | ||
| download_urls.find { |url| url[SSG_DS_REGEX] } | ||
| ) | ||
| end | ||
|
|
||
| def download_urls | ||
| get_json(@release_uri).dig('assets').map do |asset| | ||
| asset.dig('browser_download_url') | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is coupled to GitHub's releases api, unfortunately. If they change the structure of the response, we'll have to update this method. It's essentially: http https://api.github.com/repos/ComplianceAsCode/content/releases/latest | jq '.assets[] | .browser_download_url' |
||
| end | ||
| end | ||
|
|
||
| def fetch(request, &block) | ||
| Net::HTTP.start( | ||
| request.uri.host, request.uri.port, | ||
| use_ssl: request.uri.scheme['https'] | ||
| ) do |http| | ||
| check_response(http.request(request, &block), &block) | ||
| end | ||
| end | ||
|
|
||
| def get(uri, &block) | ||
| fetch(Net::HTTP::Get.new(uri), &block) | ||
| end | ||
|
|
||
| def head(uri, &block) | ||
| fetch(Net::HTTP::Head.new(uri), &block) | ||
| end | ||
|
|
||
| def check_response(response, &block) | ||
| case response | ||
| when Net::HTTPSuccess | ||
| response | ||
| when Net::HTTPRedirection | ||
| get(URI(response['location']), &block) | ||
| else | ||
| response.value | ||
| end | ||
| end | ||
|
|
||
| def get_chunked(uri, filename: datastream_filename) | ||
| head(uri) do |response| | ||
| next unless Net::HTTPSuccess === response | ||
| open(filename, 'wb') do |file| | ||
| response.read_body do |chunk| | ||
| file.write(chunk) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def datastream_filename | ||
| datastream_uri.path.split('/').last[SSG_DS_REGEX] | ||
| end | ||
|
|
||
| def get_json(uri) | ||
| JSON.parse(get(uri).body) | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| module Ssg | ||
| class Unarchiver | ||
| UNZIP_CMD = ['unzip', '-o'] | ||
|
|
||
| def initialize(ds_zip_filename, datastreams) | ||
| @ds_zip_filename = ds_zip_filename | ||
| @datastreams = datastreams | ||
| end | ||
|
|
||
| def self.unarchive!(ds_zip_filenames, datastreams) | ||
| ds_zip_filenames.map do |version, ds_zip_filename| | ||
| new(ds_zip_filename, [datastreams[version]].flatten).datastream_files | ||
| end | ||
| end | ||
|
|
||
| def datastream_files | ||
| datastream_filenames if system( | ||
| *UNZIP_CMD, @ds_zip_filename, *datastream_filenames | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def datastream_filenames | ||
| @datastreams.map do |datastream| | ||
| "#{datastream_dir}/ssg-#{datastream}-ds.xml" | ||
| end | ||
| end | ||
|
|
||
| def datastream_dir | ||
| @ds_zip_filename.split('.')[0...-1].join('.') | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| desc 'Import or update SCAP datastreams from the SCAP Security Guide' | ||
| namespace :ssg do | ||
| desc 'Import or update SCAP datastreams for RHEL 6, 7, and 8' | ||
| task :sync_rhel do |task| | ||
| RHEL_SSG_VERSIONS = ( | ||
| 'v0.1.28:rhel6,'\ | ||
| 'v0.1.43:rhel7,'\ | ||
| 'v0.1.42:rhel8' | ||
| ) | ||
|
|
||
| ENV['DATASTREAMS'] = RHEL_SSG_VERSIONS | ||
| Rake::Task['ssg:sync'].invoke | ||
| end | ||
|
|
||
| desc 'Import or update SCAP datastreams, '\ | ||
| 'provided as a comma separated list: '\ | ||
| '`rake ssg:sync DATASTREAMS=v0.1.43:rhel7,latest:fedora`' | ||
| task :sync do |task| | ||
| DATASTREAMS = ENV.fetch('DATASTREAMS', '').split(',') | ||
| .inject({}) do |datastreams, arg| | ||
| version, datastream = arg.split(':') | ||
| datastreams[version] = (datastreams[version] || []).push(datastream) | ||
|
|
||
| datastreams | ||
| end | ||
|
|
||
| require 'ssg' | ||
|
|
||
| ds_zip_filenames = Ssg::Downloader.download!(DATASTREAMS.keys) | ||
| Ssg::Unarchiver.unarchive!(ds_zip_filenames, DATASTREAMS) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'test_helper' | ||
| require 'ssg/downloader' | ||
|
|
||
| module Ssg | ||
| class DownloaderTest < MiniTest::Test | ||
| context 'fetch_datastream_file' do | ||
| test 'returns the fetched file' do | ||
| FILE = 'scap-security-guide-0.0.0.zip' | ||
| uri = URI("https://example.com/#{FILE}") | ||
| downloader = Downloader.new | ||
| downloader.expects(:datastream_uri). | ||
| at_least_once.returns(uri) | ||
| downloader.expects(:get_chunked).with uri | ||
|
|
||
| assert_equal FILE, downloader.fetch_datastream_file | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'test_helper' | ||
| require 'ssg/unarchiver' | ||
|
|
||
| module Ssg | ||
| class UnarchiverTest < MiniTest::Test | ||
| context 'datastream_files' do | ||
| test 'properly shells out to unzip' do | ||
| ZIP_FILE = 'scap-security-guide-0.0.0.zip' | ||
| DATASTREAMS = ['rhel6'] | ||
| FILES = [] | ||
| unarchiver = Unarchiver.new(ZIP_FILE, DATASTREAMS) | ||
| unarchiver.expects(:system).with( | ||
| "unzip", "-o", | ||
| "scap-security-guide-0.0.0.zip", | ||
| "scap-security-guide-0.0.0/ssg-rhel6-ds.xml" | ||
| ).returns(true) | ||
|
|
||
| assert_equal ['scap-security-guide-0.0.0/ssg-rhel6-ds.xml'], | ||
| unarchiver.datastream_files | ||
| end | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, I've tried to design these two classes so that the methods are short and well-named so it's easy to understand what is happening without a bunch of comments.