Skip to content

Commit 3bddfc7

Browse files
Upload scan details as artifacts instead of directly uploading to S3
1 parent 083f6ee commit 3bddfc7

13 files changed

+22
-175
lines changed

README.md

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,8 @@ The following parameters can be set in config files or in env variables:
4040
- SONARQUBE_TOKEN: SonarQube access token. Optional parameter, must be set when ACL is configured for the project.
4141
More details about access tokens - https://docs.sonarqube.org/latest/user-guide/user-token/
4242

43-
- AWS_S3_BUCKET: AWS S3 Bucket name. Bucket must be already exist
44-
- AWS_S3_PREFIX: AWS S3 Key prefix. Optional parameter. Must end with slash
45-
4643
- All variables starting with prefix `AUTH0` corresponds to Auth0 related credentials
4744

48-
### AWS Credentials
49-
* Please refer to following [documentation](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/getting-your-credentials.html) in order to get AWS credentials.
50-
* Depending on your Operating System, create AWS credentials file in the path listed below
51-
```
52-
Linux, Unix, and macOS users: ~/.aws/credentials
53-
Windows users: C:\Users\USER_NAME\.aws\credentials
54-
```
55-
Credentials file should look like below
56-
```
57-
[default]
58-
aws_access_key_id = SOME_ACCESS_KEY_ID
59-
aws_secret_access_key = SOME_SECRET_ACCESS_KEY
60-
```
61-
* Credentials can be set in environment variables. [Documentation](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-environment.html)
62-
6345
## Local deployment
6446

6547
1. From the project root directory, run the following command to install the dependencies
@@ -112,7 +94,7 @@ To run the Submission quality processor using docker, follow the below steps
11294

11395
2. Rename the file `sample.api.env` to `api.env`
11496

115-
3. Set the required Auth0, Submission API URL, SonarQube config and AWS S3 configuration in the file `api.env`
97+
3. Set the required Auth0, Submission API URL and SonarQube config in the file `api.env`
11698

11799
4. Once that is done, run the following command
118100

config/default.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ module.exports = {
1111
SONARQUBE_HOST: process.env.SONARQUBE_HOST || 'http://localhost:9000',
1212
SONARQUBE_TOKEN: process.env.SONARQUBE_TOKEN,
1313

14-
AWS_S3_BUCKET: process.env.AWS_S3_BUCKET,
15-
AWS_S3_PREFIX: process.env.AWS_S3_PREFIX || '',
16-
1714
AUTH0_URL: process.env.AUTH0_URL,
1815
AUTH0_AUDIENCE: process.env.AUTH0_AUDIENCE,
1916
TOKEN_CACHE_TIME: process.env.TOKEN_CACHE_TIME,

config/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
module.exports = {
66
AUTH0_URL: 'http://test', // Since we use Nock, just dummy value stored
7-
AWS_S3_BUCKET: 'dummy-bucket'
7+
SONARQUBE_HOST: 'http://localhost',
8+
SONARQUBE_TOKEN: 'access-token'
89
}

docker/sample.api.env

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ AUTH0_AUDIENCE=<Auth0 Audience>
33
AUTH0_CLIENT_ID=<Auth0 Client ID>
44
AUTH0_CLIENT_SECRET=<Auth0 Client Secret>
55
SUBMISSION_API_URL=<Submission API Endpoint>
6-
AWS_S3_BUCKET=<S3 bucket>
7-
SONARQUBE_HOST=<SonarQube Host>
6+
SONARQUBE_HOST=<SonarQube Host>

package-lock.json

Lines changed: 0 additions & 81 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"standard": "^12.0.1"
2828
},
2929
"dependencies": {
30-
"aws-sdk": "^2.503.0",
3130
"bluebird": "^3.5.5",
3231
"body-parser": "^1.19.0",
3332
"config": "^2.0.2",

src/app.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const cors = require('cors')
1010
const bodyParser = require('body-parser')
1111
const httpStatus = require('http-status-codes')
1212
const _ = require('lodash')
13-
const sonarService = require('./services/SonarService')
1413
const helper = require('./common/helper')
1514
const logger = require('./common/logger')
1615
const routes = require('./routes')
@@ -32,10 +31,6 @@ app.use(cors())
3231
app.use(bodyParser.json())
3332
app.use(bodyParser.urlencoded({ extended: true }))
3433

35-
// configure sonarqube service
36-
sonarService.configure(config.get('SONARQUBE_HOST'),
37-
config.has('SONARQUBE_TOKEN') ? config.get('SONARQUBE_TOKEN') : undefined)
38-
3934
const apiRouter = express.Router()
4035

4136
app.use(healthcheck.middleware([check]))

src/common/helper.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@
55
const config = require('config')
66
const _ = require('lodash')
77
const request = require('superagent')
8-
const AWS = require('aws-sdk')
98

109
const m2mAuth = require('tc-core-library-js').auth.m2m
1110
const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'AUTH0_PROXY_SERVER_URL']))
1211

