Skip to content

Commit 969c6a1

Browse files
author
Justin Kufro
committed
added github workflows
1 parent 90e1aee commit 969c6a1

File tree

6 files changed

+131
-42
lines changed

6 files changed

+131
-42
lines changed

.github/workflows/release.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Release
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
push:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
packages: write
12+
contents: read
13+
14+
steps:
15+
- uses: actions/checkout@v2
16+
17+
- name: Log into GitHub Container Registry
18+
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
19+
20+
- name: Build and Push image
21+
run: |
22+
# Calculate ENV variables
23+
VERSION=$(cat ./version)
24+
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:$VERSION
25+
IMAGE_LATEST_ID=ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:latest
26+
27+
# Build the image
28+
docker build ./src/ --file ./src/Dockerfile --tag $IMAGE_ID --label "runnumber=${GITHUB_RUN_ID}"
29+
30+
# Tag image as latest
31+
docker tag $IMAGE_ID $IMAGE_LATEST_ID
32+
33+
# Push the image to GitHub Container Registry
34+
docker push $IMAGE_ID
35+
docker push $IMAGE_LATEST_ID

.github/workflows/static.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Static Analysis
2+
3+
on: [push, workflow_dispatch]
4+
5+
jobs:
6+
static:
7+
runs-on: ubuntu-18.04
8+
steps:
9+
- uses: actions/checkout@v2
10+
11+
- name: Setup Ruby
12+
uses: ruby/setup-ruby@v1
13+
with:
14+
ruby-version: 2.7.2
15+
16+
- name: Rubocop
17+
run: |
18+
gem install rubocop
19+
rubocop
20+
21+
- name: Bundle Audit
22+
run: |
23+
gem install bundler bundle-audit
24+
cd ./src/
25+
bundle-audit

.rubocop.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
AllCops:
2+
TargetRubyVersion: 2.7
3+
NewCops: enable
4+
Layout/LineLength:
5+
Max: 160
6+
Metrics/PerceivedComplexity:
7+
Enabled: false
8+
Metrics/CyclomaticComplexity:
9+
Enabled: false
10+
Metrics/MethodLength:
11+
Max: 160
12+
Metrics/AbcSize:
13+
Enabled: false
14+
Metrics/ClassLength:
15+
Max: 750
16+
Metrics/ParameterLists:
17+
Max: 8
18+
Metrics/BlockLength:
19+
Enabled: false
20+
Style/ClassAndModuleChildren:
21+
Enabled: false
22+
Naming/VariableNumber:
23+
EnforcedStyle: snake_case
24+
Style/MultilineTernaryOperator:
25+
Enabled: false
26+
Style/NestedTernaryOperator:
27+
Enabled: false
28+
Style/GlobalVars:
29+
Enabled: false

src/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
source 'https://rubygems.org'
44

55
gem 'aws-sdk-lambda', '~> 1'
6-
gem 'aws-sdk-ssm', '~> 1'
76
gem 'aws-sdk-s3', '~> 1'
7+
gem 'aws-sdk-ssm', '~> 1'
88
gem 'multipart-post'

src/lambda_function.rb

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@
3333
# Invoking lambda from the Ruby SDK:
3434
# https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/Lambda/Client.html#invoke_async-instance_method
3535
#
36-
def lambda_handler(event:, context:)
36+
def lambda_handler(event:, _context:)
3737
$logger.info(event)
3838

3939
validate_variables(event)
4040

4141
records = (event['Records'] || [])
4242
records.each do |record|
43-
bucket_name = record.dig('s3', 'bucket', 'name')
44-
object_key = record.dig('s3', 'object', 'key')
45-
process_record(event, bucket_name, object_key)
43+
bucket_name = record.dig('s3', 'bucket', 'name')
44+
object_key = record.dig('s3', 'object', 'key')
45+
process_record(event, bucket_name, object_key)
4646
end
4747

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

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

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

6464
# Save to Heimdall
65-
heimdall_user_password = get_heimdall_password
65+
heimdall_user_password = heimdall_password
6666
user_id, token = get_heimdall_api_token(heimdall_user_password)
6767
push_to_heimdall(filename, hdf, user_id, token, record_contents['eval_tags'])
6868

