-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
178 additions
and
27 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[template] | ||
src = "cloudfront-pk.pem.tmpl" | ||
dest = "/etc/ssl/private/cloudfront-pk.pem" | ||
keys = [ | ||
"WEBRESOURCES_CLOUDFRONT_PRIVATEKEY", | ||
] | ||
mode = "0400" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{base64Decode (getenv "WEBRESOURCES_CLOUDFRONT_PRIVATEKEY" "")}} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { webResourceHandler } from '@balena/pinejs'; | ||
import { | ||
CloudFrontHandler, | ||
CloudFrontHandlerProps, | ||
} from '@balena/pinejs-webresource-cloudfront'; | ||
import { | ||
WEBRESOURCES_S3_ACCESS_KEY, | ||
WEBRESOURCES_S3_SECRET_KEY, | ||
WEBRESOURCES_S3_REGION, | ||
WEBRESOURCES_S3_HOST, | ||
WEBRESOURCES_S3_BUCKET, | ||
WEBRESOURCES_S3_MAX_FILESIZE, | ||
WEBRESOURCES_CLOUDFRONT_PRIVATEKEY, | ||
WEBRESOURCES_CLOUDFRONT_PUBLICKEY, | ||
WEBRESOURCES_CLOUDFRONT_HOST, | ||
} from './lib/config'; | ||
|
||
const getS3Config = (): webResourceHandler.S3HandlerProps | undefined => { | ||
if ( | ||
WEBRESOURCES_S3_ACCESS_KEY != null && | ||
WEBRESOURCES_S3_SECRET_KEY != null && | ||
WEBRESOURCES_S3_REGION != null && | ||
WEBRESOURCES_S3_HOST != null && | ||
WEBRESOURCES_S3_BUCKET != null | ||
) { | ||
return { | ||
accessKey: WEBRESOURCES_S3_ACCESS_KEY, | ||
secretKey: WEBRESOURCES_S3_SECRET_KEY, | ||
region: WEBRESOURCES_S3_REGION, | ||
endpoint: WEBRESOURCES_S3_HOST, | ||
bucket: WEBRESOURCES_S3_BUCKET, | ||
maxSize: WEBRESOURCES_S3_MAX_FILESIZE, | ||
}; | ||
} | ||
}; | ||
|
||
const getCloudfrontConfig = (): CloudFrontHandlerProps | undefined => { | ||
const s3Config = getS3Config(); | ||
if ( | ||
s3Config != null && | ||
WEBRESOURCES_CLOUDFRONT_PRIVATEKEY != null && | ||
WEBRESOURCES_CLOUDFRONT_PUBLICKEY != null && | ||
WEBRESOURCES_CLOUDFRONT_HOST != null | ||
) { | ||
return { | ||
cfPublicKeyId: WEBRESOURCES_CLOUDFRONT_PUBLICKEY, | ||
cfSecretKey: WEBRESOURCES_CLOUDFRONT_PRIVATEKEY, | ||
cfDistributionDomain: WEBRESOURCES_CLOUDFRONT_HOST, | ||
...s3Config, | ||
}; | ||
} | ||
}; | ||
|
||
let handler: webResourceHandler.WebResourceHandler | undefined; | ||
export const getFileUploadHandler = () => { | ||
if (handler == null) { | ||
const cfConfig = getCloudfrontConfig(); | ||
if (cfConfig != null) { | ||
handler = new CloudFrontHandler(cfConfig); | ||
console.log('Successfully initialised webresource CloudFront handler.'); | ||
console.log({ | ||
region: cfConfig.region, | ||
endpoint: cfConfig.endpoint, | ||
bucket: cfConfig.bucket, | ||
cFhost: cfConfig.cfDistributionDomain, | ||
}); | ||
return handler; | ||
} | ||
|
||
const s3Config = getS3Config(); | ||
if (s3Config != null) { | ||
handler = new webResourceHandler.S3Handler(s3Config); | ||
console.log('Successfully initialised webresource S3 handler.'); | ||
console.log({ | ||
region: s3Config.region, | ||
endpoint: s3Config.endpoint, | ||
bucket: s3Config.bucket, | ||
}); | ||
return handler; | ||
} | ||
|
||
console.log('No webresource handler loaded.'); | ||
} | ||
return handler; | ||
}; |
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