-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathvaultSettingsTab.component.ts
168 lines (145 loc) · 5.63 KB
/
vaultSettingsTab.component.ts
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
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { Component, HostBinding } from '@angular/core'
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
import { BaseComponent, VaultService, VaultSecret, Vault, PlatformService, ConfigService, VAULT_SECRET_TYPE_FILE, PromptModalComponent, VaultFileSecret, TranslateService } from 'tabby-core'
import { SetVaultPassphraseModalComponent } from './setVaultPassphraseModal.component'
import { ShowSecretModalComponent } from './showSecretModal.component'
/** @hidden */
@Component({
selector: 'vault-settings-tab',
templateUrl: './vaultSettingsTab.component.pug',
})
export class VaultSettingsTabComponent extends BaseComponent {
vaultContents: Vault|null = null
VAULT_SECRET_TYPE_FILE = VAULT_SECRET_TYPE_FILE
@HostBinding('class.content-box') true
constructor (
public vault: VaultService,
public config: ConfigService,
private platform: PlatformService,
private ngbModal: NgbModal,
private translate: TranslateService,
) {
super()
if (vault.isOpen()) {
this.loadVault()
}
}
async loadVault (): Promise<void> {
this.vaultContents = await this.vault.load()
}
async enableVault () {
const modal = this.ngbModal.open(SetVaultPassphraseModalComponent)
const newPassphrase = await modal.result.catch(() => null)
if (newPassphrase) {
await this.vault.setEnabled(true, newPassphrase)
this.vaultContents = await this.vault.load(newPassphrase)
}
}
async disableVault () {
if ((await this.platform.showMessageBox(
{
type: 'warning',
message: this.translate.instant('Delete vault contents?'),
buttons: [
this.translate.instant('Delete'),
this.translate.instant('Keep'),
],
defaultId: 1,
cancelId: 1,
},
)).response === 0) {
await this.vault.setEnabled(false)
}
}
async changePassphrase () {
if (!this.vaultContents) {
this.vaultContents = await this.vault.load()
}
if (!this.vaultContents) {
return
}
const modal = this.ngbModal.open(SetVaultPassphraseModalComponent)
const newPassphrase = await modal.result.catch(() => null)
if (newPassphrase) {
this.vault.save(this.vaultContents, newPassphrase)
}
}
async toggleConfigEncrypted () {
this.config.store.encrypted = !this.config.store.encrypted
try {
await this.config.save()
} catch (e) {
this.config.store.encrypted = !this.config.store.encrypted
throw e
}
}
getSecretLabel (secret: VaultSecret) {
if (secret.type === 'ssh:password') {
return this.translate.instant('SSH password for {user}@{host}:{port}', (secret as any).key)
}
if (secret.type === 'ssh:key-passphrase') {
return this.translate.instant('Passphrase for a private key with hash {hash}...', { hash: (secret as any).key.hash.substring(0, 8) })
}
if (secret.type === VAULT_SECRET_TYPE_FILE) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return this.translate.instant('File: {description}', (secret as VaultFileSecret).key)
}
return this.translate.instant('Unknown secret of type {type} for {key}', { type: secret.type, key: JSON.stringify(secret.key) })
}
showSecret (secret: VaultSecret) {
if (!this.vaultContents) {
return
}
const modal = this.ngbModal.open(ShowSecretModalComponent)
modal.componentInstance.title = this.getSecretLabel(secret)
modal.componentInstance.secret = secret
}
removeSecret (secret: VaultSecret) {
if (!this.vaultContents) {
return
}
this.vaultContents.secrets = this.vaultContents.secrets.filter(x => x !== secret)
this.vault.removeSecret(secret.type, secret.key)
}
async replaceFileContent (secret: VaultFileSecret) {
const transfers = await this.platform.startUpload()
if (!transfers.length) {
return
}
await this.vault.updateSecret(secret, {
...secret,
value: Buffer.from(await transfers[0].readAll()).toString('base64'),
})
this.loadVault()
}
async renameFile (secret: VaultFileSecret) {
const modal = this.ngbModal.open(PromptModalComponent)
modal.componentInstance.prompt = this.translate.instant('New name')
modal.componentInstance.value = secret.key.description
const description = (await modal.result.catch(() => null))?.value
if (!description) {
return
}
await this.vault.updateSecret(secret, {
...secret,
key: {
...secret.key,
description,
},
})
this.loadVault()
}
async exportFile (secret: VaultFileSecret) {
this.vault.forgetPassphrase()
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
secret = (await this.vault.getSecret(secret.type, secret.key)) as VaultFileSecret
const content = Buffer.from(secret.value, 'base64')
const download = await this.platform.startDownload(secret.key.description, 0o600, content.length)
if (download) {
await download.write(content)
download.close()
}
}
castAny = (x: any) => x
}