forked from novking/aws-profile-handler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
129 lines (108 loc) · 4.35 KB
/
index.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
'use strict';
const path = require('path');
const os = require('os');
const Ini = require('./lib/ini');
const Utils = require('./lib/utils');
const defaultFilePath = path.join(os.homedir(), '.aws', 'credentials');
class awsProfileHandler {
/**
* @deprecated
* @param filePath
*/
constructor(filePath) {
const defaultFilePath = path.join(os.homedir(), '.aws', 'credentials');
this.filePath = filePath || defaultFilePath;
this.profileObject = Ini.decodeIniData(Utils.readFile(this.filePath));
}
/**
* @deprecated
* @return {Array}
*/
listProfiles() {
return Object.keys(this.profileObject);
}
/**
* @deprecated
* @param profile
* @return {*}
*/
getProfileCredentials(profile) {
let credentials = this.profileObject[profile];
if (!credentials) return null;
else return Utils.deepCopy(credentials);
}
/**
* @deprecated
* @param profile
*/
deleteProfile(profile) {
let outputProfileObject = Utils.deepCopy(this.profileObject);
delete outputProfileObject[profile];
let encodedProfile = Ini.encodeIniFormat(outputProfileObject);
Utils.writeFile(this.filePath, encodedProfile);
}
/**
* @deprecated
* @param profile
* @param credentials
*/
addProfile(profile, credentials) {
let outputProfileObject = Utils.deepCopy(this.profileObject);
outputProfileObject[profile] = credentials;
let encodedProfile = Ini.encodeIniFormat(outputProfileObject);
Utils.writeFile(this.filePath, encodedProfile);
}
static listProfiles(filePath) {
let credentialPath = filePath || defaultFilePath;
let profileObject = Ini.decodeIniData(Utils.readFile(credentialPath));
return Object.keys(profileObject);
}
static getProfileCredentials(profile, filePath) {
if (!profile || profile.trim().length === 0) {
throw new Error('Invalid Input: profile name cannot be omitted nor only contains white spaces.');
}
let credentialPath = filePath || defaultFilePath;
let profileObject = Ini.decodeIniData(Utils.readFile(credentialPath));
let credentials = profileObject[profile];
if (!credentials) return null;
else return Utils.deepCopy(credentials);
}
static deleteProfile(profile, filePath) {
if (!profile || profile.trim().length === 0) {
throw new Error('Invalid Input: profile name cannot be omitted nor only contains white spaces.');
}
let credentialPath = filePath || defaultFilePath;
let profileObject = Ini.decodeIniData(Utils.readFile(credentialPath));
// profileObject will never be 'null'. worst case is {}
let outputProfileObject = Utils.deepCopy(profileObject);
delete outputProfileObject[profile];
let encodedProfile = Ini.encodeIniFormat(outputProfileObject);
Utils.writeFile(credentialPath, encodedProfile);
}
static addProfile(profile, credentials, filePath) {
if (!profile || profile.trim().length === 0) {
throw new Error('Invalid Input: profile name cannot be omitted nor only contains white spaces.');
}
if (!credentials || Object.keys(credentials).length === 0) {
throw new Error('Invalid Input: credentials cannot be omitted nor empty.');
}
if (Object.keys(credentials).length !== 2 ||
!this.isValidSchema(credentials) &&
!this.isValidAltSchema(credentials)) {
throw new Error('Invalid input: credentials schema is invalid.');
}
let credentialPath = filePath || defaultFilePath;
let profileObject = Ini.decodeIniData(Utils.readFile(credentialPath));
let outputProfileObject = Utils.deepCopy(profileObject);
outputProfileObject[profile] = credentials;
let encodedProfile = Ini.encodeIniFormat(outputProfileObject);
Utils.writeFile(credentialPath, encodedProfile);
}
static isValidSchema(credentials) {
return (credentials.hasOwnProperty('aws_access_key_id') && credentials.hasOwnProperty('aws_secret_access_key'))
}
static isValidAltSchema(credentials) {
return (credentials.hasOwnProperty('role_arn') && credentials.hasOwnProperty('source_profile'))
}
}
module.exports = awsProfileHandler;