1
1
import * as vscode from "vscode" ;
2
2
import untildify from "untildify" ;
3
- import * as tmp from "tmp" ;
4
- import * as fs from "fs" ;
5
3
import * as util from "./util" ;
6
4
import { Vault } from "ansible-vault" ;
7
5
@@ -30,17 +28,44 @@ export function activate(context: vscode.ExtensionContext) {
30
28
31
29
// Read `ansible.cfg`
32
30
const rootPath = util . getRootPath ( logs , editor . document . uri ) ;
33
- const otherPath = util . findAnsibleCfgFile ( logs , rootPath ) ;
34
- let keyInCfg : string , vaultIds : false | Array < string > ;
31
+ let otherPath = util . findAnsibleCfgFile ( logs , rootPath , "ansible.cfg" ) ;
32
+
33
+ if ( otherPath !== undefined ) {
34
+ otherPath = util . verifyAnsibleDirectory ( logs , editor . document . uri , otherPath ) ;
35
+ }
36
+
37
+
38
+ let keyInCfg : string ,
39
+ vaultIds : false | Array < string > ,
40
+ vaultPass : false | { [ key : string ] : string } ;
35
41
// eslint-disable-next-line prefer-const
36
- [ keyInCfg , vaultIds ] = util . scanAnsibleCfg ( logs , otherPath , rootPath ) ;
42
+ [ keyInCfg , vaultIds , vaultPass ] = util . scanAnsibleCfg (
43
+ logs ,
44
+ otherPath ,
45
+ rootPath
46
+ ) ;
47
+
48
+ const vaultId = await encryptVaultId ( vaultIds ) ;
37
49
38
50
// Extract `ansible-vault` password
39
51
if ( keyInCfg ) {
40
52
logs . appendLine ( `Getting vault keyfile from ${ keyInCfg } ` ) ;
41
53
vscode . window . showInformationMessage (
42
54
`Getting vault keyfile from ${ keyInCfg } `
43
55
) ;
56
+ if ( vaultPass ) {
57
+ if ( vaultPass [ "default" ] !== undefined ) {
58
+ pass = util . findPassword ( logs , rootPath , vaultPass [ "default" ] ) ;
59
+ } else if ( vaultPass [ vaultId ] !== undefined ) {
60
+ pass = util . findPassword ( logs , rootPath , vaultPass [ vaultId ] ) ;
61
+ } else {
62
+ // Handle case when neither default nor vaultId specific password is found
63
+ vscode . window . showErrorMessage (
64
+ "No password found for the specified vault ID."
65
+ ) ;
66
+ return ;
67
+ }
68
+ }
44
69
} else {
45
70
logs . appendLine ( `Found nothing from config files` ) ;
46
71
@@ -64,10 +89,6 @@ export function activate(context: vscode.ExtensionContext) {
64
89
pass = val ;
65
90
} ) ;
66
91
}
67
-
68
- keypath = tmp . tmpNameSync ( ) ;
69
- fs . writeFileSync ( keypath , pass , "utf8" ) ;
70
- logs . appendLine ( `Wrote password to temporary file: '${ keypath } '` ) ;
71
92
}
72
93
}
73
94
@@ -79,14 +100,8 @@ export function activate(context: vscode.ExtensionContext) {
79
100
80
101
if ( type === "plaintext" ) {
81
102
logs . appendLine ( `Encrypt selected text` ) ;
82
- const vaultId = await encryptVaultId ( vaultIds ) ;
83
103
84
- let encryptedText = await encryptInline (
85
- text ,
86
- rootPath ,
87
- pass ,
88
- vaultId ,
89
- ) ;
104
+ let encryptedText = await encryptInline ( text , rootPath , pass , vaultId ) ;
90
105
encryptedText = "!vault |\n" + encryptedText ;
91
106
editor . edit ( ( editBuilder ) => {
92
107
editBuilder . replace (
@@ -99,17 +114,26 @@ export function activate(context: vscode.ExtensionContext) {
99
114
} ) ;
100
115
} else if ( type === "encrypted" ) {
101
116
logs . appendLine ( `Decrypt selected text` ) ;
102
- const test = text . replace ( '!vault |' , '' ) . trim ( ) . replace ( / [ ^ \S \r \n ] + / gm, '' ) ;
103
- logs . appendLine ( test ) ;
117
+ const test = text
118
+ . replace ( "!vault |" , "" )
119
+ . trim ( )
120
+ . replace ( / [ ^ \S \r \n ] + / gm, "" ) ;
104
121
const decryptedText = await decryptInline (
105
- text . replace ( '!vault |' , '' ) . trim ( ) . replace ( / [ ^ \S \r \n ] + / gm, '' ) ,
122
+ text
123
+ . replace ( "!vault |" , "" )
124
+ . trim ( )
125
+ . replace ( / [ ^ \S \r \n ] + / gm, "" ) ,
106
126
rootPath ,
107
127
pass ,
108
- await encryptVaultId ( vaultIds )
128
+ vaultId
109
129
) ;
110
- editor . edit ( ( editBuilder ) => {
111
- editBuilder . replace ( selection , decryptedText ) ;
112
- } ) ;
130
+ if ( decryptedText === undefined ) {
131
+ vscode . window . showErrorMessage ( `Decryption failed: Invalid Vault` ) ;
132
+ } else {
133
+ editor . edit ( ( editBuilder ) => {
134
+ editBuilder . replace ( selection , decryptedText ) ;
135
+ } ) ;
136
+ }
113
137
}
114
138
} else {
115
139
const content = editor . document . getText ( ) ;
@@ -122,7 +146,7 @@ export function activate(context: vscode.ExtensionContext) {
122
146
content ,
123
147
rootPath ,
124
148
pass ,
125
- await encryptVaultId ( vaultIds )
149
+ vaultId
126
150
) ;
127
151
editor . edit ( ( builder ) => {
128
152
builder . replace (
@@ -143,27 +167,26 @@ export function activate(context: vscode.ExtensionContext) {
143
167
content ,
144
168
rootPath ,
145
169
pass ,
146
- await encryptVaultId ( vaultIds )
170
+ vaultId
147
171
) ;
148
- editor . edit ( ( builder ) => {
149
- builder . replace (
150
- new vscode . Range (
151
- doc . lineAt ( 0 ) . range . start ,
152
- doc . lineAt ( doc . lineCount - 1 ) . range . end
153
- ) ,
154
- decryptedText
172
+ if ( decryptedText === undefined ) {
173
+ vscode . window . showErrorMessage ( `Decryption failed: Invalid Vault` ) ;
174
+ } else {
175
+ editor . edit ( ( builder ) => {
176
+ builder . replace (
177
+ new vscode . Range (
178
+ doc . lineAt ( 0 ) . range . start ,
179
+ doc . lineAt ( doc . lineCount - 1 ) . range . end
180
+ ) ,
181
+ decryptedText
182
+ ) ;
183
+ } ) ;
184
+ vscode . window . showInformationMessage (
185
+ `File decrypted: '${ doc . fileName } '`
155
186
) ;
156
- } ) ;
157
- vscode . window . showInformationMessage (
158
- `File decrypted: '${ doc . fileName } '`
159
- ) ;
187
+ }
160
188
}
161
189
}
162
-
163
- if ( ! ! pass && ! ! keypath ) {
164
- fs . unlinkSync ( keypath ) ;
165
- logs . appendLine ( `Removed temporary file: '${ keypath } '` ) ;
166
- }
167
190
} ;
168
191
169
192
const selectVaultId = async ( ) => {
@@ -174,17 +197,23 @@ export function activate(context: vscode.ExtensionContext) {
174
197
let otherPath = undefined ;
175
198
if ( editor ) {
176
199
rootPath = util . getRootPath ( logs , editor . document . uri ) ;
177
- otherPath = util . findAnsibleCfgFile ( logs , rootPath ) ;
200
+ otherPath = util . findAnsibleCfgFile ( logs , rootPath , "ansible.cfg" ) ;
178
201
} else {
179
202
vscode . window . showWarningMessage (
180
203
"No editor opened! Failed to determine current workspace root folder"
181
204
) ;
182
205
}
183
206
const config = vscode . workspace . getConfiguration ( "ansibleVault" ) ;
184
207
185
- let keyInCfg : string , vaultIds : false | Array < string > ;
208
+ let keyInCfg : string ,
209
+ vaultIds : false | Array < string > ,
210
+ vaultPass : false | { [ key : string ] : string } ;
186
211
// eslint-disable-next-line prefer-const
187
- [ keyInCfg , vaultIds ] = util . scanAnsibleCfg ( logs , otherPath , rootPath ) ;
212
+ [ keyInCfg , vaultIds , vaultPass ] = util . scanAnsibleCfg (
213
+ logs ,
214
+ otherPath ,
215
+ rootPath
216
+ ) ;
188
217
// Try to get vault list from workspace config
189
218
if ( ! keyInCfg && ! ! config . keyfile && isVaultIdList ( config . keyfile ) ) {
190
219
vaultIds = util . getVaultIdList ( config . keyfile ) ;
@@ -276,21 +305,23 @@ const decryptInline = async (
276
305
encryptVaultId : any
277
306
) => {
278
307
const vault = new Vault ( { password : pass } ) ;
308
+ let decryptedContent = undefined ;
279
309
280
310
try {
281
311
if ( encryptVaultId ) {
282
- const decryptedContent = await vault . decrypt ( text , encryptVaultId ) ;
283
- return < string > decryptedContent ;
312
+ decryptedContent = await vault . decrypt ( text , encryptVaultId ) ;
284
313
} else {
285
- const decryptedContent = await vault . decrypt ( text , "" ) ;
286
- return < string > decryptedContent ;
314
+ decryptedContent = await vault . decrypt ( text , "" ) ;
287
315
}
288
316
} catch ( error : any ) {
289
317
console . error ( "Decryption failed:" , error ) ;
290
318
vscode . window . showErrorMessage ( `Decryption failed: ${ error . message } ` ) ;
291
- throw error ;
319
+ // Instead of throwing an error, return the original text
320
+ return text ;
292
321
}
322
+ return < string > decryptedContent ;
293
323
} ;
324
+
294
325
const encryptVaultId = async ( vaultIds : false | Array < string > ) => {
295
326
if ( ! vaultIds ) {
296
327
return "" ;
0 commit comments