Skip to content

Commit

Permalink
added github workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Kufro committed Jun 18, 2021
1 parent 90e1aee commit 969c6a1
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 42 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Release

on:
release:
types: [published]

jobs:
push:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read

steps:
- uses: actions/checkout@v2

- name: Log into GitHub Container Registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin

- name: Build and Push image
run: |
# Calculate ENV variables
VERSION=$(cat ./version)
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:$VERSION
IMAGE_LATEST_ID=ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:latest
# Build the image
docker build ./src/ --file ./src/Dockerfile --tag $IMAGE_ID --label "runnumber=${GITHUB_RUN_ID}"
# Tag image as latest
docker tag $IMAGE_ID $IMAGE_LATEST_ID
# Push the image to GitHub Container Registry
docker push $IMAGE_ID
docker push $IMAGE_LATEST_ID
25 changes: 25 additions & 0 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Static Analysis

on: [push, workflow_dispatch]

jobs:
static:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2

- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7.2

- name: Rubocop
run: |
gem install rubocop
rubocop
- name: Bundle Audit
run: |
gem install bundler bundle-audit
cd ./src/
bundle-audit
29 changes: 29 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
AllCops:
TargetRubyVersion: 2.7
NewCops: enable
Layout/LineLength:
Max: 160
Metrics/PerceivedComplexity:
Enabled: false
Metrics/CyclomaticComplexity:
Enabled: false
Metrics/MethodLength:
Max: 160
Metrics/AbcSize:
Enabled: false
Metrics/ClassLength:
Max: 750
Metrics/ParameterLists:
Max: 8
Metrics/BlockLength:
Enabled: false
Style/ClassAndModuleChildren:
Enabled: false
Naming/VariableNumber:
EnforcedStyle: snake_case
Style/MultilineTernaryOperator:
Enabled: false
Style/NestedTernaryOperator:
Enabled: false
Style/GlobalVars:
Enabled: false
2 changes: 1 addition & 1 deletion src/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
source 'https://rubygems.org'

gem 'aws-sdk-lambda', '~> 1'
gem 'aws-sdk-ssm', '~> 1'
gem 'aws-sdk-s3', '~> 1'
gem 'aws-sdk-ssm', '~> 1'
gem 'multipart-post'
52 changes: 26 additions & 26 deletions src/lambda_function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
# Invoking lambda from the Ruby SDK:
# https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/Lambda/Client.html#invoke_async-instance_method
#
def lambda_handler(event:, context:)
def lambda_handler(event:, _context:)
$logger.info(event)

validate_variables(event)

records = (event['Records'] || [])
records.each do |record|
bucket_name = record.dig('s3', 'bucket', 'name')
object_key = record.dig('s3', 'object', 'key')
process_record(event, bucket_name, object_key)
bucket_name = record.dig('s3', 'bucket', 'name')
object_key = record.dig('s3', 'object', 'key')
process_record(event, bucket_name, object_key)
end

$logger.info('Lambda completed successfully!')
Expand All @@ -51,18 +51,18 @@ def lambda_handler(event:, context:)
##
# Process a S3 record that was passed via the event
#
def process_record(event, bucket_name, object_key)
def process_record(_event, bucket_name, object_key)
return if bucket_name.nil? || object_key.nil?

record_contents = get_record_contents(bucket_name, object_key)
hdf = record_contents['data']
filename = object_key.split('/').last
$logger.info("Processing file (#{object_key}) with filename (#{filename})")

record_contents['eval_tags'] = record_contents['eval_tags'].nil? ? 'HeimdallPusher' : record_contents['eval_tags'] + ',HeimdallPusher'
record_contents['eval_tags'] = record_contents['eval_tags'].nil? ? 'HeimdallPusher' : "#{record_contents['eval_tags']},HeimdallPusher"

# Save to Heimdall
heimdall_user_password = get_heimdall_password
heimdall_user_password = heimdall_password
user_id, token = get_heimdall_api_token(heimdall_user_password)
push_to_heimdall(filename, hdf, user_id, token, record_contents['eval_tags'])

Expand All @@ -82,29 +82,29 @@ def save_hdf_to_bucket(hdf, bucket_name, filename)
$logger.info('Saving processed HDF to bucket.')
s3_client = Aws::S3::Client.new
s3_client.put_object({
body: StringIO.new(hdf.to_json),
bucket: bucket_name,
key: "hdf/#{filename}",
})
body: StringIO.new(hdf.to_json),
bucket: bucket_name,
key: "hdf/#{filename}"
})
end

def save_results_to_bucket(results, bucket_name, filename)
$logger.info('Saving processed result to bucket.')
s3_client = Aws::S3::Client.new
s3_client.put_object({
body: StringIO.new(results.to_json),
bucket: bucket_name,
key: "processed/#{filename}",
})
body: StringIO.new(results.to_json),
bucket: bucket_name,
key: "processed/#{filename}"
})
end

def remove_unprocessed_from_bucket(bucket_name, object_key)
$logger.info('Removing unprocessed result from bucket.')
s3_client = Aws::S3::Client.new
s3_client.delete_object({
bucket: bucket_name,
key: object_key,
})
bucket: bucket_name,
key: object_key
})
end

##
Expand Down Expand Up @@ -136,7 +136,7 @@ def validate_variables(event)
# specifying the SSM_ENDPOINT variable will allow reaching
# SSM parameter store properly.
#
def get_heimdall_password
def heimdall_password
$logger.info('Fetching Heimdall Password Secret from SSM parameter store...')
ssm_client = nil

Expand Down Expand Up @@ -165,8 +165,8 @@ def get_heimdall_password
def get_heimdall_api_token(heimdall_user_password)
$logger.info('Getting token from Heimdall Server...')
payload = {
'email': ENV['HEIMDALL_API_USER'],
'password': heimdall_user_password
email: ENV['HEIMDALL_API_USER'],
password: heimdall_user_password
}
resp = Net::HTTP.post(
URI("#{ENV['HEIMDALL_URL']}/authn/login"),
Expand Down Expand Up @@ -205,11 +205,11 @@ def push_to_heimdall(filename, hdf, user_id, token, eval_tags)
$logger.info('Pushing HDF results to Heimdall Server...')
url = URI("#{ENV['HEIMDALL_URL']}/evaluations")
payload = {
'data': UploadIO.new(StringIO.new(hdf.to_json), 'application/json', filename),
'filename': filename,
'userId': user_id,
'public': ENV['HEIMDALL_PUBLIC'] || 'true',
'evaluationTags': eval_tags
data: UploadIO.new(StringIO.new(hdf.to_json), 'application/json', filename),
filename: filename,
userId: user_id,
public: ENV['HEIMDALL_PUBLIC'] || 'true',
evaluationTags: eval_tags
}
request = Net::HTTP::Post::Multipart.new(url.path, payload)
request['Authorization'] = "Bearer #{token}"
Expand Down
30 changes: 15 additions & 15 deletions src/run_lambda_locally.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
require_relative 'lambda_function'

lambda_handler(
event: {
"Records" => [
{
"s3" => {
"bucket" => {
"name" => "inspec-results-bucket-dev-myzr"
},
"object" => {
"key" => "unprocessed/2021-05-27_14-14-46_ConfigToHdf.json"
}
}
}
]
},
context: nil
event: {
'Records' => [
{
's3' => {
'bucket' => {
'name' => 'inspec-results-bucket-dev-myzr'
},
'object' => {
'key' => 'unprocessed/2021-05-27_14-14-46_ConfigToHdf.json'
}
}
}
]
},
context: nil
)

0 comments on commit 969c6a1

Please sign in to comment.