13-
AWS.config.region = config.REGION
14-
const s3 = new AWS.S3()
15-
const AWS_S3_BUCKET = config.get('AWS_S3_BUCKET')
16-
const AWS_S3_PREFIX = config.get('AWS_S3_PREFIX')
17-
1812
/**
1913
* Wrap async function to standard express function
2014
* @param {Function} fn the async function
@@ -116,29 +110,9 @@ const reqToV5APIWithFile = async (path, formData, fileFieldName) => {
116110
.attach(fileFieldName, formData[fileFieldName].data, formData[fileFieldName].name)
117111
}
118112

119-
/**
120-
* Upload specified content on s3.
121-
* @param {String} key s3 file key
122-
* @param {Object} content Content to upload
123-
* @param {String} contentType Content type
124-
*
125-
* @returns {Promise}
126-
*/
127-
const s3upload = async (key, content, contentType) => {
128-
return new Promise((resolve, reject) => {
129-
s3.upload({
130-
Bucket: AWS_S3_BUCKET,
131-
Key: `${AWS_S3_PREFIX}${key}`,
132-
ContentType: contentType,
133-
Body: content
134-
}, err => err ? reject(err) : resolve())
135-
})
136-
}
137-
138113
module.exports = {
139114
wrapExpress,
140115
autoWrapExpress,
141116
reqToSubmissionAPI,
142-
reqToV5APIWithFile,
143-
s3upload
117+
reqToV5APIWithFile
144118
}

src/services/SonarService.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const request = require('superagent')
55
const _ = require('lodash')
66
const joi = require('joi')
77
const logger = require('../common/logger')
8+
const config = require('config')
89

910
// List of issue types that metrics and details must be collected for
1011
const ISSUE_TYPES = ['code_smells', 'bugs', 'security_hotspots', 'vulnerabilities']
@@ -34,8 +35,8 @@ const ISSUES_SCHEMA = joi.object().keys({
3435
}).unknown(true).required()
3536

3637
// Configuration
37-
let SONARQUBE_HOST
38-
let SONARQUBE_TOKEN
38+
const SONARQUBE_HOST = config.get('SONARQUBE_HOST')
39+
const SONARQUBE_TOKEN = config.has('SONARQUBE_TOKEN') ? config.get('SONARQUBE_TOKEN') : undefined
3940

4041
/**
4142
* Send GET request to SonarQube's endpoint
@@ -185,24 +186,8 @@ getScanResults.schema = joi.object().keys({
185186
analysedAt: joi.string().required()
186187
}).required()
187188

188-
/**
189-
* Configure SonarQube service
190-
* @param {String} sqHost SonarQube host
191-
* @param {String} sqToken SonarQube token
192-
*/
193-
function configure (sqHost, sqToken) {
194-
SONARQUBE_HOST = sqHost
195-
SONARQUBE_TOKEN = sqToken
196-
}
197-
198-
configure.schema = joi.object().keys({
199-
sqHost: joi.string().required(),
200-
sqToken: joi.string()
201-
}).required()
202-
203189
module.exports = {
204-
getScanResults,
205-
configure
190+
getScanResults
206191
}
207192

208193
logger.buildService(module.exports)

src/services/SubmissionService.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const config = require('config')
77
const logger = require('../common/logger')
88
const helper = require('../common/helper')
99

10-
const RESULT_FILE_NAME = 'SonarQubeResults'
1110
const TYPE_ID = 'c56a4180-65aa-42ec-a945-5fd21dec0501'
1211

1312
/**
@@ -33,26 +32,31 @@ updateSubmissionStatus.schema = joi.object().keys({
3332
})
3433
/**
3534
* Upload submission artifacts
35+
* @param {String} submissionId The submission id to upload the artifact to
36+
* @param {String} filename Artifact filename
37+
* @param {String} typeId Associated artifact type id
3638
* @param {Object} body Artifacts content
37-
*
3839
* @returns {Promise}
3940
*/
40-
async function uploadArtifacts (body) {
41+
async function uploadArtifacts (submissionId, filename, typeId, body) {
4142
const zip = new JSZip()
42-
zip.file(`${RESULT_FILE_NAME}.json`, JSON.stringify(body, null, 2))
43+
zip.file(`${filename}.json`, JSON.stringify(body, null, 2))
4344
const content = await zip.generateAsync({ type: 'nodebuffer' })
4445
const artifactPayload = {
4546
artifact: {
46-
name: `${RESULT_FILE_NAME}.zip`,
47+
name: `${filename}.zip`,
4748
data: content
4849
},
49-
typeId: TYPE_ID
50+
typeId: typeId
5051
}
5152

52-
return helper.reqToV5APIWithFile(`${config.SUBMISSION_API_URL}/submissions/${body.project.key}/artifacts`, artifactPayload, 'artifact')
53+
return helper.reqToV5APIWithFile(`${config.SUBMISSION_API_URL}/submissions/${submissionId}/artifacts`, artifactPayload, 'artifact')
5354
}
5455

5556
uploadArtifacts.schema = joi.object().keys({
57+
submissionId: joi.string().required(),
58+
filename: joi.string().required(),
59+
typeId: joi.string().required(),
5660
body: joi.object().required()
5761
})
5862

src/services/WebhookService.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
const joi = require('joi')
66
const logger = require('../common/logger')
77
const uuid = require('uuid/v4')
8-
const { s3upload } = require('../common/helper')
98

109
const { updateSubmissionStatus, uploadArtifacts } = require('./SubmissionService')
1110
const { getScanResults } = require('./SonarService')
@@ -27,12 +26,12 @@ async function processScanResults (body) {
2726
scoreCardId: randomId
2827
})
2928

30-
await uploadArtifacts(body)
29+
await uploadArtifacts(body.project.key, 'SonarQubeSummary', 'c56a4180-65aa-42ec-a945-5fd21dec0501', body) // temporary type id
3130
})(),
3231

3332
(async () => {
3433
const scanResults = await getScanResults(body.project.key, body.analysedAt)
35-
await s3upload(`${body.project.key}.json`, JSON.stringify(scanResults), 'application/json')
34+
await uploadArtifacts(body.project.key, 'SonarQubeDetails', '50b917df-5b81-4081-8e20-7fc8a6aabe54', scanResults) // temporary type id
3635
})()
3736
])
3837
}

0 commit comments

Comments
 (0)