First Commit of new way of building the Platform
This commit is contained in:
1 parent
e48cf0dae5
commit
40e60b42cc
4616 files changed
+713402
No files matched your search
+332
@@ -0,0 +1,332 @@
|
||||
var aws4 = exports,
|
||||
url = require('url'),
|
||||
querystring = require('querystring'),
|
||||
crypto = require('crypto'),
|
||||
lru = require('./lru'),
|
||||
credentialsCache = lru(1000)
|
||||
|
||||
// http://docs.amazonwebservices.com/general/latest/gr/signature-version-4.html
|
||||
|
||||
function hmac(key, string, encoding) {
|
||||
return crypto.createHmac('sha256', key).update(string, 'utf8').digest(encoding)
|
||||
}
|
||||
|
||||
function hash(string, encoding) {
|
||||
return crypto.createHash('sha256').update(string, 'utf8').digest(encoding)
|
||||
}
|
||||
|
||||
// This function assumes the string has already been percent encoded
|
||||
function encodeRfc3986(urlEncodedString) {
|
||||
return urlEncodedString.replace(/[!'()*]/g, function(c) {
|
||||
return '%' + c.charCodeAt(0).toString(16).toUpperCase()
|
||||
})
|
||||
}
|
||||
|
||||
// request: { path | body, [host], [method], [headers], [service], [region] }
|
||||
// credentials: { accessKeyId, secretAccessKey, [sessionToken] }
|
||||
function RequestSigner(request, credentials) {
|
||||
|
||||
if (typeof request === 'string') request = url.parse(request)
|
||||
|
||||
var headers = request.headers = (request.headers || {}),
|
||||
hostParts = this.matchHost(request.hostname || request.host || headers.Host || headers.host)
|
||||
|
||||
this.request = request
|
||||
this.credentials = credentials || this.defaultCredentials()
|
||||
|
||||
this.service = request.service || hostParts[0] || ''
|
||||
this.region = request.region || hostParts[1] || 'us-east-1'
|
||||
|
||||
// SES uses a different domain from the service name
|
||||
if (this.service === 'email') this.service = 'ses'
|
||||
|
||||
if (!request.method && request.body)
|
||||
request.method = 'POST'
|
||||
|
||||
if (!headers.Host && !headers.host) {
|
||||
headers.Host = request.hostname || request.host || this.createHost()
|
||||
|
||||
// If a port is specified explicitly, use it as is
|
||||
if (request.port)
|
||||
headers.Host += ':' + request.port
|
||||
}
|
||||
if (!request.hostname && !request.host)
|
||||
request.hostname = headers.Host || headers.host
|
||||
|
||||
this.isCodeCommitGit = this.service === 'codecommit' && request.method === 'GIT'
|
||||
}
|
||||
|
||||
RequestSigner.prototype.matchHost = function(host) {
|
||||
var match = (host || '').match(/([^\.]+)\.(?:([^\.]*)\.)?amazonaws\.com(\.cn)?$/)
|
||||
var hostParts = (match || []).slice(1, 3)
|
||||
|
||||
// ES's hostParts are sometimes the other way round, if the value that is expected
|
||||
// to be region equals ‘es’ switch them back
|
||||
// e.g. search-cluster-name-aaaa00aaaa0aaa0aaaaaaa0aaa.us-east-1.es.amazonaws.com
|
||||
if (hostParts[1] === 'es')
|
||||
hostParts = hostParts.reverse()
|
||||
|
||||
return hostParts
|
||||
}
|
||||
|
||||
// http://docs.aws.amazon.com/general/latest/gr/rande.html
|
||||
RequestSigner.prototype.isSingleRegion = function() {
|
||||
// Special case for S3 and SimpleDB in us-east-1
|
||||
if (['s3', 'sdb'].indexOf(this.service) >= 0 && this.region === 'us-east-1') return true
|
||||
|
||||
return ['cloudfront', 'ls', 'route53', 'iam', 'importexport', 'sts']
|
||||
.indexOf(this.service) >= 0
|
||||
}
|
||||
|
||||
RequestSigner.prototype.createHost = function() {
|
||||
var region = this.isSingleRegion() ? '' :
|
||||
(this.service === 's3' && this.region !== 'us-east-1' ? '-' : '.') + this.region,
|
||||
service = this.service === 'ses' ? 'email' : this.service
|
||||
return service + region + '.amazonaws.com'
|
||||
}
|
||||
|
||||
RequestSigner.prototype.prepareRequest = function() {
|
||||
this.parsePath()
|
||||
|
||||
var request = this.request, headers = request.headers, query
|
||||
|
||||
if (request.signQuery) {
|
||||
|
||||
this.parsedPath.query = query = this.parsedPath.query || {}
|
||||
|
||||
if (this.credentials.sessionToken)
|
||||
query['X-Amz-Security-Token'] = this.credentials.sessionToken
|
||||
|
||||
if (this.service === 's3' && !query['X-Amz-Expires'])
|
||||
query['X-Amz-Expires'] = 86400
|
||||
|
||||
if (query['X-Amz-Date'])
|
||||
this.datetime = query['X-Amz-Date']
|
||||
else
|
||||
query['X-Amz-Date'] = this.getDateTime()
|
||||
|
||||
query['X-Amz-Algorithm'] = 'AWS4-HMAC-SHA256'
|
||||
query['X-Amz-Credential'] = this.credentials.accessKeyId + '/' + this.credentialString()
|
||||
query['X-Amz-SignedHeaders'] = this.signedHeaders()
|
||||
|
||||
} else {
|
||||
|
||||
if (!request.doNotModifyHeaders && !this.isCodeCommitGit) {
|
||||
if (request.body && !headers['Content-Type'] && !headers['content-type'])
|
||||
headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
|
||||
|
||||
if (request.body && !headers['Content-Length'] && !headers['content-length'])
|
||||
headers['Content-Length'] = Buffer.byteLength(request.body)
|
||||
|
||||
if (this.credentials.sessionToken && !headers['X-Amz-Security-Token'] && !headers['x-amz-security-token'])
|
||||
headers['X-Amz-Security-Token'] = this.credentials.sessionToken
|
||||
|
||||
if (this.service === 's3' && !headers['X-Amz-Content-Sha256'] && !headers['x-amz-content-sha256'])
|
||||
headers['X-Amz-Content-Sha256'] = hash(this.request.body || '', 'hex')
|
||||
|
||||
if (headers['X-Amz-Date'] || headers['x-amz-date'])
|
||||
this.datetime = headers['X-Amz-Date'] || headers['x-amz-date']
|
||||
else
|
||||
headers['X-Amz-Date'] = this.getDateTime()
|
||||
}
|
||||
|
||||
delete headers.Authorization
|
||||
delete headers.authorization
|
||||
}
|
||||
}
|
||||
|
||||
RequestSigner.prototype.sign = function() {
|
||||
if (!this.parsedPath) this.prepareRequest()
|
||||
|
||||
if (this.request.signQuery) {
|
||||
this.parsedPath.query['X-Amz-Signature'] = this.signature()
|
||||
} else {
|
||||
this.request.headers.Authorization = this.authHeader()
|
||||
}
|
||||
|
||||
this.request.path = this.formatPath()
|
||||
|
||||
return this.request
|
||||
}
|
||||
|
||||
RequestSigner.prototype.getDateTime = function() {
|
||||
if (!this.datetime) {
|
||||
var headers = this.request.headers,
|
||||
date = new Date(headers.Date || headers.date || new Date)
|
||||
|
||||
this.datetime = date.toISOString().replace(/[:\-]|\.\d{3}/g, '')
|
||||
|
||||
// Remove the trailing 'Z' on the timestamp string for CodeCommit git access
|
||||
if (this.isCodeCommitGit) this.datetime = this.datetime.slice(0, -1)
|
||||
}
|
||||
return this.datetime
|
||||
}
|
||||
|
||||
RequestSigner.prototype.getDate = function() {
|
||||
return this.getDateTime().substr(0, 8)
|
||||
}
|
||||
|
||||
RequestSigner.prototype.authHeader = function() {
|
||||
return [
|
||||
'AWS4-HMAC-SHA256 Credential=' + this.credentials.accessKeyId + '/' + this.credentialString(),
|
||||
'SignedHeaders=' + this.signedHeaders(),
|
||||
'Signature=' + this.signature(),
|
||||
].join(', ')
|
||||
}
|
||||
|
||||
RequestSigner.prototype.signature = function() {
|
||||
var date = this.getDate(),
|
||||
cacheKey = [this.credentials.secretAccessKey, date, this.region, this.service].join(),
|
||||
kDate, kRegion, kService, kCredentials = credentialsCache.get(cacheKey)
|
||||
if (!kCredentials) {
|
||||
kDate = hmac('AWS4' + this.credentials.secretAccessKey, date)
|
||||
kRegion = hmac(kDate, this.region)
|
||||
kService = hmac(kRegion, this.service)
|
||||
kCredentials = hmac(kService, 'aws4_request')
|
||||
credentialsCache.set(cacheKey, kCredentials)
|
||||
}
|
||||
return hmac(kCredentials, this.stringToSign(), 'hex')
|
||||
}
|
||||
|
||||
RequestSigner.prototype.stringToSign = function() {
|
||||
return [
|
||||
'AWS4-HMAC-SHA256',
|
||||
this.getDateTime(),
|
||||
this.credentialString(),
|
||||
hash(this.canonicalString(), 'hex'),
|
||||
].join('\n')
|
||||
}
|
||||
|
||||
RequestSigner.prototype.canonicalString = function() {
|
||||
if (!this.parsedPath) this.prepareRequest()
|
||||
|
||||
var pathStr = this.parsedPath.path,
|
||||
query = this.parsedPath.query,
|
||||
headers = this.request.headers,
|
||||
queryStr = '',
|
||||
normalizePath = this.service !== 's3',
|
||||
decodePath = this.service === 's3' || this.request.doNotEncodePath,
|
||||
decodeSlashesInPath = this.service === 's3',
|
||||
firstValOnly = this.service === 's3',
|
||||
bodyHash
|
||||
|
||||
if (this.service === 's3' && this.request.signQuery) {
|
||||
bodyHash = 'UNSIGNED-PAYLOAD'
|
||||
} else if (this.isCodeCommitGit) {
|
||||
bodyHash = ''
|
||||
} else {
|
||||
bodyHash = headers['X-Amz-Content-Sha256'] || headers['x-amz-content-sha256'] ||
|
||||
hash(this.request.body || '', 'hex')
|
||||
}
|
||||
|
||||
if (query) {
|
||||
queryStr = encodeRfc3986(querystring.stringify(Object.keys(query).sort().reduce(function(obj, key) {
|
||||
if (!key) return obj
|
||||
obj[key] = !Array.isArray(query[key]) ? query[key] :
|
||||
(firstValOnly ? query[key][0] : query[key].slice().sort())
|
||||
return obj
|
||||
}, {})))
|
||||
}
|
||||
if (pathStr !== '/') {
|
||||
if (normalizePath) pathStr = pathStr.replace(/\/{2,}/g, '/')
|
||||
pathStr = pathStr.split('/').reduce(function(path, piece) {
|
||||
if (normalizePath && piece === '..') {
|
||||
path.pop()
|
||||
} else if (!normalizePath || piece !== '.') {
|
||||
if (decodePath) piece = decodeURIComponent(piece)
|
||||
path.push(encodeRfc3986(encodeURIComponent(piece)))
|
||||
}
|
||||
return path
|
||||
}, []).join('/')
|
||||
if (pathStr[0] !== '/') pathStr = '/' + pathStr
|
||||
if (decodeSlashesInPath) pathStr = pathStr.replace(/%2F/g, '/')
|
||||
}
|
||||
|
||||
return [
|
||||
this.request.method || 'GET',
|
||||
pathStr,
|
||||
queryStr,
|
||||
this.canonicalHeaders() + '\n',
|
||||
this.signedHeaders(),
|
||||
bodyHash,
|
||||
].join('\n')
|
||||
}
|
||||
|
||||
RequestSigner.prototype.canonicalHeaders = function() {
|
||||
var headers = this.request.headers
|
||||
function trimAll(header) {
|
||||
return header.toString().trim().replace(/\s+/g, ' ')
|
||||
}
|
||||
return Object.keys(headers)
|
||||
.sort(function(a, b) { return a.toLowerCase() < b.toLowerCase() ? -1 : 1 })
|
||||
.map(function(key) { return key.toLowerCase() + ':' + trimAll(headers[key]) })
|
||||
.join('\n')
|
||||
}
|
||||
|
||||
RequestSigner.prototype.signedHeaders = function() {
|
||||
return Object.keys(this.request.headers)
|
||||
.map(function(key) { return key.toLowerCase() })
|
||||
.sort()
|
||||
.join(';')
|
||||
}
|
||||
|
||||
RequestSigner.prototype.credentialString = function() {
|
||||
return [
|
||||
this.getDate(),
|
||||
this.region,
|
||||
this.service,
|
||||
'aws4_request',
|
||||
].join('/')
|
||||
}
|
||||
|
||||
RequestSigner.prototype.defaultCredentials = function() {
|
||||
var env = process.env
|
||||
return {
|
||||
accessKeyId: env.AWS_ACCESS_KEY_ID || env.AWS_ACCESS_KEY,
|
||||
secretAccessKey: env.AWS_SECRET_ACCESS_KEY || env.AWS_SECRET_KEY,
|
||||
sessionToken: env.AWS_SESSION_TOKEN,
|
||||
}
|
||||
}
|
||||
|
||||
RequestSigner.prototype.parsePath = function() {
|
||||
var path = this.request.path || '/',
|
||||
queryIx = path.indexOf('?'),
|
||||
query = null
|
||||
|
||||
if (queryIx >= 0) {
|
||||
query = querystring.parse(path.slice(queryIx + 1))
|
||||
path = path.slice(0, queryIx)
|
||||
}
|
||||
|
||||
// S3 doesn't always encode characters > 127 correctly and
|
||||
// all services don't encode characters > 255 correctly
|
||||
// So if there are non-reserved chars (and it's not already all % encoded), just encode them all
|
||||
if (/[^0-9A-Za-z!'()*\-._~%/]/.test(path)) {
|
||||
path = path.split('/').map(function(piece) {
|
||||
return encodeURIComponent(decodeURIComponent(piece))
|
||||
}).join('/')
|
||||
}
|
||||
|
||||
this.parsedPath = {
|
||||
path: path,
|
||||
query: query,
|
||||
}
|
||||
}
|
||||
|
||||
RequestSigner.prototype.formatPath = function() {
|
||||
var path = this.parsedPath.path,
|
||||
query = this.parsedPath.query
|
||||
|
||||
if (!query) return path
|
||||
|
||||
// Services don't support empty query string keys
|
||||
if (query[''] != null) delete query['']
|
||||
|
||||
return path + '?' + encodeRfc3986(querystring.stringify(query))
|
||||
}
|
||||
|
||||
aws4.RequestSigner = RequestSigner
|
||||
|
||||
aws4.sign = function(request, credentials) {
|
||||
return new RequestSigner(request, credentials).sign()
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
module.exports = function(size) {
|
||||
return new LruCache(size)
|
||||
}
|
||||
|
||||
function LruCache(size) {
|
||||
this.capacity = size | 0
|
||||
this.map = Object.create(null)
|
||||
this.list = new DoublyLinkedList()
|
||||
}
|
||||
|
||||
LruCache.prototype.get = function(key) {
|
||||
var node = this.map[key]
|
||||
if (node == null) return undefined
|
||||
this.used(node)
|
||||
return node.val
|
||||
}
|
||||
|
||||
LruCache.prototype.set = function(key, val) {
|
||||
var node = this.map[key]
|
||||
if (node != null) {
|
||||
node.val = val
|
||||
} else {
|
||||
if (!this.capacity) this.prune()
|
||||
if (!this.capacity) return false
|
||||
node = new DoublyLinkedNode(key, val)
|
||||
this.map[key] = node
|
||||
this.capacity--
|
||||
}
|
||||
this.used(node)
|
||||
return true
|
||||
}
|
||||
|
||||
LruCache.prototype.used = function(node) {
|
||||
this.list.moveToFront(node)
|
||||
}
|
||||
|
||||
LruCache.prototype.prune = function() {
|
||||
var node = this.list.pop()
|
||||
if (node != null) {
|
||||
delete this.map[node.key]
|
||||
this.capacity++
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function DoublyLinkedList() {
|
||||
this.firstNode = null
|
||||
this.lastNode = null
|
||||
}
|
||||
|
||||
DoublyLinkedList.prototype.moveToFront = function(node) {
|
||||
if (this.firstNode == node) return
|
||||
|
||||
this.remove(node)
|
||||
|
||||
if (this.firstNode == null) {
|
||||
this.firstNode = node
|
||||
this.lastNode = node
|
||||
node.prev = null
|
||||
node.next = null
|
||||
} else {
|
||||
node.prev = null
|
||||
node.next = this.firstNode
|
||||
node.next.prev = node
|
||||
this.firstNode = node
|
||||
}
|
||||
}
|
||||
|
||||
DoublyLinkedList.prototype.pop = function() {
|
||||
var lastNode = this.lastNode
|
||||
if (lastNode != null) {
|
||||
this.remove(lastNode)
|
||||
}
|
||||
return lastNode
|
||||
}
|
||||
|
||||
DoublyLinkedList.prototype.remove = function(node) {
|
||||
if (this.firstNode == node) {
|
||||
this.firstNode = node.next
|
||||
} else if (node.prev != null) {
|
||||
node.prev.next = node.next
|
||||
}
|
||||
if (this.lastNode == node) {
|
||||
this.lastNode = node.prev
|
||||
} else if (node.next != null) {
|
||||
node.next.prev = node.prev
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function DoublyLinkedNode(key, val) {
|
||||
this.key = key
|
||||
this.val = val
|
||||
this.prev = null
|
||||
this.next = null
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"aws4@1.8.0",
|
||||
"C:\\Users\\Luis\\source\\repos\\AAMasters\\TaskServer"
|
||||
]
|
||||
],
|
||||
"_from": "aws4@1.8.0",
|
||||
"_id": "aws4@1.8.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==",
|
||||
"_location": "/aws4",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "aws4@1.8.0",
|
||||
"name": "aws4",
|
||||
"escapedName": "aws4",
|
||||
"rawSpec": "1.8.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "1.8.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/request"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/aws4/-/aws4-1.8.0.tgz",
|
||||
"_spec": "1.8.0",
|
||||
"_where": "C:\\Users\\Luis\\source\\repos\\AAMasters\\TaskServer",
|
||||
"author": {
|
||||
"name": "Michael Hart",
|
||||
"email": "michael.hart.au@gmail.com",
|
||||
"url": "http://github.com/mhart"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/mhart/aws4/issues"
|
||||
},
|
||||
"description": "Signs and prepares requests using AWS Signature Version 4",
|
||||
"devDependencies": {
|
||||
"mocha": "^2.4.5",
|
||||
"should": "^8.2.2"
|
||||
},
|
||||
"homepage": "https://github.com/mhart/aws4#readme",
|
||||
"keywords": [
|
||||
"amazon",
|
||||
"aws",
|
||||
"signature",
|
||||
"s3",
|
||||
"ec2",
|
||||
"autoscaling",
|
||||
"cloudformation",
|
||||
"elasticloadbalancing",
|
||||
"elb",
|
||||
"elasticbeanstalk",
|
||||
"cloudsearch",
|
||||
"dynamodb",
|
||||
"kinesis",
|
||||
"lambda",
|
||||
"glacier",
|
||||
"sqs",
|
||||
"sns",
|
||||
"iam",
|
||||
"sts",
|
||||
"ses",
|
||||
"swf",
|
||||
"storagegateway",
|
||||
"datapipeline",
|
||||
"directconnect",
|
||||
"redshift",
|
||||
"opsworks",
|
||||
"rds",
|
||||
"monitoring",
|
||||
"cloudtrail",
|
||||
"cloudfront",
|
||||
"codedeploy",
|
||||
"elasticache",
|
||||
"elasticmapreduce",
|
||||
"elastictranscoder",
|
||||
"emr",
|
||||
"cloudwatch",
|
||||
"mobileanalytics",
|
||||
"cognitoidentity",
|
||||
"cognitosync",
|
||||
"cognito",
|
||||
"containerservice",
|
||||
"ecs",
|
||||
"appstream",
|
||||
"keymanagementservice",
|
||||
"kms",
|
||||
"config",
|
||||
"cloudhsm",
|
||||
"route53",
|
||||
"route53domains",
|
||||
"logs"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "aws4.js",
|
||||
"name": "aws4",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/mhart/aws4.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha ./test/fast.js ./test/slow.js -b -t 100s -R list"
|
||||
},
|
||||
"version": "1.8.0"
|
||||
}
|
||||
Reference in new issue
Block a user