Skip to content

Commit

Permalink
[Artifacts] Save md5 hash for each artifact upload (#1494)
Browse files Browse the repository at this point in the history
* Hash artifact upload using md5

* Add imports

* Small tweaks

* PR feedback

* PR Feedback
  • Loading branch information
konradpabjan committed Aug 15, 2023
1 parent 45c49b0 commit c9dab8c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 14 deletions.
7 changes: 7 additions & 0 deletions packages/artifact/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/artifact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@azure/storage-blob": "^12.15.0",
"@protobuf-ts/plugin": "^2.2.3-alpha.1",
"archiver": "^5.3.1",
"crypto": "^1.0.1",
"jwt-decode": "^3.1.2",
"twirp-ts": "^2.5.0"
},
Expand Down
34 changes: 27 additions & 7 deletions packages/artifact/src/internal/upload/blob-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {TransferProgressEvent} from '@azure/core-http'
import {ZipUploadStream} from './zip'
import {getUploadChunkSize} from '../shared/config'
import * as core from '@actions/core'
import * as crypto from 'crypto'
import * as stream from 'stream'

export interface BlobUploadResponse {
/**
Expand All @@ -14,6 +16,11 @@ export interface BlobUploadResponse {
* The total reported upload size in bytes. Empty if the upload failed
*/
uploadSize?: number

/**
* The MD5 hash of the uploaded file. Empty if the upload failed
*/
md5Hash?: string
}

export async function uploadZipToBlobStorage(
Expand Down Expand Up @@ -41,15 +48,31 @@ export async function uploadZipToBlobStorage(
onProgress: uploadCallback
}

let md5Hash: string | undefined = undefined
const uploadStream = new stream.PassThrough()
const hashStream = crypto.createHash('md5')

zipUploadStream.pipe(uploadStream) // This stream is used for the upload
zipUploadStream.pipe(hashStream).setEncoding('hex') // This stream is used to compute a hash of the zip content that gets used. Integrity check

try {
core.info('Beginning upload of artifact content to blob storage')

await blockBlobClient.uploadStream(
zipUploadStream,
uploadStream,
bufferSize,
maxBuffers,
options
)

core.info('Finished uploading artifact content to blob storage!')

hashStream.end()
md5Hash = hashStream.read() as string
core.info(`MD5 hash of uploaded artifact zip is ${md5Hash}`)

} catch (error) {
core.info(`Failed to upload artifact zip to blob storage, error: ${error}`)
core.warning(`Failed to upload artifact zip to blob storage, error: ${error}`)
return {
isSuccess: false
}
Expand All @@ -62,12 +85,9 @@ export async function uploadZipToBlobStorage(
}
}

core.info(
`Successfully uploaded all artifact file content. Total reported size: ${uploadByteCount}`
)

return {
isSuccess: true,
uploadSize: uploadByteCount
uploadSize: uploadByteCount,
md5Hash: md5Hash
}
}
38 changes: 32 additions & 6 deletions packages/artifact/src/internal/upload/upload-artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import {
validateRootDirectory
} from './upload-zip-specification'
import {getBackendIdsFromToken} from '../shared/util'
import {CreateArtifactRequest} from 'src/generated'
import {uploadZipToBlobStorage} from './blob-upload'
import {createZipUploadStream} from './zip'
import {
CreateArtifactRequest,
FinalizeArtifactRequest,
StringValue
} from '../../generated'

export async function uploadArtifact(
name: string,
Expand All @@ -39,7 +43,9 @@ export async function uploadArtifact(
// get the IDs needed for the artifact creation
const backendIds = getBackendIdsFromToken()
if (!backendIds.workflowRunBackendId || !backendIds.workflowJobRunBackendId) {
core.warning(`Failed to get backend ids`)
core.warning(
`Failed to get the necessary backend ids which are required to create the artifact`
)
return {
success: false
}
Expand Down Expand Up @@ -77,30 +83,50 @@ export async function uploadArtifact(
}

// Upload zip to blob storage
const uploadResult = await uploadZipToBlobStorage(createArtifactResp.signedUploadUrl, zipUploadStream)
const uploadResult = await uploadZipToBlobStorage(
createArtifactResp.signedUploadUrl,
zipUploadStream
)
if (uploadResult.isSuccess === false) {
return {
success: false
}
}

// finalize the artifact
const finalizeArtifactResp = await artifactClient.FinalizeArtifact({
const finalizeArtifactReq: FinalizeArtifactRequest = {
workflowRunBackendId: backendIds.workflowRunBackendId,
workflowJobRunBackendId: backendIds.workflowJobRunBackendId,
name: name,
size: uploadResult.uploadSize!.toString()
})
}

if (uploadResult.md5Hash) {
finalizeArtifactReq.hash = StringValue.create({
value: `md5:${uploadResult.md5Hash!}`
})
}

core.info(`Finalizing artifact upload`)

const finalizeArtifactResp = await artifactClient.FinalizeArtifact(
finalizeArtifactReq
)
if (!finalizeArtifactResp.ok) {
core.warning(`Failed to finalize artifact`)
return {
success: false
}
}

const artifactId = BigInt(finalizeArtifactResp.artifactId)
core.info(
`Artifact ${name}.zip successfully finalized. Artifact ID ${artifactId}`
)

return {
success: true,
size: uploadResult.uploadSize,
id: parseInt(finalizeArtifactResp.artifactId) // TODO - will this be a problem due to the id being a bigint?
id: Number(artifactId)
}
}
4 changes: 3 additions & 1 deletion packages/artifact/src/internal/upload/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ const zipErrorCallback = (error: any): void => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const zipWarningCallback = (error: any): void => {
if (error.code === 'ENOENT') {
core.warning('ENOENT warning during artifact zip creation. No such file or directory')
core.warning(
'ENOENT warning during artifact zip creation. No such file or directory'
)
core.info(error)
} else {
core.warning(
Expand Down

0 comments on commit c9dab8c

Please sign in to comment.