Skip to content
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

Add lib/inspec-objects.rb so inspec-objects can be required #3

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
789a880
Add lib/inspec-objects.rb so inspec-objects can be required
lukemalinowski Nov 21, 2019
d007ab9
Restructure objects imports
Apr 2, 2020
1d24379
Merge branch 'inspec:main' into master
aaronlippold Jan 31, 2022
b441bf0
Setup unit tests and linting for project
aaronlippold Feb 8, 2022
0f6dd26
changed to the chefstyle gem for now
aaronlippold Feb 8, 2022
2d91292
Update ruby.yml
aaronlippold Feb 8, 2022
1f01e17
added missing rake command
aaronlippold Feb 8, 2022
e6d1075
removed autocorrect
aaronlippold Feb 8, 2022
59144fa
Control Header Object (#5)
aaronlippold Feb 8, 2022
d1cd834
Control Post-Body Object (#6)
aaronlippold Feb 8, 2022
9f3db79
updated github actions to simplify things
aaronlippold Feb 8, 2022
6eac6a6
removed ruby 3 since it doesn't seem to be happy with chefstyle
aaronlippold Feb 8, 2022
b3cd495
Updated version to account for post-body & header
aaronlippold Feb 8, 2022
f2de22d
Update version.rb
aaronlippold Feb 8, 2022
0c00d3c
Rename package to mitre-inspec-objects
camdenmoors Feb 8, 2022
5e0d1c3
Rename folders to mitre-inspec-objects as well
camdenmoors Feb 8, 2022
e6b8a48
Update Gem Version badge
camdenmoors Feb 8, 2022
722093e
0.3.1
camdenmoors Feb 8, 2022
bf216ce
Update push action
camdenmoors Feb 8, 2022
52788d7
Revert "0.3.1"
camdenmoors Feb 8, 2022
74217c9
Update github release workflow
camdenmoors Feb 8, 2022
7ff83d5
0.3.1
camdenmoors Feb 8, 2022
04749db
0.3.2
camdenmoors Feb 8, 2022
8a4a6f2
Add repo to gemspec metadata
camdenmoors Feb 8, 2022
a6485bf
Satisfy rubocop for gemspec metadata
camdenmoors Feb 8, 2022
8924abf
Remove extra blank line
camdenmoors Feb 8, 2022
0c69771
0.3.3
camdenmoors Feb 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .expeditor/config.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
project:
alias: inspec-objects
alias: mitre-inspec-objects

slack:
notify_channel: "compliance-profile"

rubygems:
- inspec-objects
- mitre-inspec-objects

github:
# This deletes the GitHub PR branch after successfully merged into the release branch
Expand Down
43 changes: 43 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Release Management

on:
release:
types: [published]

jobs:
release:
name: Release to gem hosts
runs-on: ubuntu-latest
steps:
- name: Setup ruby
uses: actions/setup-ruby@v1
- uses: actions/checkout@v2
with:
fetch-depth: 0
lfs: true
- run: git fetch origin +refs/tags/*:refs/tags/*
- name: Setup credentials and versioning
run: |
gem install git-lite-version-bump keycutter roo
mkdir -p $HOME/.gem
touch $HOME/.gem/credentials
chmod 0600 $HOME/.gem/credentials
printf -- "---\n:push_api_key: ${RUBYGEMS_API_KEY}\n" > $HOME/.gem/credentials
printf -- ":github_api_key: Bearer ${GPR_API_KEY}\n" >> $HOME/.gem/credentials
env:
RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
GPR_API_KEY: ${{secrets.GITHUB_TOKEN}}
- name: Build gem
run: gem build ./mitre-inspec-objects.gemspec
- name: Get gem version
run: echo "::set-env name=VERSION::$(ls *.gem | grep -Po '(\d+.)+\d+')"
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
- name: Publish to RubyGems
run: |
gem push -k push_api_key --host https://rubygems.org *.gem
- name: Publish to GPR
run: |
gem push -k github_api_key --host https://rubygems.pkg.github.com/${OWNER} *.gem
env:
OWNER: mitre
36 changes: 36 additions & 0 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby

name: Ruby

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
test:

runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.6', '2.7']

steps:
- uses: actions/checkout@v2
- name: Set up Ruby
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# change this to (see https://github.com/ruby/setup-ruby#versioning):
# uses: ruby/setup-ruby@v1
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- name: Run tests
run: |
bundle exec rake
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ Gemfile.lock

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
.vscode/*
14 changes: 14 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2022-02-07 20:29:42 -0500 using RuboCop version 0.72.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: AllowInHeredoc.
Layout/TrailingWhitespace:
Exclude:
- 'test/unit/objects_test.rb'
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem "inspec-objects", path: "."
gem "mitre-inspec-objects", path: "."

group :test do
gem "chef-utils", "~> 16.6.14"
gem "chefstyle", "0.13.0"
gem "minitest", "~> 5.5"
gem "rake", ">= 10"
gem "chef-utils", "~> 16.6.14"
end
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# inspec-objects
[![Gem Version](https://badge.fury.io/rb/inspec-objects.svg)](https://badge.fury.io/rb/inspec-objects)
[![Gem Version](https://badge.fury.io/rb/mitre-inspec-objects.svg)](https://badge.fury.io/rb/mitre-inspec-objects)

**Umbrella Project**: [InSpec](https://github.com/chef/chef-oss-practices/blob/master/projects/inspec.md)

Expand Down
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# frozen_string_literal: true

require "bundler"
require "rake/testtask"

Bundler::GemHelper.install_tasks name: "inspec-objects"
Bundler::GemHelper.install_tasks name: "mitre-inspec-objects"

Rake::TestTask.new(:unit) do |t|
t.libs << "test"
t.test_files = Dir.glob([
"test/unit/**/*_test.rb",
])
"test/unit/**/*_test.rb",
])
end

