-
-
Notifications
You must be signed in to change notification settings - Fork 87
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
Adding HCL v2 (Terraform v12) language support #51
Merged
outsideris
merged 1 commit into
outsideris:master
from
eu-evops:feature/add-hclv2-support-terraform-v12
Dec 28, 2020
Merged
Changes from all commits
Commits
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
This file contains 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,13 +1,16 @@ | ||
const { readFile } = require('fs'); | ||
const { basename, extname, join } = require('path'); | ||
const { promisify } = require('util'); | ||
const hcl = require('gopher-hcl'); | ||
const tar = require('tar'); | ||
const { Duplex } = require('stream'); | ||
const { URLSearchParams } = require('url'); | ||
const recursive = require('recursive-readdir'); | ||
const _ = require('lodash'); | ||
const tmp = require('tmp'); | ||
const debug = require('debug')('citizen:server'); | ||
const util = require('util'); | ||
|
||
const hcl = require('@evops/hcl-terraform-parser'); | ||
|
||
const readFileProm = promisify(readFile); | ||
|
||
|
@@ -20,22 +23,37 @@ const makeUrl = (req, search) => { | |
}; | ||
|
||
const ignore = (file, stats) => { | ||
if ((stats.isDirectory() || extname(file) === '.tf') && !basename(file).startsWith('._')) { | ||
if (stats.isDirectory()) { | ||
return false; | ||
} | ||
|
||
if (extname(file) === '.tf') { | ||
return false; | ||
} | ||
|
||
if (!basename(file).startsWith('._')) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
const hclToJson = async (filePath) => { | ||
const content = await readFileProm(filePath); | ||
const json = hcl.parse(content.toString()); | ||
|
||
return json; | ||
return hcl.parse(content.toString(), filePath); | ||
}; | ||
|
||
const extractDefinition = async (files, targetPath) => { | ||
const tfFiles = files.filter((f) => { | ||
// Need to constraint lookup to files within target path | ||
// otherwise we are exposing ourselves to FS based security | ||
// attacks | ||
if (f.indexOf(targetPath) !== 0) { | ||
return false; | ||
} | ||
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. Good point! |
||
|
||
const relativePath = f.replace(targetPath, ''); | ||
|
||
if (relativePath.lastIndexOf('/') > 0) { | ||
return false; | ||
} | ||
|
@@ -51,36 +69,54 @@ const extractDefinition = async (files, targetPath) => { | |
return _.reduce(list, (l, accum) => _.merge(accum, l), {}); | ||
}; | ||
|
||
// As module information is stored directly | ||
// in the database, we need to normalise the | ||
// object keys to ensure no invalid characters | ||
// in the names e.g. full stop (.) | ||
const normalizeKeyNamesForDbStorage = (obj) => { | ||
if (!obj) { | ||
return obj; | ||
} | ||
|
||
const result = {}; | ||
Object.keys(obj).forEach((key) => { | ||
const newKey = key.replace(/[^\w\d_]/g, '__'); | ||
result[newKey] = obj[key]; | ||
}); | ||
return result; | ||
}; | ||
|
||
const nomarlizeModule = (module) => ({ | ||
path: '', | ||
name: module.name || '', | ||
readme: '', | ||
empty: !module, | ||
inputs: module.variable | ||
? Object.keys(module.variable).map((name) => ({ name, ...module.variable[name] })) | ||
: [], | ||
outputs: module.output | ||
? Object.keys(module.output).map((name) => ({ name, ...module.output[name] })) | ||
: [], | ||
inputs: normalizeKeyNamesForDbStorage(module.inputs), | ||
outputs: normalizeKeyNamesForDbStorage(module.outputs), | ||
dependencies: [], | ||
resources: module.resource | ||
? Object.keys(module.resource).map((type) => ({ name: Object.keys(module.resource)[0], type })) | ||
: [], | ||
module_calls: normalizeKeyNamesForDbStorage(module.module_calls), | ||
resources: normalizeKeyNamesForDbStorage(module.managed_resources), | ||
}); | ||
|
||
const extractSubmodules = async (definition, files, targetPath) => { | ||
let pathes = []; | ||
if (definition.module) { | ||
const submodules = Object.keys(definition.module).map((key) => definition.module[key].source); | ||
pathes = _.uniq(submodules); | ||
let submodulePaths = []; | ||
if (definition.module_calls) { | ||
const submodules = Object.keys(definition.module_calls) | ||
.map((k) => definition.module_calls[k].source); | ||
|
||
submodulePaths = _.uniq(submodules); | ||
} | ||
|
||
const promises = pathes.map(async (p) => { | ||
const promises = submodulePaths.map(async (p) => { | ||
const data = await extractDefinition(files, join(targetPath, p)); | ||
// Submodule was not found in the archive, ignore | ||
if (Object.keys(data).length === 0) { | ||
return []; | ||
} | ||
data.name = p.substr(p.lastIndexOf('/') + 1); | ||
|
||
let result = [data]; | ||
if (data.module) { | ||
if (data.module_calls) { | ||
const m = await extractSubmodules(data, files, join(targetPath, p)); | ||
result = result.concat(m); | ||
} | ||
|
@@ -104,11 +140,15 @@ const parseHcl = (moduleName, compressedModule) => new Promise((resolve, reject) | |
try { | ||
const files = await recursive(tempDir, [ignore]); | ||
|
||
debug('Files found in the archive: %s', files); | ||
|
||
// make a root module definition | ||
const rootData = await extractDefinition(files, tempDir); | ||
rootData.name = moduleName; | ||
const rootDefinition = nomarlizeModule(rootData); | ||
|
||
debug('Module definition: %s', util.inspect(rootDefinition, false, 15)); | ||
|
||
// make submodules definition | ||
const submodulesData = await extractSubmodules(rootData, files, tempDir); | ||
const submodulesDefinition = submodulesData.map((s) => nomarlizeModule(s)); | ||
|
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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
Binary file not shown.
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.
filePath
doesn't needed here.