- Notifications
You must be signed in to change notification settings - Fork 4
MVP saf-cli Lambda Function #2
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
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits Select commit Hold shift + click to select a range
a4ebf15
init
yarick d6e8842
remove saf cli dependency on branch
yarick 779b994
- linted and formated file
aaronlippold 024c2b1
minor fix to README
aaronlippold 69535f5
Added local Lambda runtime env.
yarick bfa6ae0
Migrated from console.log to winston logger
yarick e81cc7f
Logged last few spots, cleanup/fixes in various files
aaronlippold d397891
starting adding the structure for local mock testing
aaronlippold 5aa5e40
fixed error with release drafter files being switched around
aaronlippold 4b0aa77
updated release-drafter to also run on PRs for now
aaronlippold 231b7c4
cleanup: typo
yarick 3415864
Merge remote-tracking branch 'origin/basic_s3' into basic_s3
yarick 002f55b
cleanup: typo
yarick 92289b7
cleanup: typos
yarick d09931a
reverted to variable declaration working sequence
yarick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
8 changes: 3 additions & 5 deletions 8 .github/draft-release.yml → .github/workflows/draft-release.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tests/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
Licensed under the apache-2.0 license, except as noted below. | ||
| ||
Licensed under the apache-2.0 license, except as noted below. | ||
| ||
Redistribution and use in source and binary forms, with or without modification, | ||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
| ||
* Redistributions of source code must retain the above copyright/ digital rights | ||
legend, this list of conditions and the following Notice. | ||
| ||
* Redistributions in binary form must reproduce the above copyright copyright/digital | ||
rights legend, this list of conditions and the following Notice in the documentation | ||
and/or other materials provided with the distribution. | ||
- Redistributions of source code must retain the above copyright/ digital rights | ||
legend, this list of conditions and the following Notice. | ||
| ||
* Neither the name of The MITRE Corporation nor the names of its contributors may be | ||
used to endorse or promote products derived from this software without specific prior | ||
written permission. | ||
- Redistributions in binary form must reproduce the above copyright copyright/digital | ||
rights legend, this list of conditions and the following Notice in the documentation | ||
and/or other materials provided with the distribution. | ||
| ||
- Neither the name of The MITRE Corporation nor the names of its contributors may be | ||
used to endorse or promote products derived from this software without specific prior | ||
written permission. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,80 @@ | ||
# saf-lambda-function | ||
| ||
AWS Lambda Function for the [SAF CLI](https://github.com/mitre/saf-lambda-function) | ||
| ||
This is based on [saf_action](https://github.com/mitre/saf) | ||
| ||
## Input and Output Arguments | ||
| ||
### Input | ||
| ||
#### `command_string` (Required) | ||
| ||
Command string to be executed by SAF CLI. The action will run `saf <command_string>`. | ||
| ||
Example: | ||
| ||
- `convert:asff2hdf -i asff-findings.json -o output-file-name.json` | ||
- More examples can be found at [SAF CLI Usage](https://github.com/mitre/saf#usage) | ||
- NOTE: This action does not support `view:heimdall`. | ||
| ||
### Output | ||
| ||
As determined by input command. | ||
| ||
## Secrets | ||
| ||
This action does not use any GitHub secrets at this time. | ||
| ||
## Example | ||
| ||
Below is an example action. | ||
| ||
``` | ||
on: [push] | ||
jobs: | ||
saf_hdf_conversion: | ||
runs-on: ubuntu-latest | ||
name: SAF CLI Convert ASFF to HDF | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
- name: Convert ASFF | ||
uses: mitre/saf_action@v1 | ||
with: | ||
command_string: 'convert:asff2hdf -i asff_sample.json -o asff_sample_hdf.json' | ||
- name: Artifacts | ||
uses: actions/upload-artifact@v1 | ||
if: success() | ||
with: | ||
name: asff | ||
path: asff_sample_hdf.json | ||
``` | ||
| ||
## Contributing, Issues and Support | ||
| ||
### Contributing | ||
| ||
Please feel free to look through our issues, make a fork and submit PRs and improvements. We love hearing from our end-users and the community and will be happy to engage with you on suggestions, updates, fixes or new capabilities. | ||
| ||
### Issues and Support | ||
| ||
Please feel free to contact us by **opening an issue** on the issue board, or, at [saf@mitre.org](mailto:saf@mitre.org) should you have any suggestions, questions or issues. | ||
| ||
### NOTICE | ||
| ||
© 2022 The MITRE Corporation. | ||
| ||
Approved for Public Release; Distribution Unlimited. Case Number 18-3678. | ||
| ||
### NOTICE | ||
| ||
MITRE hereby grants express written permission to use, reproduce, distribute, modify, and otherwise leverage this software to the extent permitted by the licensed terms provided in the LICENSE.md file included with this project. | ||
| ||
### NOTICE | ||
| ||
This software was produced for the U. S. Government under Contract Number HHSM-500-2012-00008I, and is subject to Federal Acquisition Regulation Clause 52.227-14, Rights in Data-General. | ||
| ||
No other use other than that granted to the U. S. Government, or to those acting on behalf of the U. S. Government under that Clause is authorized without the express written permission of The MITRE Corporation. | ||
| ||
For further information, please contact The MITRE Corporation, Contracts Management Office, 7515 Colshire Drive, McLean, VA 22102-7539, (703) 983-6000. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,166 @@ | ||||||||||
// const axios = require('axios') | ||||||||||
// const url = 'http://checkip.amazonaws.com/'; | ||||||||||
const aws = require('aws-sdk'); | ||||||||||
const s3 = new aws.S3({ apiVersion: '2006-03-01' }); | ||||||||||
//const core = require('@actions/core'); specific to github actions | ||||||||||
const saf = require('@mitre/saf'); | ||||||||||
const fs = require('fs'); | ||||||||||
const path = require("path"); | ||||||||||
//const winston = require('winston'); | ||||||||||
//const logging = require('utils/logging'); | ||||||||||
const winston = require('winston'); | ||||||||||
const { createLogger, format, transports } = winston; | ||||||||||
let response; | ||||||||||
| ||||||||||
/** | ||||||||||
* | ||||||||||
* Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format | ||||||||||
* @param {Object} event - API Gateway Lambda Proxy Input Format | ||||||||||
* | ||||||||||
* Context doc: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html | ||||||||||
* @param {Object} context | ||||||||||
* | ||||||||||
* Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html | ||||||||||
* @returns {Object} object - API Gateway Lambda Proxy Output Format | ||||||||||
* | ||||||||||
*/ | ||||||||||
| ||||||||||
exports.lambdaHandler = async (event, context) => { | ||||||||||
| ||||||||||
const logger = createLogger({ | ||||||||||
level: process.env.LOG_LEVEL || 'debug', | ||||||||||
format: format.combine( | ||||||||||
format.timestamp(), | ||||||||||
format.simple() | ||||||||||
), | ||||||||||
transports: [ | ||||||||||
new transports.Console({ | ||||||||||
format: format.combine( | ||||||||||
format.timestamp({ | ||||||||||
format: 'YYYY-MM-DDTHH:mm:ss.SSSZ', | ||||||||||
}), | ||||||||||
format.printf( | ||||||||||
info => `${[info.timestamp]}\t${context.awsRequestId}\t${logger.level.toUpperCase()}\t${info.message}`, | ||||||||||
) | ||||||||||
) | ||||||||||
}) | ||||||||||
] | ||||||||||
}) | ||||||||||
| ||||||||||
logger.log({ | ||||||||||
level: 'debug', | ||||||||||
message: 'Logging Level set to : ' + logger.level.toUpperCase() | ||||||||||
}); | ||||||||||
| ||||||||||
// TODO: Decide is we want to catch undefined saf-cli command groupings | ||||||||||
// https://stackoverflow.com/questions/15201939/jquery-javascript-check-string-for-multiple-substringsa | ||||||||||
// TODO: Removed hardcoded data and move to lambda paramaters | ||||||||||
const HEC_TOKEN = "473b3297-1d88-4740-96ff-e6048e51b785"; | ||||||||||
const SPLUNK_SERVER = "splk1.efficacy.online"; | ||||||||||
Comment on lines +58 to +59 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. Might want to remove this and clear git history Suggested change
| ||||||||||
const CLI_COMMAND = "convert" | ||||||||||
const CLI_FUNCTION = "hdf2splunk" | ||||||||||
| ||||||||||
// TODO: Add the rest of the parameters | ||||||||||
/* | ||||||||||
- -t HEC_TOKEN | ||||||||||
- -i HDF_FILE | ||||||||||
- SPLUNK_PORT(defults to 8089) | ||||||||||
- SPLUNK_INDEX(defauls to HEC default ) | ||||||||||
- INSECURE(ignore_ssl) | ||||||||||
- PROTOCOL(defults to https) | ||||||||||
- DEBUG - for logging in lambda logging | ||||||||||
*/ | ||||||||||
| ||||||||||
// TODO: Remove in final release | ||||||||||
logger.debug("Loading Function"); | ||||||||||
logger.debug("Received context:" + JSON.stringify(context)); | ||||||||||
| ||||||||||
// Get the object from the event and show its content type | ||||||||||
const bucket = event.Records[0].s3.bucket.name; | ||||||||||
| ||||||||||
const key = decodeURIComponent( | ||||||||||
event.Records[0].s3.object.key.replace(/\+/g, " ") | ||||||||||
); | ||||||||||
| ||||||||||
// READ PARAMS | ||||||||||
const params = { | ||||||||||
Bucket: bucket, | ||||||||||
Key: key, | ||||||||||
}; | ||||||||||
| ||||||||||
try { | ||||||||||
// const ret = await axios(url); | ||||||||||
| ||||||||||
logger.info("Read from bucket: " + params.Bucket); | ||||||||||
logger.info("Reading File: " + params.Key); | ||||||||||
| ||||||||||
let { ContentType, Body } = await s3.getObject(params).promise(); | ||||||||||
| ||||||||||
logger.debug("Received File ContentType - " + ContentType); | ||||||||||
| ||||||||||
let HDF_FILE = path.resolve('/tmp/', params.Key.toString()); | ||||||||||
| ||||||||||
Body = Body.toString(); | ||||||||||
| ||||||||||
const command_string = [CLI_COMMAND+':'+CLI_FUNCTION, '-i', HDF_FILE, '-H', SPLUNK_SERVER, '-t', HEC_TOKEN ]; | ||||||||||
| ||||||||||
await fs.writeFileSync(HDF_FILE, Body) | ||||||||||
| ||||||||||
logger.info("Wrote file into runtime environment: " + HDF_FILE); | ||||||||||
| ||||||||||
//const data = fs.readFileSync(HDF_FILE, "utf8" ); | ||||||||||
| ||||||||||
logger.debug("Finished reading object type: " + JSON.stringify(ContentType)); | ||||||||||
| ||||||||||
// TODO: Remove the hardcoded saf-cli command | ||||||||||
// TODO: Remove the || | ||||||||||
/* TODO: Add the rest of the possible options to the command_string builder | ||||||||||
- SPLUNK_PORT (defults to 8089) | ||||||||||
- SPLUNK_INDEX (defauls to HEC default) | ||||||||||
- INSECURE (ignore_ssl) | ||||||||||
- PROTOCOL (defults to https) | ||||||||||
- DEBUG - for logging in lambda logging | ||||||||||
*/ | ||||||||||
| ||||||||||
if (!command_string) { | ||||||||||
throw new Error("SAF CLI Command String argument is required. See http://saf-cli.mitre.org for more details."); | ||||||||||
} | ||||||||||
| ||||||||||
if (CLI_COMMAND.trim() === "view" && CLI_FUNCTION.trim() === "heimdall") { | ||||||||||
throw new Error( | ||||||||||
"You cannot use the 'saf view:heimdall' command in this environment." | ||||||||||
); | ||||||||||
} | ||||||||||
| ||||||||||
logger.debug("command_string: " + command_string.join(' ')); | ||||||||||
| ||||||||||
// Normal logging - perhpas we add a 'silent' to just have an ACK at the end | ||||||||||
logger.info("Pushing HDF Data: " + HDF_FILE + " to server: " + SPLUNK_SERVER) | ||||||||||
| ||||||||||
let saf_cli_response = await saf.run(command_string); | ||||||||||
| ||||||||||
response = { | ||||||||||
'statusCode': 200, | ||||||||||
'body': JSON.stringify({ | ||||||||||
message: saf_cli_response | ||||||||||
}) | ||||||||||
} | ||||||||||
} catch (err) { | ||||||||||
logger.info(err); | ||||||||||
return err; | ||||||||||
} | ||||||||||
| ||||||||||
return response | ||||||||||
}; | ||||||||||
| ||||||||||
| ||||||||||
exports.handler = async function(event, context) { | ||||||||||
console.log("ENVIRONMENT VARIABLES\n" + JSON.stringify(process.env, null, 2)) | ||||||||||
console.info("EVENT\n" + JSON.stringify(event, null, 2)) | ||||||||||
console.warn("Event not processed.") | ||||||||||
return context.logStreamName | ||||||||||
} | ||||||||||
| ||||||||||
| ||||||||||
| ||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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.
Why do you want to draft releases on pull requests?