begin
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.3.3
3 changes: 0 additions & 3 deletions lib/inspec-objects/version.rb

This file was deleted.

24 changes: 14 additions & 10 deletions lib/inspec/objects.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# frozen_string_literal: true

module Inspec
module Object
autoload :Tag, "inspec/objects/tag"
autoload :Control, "inspec/objects/control"
autoload :Describe, "inspec/objects/describe"
autoload :EachLoop, "inspec/objects/each_loop"
autoload :List, "inspec/objects/list"
autoload :OrTest, "inspec/objects/or_test"
autoload :RubyHelper, "inspec/objects/ruby_helper"
autoload :Test, "inspec/objects/test"
autoload :Value, "inspec/objects/value"
autoload :Input, "inspec/objects/input"
require_relative "objects/tag"
require_relative "objects/control"
require_relative "objects/ruby_helper"
require_relative "objects/describe"
require_relative "objects/value"
require_relative "objects/list"
require_relative "objects/each_loop"
require_relative "objects/or_test"
require_relative "objects/test"
require_relative "objects/input"
require_relative "objects/post_body"
require_relative "objects/header"
end
end
23 changes: 20 additions & 3 deletions lib/inspec/objects/control.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# frozen_string_literal: true

module Inspec::Object
class Control
attr_accessor :id, :title, :descriptions, :impact, :tests, :tags, :refs, :only_if
attr_accessor :header, :id, :title, :descriptions, :impact, :tests, :post_body, :tags, :refs, :only_if
def initialize
@header = ""
@tests = []
@tags = []
@refs = []
@descriptions = {}
@post_body = ""
end

def add_header(header)
@header = header
end

def add_test(t)
Expand All @@ -16,19 +24,27 @@ def add_tag(t)
@tags.push(t)
end

def add_post_body(post_body)
@post_body = post_body
end

def to_hash
{
header: header,
id: id,
title: title,
descriptions: descriptions,
impact: impact,
tests: tests.map(&:to_hash),
tags: tags.map(&:to_hash),
post_body: post_body,
}
end

