forked from iann0036/cloud9-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
176 lines (176 loc) · 6.64 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var vscode = require("vscode");
var path = require("path");
var aws4 = require("aws4");
var request = require("request");
var xml2js = require("xml2js");
var awsSession = null;
function promptForMFAIfRequired() {
return new Promise(function (resolve, reject) {
var extensionConfig = vscode.workspace.getConfiguration('cloud9sync');
if (extensionConfig.get('mfaSerial') != "") {
vscode.window.showInputBox({
placeHolder: "",
prompt: "Enter your MFA code.",
value: "",
ignoreFocusOut: false
}).then(function (mfa_token) {
resolve("&TokenCode=" + mfa_token);
});
}
else {
resolve("");
}
});
}
function GetAWSCreds() {
return new Promise(function (resolve, reject) {
var extensionConfig = vscode.workspace.getConfiguration('cloud9sync');
var awsregion = extensionConfig.get('region');
if (extensionConfig.get('accessKey') == "" ||
extensionConfig.get('secretKey') == "" ||
!extensionConfig.get('accessKey') ||
!extensionConfig.get('secretKey')) {
resolve(null);
}
if (awsSession) {
resolve(awsSession['value']);
}
else if (extensionConfig.get('mfaSerial') != "" || extensionConfig.get('assumeRole') != "") {
var path_1 = "/?Version=2011-06-15&DurationSeconds=" + extensionConfig.get('sessionDuration');
if (extensionConfig.get('mfaSerial') != "") {
path_1 += "&SerialNumber=" + extensionConfig.get('mfaSerial');
}
if (extensionConfig.get('assumeRole') != "") {
path_1 += '&Action=AssumeRole&RoleSessionName=VSCodeCloud9&RoleArn=' + extensionConfig.get('assumeRole');
}
else {
path_1 += '&Action=GetSessionToken';
}
promptForMFAIfRequired().then(function (mfa_token) {
path_1 += mfa_token;
var awsreq = aws4.sign({
service: 'sts',
region: 'us-east-1',
method: 'POST',
path: path_1,
headers: {
'Content-Type': 'application/x-amz-json-1.1'
},
}, {
secretAccessKey: extensionConfig.get('secretKey'),
accessKeyId: extensionConfig.get('accessKey')
});
request.post({
url: "https://" + awsreq.hostname + awsreq.path,
headers: awsreq.headers,
body: awsreq.body,
rejectUnauthorized: false,
proxy: GetProxy()
}, function (err, httpResponse, cred_response) {
console.log(httpResponse);
xml2js.parseString(cred_response, {
ignoreAttrs: true
}, function (err, result) {
// TODO: Handle failure here
var creds = null;
if (extensionConfig.get('assumeRole') != "") {
creds = result['AssumeRoleResponse']['AssumeRoleResult'][0]['Credentials'][0];
}
else {
creds = result['GetSessionTokenResponse']['GetSessionTokenResult'][0]['Credentials'][0];
}
awsSession = {
'value': {
secretAccessKey: creds['SecretAccessKey'][0],
accessKeyId: creds['AccessKeyId'][0],
sessionToken: creds['SessionToken'][0]
},
'expiry': creds['Expiration'][0]
};
// TODO: Handle refresh here
resolve(awsSession['value']);
});
});
});
}
else {
resolve({
secretAccessKey: extensionConfig.get('secretKey'),
accessKeyId: extensionConfig.get('accessKey')
});
}
});
}
exports.GetAWSCreds = GetAWSCreds;
function FileNameToUri(fileName) {
var rootPath = "";
vscode.workspace.workspaceFolders.forEach(function (workspaceFolder) {
if (workspaceFolder.uri.scheme == "file") {
rootPath = workspaceFolder.uri.fsPath;
}
});
var filePath = path.join(rootPath, fileName);
return vscode.Uri.file(filePath);
}
exports.FileNameToUri = FileNameToUri;
function ShortenFilePath(filePath) {
var rootPath = "";
vscode.workspace.workspaceFolders.forEach(function (workspaceFolder) {
if (workspaceFolder.uri.scheme == "file") {
rootPath = workspaceFolder.uri.fsPath;
}
});
return EnsureLeadingSlash(filePath.replace(rootPath, "").replace("\\", "/"));
}
exports.ShortenFilePath = ShortenFilePath;
function GetShortFilePath(document) {
var filePath = document.fileName;
var rootPath = "";
if (document.uri.scheme == "cloud9") {
filePath = "/" + document.uri.path.split("/").slice(2).join('/');
}
vscode.workspace.workspaceFolders.forEach(function (workspaceFolder) {
if (workspaceFolder.uri.scheme == "file") {
rootPath = workspaceFolder.uri.fsPath;
}
});
return filePath.replace(rootPath, "").replace("\\", "/");
}
exports.GetShortFilePath = GetShortFilePath;
function EnsureLeadingSlash(str) {
if (str[0] == '/' || str[0] == '\\')
return str;
return "/" + str;
}
exports.EnsureLeadingSlash = EnsureLeadingSlash;
function GetRegion() {
var extensionConfig = vscode.workspace.getConfiguration('cloud9sync');
return extensionConfig.get('region');
}
exports.GetRegion = GetRegion;
function GetProxy() {
var extensionConfig = vscode.workspace.getConfiguration('cloud9sync');
var proxy = extensionConfig.get('proxy');
if (proxy == "")
return null;
return proxy;
}
exports.GetProxy = GetProxy;
function ReducePromises(array, fn) {
var results = [];
return array.reduce(function (p, item) {
return p.then(function () {
return fn(item).then(function (data) {
results.push(data);
return results;
}).catch(function (y) {
console.error(y);
});
}).catch(function (x) {
console.error(x);
});
}, Promise.resolve());
}
exports.ReducePromises = ReducePromises;