@@ -82,29 +82,29 @@ def save_hdf_to_bucket(hdf, bucket_name, filename)
8282
$logger.info('Saving processed HDF to bucket.')
8383
s3_client = Aws::S3::Client.new
8484
s3_client.put_object({
85-
body: StringIO.new(hdf.to_json),
86-
bucket: bucket_name,
87-
key: "hdf/#{filename}",
88-
})
85+
body: StringIO.new(hdf.to_json),
86+
bucket: bucket_name,
87+
key: "hdf/#{filename}"
88+
})
8989
end
9090

9191
def save_results_to_bucket(results, bucket_name, filename)
9292
$logger.info('Saving processed result to bucket.')
9393
s3_client = Aws::S3::Client.new
9494
s3_client.put_object({
95-
body: StringIO.new(results.to_json),
96-
bucket: bucket_name,
97-
key: "processed/#{filename}",
98-
})
95+
body: StringIO.new(results.to_json),
96+
bucket: bucket_name,
97+
key: "processed/#{filename}"
98+
})
9999
end
100100

101101
def remove_unprocessed_from_bucket(bucket_name, object_key)
102102
$logger.info('Removing unprocessed result from bucket.')
103103
s3_client = Aws::S3::Client.new
104104
s3_client.delete_object({
105-
bucket: bucket_name,
106-
key: object_key,
107-
})
105+
bucket: bucket_name,
106+
key: object_key
107+
})
108108
end
109109

110110
##
@@ -136,7 +136,7 @@ def validate_variables(event)
136136
# specifying the SSM_ENDPOINT variable will allow reaching
137137
# SSM parameter store properly.
138138
#
139-
def get_heimdall_password
139+
def heimdall_password
140140
$logger.info('Fetching Heimdall Password Secret from SSM parameter store...')
141141
ssm_client = nil
142142

@@ -165,8 +165,8 @@ def get_heimdall_password
165165
def get_heimdall_api_token(heimdall_user_password)
166166
$logger.info('Getting token from Heimdall Server...')
167167
payload = {
168-
'email': ENV['HEIMDALL_API_USER'],
169-
'password': heimdall_user_password
168+
email: ENV['HEIMDALL_API_USER'],
169+
password: heimdall_user_password
170170
}
171171
resp = Net::HTTP.post(
172172
URI("#{ENV['HEIMDALL_URL']}/authn/login"),
@@ -205,11 +205,11 @@ def push_to_heimdall(filename, hdf, user_id, token, eval_tags)
205205
$logger.info('Pushing HDF results to Heimdall Server...')
206206
url = URI("#{ENV['HEIMDALL_URL']}/evaluations")
207207
payload = {
208-
'data': UploadIO.new(StringIO.new(hdf.to_json), 'application/json', filename),
209-
'filename': filename,
210-
'userId': user_id,
211-
'public': ENV['HEIMDALL_PUBLIC'] || 'true',
212-
'evaluationTags': eval_tags
208+
data: UploadIO.new(StringIO.new(hdf.to_json), 'application/json', filename),
209+
filename: filename,
210+
userId: user_id,
211+
public: ENV['HEIMDALL_PUBLIC'] || 'true',
212+
evaluationTags: eval_tags
213213
}
214214
request = Net::HTTP::Post::Multipart.new(url.path, payload)
215215
request['Authorization'] = "Bearer #{token}"

src/run_lambda_locally.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414
require_relative 'lambda_function'
1515

1616
lambda_handler(
17-
event: {
18-
"Records" => [
19-
{
20-
"s3" => {
21-
"bucket" => {
22-
"name" => "inspec-results-bucket-dev-myzr"
23-
},
24-
"object" => {
25-
"key" => "unprocessed/2021-05-27_14-14-46_ConfigToHdf.json"
26-
}
27-
}
28-
}
29-
]
30-
},
31-
context: nil
17+
event: {
18+
'Records' => [
19+
{
20+
's3' => {
21+
'bucket' => {
22+
'name' => 'inspec-results-bucket-dev-myzr'
23+
},
24+
'object' => {
25+
'key' => 'unprocessed/2021-05-27_14-14-46_ConfigToHdf.json'
26+
}
27+
}
28+
}
29+
]
30+
},
31+
context: nil
3232
)

0 commit comments

Comments
 (0)