def to_ruby # rubocop:disable Metrics/AbcSize
res = ["control #{id.inspect} do"]
def to_ruby
res = []
res.push header unless header.nil? || header.empty?
res.push "control #{id.inspect} do"
res.push " title #{title.inspect}" unless title.to_s.empty?
descriptions.each do |label, text|
if label == :default
Expand All @@ -44,6 +60,7 @@ def to_ruby # rubocop:disable Metrics/AbcSize
refs.each { |t| res.push(" ref #{print_ref(t)}") }
res.push " only_if { #{only_if} }" if only_if
tests.each { |t| res.push(indent(t.to_ruby, 2)) }
res.push(indent(post_body, 2)) unless post_body.nil? || post_body.empty?
res.push "end"
res.join("\n")
end
Expand Down
14 changes: 8 additions & 6 deletions lib/inspec/objects/describe.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec::Object
class Describe
# Internal helper to structure test objects.
Expand All @@ -11,11 +13,11 @@ def negate!
def to_ruby
itsy = "it"
unless its.nil?
if its.is_a? Array
itsy = "its(" + its.inspect + ")"
else
itsy = "its(" + its.to_s.inspect + ")"
end
itsy = if its.is_a? Array
"its(" + its.inspect + ")"
else
"its(" + its.to_s.inspect + ")"
end
end
naughty = negated ? "_not" : ""
xpect = if expectation.nil?
Expand Down Expand Up @@ -72,7 +74,7 @@ def to_hash
end

def resource
return nil if qualifier.empty? || qualifier[0].empty? || qualifier[0][0].empty?
return if qualifier.empty? || qualifier[0].empty? || qualifier[0][0].empty?

qualifier[0][0]
end
Expand Down
2 changes: 2 additions & 0 deletions lib/inspec/objects/each_loop.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec::Object
class EachLoop < List
attr_reader :variables
Expand Down
25 changes: 25 additions & 0 deletions lib/inspec/objects/header.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Inspec::Object
class Header
attr_accessor :header

def initialize(header)
@header = header
end

def to_hash
{
header: header,
}
end

def to_ruby
header.inspect.to_s
end

def to_s
"Header #{header}"
end
end
end
3 changes: 2 additions & 1 deletion lib/inspec/objects/input.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

require "inspec/input"

module Inspec::Object
class Input < ::Inspec::Input

# NOTE: No initialize method or accessors for the reasons listed above

#--------------------------------------------------------------------------#
Expand Down
2 changes: 2 additions & 0 deletions lib/inspec/objects/list.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec::Object
class List < Value
def map
Expand Down
5 changes: 3 additions & 2 deletions lib/inspec/objects/or_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec::Object
class OrTest
attr_reader :tests
Expand All @@ -18,12 +20,11 @@ def to_ruby
if @negated
# We don't use the describe.one wrapper when negated because:
# !(test1 || test2) same as (!test1 && !test2) where && is implicit in inspec
all_tests = @tests.map do |test|
@tests.map do |test|
test.negate!
test
end.map(&:to_ruby).join("\n")

all_tests
else
all_tests = @tests.map(&:to_ruby).join("\n").gsub("\n", "\n ")

Expand Down
25 changes: 25 additions & 0 deletions lib/inspec/objects/post_body.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Inspec::Object
class PostBody
attr_accessor :post_body

def initialize(post_body)
@post_body = post_body
end

def to_hash
{
post_body: post_body,
}
end

def to_ruby
post_body.inspect.to_s
end

def to_s
"Post Body #{post_body}"
end
end
end
2 changes: 2 additions & 0 deletions lib/inspec/objects/ruby_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec::Object
module RubyHelper
def ruby_qualifier(q)
Expand Down
2 changes: 2 additions & 0 deletions lib/inspec/objects/tag.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Inspec::Object
class Tag
attr_accessor :key, :value
Expand Down
Loading